7
7
# TODO: Convert STRINGIO calls to string or dict format
8
8
9
9
10
- def find_file (model , fileName ):
10
+ def _find_file (model , file_name ):
11
11
"""
12
- Retrieves first file from specified model that contains fileName as a substring.
12
+ Retrieves the first file from a registered model on SAS Model Manager that contains the provided
13
+ file_name as an exact match or substring.
14
+
13
15
Parameters
14
16
----------
15
- model : str
16
- ID of the model the desired file is located in
17
- fileName : str
18
- The name of the desired file, or a substring that is contained within the file name
17
+ model : str or dict
18
+ The name or id of the model, or a dictionary representation of the model.
19
+ file_name : str
20
+ The name of the desired file, or a substring that is contained within the file name.
21
+
19
22
Returns
20
23
-------
21
24
RestObj
22
- The first file with a name containing fileName
23
-
25
+ The first file with a name containing file_name.
24
26
"""
25
27
from ..core import current_session
26
28
27
29
sess = current_session ()
28
- fileList = mr .get_model_contents (model )
29
- for file in fileList :
30
+ file_list = mr .get_model_contents (model )
31
+ for file in file_list :
30
32
print (file .name )
31
- if fileName .lower () in file .name .lower ():
32
- correctFile = sess .get (
33
- "modelRepository/models/{}/contents/{}/content" .format (model , file .id )
34
- )
35
- break
36
- return correctFile
33
+ if file_name .lower () in file .name .lower ():
34
+ correct_file = sess .get (f"modelRepository/models/{ model } /contents/{ file .id } /content" )
35
+ return correct_file
37
36
38
37
39
38
class ModelParameters :
40
39
@classmethod
41
- def generate_hyperparameters (cls , model , modelPrefix , pPath ):
40
+ def generate_hyperparameters (cls , model , model_prefix , pickle_path ):
42
41
"""
43
- Generates hyperparameters for a given model
42
+ Generates hyperparameters for a given model and creates a JSON file representation.
43
+
44
+ Currently only supports generation of scikit-learn model hyperparameters.
45
+
44
46
Parameters
45
47
----------
46
- model : str, list, dict
47
- Name, id, or dictionary representation of the model
48
- modelPrefix : str
49
- Name used to create model files
50
- e.g. (modelPrefix) + "Hyperparameters.json")
51
- pPath : str, Path
52
- Directory location of model files
48
+ model : Python object
49
+ Python object representing the model.
50
+ model_prefix : str
51
+ Name used to create model files. (e.g. (modelPrefix) + "Hyperparameters.json")
52
+ pickle_path : str, Path
53
+ Directory location of model files.
54
+
55
+ Yields
56
+ ------
57
+ JSON file
58
+ Named {model_prefix}Hyperparameters.json.
53
59
"""
60
+ def sklearn_params ():
61
+ """
62
+ Generates hyperparameters for the models generated by scikit-learn.
63
+ """
64
+ hyperparameters = model .get_params ()
65
+ model_json = {"hyperparameters" : hyperparameters }
66
+ with open (Path (pickle_path ) / f"{ model_prefix } Hyperparameters.json" , "w" ) as f :
67
+ f .write (json .dumps (model_json , indent = 4 ))
68
+
54
69
if all (hasattr (model , attr ) for attr in ["_estimator_type" , "get_params" ]):
55
- cls . sklearn_params (model , modelPrefix , pPath )
70
+ sklearn_params (model , model_prefix , pickle_path )
56
71
else :
57
72
raise ValueError (
58
- "Other model types not currently supported for hyperparameter generation."
73
+ "This model type is not currently supported for hyperparameter generation."
59
74
)
60
75
61
76
@classmethod
@@ -68,10 +83,11 @@ def update_kpis(
68
83
"""
69
84
Updates hyperparameter file to include KPIs generated by performance definitions, as well
70
85
as any custom KPIs imported by user to the SAS KPI data table.
86
+
71
87
Parameters
72
88
----------
73
- project : dict, str, list
74
- Name, id , or dictionary representation of the project
89
+ project : str or dict
90
+ The name or id of the project , or a dictionary representation of the project.
75
91
server : str, optional
76
92
Server on which the KPI data table is stored. Defaults to "cas-shared-default".
77
93
caslib : str, optional
@@ -81,36 +97,38 @@ def update_kpis(
81
97
from io import StringIO
82
98
83
99
kpis = get_project_kpis (project , server , caslib )
84
- modelsToUpdate = kpis ["ModelUUID" ].unique ().tolist ()
85
- for model in modelsToUpdate :
86
- currentParams = find_file (model , "hyperparameters" )
87
- currentJSON = currentParams .json ()
88
- modelRows = kpis .loc [kpis ["ModelUUID" ] == model ]
89
- modelRows .set_index ("TimeLabel" , inplace = True )
90
- kpiJSON = modelRows .to_json (orient = "index" )
91
- parsedJSON = json .loads (kpiJSON )
92
- currentJSON ["kpis" ] = parsedJSON
93
- fileName = "{}Hyperparameters.json" .format (
94
- currentJSON ["kpis" ][list (currentJSON ["kpis" ].keys ())[0 ]]["ModelName" ]
100
+ models_to_update = kpis ["ModelUUID" ].unique ().tolist ()
101
+ for model in models_to_update :
102
+ current_params = _find_file (model , "hyperparameters" )
103
+ current_json = current_params .json ()
104
+ model_rows = kpis .loc [kpis ["ModelUUID" ] == model ]
105
+ model_rows .set_index ("TimeLabel" , inplace = True )
106
+ kpi_json = model_rows .to_json (orient = "index" )
107
+ parsed_json = json .loads (kpi_json )
108
+ current_json ["kpis" ] = parsed_json
109
+ file_name = "{}Hyperparameters.json" .format (
110
+ current_json ["kpis" ][list (current_json ["kpis" ].keys ())[0 ]]["ModelName" ]
95
111
)
96
112
mr .add_model_content (
97
113
model ,
98
- StringIO (( json .dumps (currentJSON , indent = 4 ) )),
99
- fileName ,
114
+ StringIO (json .dumps (current_json , indent = 4 )),
115
+ file_name ,
100
116
)
101
117
102
118
@classmethod
103
119
def get_hyperparameters (cls , model ):
104
120
"""
105
- Gets hyperparameter json file from specified model.
121
+ Retrieves the hyperparameter json file from specified model on SAS Model Manager.
122
+
106
123
Parameters
107
124
----------
108
- model : str, dict, list
109
- Name, id, or dictionary representation of the model
125
+ model : str or dict
126
+ The name or id of the model, or a dictionary representation of the model.
127
+
110
128
Returns
111
129
-------
112
130
dict
113
- dictionary containing the contents of the hyperparameter file
131
+ Dictionary containing the contents of the hyperparameter file.
114
132
"""
115
133
if mr .is_uuid (model ):
116
134
id_ = model
@@ -119,19 +137,20 @@ def get_hyperparameters(cls, model):
119
137
else :
120
138
model = mr .get_model (model )
121
139
id_ = model ["id" ]
122
- file = find_file (id_ , "hyperparameters" )
140
+ file = _find_file (id_ , "hyperparameters" )
123
141
return file .json ()
124
142
125
143
@classmethod
126
144
def add_hyperparameters (cls , model , ** kwargs ):
127
145
"""
128
- Adds custom hyperparameters to the hyperparameter file contained within the model in Model Manager.
146
+ Adds custom hyperparameters to the hyperparameter file contained within the model in SAS Model Manager.
147
+
129
148
Parameters
130
149
----------
131
- model : str, dict
132
- name, id , or dictionary representation of the model
150
+ model : str or dict
151
+ The name or id of the model , or a dictionary representation of the model.
133
152
kwargs
134
- named variables representing hyperparameters to be added to the hyperparameter file
153
+ Named variables pairs representing hyperparameters to be added to the hyperparameter file.
135
154
"""
136
155
from io import StringIO
137
156
@@ -142,23 +161,6 @@ def add_hyperparameters(cls, model, **kwargs):
142
161
hyperparameters ["hyperparameters" ][key ] = value
143
162
mr .add_model_content (
144
163
model ,
145
- StringIO (( json .dumps (hyperparameters , indent = 4 ) )),
146
- "{ }Hyperparameters.json". format ( model . name ),
164
+ StringIO (json .dumps (hyperparameters , indent = 4 )),
165
+ f" { model . name } Hyperparameters.json"
147
166
)
148
-
149
- def sklearn_params (model , modelPrefix , pPath ):
150
- """
151
- Generates hyperparameters for the models genereated by SciKit Learn.
152
- Parameters
153
- ----------
154
- modelPrefix : str
155
- Name used to create model files
156
- pPath : str, Path
157
- Directory location of model files
158
- """
159
- hyperparameters = model .get_params ()
160
- modelJson = {"hyperparameters" : hyperparameters }
161
- with open (
162
- Path (pPath ) / ("{}Hyperparameters.json" .format (modelPrefix )), "w"
163
- ) as f :
164
- f .write (json .dumps (modelJson , indent = 4 ))
0 commit comments