45
45
_PROP_NAME_MAXLEN = 60
46
46
47
47
48
-
49
-
50
48
def _property (k , v ):
51
49
return {"name" : str (k )[:_PROP_NAME_MAXLEN ], "value" : str (v )[:_PROP_VALUE_MAXLEN ]}
52
50
53
51
54
-
55
52
def _sklearn_to_dict (model ):
56
53
# Convert Scikit-learn values to built-in Model Manager values
57
54
mappings = {
@@ -106,7 +103,9 @@ def _sklearn_to_dict(model):
106
103
return result
107
104
108
105
109
- def _register_sklearn_40 (model , model_name , project_name , input_data , output_data , overwrite = False ):
106
+ def _register_sklearn_40 (
107
+ model , model_name , project_name , input_data , output_data , overwrite = False
108
+ ):
110
109
model_info = get_model_info (model , input_data , output_data )
111
110
112
111
# TODO: allow passing description in register_model()
@@ -121,22 +120,16 @@ def _register_sklearn_40(model, model_name, project_name, input_data, output_dat
121
120
files .update (pzmm .JSONFiles .write_var_json (input_data ))
122
121
files .update (pzmm .JSONFiles .write_var_json (output_data , is_input = False ))
123
122
124
- if model_info .is_binary_classifier :
125
- num_categories = 2
126
- elif model_info .is_classifier :
127
- num_categories = len (model_info .target_values )
128
- else :
129
- num_categories = 0
130
-
131
- files .update (pzmm .JSONFiles .write_model_properties_json (model_name ,
132
- target_variable = model_info .output_column_names ,
133
- target_event = model_info .target_values ,
134
- num_target_categories = num_categories ,
135
- event_prob_var = None ,
136
- model_desc = model_info .description [:_DESC_MAXLEN ],
137
- model_function = model_info .analytic_function ,
138
- model_type = model_info .algorithm
139
- ))
123
+ files .update (
124
+ pzmm .JSONFiles .write_model_properties_json (
125
+ model_name ,
126
+ target_variable = model_info .output_column_names ,
127
+ target_values = model_info .target_values ,
128
+ model_desc = model_info .description [:_DESC_MAXLEN ],
129
+ model_function = model_info .analytic_function ,
130
+ model_algorithm = model_info .algorithm ,
131
+ )
132
+ )
140
133
"""
141
134
target_variable : string
142
135
Target variable to be predicted by the model.
@@ -151,15 +144,16 @@ def _register_sklearn_40(model, model_name, project_name, input_data, output_dat
151
144
files .update (pzmm .JSONFiles .write_file_metadata_json (model_name ))
152
145
153
146
# TODO: How to determine if should call .predict() or .predict_proba()? Base on output data?
154
- pzmm .ImportModel .import_model (model_files = files ,
155
- model_prefix = model_name ,
156
- project = project_name ,
157
- predict_method = model .predict ,
158
- input_data = input_data ,
159
- output_variables = [],
160
- score_cas = True ,
161
- missing_values = False # assuming Pipeline will be used for imputing.
162
- )
147
+ pzmm .ImportModel .import_model (
148
+ model_files = files ,
149
+ model_prefix = model_name ,
150
+ project = project_name ,
151
+ predict_method = model .predict ,
152
+ input_data = input_data ,
153
+ output_variables = [],
154
+ score_cas = True ,
155
+ missing_values = False , # assuming Pipeline will be used for imputing.
156
+ )
163
157
164
158
165
159
def _create_project (project_name , model , repo , input_vars = None , output_vars = None ):
@@ -235,7 +229,7 @@ def register_model(
235
229
name ,
236
230
project ,
237
231
repository = None ,
238
- input = None ,
232
+ input_data = None ,
239
233
version = None ,
240
234
files = None ,
241
235
force = False ,
@@ -246,11 +240,11 @@ def register_model(
246
240
Parameters
247
241
----------
248
242
model : swat.CASTable or sklearn.BaseEstimator
249
- The model to register. If an instance of ``swat.CASTable`` the table
250
- is assumed to hold an ASTORE, which will be downloaded and used to
251
- construct the model to register. If a scikit-learn estimator, the
252
- model will be pickled and uploaded to the registry and score code will
253
- be generated for publishing the model to MAS.
243
+ The model to register. If an instance of ``swat.CASTable`` the table is assumed
244
+ to hold an ASTORE, which will be downloaded and used to construct the model to
245
+ register. If a scikit-learn estimator, the model will be pickled and uploaded
246
+ to the registry and score code will be generated for publishing the model to
247
+ CAS or MAS.
254
248
name : str
255
249
Designated name for the model in the repository.
256
250
project : str or dict
@@ -259,14 +253,14 @@ def register_model(
259
253
repository : str or dict, optional
260
254
The name or id of the repository, or a dictionary representation of
261
255
the repository. If omitted, the default repository will be used.
262
- input : DataFrame, type, list of type, or dict of str: type, optional
256
+ input_data : DataFrame, type, list of type, or dict of str: type, optional
263
257
The expected type for each input value of the target function.
264
258
Can be omitted if target function includes type hints. If a DataFrame
265
259
is provided, the columns will be inspected to determine type
266
260
information. If a single type is provided, all columns will be assumed
267
261
to be that type, otherwise a list of column types or a dictionary of
268
262
column_name: type may be provided.
269
- output : array-like
263
+ output_data : array-like
270
264
A Numpy array or Pandas DataFrame that contains
271
265
version : {'new', 'latest', int}, optional
272
266
Version number of the project in which the model should be created.
@@ -305,7 +299,8 @@ def register_model(
305
299
Added `record_packages` parameter.
306
300
307
301
.. versionchanged:: v1.7.4
308
- Update ASTORE handling for ease of use and removal of SAS Viya 4 score code errors
302
+ Update ASTORE handling for ease of use and removal of SAS Viya 4 score code
303
+ errors
309
304
310
305
"""
311
306
# If version not specified, default to creating a new version
@@ -320,7 +315,7 @@ def register_model(
320
315
create_project = bool (p is None and force is True )
321
316
322
317
if p is None and not create_project :
323
- raise ValueError ("Project '{}' not found" . format ( project ) )
318
+ raise ValueError (f "Project '{ project } ' not found" )
324
319
325
320
# Use default repository if not specified
326
321
try :
@@ -331,7 +326,7 @@ def register_model(
331
326
except HTTPError as e :
332
327
if e .code == 403 :
333
328
raise AuthorizationError (
334
- "Unable to register model. User account does not have read permissions "
329
+ "Unable to register model. User account does not have read permissions "
335
330
"for the /modelRepository/repositories/ URL. Please contact your SAS "
336
331
"Viya administrator."
337
332
)
@@ -342,9 +337,9 @@ def register_model(
342
337
raise ValueError ("Unable to find a default repository" )
343
338
344
339
if repo_obj is None :
345
- raise ValueError ("Unable to find repository '{}'" . format ( repository ) )
340
+ raise ValueError (f "Unable to find repository '{ repository } '" )
346
341
347
- # If model is a CASTable then assume it holds an ASTORE model. Import these via a ZIP file.
342
+ # If model is a CASTable then assume it holds an ASTORE model; import with zip file
348
343
if "swat.cas.table.CASTable" in str (type (model )):
349
344
if swat is None :
350
345
raise RuntimeError (
@@ -357,7 +352,7 @@ def register_model(
357
352
)
358
353
359
354
if "DataStepSrc" in model .columns :
360
- zip_file = utils .create_package_from_datastep (model , input = input )
355
+ zip_file = utils .create_package_from_datastep (model , input = input_data )
361
356
if create_project :
362
357
out_var = []
363
358
in_var = []
@@ -427,7 +422,7 @@ def register_model(
427
422
428
423
if current_session ().version_info () < 4 :
429
424
# Upload the model as a ZIP file if using Viya 3.
430
- zipfile = utils .create_package (model , input = input )
425
+ zipfile = utils .create_package (model , input = input_data )
431
426
model = mr .import_model_from_zip (
432
427
name , project , zipfile , version = version
433
428
)
@@ -456,17 +451,17 @@ def register_model(
456
451
# If the model is a scikit-learn model, generate the model dictionary
457
452
# from it and pickle the model for storage
458
453
if all (hasattr (model , attr ) for attr in ["_estimator_type" , "get_params" ]):
459
-
460
454
# Pickle the model so we can store it
461
455
model_pkl = pickle .dumps (model )
462
- files .append ({"name" : "model.pkl" , "file" : model_pkl , "role" : "Python Pickle " })
456
+ files .append ({"name" : "model.pkl" , "file" : model_pkl , "role" : "Python pickle " })
463
457
464
458
target_funcs = [f for f in ("predict" , "predict_proba" ) if hasattr (model , f )]
465
459
466
460
# Extract model properties
467
461
model = _sklearn_to_dict (model )
468
462
model ["name" ] = name
469
463
464
+ # TODO: Swap for pzmm.JSONFiles.create_requirements_json()
470
465
# Get package versions in environment
471
466
packages = installed_packages ()
472
467
if record_packages and packages is not None :
@@ -485,10 +480,11 @@ def register_model(
485
480
# Generate and upload a requirements.txt file
486
481
files .append ({"name" : "requirements.txt" , "file" : "\n " .join (packages )})
487
482
483
+ # TODO: Swap for pzmm.ScoreCode.write_score_code()
488
484
# Generate PyMAS wrapper
489
485
try :
490
486
mas_module = from_pickle (
491
- model_pkl , target_funcs , input_types = input , array_input = True
487
+ model_pkl , target_funcs , input_types = input_data , array_input = True
492
488
)
493
489
494
490
# Include score code files from ESP and MAS
0 commit comments