Introduction
One of the repeating pattern I encounter in working with our pre-sales
teams is scoring user input using the cas server.
The three common use cases are:
- Model is published to a CAS destination
- Model code is datastep code stored in a modelTable(sashdat)
- Model code is in an astore stored in a sashdat.
The other common use case is scoring with models published to MAS. The MAS use case will be discussed in a
later blog
Main tools used in this blog
restaflib is a Javascript library build on top of SAS REST APIs and restaf library to address several common use cases.
Setup
Web application
Include the following two script tags.
<script src="https://unpkg.com/restaf@dev/dist/restaf.min.js"></script>
<script src="https://unpkg.com/restaflib@dev/dist/restaflib.min.js"></script>
Two globals restaf and restaflib will be available for use in your script tags.
Nodejs application
Install restaf and restaflib using the following command
npm install restaf@dev restaflib@dev
Import these into your nodejs application.
let restaf = require('restaf');
let restaflib = require('restaflib');
Flow
The flow for scoring is shown in the diagram below. This flow applies for the three scoring scenarios mentioned earlier.
Sample Code for scoring with model published to a Cas destination
The payload to store.logon depends on your authentication flow. Please see this link for details on the payload.
A typical code for scoring using models published to cas destination is shown below. The payload for the scoring has three key parts:
- The name of the model - this is used only in the case of model published to a cas destination
- The caslib and name of where the model is stored(a sashdat)
- An object with the values to be used for scoring
let store = restaf.initStore(); /* (1) */
store.logon(payload)
.then ( () => example())
.catch(err) => console.log(e)
async function example() {
let {session} = await casSetup(store); /* (2) */
let scenario = { /* (3) */
modelName: 'Gradient_Boosting_7adb0404_85e3_474d_9d03_1059ef7ae008',
model : { caslib: 'public', name: 'testpublish' },
scenario : {
sensor_ratio : 4.3,
days_out_of_service: 5
}
};
let r = await caslScore(store, session, scenario); /* (4) */
console.log(JSON.stringify(r, null,4));
}
The result is a Javascript object with the data returned from scoring. In the example above the result is:
{
"days_out_of_service": 5,
"sensor_ratio": 4.3,
"_Index_": 1,
"EM_EVENTPROBABILITY": 0.10521221221641,
"I_FAILURE": " 0",
"P_FAILURE0": 0.89478778778358,
"P_FAILURE1": 0.10521221221641,
"_WARN_": "",
"EM_CLASSIFICATION": " 0",
"EM_PROBABILITY": 0.89478778778358
}
Other scoring use cases
The code is identical to the one show above. The modelName is ignored in these cases. The model refers to the sashdat where the datastep
code or the astore is saved. See this link for examples of all 3 use cases.
Notes
- caslScore method will load the model if it is not in memory
- If there are multiple models whose name only vary in case, the first one found will be used
Finally
The next blog will discuss executing any casl program on the cas server using a flow similar to the one
discussed here.
All comments are welcomed.
Cheers...