8000 Merge pull request #43 from jameskochubasas/master · teoland/python-sasctl@91179e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 91179e2

Browse files
authored
Merge pull request sassoftware#43 from jameskochubasas/master
Adding python model version capabilities and python null value scoring
2 parents 77baa33 + ca1e24a commit 91179e2

File tree

4 files changed

+45
-39
lines changed

4 files changed

+45
-39
lines changed

src/sasctl/_services/model_repository.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""The Model Repository service supports registering and managing models."""
88

99
from .service import Service
10-
from ..core import current_session, get
10+
from ..core import current_session, get, delete
1111

1212
FUNCTIONS = {'Analytical', 'Classification', 'Clustering', 'Forecasting',
1313
'Prediction', 'Text categorization', 'Text extraction',
@@ -497,3 +497,24 @@ def copy_analytic_store(cls, model):
497497
model = cls.get_model(model, refresh=True)
498498

499499
return cls.request_link(model, 'copyAnalyticStore')
500+
501+
502+
@classmethod
503+
def delete_model_contents(cls, model):
504+
"""Deletes all contents (files) in the model.
505+
506+
Parameters
507+
----------
508+
model : str or dict
509+
The name, id, or dictionary representation of a model.
510+
511+
Returns
512+
-------
513+
514+
"""
515+
rel = 'delete'
516+
517+
filelist=cls.get_model_contents(model)
518+
for delfile in filelist:
519+
modelfileuri=cls.get_link(delfile, rel)
520+
delete(modelfileuri['uri'])

src/sasctl/tasks.py

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -136,41 +136,6 @@ def register_model(model, name, project, repository=None, input=None,
136136
# If version not specified, default to creating a new version
137137
version = version or 'new'
138138

139-
# If replacing an existing version, make sure the model version exists
140-
if str(version).lower() != 'new':
141-
model_obj = mr.get_model(name)
142-
if model_obj is None:
143-
raise ValueError("Unable to update version '%s' of model '%s%. "
144-
"Model not found." % (version, name))
145-
model_versions = request_link(model_obj, 'modelVersions')
146-
assert isinstance(model_versions, list)
147-
148-
# Use 'new' to create a new version if one doesn't exist yet.
149-
if len(model_versions) == 0:
150-
raise ValueError("No existing version of model '%s' to update."
151-
% name)
152-
153-
# Help function for extracting version number of REST response
154-
def get_version(x):
155-
return float(x.get('modelVersionName', 0))
156-
157-
if str(version).isnumeric():
158-
match = [x for x in model_versions if float(version) ==
159-
get_version(x)]
160-
assert len(match) <= 1
161-
162-
match = match[0] if len(match) else None
163-
elif str(version).lower() == 'latest':
164-
# Sort by version number and get first
165-
match = sorted(model_versions, key=get_version)[0]
166-
else:
167-
raise ValueError("Unrecognized version '%s'." % version)
168-
169-
# TODO: get ID of correct model version
170-
# if version != new, get existing model
171-
# get model (modelVersions) rel
172-
# -> returns list w/ id, modelVersionName, etc
173-
174139
files = files or []
175140

176141
# Find the project if it already exists
@@ -340,7 +305,18 @@ def get_version(x):
340305
project['eventProbabilityVariable'] = prediction_variable
341306
mr.update_project(project)
342307

343-
model = mr.create_model(model, project)
308+
# If replacing an existing version, make sure the model version exists
309+
if str(version).lower() != 'new':
310+
#Update an existing model with new files
311+
model_obj = mr.get_model(name)
312+
if model_obj is None:
313+
raise ValueError("Unable to update version '%s' of model '%s%. "
314+
"Model not found." % (version, name))
315+
model = mr.create_model_version(name)
316+
mr.delete_model_contents(model)
317+
else:
318+
#Assume new model to create
319+
model = mr.create_model(model, project)
344320

345321
assert isinstance(model, RestObj)
346322

src/sasctl/utils/pymas/core.py

Lines changed: 7 additions & 2 deletions
< 9E81 td data-grid-cell-id="diff-07e859046345c144cbd0efe822bd0a97785447ba5cfaa427bc00cf2c1351ed29-66-69-2" data-line-anchor="diff-07e859046345c144cbd0efe822bd0a97785447ba5cfaa427bc00cf2c1351ed29R69" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionLine-bgColor, var(--diffBlob-addition-bgColor-line));padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">+
else:
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,15 @@ def build_wrapper_function(func, variables, array_input, setup=None,
6060

6161
# HELPER: SAS to python char issue where SAS char have spaces and python string does not.
6262
# NOTE: we assume SAS char always need white space to be trimmed. This seems to match python model built so far
63+
# Note: Using this string fix to help with None or blank input situation for numerics
6364
string_input = ('', )
6465
for v in variables:
65-
if v.type == 'char' and not v.out:
66-
string_input += (" if {0}: {0} = {0}.strip()".format(v.name), )
66+
if not v.out:
67+
if v.type == 'char':
68+
string_input += (" if {0}: {0} = {0}.strip()".format(v.name), )
69
70+
string_input += (" if {0} == None: {0} = np.nan".format(v.name), )
71+
6772

6873
# Statement to execute the function w/ provided parameters
6974
if array_input:

tests/integration/test_pymas.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ def test_from_pickle(train_data, pickle_file):
185185
rc = py.appendSrcLine(' msg = ""');
186186
rc = py.appendSrcLine(' import numpy as np');
187187
rc = py.appendSrcLine('');
188+
rc = py.appendSrcLine(' if SepalLength == None: SepalLength = np.nan');
189+
rc = py.appendSrcLine(' if SepalWidth == None: SepalWidth = np.nan');
190+
rc = py.appendSrcLine(' if PetalLength == None: PetalLength = np.nan');
191+
rc = py.appendSrcLine(' if PetalWidth == None: PetalWidth = np.nan');
188192
rc = py.appendSrcLine(' inputarray = np.array([SepalLength,SepalWidth,PetalLength,PetalWidth]).reshape((1, -1))');
189193
rc = py.appendSrcLine(' column = ["SepalLength","SepalWidth","PetalLength","PetalWidth"]');
190194
rc = py.appendSrcLine(' import pandas as pd');

0 commit comments

Comments
 (0)
0