Skip to content

Commit 9ba15e0

Browse files
committed
h2o models no longer require creating a zip file in SAS Model Manager
1 parent af9a468 commit 9ba15e0

File tree

3 files changed

+7
-30
lines changed

3 files changed

+7
-30
lines changed

src/sasctl/pzmm/import_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def import_model(
136136
model_prefix: str,
137137
project: Union[str, dict, RestObj],
138138
input_data: Optional[DataFrame] = None,
139-
predict_method: [Callable[..., List], List[Any]] = None,
139+
predict_method: Union[Callable[..., List], List[Any]] = None,
140140
score_metrics: Optional[List[str]] = None,
141141
pickle_type: str = "pickle",
142142
project_version: str = "latest",

src/sasctl/pzmm/pickle_model.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import shutil
88
from pathlib import Path
99
from typing import Optional, Union
10+
import h2o
1011

1112
from ..utils.misc import check_if_jupyter
1213

@@ -41,8 +42,7 @@ def pickle_trained_model(
4142
Parameters
4243
---------------
4344
trained_model : model object, str, or Path
44-
For non-H2O models, this argument contains the model variable. Otherwise,
45-
this should be the file path of the MOJO file.
45+
The trained model to be exported
4646
model_prefix : str or Path
4747
Variable name for the model to be displayed in SAS Open Model Manager
4848
(i.e. hmeqClassTree + [Score.py || .pickle]).
@@ -112,15 +112,7 @@ def pickle_trained_model(
112112
binary_file.rename(binary_file.with_suffix(PICKLE))
113113
# For MOJO H2O models, gzip the model file and adjust the file extension
114114
elif is_h2o_model and pickle_path:
115-
with open(Path(trained_model), "rb") as fileIn, gzip.open(
116-
Path(pickle_path) / (model_prefix + ".mojo"), "wb"
117-
) as fileOut:
118-
fileOut.writelines(fileIn)
119-
if cls.notebook_output:
120-
print(
121-
f"MOJO model {model_prefix} was successfully gzipped and saved "
122-
f"to {Path(pickle_path) / (model_prefix + '.mojo')}."
123-
)
115+
h2o.save_model(model=trained_model, force=True, path=pickle_path, filename=f'{model_prefix}.mojo')
124116
else:
125117
raise ValueError(
126118
"There is currently no support for file-less H2O.ai model handling."

src/sasctl/pzmm/write_score_code.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def write_score_code(
2424
cls,
2525
model_prefix: str,
2626
input_data: Union[DataFrame, List[dict]],
27-
predict_method: [Callable[..., List], List[Any]],
27+
predict_method: Union[Callable[..., List], List[Any]],
2828
target_variable: Optional[str] = None,
2929
target_values: Optional[List] = None,
3030
score_metrics: Optional[List[str]] = None,
@@ -515,23 +515,8 @@ def _viya4_model_load(
515515
pickle_type = pickle_type if pickle_type else "pickle"
516516

517517
if mojo_model:
518-
cls.score_code += (
519-
f"with gzip.open(Path(settings.pickle_path) / "
520-
'"{model_file_name}", "r") as fileIn, '
521-
"open(Path(settings.pickle_path) / "
522-
f"\"{str(Path(model_file_name).with_suffix('.zip'))}\","
523-
f" \"wb\") as fileOut:\n{'':4}shutil.copyfileobj(fileIn,"
524-
" fileOut)\nos.chmod(Path(settings.pickle_path) / "
525-
f"\"{str(Path(model_file_name).with_suffix('.zip'))}\""
526-
", 0o777)\nmodel = h2o.import_mojo("
527-
"Path(settings.pickle_path) / "
528-
f"\"{str(Path(model_file_name).with_suffix('.zip'))}\")"
529-
"\n\n"
530-
)
531-
return (
532-
f"{'':8}model = h2o.import_mojo(Path(settings.pickle_path) / "
533-
f"\"{str(Path(model_file_name).with_suffix('.zip'))}\")\n\n"
534-
)
518+
cls.score_code += "model = h2o.import_mojo(Path(settings.pickle_path))\n\n"
519+
return f"{'':8}model = h2o.import_mojo(Path(settings.pickle_path))\n\n"
535520
elif binary_h2o_model:
536521
cls.score_code += "model = h2o.load(Path(settings.pickle_path))\n\n"
537522
return f"{'':8}model = h2o.load(Path(settings.pickle_path))\n\n"

0 commit comments

Comments
 (0)