4
4
# Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
5
5
# SPDX-License-Identifier: Apache-2.0
6
6
7
+ """Commonly used tasks in the analytics life cycle."""
8
+
7
9
import json
8
10
import logging
9
11
import pickle
10
12
import re
11
- import time
12
13
import sys
13
14
import warnings
14
15
15
- from .utils .pymas import from_pickle , PyMAS
16
- from sasctl .services import model_management as mm , model_publish as mp , \
17
- model_repository as mr
18
16
from . import utils
19
- from .core import get , get_link , request_link , RestObj
17
+ from .core import RestObj , get , get_link , request_link
18
+ from .services import model_management as mm , model_publish as mp , \
19
+ model_repository as mr
20
+ from .utils .pymas import PyMAS , from_pickle
21
+
20
22
21
23
logger = logging .getLogger (__name__ )
22
24
@@ -40,35 +42,42 @@ def _sklearn_to_dict(model):
40
42
scoreCodeType = 'ds2MultiType' ,
41
43
trainCodeType = 'Python' ,
42
44
function = mappings .get (model ._estimator_type , model ._estimator_type ),
43
- tool = 'Python {}.{}' .format (sys .version_info .major , sys .version_info .minor ),
44
- properties = [{'name' : k , 'value' : v } for k , v in model .get_params ().items ()]
45
+ tool = 'Python %s.%s'
46
+ % (sys .version_info .major , sys .version_info .minor ),
47
+ properties = [{'name' : k , 'value' : v }
48
+ for k , v in model .get_params ().items ()]
45
49
)
46
50
47
51
return result
48
52
49
53
50
- def register_model (model , name , project , repository = None , input = None , version = 'latest' , files = None , force = False ):
54
+ def register_model (model , name , project , repository = None , input = None ,
55
+ version = 'latest' , files = None , force = False ):
51
56
"""Register a model in the model repository.
52
57
53
58
Parameters
54
59
----------
55
60
model : swat.CASTable or sklearn.BaseEstimator
56
- The model to register. If an instance of ``swat.CASTable`` the table is assumed to hold an ASTORE, which will
57
- be downloaded and used to construct the model to register. If a scikit-learn estimator, the model will be
58
- pickled and uploaded to the registry and score code will be generated for publishing the model to MAS.
61
+ The model to register. If an instance of ``swat.CASTable`` the table
62
+ is assumed to hold an ASTORE, which will be downloaded and used to
63
+ construct the model to register. If a scikit-learn estimator, the
64
+ model will be pickled and uploaded to the registry and score code will
65
+ be generated for publishing the model to MAS.
59
66
name : str
60
67
Designated name for the model in the repository.
61
68
project : str or dict
62
- The name or id of the project, or a dictionary representation of the project.
69
+ The name or id of the project, or a dictionary representation of
70
+ the project.
63
71
repository : str or dict, optional
64
- The name or id of the repository, or a dictionary representation of the repository. If omitted, the default
65
- repository will be used.
72
+ The name or id of the repository, or a dictionary representation of
73
+ the repository. If omitted, the default repository will be used.
66
74
input
67
75
version : {'new', 'latest', int}, optional
68
76
Version number of the project in which the model should be created.
69
77
files :
70
78
force : bool, optional
71
- Create dependencies such as projects and repositories if they do not already exist.
79
+ Create dependencies such as projects and repositories if they do not
80
+ already exist.
72
81
73
82
Returns
74
83
-------
@@ -77,17 +86,15 @@ def register_model(model, name, project, repository=None, input=None, version='l
77
86
78
87
Notes
79
88
-----
80
- If the specified model is a CAS table the model data and metadata will be written to a temporary zip file and then
81
- imported using model_repository.import_model_from_zip.
82
-
83
- If the specified model is from the Scikit-Learn package, the model will be created using
84
- model_repository.create_model and any additional files will be uploaded as content.
89
+ If the specified model is a CAS table the model data and metadata will be
90
+ written to a temporary zip file and then imported using
91
+ model_repository.import_model_from_zip.
85
92
86
- Examples
87
- --------
93
+ If the specified model is from the Scikit-Learn package, the model will be
94
+ created using model_repository.create_model and any additional files will
95
+ be uploaded as content.
88
96
89
97
"""
90
-
91
98
# TODO: Create new version if model already exists
92
99
# TODO: Allow file info to be specified
93
100
# TODO: Performance stats
@@ -103,7 +110,10 @@ def register_model(model, name, project, repository=None, input=None, version='l
103
110
if p is None and not create_project :
104
111
raise ValueError ("Project '{}' not found" .format (project ))
105
112
106
- repository = mr .default_repository () if repository is None else mr .get_repository (repository )
113
+ if repository is None :
114
+ repository = mr .default_repository ()
115
+ else :
116
+ repository = mr .get_repository (repository )
107
117
108
118
# Unable to find or create the repo.
109
119
if repository is None :
@@ -182,7 +192,8 @@ def register_model(model, name, project, repository=None, input=None, version='l
182
192
return model
183
193
184
194
185
- def publish_model (model , destination , code = None , max_retries = 60 , ** kwargs ):
195
+ def publish_model (model , destination , code = None , max_retries = 60 ,
196
+ replace = False , ** kwargs ):
186
197
"""Publish a model to a configured publishing destination.
187
198
188
199
Parameters
@@ -192,6 +203,9 @@ def publish_model(model, destination, code=None, max_retries=60, **kwargs):
192
203
destination : str
193
204
code : optional
194
205
max_retries : int, optional
206
+ replace : bool, optional
207
+ Whether to overwrite the model if it already exists in
208
+ the `destination`
195
209
kwargs : optional
196
210
additional arguments will be passed to the underlying publish functions.
197
211
@@ -200,17 +214,18 @@ def publish_model(model, destination, code=None, max_retries=60, **kwargs):
200
214
201
215
Notes
202
216
-----
203
- If no code is specified, the model is assumed to be already registered in the model repository and Model Manager's
204
- publishing functionality will be used.
217
+ If no code is specified, the model is assumed to be already registered in
218
+ the model repository and Model Manager's publishing functionality will be
219
+ used.
205
220
206
221
Otherwise, the model publishing API will be used.
207
222
208
223
See Also
209
224
--------
210
225
:meth:`model_management.publish_model <.ModelManagement.publish_model>`
211
226
:meth:`model_publish.publish_model <.ModelPublish.publish_model>`
212
- """
213
227
228
+ """
214
229
# Submit a publishing request
215
230
if code is None :
216
231
publish_req = mm .publish_model (model , destination , ** kwargs )
0 commit comments