Skip to content

Commit ae5fcf0

Browse files
committed
Allow writeScoreCode to include nonstandard pickle types
1 parent ea42699 commit ae5fcf0

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
@@ -114,6 +114,7 @@ def pzmmImportModel(
114114
force=False,
115115
binaryString=None,
116116
missingValues=False,
117+
mlFlowDetails=None
117118
):
118119
"""Import model to SAS Model Manager using pzmm submodule.
119120
@@ -176,10 +177,16 @@ def pzmmImportModel(
176177
missingValues : boolean, optional
177178
Sets whether data used for scoring needs to go through imputation for
178179
missing values before passed to the model. By default False.
180+
mlFlowDetails : dict, optional
181+
Model details from an MLFlow model. This dictionary is created by the readMLModelFile function.
182+
By default None.
179183
"""
180184
# Initialize no score code or binary H2O model flags
181185
noScoreCode = False
182186
binaryModel = False
187+
188+
if mlFlowDetails is None:
189+
mlFlowDetails = {'serialization_format': 'pickle'}
183190

184191
if pyPath is None:
185192
pyPath = Path(zPath)
@@ -241,6 +248,7 @@ def getFiles(extensions):
241248
isBinaryModel=binaryModel,
242249
binaryString=binaryString,
243250
missingValues=missingValues,
251+
pickleType=mlFlowDetails['serialization_format']
244252
)
245253
print(
246254
"Model score code was written successfully to {}.".format(
@@ -305,6 +313,7 @@ def getFiles(extensions):
305313
isBinaryModel=binaryModel,
306314
binaryString=binaryString,
307315
missingValues=missingValues,
316+
pickleType=mlFlowDetails['serialization_format']
308317
)
309318
print(
310319
"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:
@@ -226,8 +229,8 @@ def upload_and_copy_score_resources(model, files):
226229
cls.pyFile.write(
227230
"""\n
228231
with open('/models/resources/viya/{modelID}/{modelFileName}', 'rb') as _pFile:
229-
_thisModelFit = pickle.load(_pFile)""".format(
230-
modelID=modelID, modelFileName=modelFileName
232+
_thisModelFit = {pickleType}.load(_pFile)""".format(
233+
modelID=modelID, modelFileName=modelFileName, pickleType=pickleType
231234
)
232235
)
233236
elif isViya35 and isBinaryModel:
@@ -241,8 +244,8 @@ def upload_and_copy_score_resources(model, files):
241244
cls.pyFile.write(
242245
"""\n
243246
with open(settings.pickle_path + '{modelFileName}', 'rb') as _pFile:
244-
_thisModelFit = pickle.load(_pFile)""".format(
245-
modelFileName=modelFileName
247+
_thisModelFit = {pickleType}.load(_pFile)""".format(
248+
modelFileName=modelFileName, pickleType=pickleType
246249
)
247250
)
248251
elif not isViya35 and isBinaryModel:
@@ -286,8 +289,8 @@ def score{modelPrefix}({inputVarList}):
286289
cls.pyFile.write(
287290
"""
288291
with open('/models/resources/viya/{modelID}/{modelFileName}', 'rb') as _pFile:
289-
_thisModelFit = pickle.load(_pFile)""".format(
290-
modelID=modelID, modelFileName=modelFileName
292+
_thisModelFit = {pickleType}.load(_pFile)""".format(
293+
modelID=modelID, modelFileName=modelFileName, pickleType = pickleType
291294
)
292295
)
293296
elif isViya35 and isH2OModel and not isBinaryModel:
@@ -309,8 +312,8 @@ def score{modelPrefix}({inputVarList}):
309312
cls.pyFile.write(
310313
"""
311314
with open(settings.pickle_path + '{modelFileName}', 'rb') as _pFile:
312-
_thisModelFit = pickle.load(_pFile)""".format(
313-
modelFileName=modelFileName
315+
_thisModelFit = {pickleType}.load(_pFile)""".format(
316+
modelFileName=modelFileName, pickleType=pickleType
314317
)
315318
)
316319
elif not isViya35 and isH2OModel:

0 commit comments

Comments
 (0)