Skip to content

Commit e94d66a

Browse files
committed
code format
1 parent f1d9004 commit e94d66a

18 files changed

+418
-285
lines changed

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"python.pythonPath": "C:\\Users\\prsol\\.azureml\\envs\\azuremlsdk-0112\\python.exe",
3+
"python.linting.pep8Enabled": false,
4+
"python.linting.enabled": false,
5+
"python.linting.flake8Enabled": true
6+
}

aml_service/00-WorkSpace.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,34 @@
2626
from azureml.core import Workspace
2727
import os, json
2828
import azureml.core
29+
2930
print("SDK Version:", azureml.core.VERSION)
30-
#print('current dir is ' +os.curdir)
31+
# print('current dir is ' +os.curdir)
3132
with open("aml_config/config.json") as f:
3233
config = json.load(f)
3334

34-
workspace_name = config['workspace_name']
35-
resource_group = config['resource_group']
36-
subscription_id = config['subscription_id']
37-
location = config['location']
38-
#location = 'southcentralus'
35+
workspace_name = config["workspace_name"]
36+
resource_group = config["resource_group"]
37+
subscription_id = config["subscription_id"]
38+
location = config["location"]
39+
# location = 'southcentralus'
3940
try:
40-
ws = Workspace.get(name = workspace_name,
41-
subscription_id = subscription_id,
42-
resource_group = resource_group)
41+
ws = Workspace.get(
42+
name=workspace_name,
43+
subscription_id=subscription_id,
44+
resource_group=resource_group,
45+
)
4346

4447
except:
4548
# this call might take a minute or two.
46-
print('Creating new workspace')
47-
ws = Workspace.create(name = workspace_name,
48-
subscription_id = subscription_id,
49-
resource_group = resource_group,
50-
#create_resource_group=True,
51-
location=location)
49+
print("Creating new workspace")
50+
ws = Workspace.create(
51+
name=workspace_name,
52+
subscription_id=subscription_id,
53+
resource_group=resource_group,
54+
# create_resource_group=True,
55+
location=location,
56+
)
5257

53-
# print Workspace details
54-
print(ws.name, ws.resource_group, ws.location, ws.subscription_id, sep = '\n')
58+
# print Workspace details
59+
print(ws.name, ws.resource_group, ws.location, ws.subscription_id, sep="\n")

aml_service/01-Experiment.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@
2727
from azureml.core import Experiment
2828
from azureml.core import Workspace
2929

30+
3031
def getExperiment():
3132
ws = Workspace.from_config()
3233
script_folder = "."
33-
experiment_name = 'devops-ai-demo'
34-
exp = Experiment(workspace = ws, name = experiment_name)
35-
print(exp.name, exp.workspace.name, sep = '\n')
34+
experiment_name = "devops-ai-demo"
35+
exp = Experiment(workspace=ws, name=experiment_name)
36+
print(exp.name, exp.workspace.name, sep="\n")
3637
return exp
3738

39+
3840
if __name__ == "__main__":
39-
exp = getExperiment()
41+
exp = getExperiment()

aml_service/02-AttachTrainingVM.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,21 @@
4242
with open("aml_config/security_config.json") as f:
4343
config = json.load(f)
4444

45-
remote_vm_name = config['remote_vm_name']
46-
remote_vm_username = config['remote_vm_username']
47-
remote_vm_password = config['remote_vm_password']
48-
remote_vm_ip = config['remote_vm_ip']
45+
remote_vm_name = config["remote_vm_name"]
46+
remote_vm_username = config["remote_vm_username"]
47+
remote_vm_password = config["remote_vm_password"]
48+
remote_vm_ip = config["remote_vm_ip"]
4949

5050
try:
51-
dsvm_compute = RemoteCompute.attach(ws, name=remote_vm_name,
52-
username=remote_vm_username,
53-
address=remote_vm_ip,
54-
ssh_port=22,
55-
password=remote_vm_password)
56-
dsvm_compute.wait_for_completion(show_output =True)
51+
dsvm_compute = RemoteCompute.attach(
52+
ws,
53+
name=remote_vm_name,
54+
username=remote_vm_username,
55+
address=remote_vm_ip,
56+
ssh_port=22,
57+
password=remote_vm_password,
58+
)
59+
dsvm_compute.wait_for_completion(show_output=True)
5760

5861
except Exception as e:
5962
print("Caught = {}".format(e.message))
@@ -70,4 +73,4 @@
7073
# print('creating new.')
7174
# dsvm_config = DsvmCompute.provisioning_configuration(vm_size="Standard_D2_v2")
7275
# dsvm_compute = DsvmCompute.create(ws, name=compute_target_name, provisioning_configuration=dsvm_config)
73-
# dsvm_compute.wait_for_completion(show_output=True)
76+
# dsvm_compute.wait_for_completion(show_output=True)

aml_service/10-TrainOnLocal.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,43 @@
2828
from azureml.core import Workspace
2929
from azureml.core import Experiment
3030
from azureml.core import ScriptRunConfig
31-
import os, json
31+
import json
3232

3333
# Get workspace
3434
ws = Workspace.from_config()
3535

3636
# Attach Experiment
37-
experiment_name = 'devops-ai-demo'
38-
exp = Experiment(workspace = ws, name = experiment_name)
39-
print(exp.name, exp.workspace.name, sep = '\n')
37+
experiment_name = "devops-ai-demo"
38+
exp = Experiment(workspace=ws, name=experiment_name)
39+
print(exp.name, exp.workspace.name, sep="\n")
4040

4141
# Editing a run configuration property on-fly.
4242
run_config_user_managed = RunConfiguration()
4343
run_config_user_managed.environment.python.user_managed_dependencies = True
4444

4545
print("Submitting an experiment.")
46-
src = ScriptRunConfig(source_directory = './code', script = 'training/train.py', run_config = run_config_user_managed)
46+
src = ScriptRunConfig(
47+
source_directory="./code",
48+
script="training/train.py",
49+
run_config=run_config_user_managed,
50+
)
4751
run = exp.submit(src)
4852

4953
# Shows output of the run on stdout.
50-
run.wait_for_completion(show_output = True, wait_post_processing = True)
54+
run.wait_for_completion(show_output=True, wait_post_processing=True)
5155

5256
# Raise exception if run fails
53-
if run.get_status() == 'Failed':
54-
raise Exception('Training on local failed with following run status: {} and logs: \n {}'.format(run.get_status(),run.get_details_with_logs()))
57+
if run.get_status() == "Failed":
58+
raise Exception(
59+
"Training on local failed with following run status: {} and logs: \n {}".format(
60+
run.get_status(), run.get_details_with_logs()
61+
)
62+
)
5563

5664
# Writing the run id to /aml_config/run_id.json
5765

58-
run_id={}
59-
run_id['run_id'] = run.id
60-
run_id['experiment_name'] = run.experiment.name
61-
with open('aml_config/run_id.json', 'w') as outfile:
62-
json.dump(run_id,outfile)
66+
run_id = {}
67+
run_id["run_id"] = run.id
68+
run_id["experiment_name"] = run.experiment.name
69+
with open("aml_config/run_id.json", "w") as outfile:
70+
json.dump(run_id, outfile)

aml_service/11-TrainOnLocalEnv.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
ws = Workspace.from_config()
3737

3838
# Attach Experiment
39-
experiment_name = 'devops-ai-demo'
40-
exp = Experiment(workspace = ws, name = experiment_name)
41-
print(exp.name, exp.workspace.name, sep = '\n')
39+
experiment_name = "devops-ai-demo"
40+
exp = Experiment(workspace=ws, name=experiment_name)
41+
print(exp.name, exp.workspace.name, sep="\n")
4242

4343
# Editing a run configuration property on-fly.
4444
run_config_system_managed = RunConfiguration()
@@ -52,19 +52,27 @@
5252
# run_config_system_managed.environment.python.conda_dependencies = CondaDependencies.create(conda_packages=['scikit-learn'])
5353

5454
print("Submitting an experiment to new conda virtual env")
55-
src = ScriptRunConfig(source_directory = './code', script = 'training/train.py', run_config = run_config_user_managed)
55+
src = ScriptRunConfig(
56+
source_directory="./code",
57+
script="training/train.py",
58+
run_config=run_config_user_managed,
59+
)
5660
run = exp.submit(src)
5761

5862
# Shows output of the run on stdout.
59-
run.wait_for_completion(show_output = True, wait_post_processing = True)
63+
run.wait_for_completion(show_output=True, wait_post_processing=True)
6064

6165
# Raise exception if run fails
62-
if run.get_status() == 'Failed':
63-
raise Exception('Training on local env failed with following run status: {} and logs: \n {}'.format(run.get_status(),run.get_details_with_logs()))
66+
if run.get_status() == "Failed":
67+
raise Exception(
68+
"Training on local env failed with following run status: {} and logs: \n {}".format(
69+
run.get_status(), run.get_details_with_logs()
70+
)
71+
)
6472

6573
# Writing the run id to /aml_config/run_id.json
6674
run_id = {}
67-
run_id['run_id'] = run.id
68-
run_id['experiment_name'] = run.experiment.name
69-
with open('aml_config/run_id.json', 'w') as outfile:
70-
json.dump(run_id,outfile)
75+
run_id["run_id"] = run.id
76+
run_id["experiment_name"] = run.experiment.name
77+
with open("aml_config/run_id.json", "w") as outfile:
78+
json.dump(run_id, outfile)

aml_service/12-TrainOnVM.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,41 @@
3838
# Read the New VM Config
3939
with open("aml_config/security_config.json") as f:
4040
config = json.load(f)
41-
remote_vm_name = config['remote_vm_name']
41+
remote_vm_name = config["remote_vm_name"]
4242

4343

4444
# Attach Experiment
45-
experiment_name = 'devops-ai-demo'
46-
exp = Experiment(workspace = ws, name = experiment_name)
47-
print(exp.name, exp.workspace.name, sep = '\n')
45+
experiment_name = "devops-ai-demo"
46+
exp = Experiment(workspace=ws, name=experiment_name)
47+
print(exp.name, exp.workspace.name, sep="\n")
4848

4949
run_config = RunConfiguration()
5050
run_config.target = remote_vm_name
5151

5252
# replace with your path to the python interpreter in the remote VM found earlier
53-
run_config.environment.python.interpreter_path = '/anaconda/envs/myenv/bin/python'
53+
run_config.environment.python.interpreter_path = "/anaconda/envs/myenv/bin/python"
5454
run_config.environment.python.user_managed_dependencies = True
5555

5656

57-
src = ScriptRunConfig(source_directory = './code', script = 'training/train.py', run_config = run_config)
57+
src = ScriptRunConfig(
58+
source_directory="./code", script="training/train.py", run_config=run_config
59+
)
5860
run = exp.submit(src)
5961

6062
# Shows output of the run on stdout.
61-
run.wait_for_completion(show_output = True, wait_post_processing = True)
63+
run.wait_for_completion(show_output=True, wait_post_processing=True)
6264

6365
# Raise exception if run fails
64-
if run.get_status() == 'Failed':
65-
raise Exception('Training on local env failed with following run status: {} and logs: \n {}'.format(run.get_status(),run.get_details_with_logs()))
66+
if run.get_status() == "Failed":
67+
raise Exception(
68+
"Training on local env failed with following run status: {} and logs: \n {}".format(
69+
run.get_status(), run.get_details_with_logs()
70+
)
71+
)
6672

6773
# Writing the run id to /aml_config/run_id.json
6874
run_id = {}
69-
run_id['run_id'] = run.id
70-
run_id['experiment_name'] = run.experiment.name
71-
with open('aml_config/run_id.json', 'w') as outfile:
72-
json.dump(run_id,outfile)
75+
run_id["run_id"] = run.id
76+
run_id["experiment_name"] = run.experiment.name
77+
with open("aml_config/run_id.json", "w") as outfile:
78+
json.dump(run_id, outfile)

aml_service/15-EvaluateModel.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,46 +38,54 @@
3838

3939
# Add golden data set on which all the model performance can be evaluated
4040

41-
# Get the latest run_id
41+
# Get the latest run_id
4242
with open("aml_config/run_id.json") as f:
4343
config = json.load(f)
4444

4545
new_model_run_id = config["run_id"]
4646
experiment_name = config["experiment_name"]
47-
exp = Experiment(workspace = ws, name = experiment_name)
47+
exp = Experiment(workspace=ws, name=experiment_name)
4848

4949

5050
try:
51-
# Get most recently registered model, we assume that is the model in production. Download this model and compare it with the recently trained model by running test with same data set.
52-
model_list = Model.list(ws)
53-
production_model = next(filter(lambda x: x.created_time == max(model.created_time for model in model_list), model_list))
54-
production_model_run_id = production_model.tags.get('run_id')
55-
run_list = exp.get_runs()
56-
# production_model_run = next(filter(lambda x: x.id == production_model_run_id, run_list))
51+
# Get most recently registered model, we assume that is the model in production. Download this model and compare it with the recently trained model by running test with same data set.
52+
model_list = Model.list(ws)
53+
production_model = next(
54+
filter(
55+
lambda x: x.created_time == max(model.created_time for model in model_list),
56+
model_list,
57+
)
58+
)
59+
production_model_run_id = production_model.tags.get("run_id")
60+
run_list = exp.get_runs()
61+
# production_model_run = next(filter(lambda x: x.id == production_model_run_id, run_list))
5762

63+
# Get the run history for both production model and newly trained model and compare mse
64+
production_model_run = Run(exp, run_id=production_model_run_id)
65+
new_model_run = Run(exp, run_id=new_model_run_id)
5866

59-
# Get the run history for both production model and newly trained model and compare mse
60-
production_model_run = Run(exp,run_id=production_model_run_id)
61-
new_model_run = Run(exp,run_id=new_model_run_id)
62-
63-
production_model_mse=production_model_run.get_metrics().get('mse')
64-
new_model_mse=new_model_run.get_metrics().get('mse')
65-
print('Current Production model mse: {}, New trained model mse: {}'.format(production_model_mse,new_model_mse))
67+
production_model_mse = production_model_run.get_metrics().get("mse")
68+
new_model_mse = new_model_run.get_metrics().get("mse")
69+
print(
70+
"Current Production model mse: {}, New trained model mse: {}".format(
71+
production_model_mse, new_model_mse
72+
)
73+
)
6674

67-
promote_new_model=False
68-
if new_model_mse < production_model_mse:
69-
promote_new_model=True
70-
print('New trained model performs better, thus it will be registered')
75+
promote_new_model = False
76+
if new_model_mse < production_model_mse:
77+
promote_new_model = True
78+
print("New trained model performs better, thus it will be registered")
7179
except:
72-
promote_new_model=True
73-
print('This is the first model to be trained, thus nothing to evaluate for now')
80+
promote_new_model = True
81+
print("This is the first model to be trained, thus nothing to evaluate for now")
7482

7583
run_id = {}
76-
run_id['run_id'] = ''
84+
run_id["run_id"] = ""
7785
# Writing the run id to /aml_config/run_id.json
7886
if promote_new_model:
79-
run_id['run_id'] = new_model_run_id
87+
run_id["run_id"] = new_model_run_id
8088

81-
run_id['experiment_name'] = experiment_name
82-
with open('aml_config/run_id.json', 'w') as outfile:
83-
json.dump(run_id,outfile)
89+
run_id["experiment_name"] = experiment_name
90+
with open("aml_config/run_id.json", "w") as outfile:
91+
json.dump(run_id, outfile)

0 commit comments

Comments
 (0)