sasctl.pzmm.import_model#

class sasctl.pzmm.import_model.ImportModel[source]#

Bases: object

Methods

import_model(model_files, model_prefix, project)

Import a model to SAS Model Manager using pzmm submodule.

classmethod import_model(model_files: str | Path | dict, model_prefix: str, project: str | dict | RestObj, input_data: DataFrame | None = None, predict_method: Callable[[...], List] | List[Any] | None = None, score_metrics: List[str] | None = None, pickle_type: str = 'pickle', project_version: str = 'latest', missing_values: bool | None = False, overwrite_model: bool | None = False, score_cas: bool | None = True, mlflow_details: dict | None = None, predict_threshold: float | None = None, target_values: List[str] | None = None, overwrite_project_properties: bool | None = False, target_index: int | None = None, **kwargs) Tuple[RestObj, dict | str | Path][source]#

Import a model to SAS Model Manager using pzmm submodule.

Using pzmm, generate Python score code and import the model files into SAS Model Manager. This function automatically checks the version of SAS Viya being used through the sasctl Session object and creates the appropriate score code and API calls required for the model and its associated content to be registered in SAS Model Manager.

Generation of the score code requires that the input_data, predict_method, and score_metrics arguments are supplied. Otherwise, a warning will be generated stating that no score code is being created.

The following are generated by this function if a path is provided in the model_files argument:

  • *Score.py’

    The Python score code file for the model.

  • *.zip’

    The zip archive of the relevant model files. In Viya 3.5 the Python score code is not present in this initial zip file.

Parameters:
model_filesstring, Path, or dict

Either the directory location of the model files (string or Path object), or a dictionary containing the contents of all the model files.

model_prefixstring

The variable for the model name that is used when naming model files. (For example: hmeqClassTree + [Score.py || .pickle]).

projectstr, dict, or RestObj

The name or id of the model project, or a dictionary representation of the project.

input_datapandas.DataFrame, optional

The DataFrame object contains the training data, and includes only the predictor columns. The write_score_code function currently supports int(64), float(64), and string data types for scoring. The default value is None.

predict_method[function -> list, list], optional

The Python function used for model predictions and the expected output types. The expected output types can be passed as example values or as the value types. For example, if the model is a Scikit-Learn DecisionTreeClassifier, then pass either of the following:

  • [sklearn.tree.DecisionTreeClassifier.predict, [“A”]]

  • [sklearn.tree.DecisionTreeClassifier.predict_proba, [0.4, float]]

The default value is None.

score_metricsstring list, optional
The scoring score_metrics for the model. For classification models, it is

assumed that the first value in the list represents the classification

output. This function supports single and multi-class classification models. The default value is None.

pickle_typestring, optional

Indicator for the package used to serialize the model file to be uploaded to SAS Model Manager. The default value is pickle.

project_versionstring, optional

The project version to import the model in to on SAS Model Manager. The default value is “latest”.

overwrite_modelbool, optional

Set whether models with the same name should be overwritten when attempting to import the model. The default value is False.

score_casboolean, optional

Sets whether models registered to SAS Viya 3.5 should be able to be scored and validated through both CAS and SAS Micro Analytic Service. If set to false, then the model will only be able to be scored and validated through SAS Micro Analytic Service. The default value is True.

missing_valuesboolean, optional

Sets whether data handled by the score code will impute for missing values. The default value is False.

mlflow_detailsdict, optional

Model details from an MLFlow model. This dictionary is created by the read_mlflow_model_file function. The default value is None.

predict_thresholdfloat, optional
The prediction threshold for normalized probability score_metrics. Values

are expected to be between 0 and 1. The default value is None.

target_valueslist of strings, optional

A list of target values for the target variable. This argument and the score_metrics argument dictate the handling of the predicted values from the prediction method. The order of the target values should reflect the order of the related probabilities in the model. The default value is None.

overwrite_project_propertiesbool, optional

Set whether the project properties should be overwritten when attempting to import the model. The default value is False.

target_indexint, optional

Sets the index of success for a binary model. If target_values are given, this index should match the index of the target outcome in target_values. If target_values are not given, this index should indicate whether the the target probability variable is the first or second variable returned by the model. The default value is 1.

kwargsdict, optional
Other keyword arguments are passed to the following function:
  • sasctl.pzmm.ScoreCode.write_score_code(…,

    binary_h2o_model=False, binary_string=None, model_file_name=None, mojo_model=False, statsmodels_model=False, tf_keras_model=False

)

Returns:
RestObj

JSON response from the POST API call to SAS Model Manager for importing a zipped model

model_filesdict, str, or Path

Dictionary representation of all files or the path the model files were generated from.

notebook_output = False#
sasctl.pzmm.import_model.get_model_properties(target_values: list | str | None = None, model_files: str | Path | None = None)[source]#
sasctl.pzmm.import_model.model_exists(project: str | dict | RestObj, name: str, force: bool = 'False', version_name: str = 'latest') None[source]#

Checks if model already exists in the same project and either raises an error or delete the redundant model. If no project version is provided, the version is assumed to be “latest”.

Parameters:
projectstr, dict, or RestObj

The name or id of the model project, or a dictionary representation of the project.

namestr

The name of the model.

forcebool, optional

Sets whether to overwrite models with the same name upon upload. The default value is False.

version_namestr, optional

Name of project version to check if a model of the same name already exists. The default value is “latest”.

Raises:
ValueError

Model repository API cannot overwrite an already existing model with the upload model call. Alerts user of the force argument to allow model overwriting.

sasctl.pzmm.import_model.project_exists(project: str | dict | RestObj, response: str | dict | RestObj | None = None, target_values: list | str | None = None, model_files: str | Path | None = None, overwrite_project_properties: bool | None = False) RestObj[source]#

Checks if project exists on SAS Viya. If the project does not exist, then a new project is created or an error is raised.

Parameters:
projectstr, dict, or RestObj

The name or id of the model project, or a dictionary representation of the project.

responsestr, dict, or RestObj, optional

JSON response of the get_project() call to model repository service.

target_valueslist of strings, optional

A list of target values for the target variable. This argument and the score_metrics argument dictate the handling of the predicted values from the prediction method. The default value is None.

model_filesstring, Path, or dict

Either the directory location of the model files (string or Path object), or a dictionary containing the contents of all the model files.

overwrite_project_propertiesbool, optional

Set whether the project properties should be overwritten when attempting to import the model. The default value is False.

Returns:
responseRestObj

JSON response of the get_project() call to model repository service.

Raises:
SystemError

Alerts user that API calls cannot continue until a valid project is provided.