Skip to content

Commit b936354

Browse files
authored
Merge pull request #208 from sassoftware/score_update
Updates to score code and model card example
2 parents d945855 + 18c7c64 commit b936354

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

examples/pzmm_generate_complete_model_card.ipynb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,8 @@
568568
" df.columns = df.columns.str.replace(' ', '')\n",
569569
" df.columns = df.columns.str.replace('-', '_')\n",
570570
" df = df.drop(['Sex_Male'], axis=1)\n",
571-
" df = pd.concat([df, cat_vals], axis=1).drop('index', axis=1)\n",
571+
" if 'index' in df.columns or 'index' in cat_vals.columns:\n",
572+
" df = pd.concat([df, cat_vals], axis=1).drop('index', axis=1)\n",
572573
" # For the model to score correctly, all OHE columns must exist\n",
573574
" input_cols = [\n",
574575
" \"Education_9th\", \"Education_10th\", \"Education_11th\", \"Education_12th\", \"Education_Assoc_voc\", \"Education_Assoc_acdm\", \"Education_Masters\", \"Education_Prof_school\",\n",
@@ -579,9 +580,20 @@
579580
" 'Relationship_Not_in_family', 'Relationship_Own_child', 'Relationship_Unmarried', 'Relationship_Wife', 'Relationship_Other_relative', 'WorkClass_Private',\n",
580581
" 'Education_Bachelors'\n",
581582
" ]\n",
583+
" # OHE columns must be removed after data combination\n",
584+
" predictor_columns = ['Age', 'HoursPerWeek', 'WorkClass_Private', 'WorkClass_Self', 'WorkClass_Gov', \n",
585+
" 'WorkClass_Other', 'Education_HS_grad', 'Education_Some_HS', 'Education_Assoc', 'Education_Some_college',\n",
586+
" 'Education_Bachelors', 'Education_Adv_Degree', 'Education_No_HS', 'MartialStatus_Married_civ_spouse',\n",
587+
" 'MartialStatus_Never_married', 'MartialStatus_Divorced', 'MartialStatus_Separated', 'MartialStatus_Widowed',\n",
588+
" 'MartialStatus_Other', 'Relationship_Husband', 'Relationship_Not_in_family', 'Relationship_Own_child', 'Relationship_Unmarried',\n",
589+
" 'Relationship_Wife', 'Relationship_Other_relative', 'Race_White', 'Race_Black', 'Race_Asian_Pac_Islander',\n",
590+
" 'Race_Amer_Indian_Eskimo', 'Race_Other', 'Sex_Female']\n",
591+
"\n",
582592
" for col in input_cols:\n",
583593
" if col not in df.columns:\n",
584594
" df[col] = 0\n",
595+
" \n",
596+
"\n",
585597
" df[\"Education_Some_HS\"] = df[\"Education_9th\"] | df[\"Education_10th\"] | df[\"Education_11th\"] | df[\"Education_12th\"]\n",
586598
" df[\"Education_Assoc\"] = df[\"Education_Assoc_voc\"] | df[\"Education_Assoc_acdm\"]\n",
587599
" df[\"Education_Adv_Degree\"] = df[\"Education_Masters\"] | df[\"Education_Prof_school\"] | df[\"Education_Doctorate\"]\n",
@@ -593,6 +605,8 @@
593605
"\n",
594606
" df[\"MartialStatus_Other\"] = df[\"MartialStatus_Married_spouse_absent\"] | df[\"MartialStatus_Married_AF_spouse\"]\n",
595607
"\n",
608+
" df = df[predictor_columns]\n",
609+
"\n",
596610
" return df"
597611
]
598612
},

src/sasctl/pzmm/write_score_code.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ def _write_imports(
497497
import codecs
498498
499499
binary_string = "<binary string>"
500-
model = pickle.load(codecs.decode(binary_string.encode(), "base64"))
500+
model = pickle.loads(codecs.decode(binary_string.encode(), "base64"))
501501
"""
502502

503503
def _viya35_model_load(
@@ -562,6 +562,26 @@ def _viya35_model_load(
562562
f'{model_id}/{model_file_name}")))'
563563
)
564564
else:
565+
if pickle_type.lower() == "pickle":
566+
self.score_code += (
567+
f'model_path = Path("/models/resources/viya/{model_id}'
568+
f'")\nwith open(model_path / "{model_file_name}", '
569+
f"\"rb\") as pickle_model:\n{'':4}model = pd.read_pickle"
570+
"(pickle_model)\n\n"
571+
)
572+
"""
573+
model_path = Path("/models/resources/viya/<UUID>")
574+
with open(model_path / "model.pickle", "rb") as pickle_model:
575+
model = pd.read_pickle(pickle_model)
576+
577+
"""
578+
return (
579+
f"{'':8}model_path = Path(\"/models/resources/viya/{model_id}"
580+
f"\")\n{'':8}with open(model_path / \"{model_file_name}\", "
581+
f"\"rb\") as pickle_model:\n{'':12}model = pd.read_pickle"
582+
"(pickle_model)"
583+
)
584+
565585
self.score_code += (
566586
f'model_path = Path("/models/resources/viya/{model_id}'
567587
f'")\nwith open(model_path / "{model_file_name}", '
@@ -658,6 +678,23 @@ def _viya4_model_load(
658678
f"safe_mode=True)\n"
659679
)
660680
else:
681+
if pickle_type.lower() == "pickle":
682+
self.score_code += (
683+
f"with open(Path(settings.pickle_path) / "
684+
f'"{model_file_name}", "rb") as pickle_model:\n'
685+
f"{'':4}model = pd.read_pickle(pickle_model)\n\n"
686+
)
687+
"""
688+
with open(Path(settings.pickle_path) / "model.pickle", "rb") as pickle_model:
689+
model = pd.read_pickle(pickle_model)
690+
691+
"""
692+
return (
693+
f"{'':8}with open(Path(settings.pickle_path) / "
694+
f'"{model_file_name}", "rb") as pickle_model:\n'
695+
f"{'':12}model = pd.read_pickle(pickle_model)\n\n"
696+
)
697+
661698
self.score_code += (
662699
f"with open(Path(settings.pickle_path) / "
663700
f'"{model_file_name}", "rb") as pickle_model:\n'

tests/unit/test_write_score_code.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,13 @@ def test_viya35_model_load():
118118
"""
119119
sc = ScoreCode()
120120
load_text = sc._viya35_model_load("1234", "normal")
121-
assert "pickle.load(pickle_model)" in sc.score_code
122-
assert "pickle.load(pickle_model)" in load_text
121+
assert "pd.read_pickle(pickle_model)" in sc.score_code
122+
assert "pd.read_pickle(pickle_model)" in load_text
123+
124+
sc = ScoreCode()
125+
load_text = sc._viya35_model_load("1234", "normal", pickle_type="dill")
126+
assert "dill.load(pickle_model)" in sc.score_code
127+
assert "dill.load(pickle_model)" in load_text
123128

124129
sc = ScoreCode()
125130
mojo_text = sc._viya35_model_load("2345", "mojo", mojo_model=True)
@@ -142,8 +147,13 @@ def test_viya4_model_load():
142147
"""
143148
sc = ScoreCode()
144149
load_text = sc._viya4_model_load("normal")
145-
assert "pickle.load(pickle_model)" in sc.score_code
146-
assert "pickle.load(pickle_model)" in load_text
150+
assert "pd.read_pickle(pickle_model)" in sc.score_code
151+
assert "pd.read_pickle(pickle_model)" in load_text
152+
153+
sc = ScoreCode()
154+
load_text = sc._viya35_model_load("1234", "normal", pickle_type="dill")
155+
assert "dill.load(pickle_model)" in sc.score_code
156+
assert "dill.load(pickle_model)" in load_text
147157

148158
sc = ScoreCode()
149159
mojo_text = sc._viya4_model_load("mojo", mojo_model=True)

0 commit comments

Comments
 (0)