diff --git a/examples/score_testing_example.ipynb b/examples/score_testing_example.ipynb index 05a76e66..3bb3109d 100644 --- a/examples/score_testing_example.ipynb +++ b/examples/score_testing_example.ipynb @@ -504,7 +504,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -531,8 +531,7 @@ " # Calculate the model statistics, ROC chart, and Lift chart; then write to json files\n", " pzmm.JSONFiles.calculate_model_statistics(\n", " target_value=1, \n", - " prob_value=0.5, \n", - " train_data=train_data, \n", + " train_data=train_data, \n", " test_data=test_data, \n", " json_path=path\n", " )\n", @@ -604,7 +603,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -613,7 +612,7 @@ " score_def_name=\"example_score_def_name\", # Name of the score_definition, which can be any string\n", " model='DecisionTreeClassifier', # Can use model name, UUID, or dictionary representation of the model\n", " table_name=\"HMEQPERF_1_Q1\", # Table name for input data\n", - " use_cas_gateway=False, # Change to True if your Viya version is compatible with CAS Gateway. \n", + " use_cas_gateway=True, # Change to False if your Viya version is not compatible with CAS Gateway. \n", " table_file='data/HMEQPERF_1_Q1.csv' # add the file path of HMEQPERF_1_Q1 if HMEQPERF_1_Q1 does not yet exist on the server. If the user doesn't need the file path argument, they can comment out this line completely.\n", ")\n" ] @@ -650,7 +649,7 @@ "outputs": [], "source": [ "# The following lines print the output table with scoring results. Ensure that the use_cas_gateway argument is the same as it is in the score definition call.\n", - "score_results = se.get_score_execution_results(score_execution, use_cas_gateway=False)\n", + "score_results = se.get_score_execution_results(score_execution, use_cas_gateway=True)\n", "score_results" ] }, @@ -682,14 +681,14 @@ " score_def_name=\"score_definition_example\",\n", " model='DecisionTreeClassifier',\n", " table_name='HMEQPERF_1_Q1', # If this call is made before running the code above, the table_file argument must be included if the file is not yet on the server\n", - " use_cas_gateway=False # Change to True if your Viya version is compatible with CAS Gateway. \n", + " use_cas_gateway=True # Change to False if your Viya version is not compatible with CAS Gateway. \n", ")" ] } ], "metadata": { "kernelspec": { - "display_name": ".venv", + "display_name": ".venv311 (3.11.16)", "language": "python", "name": "python3" }, @@ -703,7 +702,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.2" + "version": "3.11.16" } }, "nbformat": 4, diff --git a/src/sasctl/_services/score_definitions.py b/src/sasctl/_services/score_definitions.py index 05733d2b..0e3f4a57 100644 --- a/src/sasctl/_services/score_definitions.py +++ b/src/sasctl/_services/score_definitions.py @@ -4,7 +4,7 @@ from pathlib import Path import json -from typing import Union, Optional +from typing import Dict, Union, Optional from ..core import current_session, delete, get, sasctl_command, RestObj from .cas_management import CASManagement @@ -41,12 +41,13 @@ def create_score_definition( score_def_name: str, model: Union[str, dict], table_name: str, - use_cas_gateway: Optional[bool] = False, + use_cas_gateway: Optional[bool] = True, table_file: Union[str, Path] = None, description: str = "", server_name: str = "cas-shared-default", library_name: str = "Public", model_version: Union[str, dict] = "latest", + input_mapping: Optional[Dict] = None, ): """Creates the score definition service. @@ -70,6 +71,8 @@ def create_score_definition( The library within the CAS server the table exists in. Defaults to "Public". model_version: str, optional The user-chosen version of the model. Deafaults to "latest". + input_mapping: list, optional + A list of dictionaries mapping variables from the data table to the variables in the model if they are different. Defaults to None. Returns ------- @@ -101,11 +104,18 @@ def create_score_definition( try: inputMapping = [] for input_item in model.get("inputVariables"): - var = { - "mappingValue": input_item["name"], - "mappingType": "datasource", - "variableName": input_item["name"], - } + if input_mapping and input_item["name"] in input_mapping: + var = { + "mappingValue": input_mapping[input_item["name"]], + "mappingType": "datasource", + "variableName": input_item["name"], + } + else: + var = { + "mappingValue": input_item["name"], + "mappingType": "datasource", + "variableName": input_item["name"], + } inputMapping.append(var) except: @@ -133,7 +143,7 @@ def create_score_definition( # Checks if the model version is valid and how to find the name save_score_def = { - "name": model_name, # used to be score_def_name + "name": score_def_name, # used to be score_def_name "description": description, "objectDescriptor": { "uri": object_uri, diff --git a/src/sasctl/_services/score_execution.py b/src/sasctl/_services/score_execution.py index 5d464da8..850a4b9b 100644 --- a/src/sasctl/_services/score_execution.py +++ b/src/sasctl/_services/score_execution.py @@ -147,7 +147,7 @@ def poll_score_execution_state( @classmethod def get_score_execution_results( - cls, score_execution: Union[dict, str], use_cas_gateway: False + cls, score_execution: Union[dict, str], use_cas_gateway: True ): """Generates an output table for the score_execution results. diff --git a/src/sasctl/tasks.py b/src/sasctl/tasks.py index ca659630..856f9165 100644 --- a/src/sasctl/tasks.py +++ b/src/sasctl/tasks.py @@ -980,8 +980,9 @@ def score_model_with_cas( server_name: str = "cas-shared-default", library_name: str = "Public", model_version: str = "latest", - use_cas_gateway: bool = False, + use_cas_gateway: bool = True, timeout: int = 300, + input_mapping: dict = None, ): score_definition = sd.create_score_definition( score_def_name, @@ -993,6 +994,7 @@ def score_model_with_cas( library_name=library_name, model_version=model_version, use_cas_gateway=use_cas_gateway, + input_mapping=input_mapping, ) score_execution = se.create_score_execution(score_definition.id) score_execution_poll = se.poll_score_execution_state(score_execution, timeout)