@@ -49,17 +49,21 @@ def project_exists(response, project):
49
49
return response
50
50
51
51
52
- def model_exists (project , name , force ):
53
- """Checks if model already exists and either raises an error or deletes the redundant model.
52
+ def model_exists (project , name , force , versionName = "latest" ):
53
+ """Checks if model already exists in the same project and either raises an error or deletes
54
+ the redundant model. If no project version is provided, the version is assumed to be "latest".
54
55
55
56
Parameters
56
57
----------
57
- project : string or dict
58
+ project : str or dict
58
59
The name or id of the model project, or a dictionary representation of the project.
59
60
name : str or dict
60
61
The name of the model.
61
62
force : bool, optional
62
63
Sets whether to overwrite models with the same name upon upload.
64
+ versionName : str, optional
65
+ Name of project version to check if a model of the same name already exists. Default
66
+ value is "latest".
63
67
64
68
Raises
65
69
------
@@ -69,7 +73,19 @@ def model_exists(project, name, force):
69
73
"""
70
74
project = mr .get_project (project )
71
75
projectId = project ["id" ]
72
- projectModels = mr .get ("/projects/{}/models" .format (projectId ))
76
+ projectVersions = mr .list_project_versions (project )
77
+ if versionName == "latest" :
78
+ modTime = [item ["modified" ] for item in projectVersions ]
79
+ latestVersion = modTime .index (max (modTime ))
80
+ versionId = projectVersions [latestVersion ]["id" ]
81
+ else :
82
+ for version in projectVersions :
83
+ if versionName is version ["name" ]:
84
+ versionId = version ["id" ]
85
+ break
86
+ projectModels = mr .get (
87
+ "/projects/{}/projectVersions/{}/models" .format (projectId , versionId )
88
+ )
73
89
74
90
for model in projectModels :
75
91
# Throws a TypeError if only one model is in the project
@@ -106,6 +122,7 @@ def pzmmImportModel(
106
122
targetDF ,
107
123
predictmethod ,
108
124
metrics = ["EM_EVENTPROBABILITY" , "EM_CLASSIFICATION" ],
125
+ projectVersion = "latest" ,
109
126
modelFileName = None ,
110
127
pyPath = None ,
111
128
threshPrediction = None ,
@@ -156,9 +173,12 @@ def pzmmImportModel(
156
173
metrics : string list, optional
157
174
The scoring metrics for the model. The default is a set of two
158
175
metrics: EM_EVENTPROBABILITY and EM_CLASSIFICATION.
176
+ projectVersion : str, optional
177
+ Name of project version to check if a model of the same name already exists. Default
178
+ value is "latest".
159
179
modelFileName : string, optional
160
180
Name of the model file that contains the model. By default None and assigned as
161
- model_prefix + '.pickle'.
181
+ modelPrefix + '.pickle'.
162
182
pyPath : string, optional
163
183
The local path of the score code file. By default None and assigned as the zPath.
164
184
threshPrediction : float, optional
@@ -265,7 +285,9 @@ def getFiles(extensions):
265
285
# Check if model with same name already exists in project.
266
286
model_exists (project , modelPrefix , force )
267
287
268
- response = mr .import_model_from_zip (modelPrefix , project , zipIOFile )
288
+ response = mr .import_model_from_zip (
289
+ modelPrefix , project , zipIOFile , version = projectVersion
290
+ )
269
291
try :
270
292
print (
271
293
"Model was successfully imported into SAS Model Manager as {} with UUID: {}." .format (
@@ -286,7 +308,9 @@ def getFiles(extensions):
286
308
# Check if model with same name already exists in project.
287
309
model_exists (project , modelPrefix , force )
288
310
289
- response = mr .import_model_from_zip (modelPrefix , project , zipIOFile , force )
311
+ response = mr .import_model_from_zip (
312
+ modelPrefix , project , zipIOFile , force , version = projectVersion
313
+ )
290
314
try :
291
315
print (
292
316
"Model was successfully imported into SAS Model Manager as {} with UUID: {}." .format (
0 commit comments