Skip to content

Instantly share code, notes, and snippets.

@dataders
Created March 6, 2021 00:12
Show Gist options
  • Save dataders/fcab67abad58e61a1b1f9fd31e3b9919 to your computer and use it in GitHub Desktop.
Save dataders/fcab67abad58e61a1b1f9fd31e3b9919 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from azureml.core import Experiment, Run, Workspace\n",
"from pygit2 import Repository\n",
"import re\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def get_branch_name(given_branch_name=None, regex_pattern='[^a-zA-Z]'):\n",
" \"\"\"\n",
" *Get the branch name during the local/remote run.\n",
" *The ML experiment name, compute name is derived from\n",
" branch name (master or feature)\n",
"\n",
" *Args:\n",
" *given_branch_name: Provide the branch name\n",
" to override default behavior.\n",
" *regex_pattern: for formatting the experiment/compute name\n",
"\n",
" *Returns:\n",
" *dict_names: Holds branch name pre-post applying the regex\n",
" \"\"\"\n",
"\n",
" if given_branch_name is None:\n",
" # This provides the branch name as \"master\" when merging to master and\n",
" # \"merge\" for PR.\n",
" branch_name = os.getenv('BUILD_SOURCEBRANCHNAME')\n",
" # Get the feature branch name during a PR. Format - refs/heads/<feature\n",
" # branch name>\n",
" branch_name_pr = os.getenv('SYSTEM_PULLREQUEST_SOURCEBRANCH')\n",
"\n",
" if branch_name is None:\n",
" # works for feature branch run\n",
" branch_name = Repository('.').head.shorthand\n",
"\n",
" elif branch_name_pr is not None: # For PRs\n",
" branch_name = branch_name_pr\n",
" branch_name = branch_name.replace('refs/heads/', '')\n",
" else:\n",
" branch_name = given_branch_name\n",
"\n",
" # apply regex to format experiment name\n",
" regex = re.compile(regex_pattern)\n",
" branch_name_org = branch_name\n",
" branch_name = regex.sub('', branch_name_org)\n",
"\n",
" dict_names = {\n",
" 'branch_name_org': branch_name_org,\n",
" 'branch_name': branch_name\n",
" }\n",
"\n",
" return dict_names\n",
"\n",
"\n",
"def make_resource_name(prefix, suffix, max_len, sep='-'):\n",
" '''\n",
" *This method helps forming resource names for\n",
" experiment and compute targets as specified by\n",
" input params\n",
" *project max_len:36, compute max_len:16\n",
"\n",
" *Args:\n",
" *prefix: custom prefix\n",
" *suffix: custom suffix\n",
" *max_len: constraints on resource name length\n",
" *sep: custom formatter\n",
"\n",
" *Returns:\n",
" *resource_name: newly formed resource name\n",
"\n",
" '''\n",
" # suffix will be abbreviated if it is longer than\n",
" # (max_length - len(prefix) - len(sep))\n",
" chrs_illgl = r'_|/|\\.'\n",
" suffix_max_len = min(len(suffix), max_len - len(prefix) - len(sep))\n",
" suffix_cut = suffix[0:suffix_max_len]\n",
" # replace illegal chrs w/ dash, ensure no trailing dash\n",
" suffix_rep = re.sub(chrs_illgl, '-', suffix_cut).rstrip('-')\n",
" resource_name = sep.join([prefix, suffix_rep])\n",
"\n",
" return resource_name"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Warning: Falling back to use azure cli login credentials.\n",
"If you run your code in unattended mode, i.e., where you can't give a user input, then we recommend to use ServicePrincipalAuthentication or MsiAuthentication.\n",
"Please refer to aka.ms/aml-notebook-auth for different authentication mechanisms in azureml-sdk.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found workspace avadevitsmlsvc at location westus2\n"
]
}
],
"source": [
"ws = Workspace.from_config(\"compute/aml_config/config.json\")\n",
"print(\"Found workspace {} at location {}\".format(ws.name, ws.location))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experiment: tacocat-rgblayer\n"
]
}
],
"source": [
"prefix = 'tacocat'\n",
"repo_name = get_branch_name()['branch_name']\n",
"project_name = make_resource_name(prefix, repo_name, max_len=36)\n",
"exp = Experiment(workspace=ws, name=project_name)\n",
"print(\"Experiment: {}\".format(exp.name))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment