Skip to content

Commit 4082772

Browse files
committed
Allow writeScoreCode to include nonstandard pickle types
1 parent 07b7452 commit 4082772

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/sasctl/pzmm/importModel.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def pzmmImportModel(
113113
isH2OModel=False,
114114
force=False,
115115
binaryString=None,
116+
mlFlowDetails=None
116117
):
117118
"""Import model to SAS Model Manager using pzmm submodule.
118119
@@ -172,10 +173,16 @@ def pzmmImportModel(
172173
Sets whether to overwrite models with the same name upon upload. By default False.
173174
binaryString : string, optional
174175
Binary string representation of the model object. By default None.
176+
mlFlowDetails : dict, optional
177+
Model details from an MLFlow model. This dictionary is created by the readMLModelFile function.
178+
By default None.
175179
"""
176180
# Initialize no score code or binary H2O model flags
177181
noScoreCode = False
178182
binaryModel = False
183+
184+
if mlFlowDetails is None:
185+
mlFlowDetails = {'serialization_format': 'pickle'}
179186

180187
if pyPath is None:
181188
pyPath = Path(zPath)
@@ -236,6 +243,7 @@ def getFiles(extensions):
236243
isH2OModel=isH2OModel,
237244
isBinaryModel=binaryModel,
238245
binaryString=binaryString,
246+
pickleType=mlFlowDetails['serialization_format']
239247
)
240248
print(
241249
"Model score code was written successfully to {}.".format(
@@ -299,6 +307,7 @@ def getFiles(extensions):
299307
isH2OModel=isH2OModel,
300308
isBinaryModel=binaryModel,
301309
binaryString=binaryString,
310+
pickleType=mlFlowDetails['serialization_format']
302311
)
303312
print(
304313
"Model score code was written successfully to {} and uploaded to SAS Model Manager".format(

src/sasctl/pzmm/writeScoreCode.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def writeScoreCode(
2828
scoreCAS=True,
2929
isBinaryModel=False,
3030
binaryString=None,
31+
pickleType=None
3132
):
3233
"""
3334
Writes a Python score code file based on training data used to generate the model
@@ -107,6 +108,8 @@ def writeScoreCode(
107108
Sets whether the H2O model provided is a binary model or a MOJO model. By default False.
108109
binaryString : string, optional
109110
Binary string representation of the model object. By default None.
111+
pickleType : string optional
112+
Indicator for MLFlow models, which may pickle by non-standard methods. By default None.
110113
"""
111114
# Check if binary string model
112115
if binaryString is not None:
@@ -174,10 +177,10 @@ def upload_and_copy_score_resources(model, files):
174177
cls.pyFile.write(
175178
"""\n
176179
import math
177-
import pickle
180+
import {pickleType}
178181
import pandas as pd
179182
import numpy as np"""
180-
)
183+
).format(pickleType=pickleType)
181184
# In SAS Viya 4.0 and SAS Open Model Manager, a settings.py file is generated that points to the resource
182185
# location
183186
if not isViya35:
@@ -219,8 +222,8 @@ def upload_and_copy_score_resources(model, files):
219222
cls.pyFile.write(
220223
"""\n
221224
with open('/models/resources/viya/{modelID}/{modelFileName}', 'rb') as _pFile:
222-
_thisModelFit = pickle.load(_pFile)""".format(
223-
modelID=modelID, modelFileName=modelFileName
225+
_thisModelFit = {pickleType}.load(_pFile)""".format(
226+
modelID=modelID, modelFileName=modelFileName, pickleType=pickleType
224227
)
225228
)
226229
elif isViya35 and isBinaryModel:
@@ -234,8 +237,8 @@ def upload_and_copy_score_resources(model, files):
234237
cls.pyFile.write(
235238
"""\n
236239
with open(settings.pickle_path + '{modelFileName}', 'rb') as _pFile:
237-
_thisModelFit = pickle.load(_pFile)""".format(
238-
modelFileName=modelFileName
240+
_thisModelFit = {pickleType}.load(_pFile)""".format(
241+
modelFileName=modelFileName, pickleType=pickleType
239242
)
240243
)
241244
elif not isViya35 and isBinaryModel:
@@ -279,8 +282,8 @@ def score{modelPrefix}({inputVarList}):
279282
cls.pyFile.write(
280283
"""
281284
with open('/models/resources/viya/{modelID}/{modelFileName}', 'rb') as _pFile:
282-
_thisModelFit = pickle.load(_pFile)""".format(
283-
modelID=modelID, modelFileName=modelFileName
285+
_thisModelFit = {pickleType}.load(_pFile)""".format(
286+
modelID=modelID, modelFileName=modelFileName, pickleType = pickleType
284287
)
285288
)
286289
elif isViya35 and isH2OModel and not isBinaryModel:
@@ -302,8 +305,8 @@ def score{modelPrefix}({inputVarList}):
302305
cls.pyFile.write(
303306
"""
304307
with open(settings.pickle_path + '{modelFileName}', 'rb') as _pFile:
305-
_thisModelFit = pickle.load(_pFile)""".format(
306-
modelFileName=modelFileName
308+
_thisModelFit = {pickleType}.load(_pFile)""".format(
309+
modelFileName=modelFileName, pickleType=pickleType
307310
)
308311
)
309312
elif not isViya35 and isH2OModel:

0 commit comments

Comments
 (0)