Skip to content

Scoring in the Cas Server

devakumaraswamy edited this page Mar 20, 2020 · 1 revision

Introduction

One of the repeating pattern I see in many of the POCs is embedding scoring in user's web application. This blog shows how you can develop such applications with minimal code. The four use cases are:

  1. Model is published to MAS
  2. Model is published to a CAS destination
  3. Model code is datastep code stored in a modelTable(sashdat)
  4. Model code is in an astore stored in a sashdat.

_The MAS use case is discussed in another section.

Flow

The flow for scoring is shown in the diagram below. This flow applies for the use cases 2 thru 4. casl scoring flow

Sample Code for scoring with model published to a Cas destination

Please see the section on Setup later in this document on how to import restaf and restaflib into your application

A typical code for scoring using models published to cas destination is shown below. Besides the usual restaf calls to logon to the server, the two key methods are:

  • casSetup - This is used to obtain a cas session. The only parameter is the store
  • caslScore - This method executes the scoring on cas and returns the score information. The three arguments to this method are:
    • store - the restaf store object
    • session - the cas session object obtained via casSetup
    • payload - Information to be used for scoring. See the description below

The payload(see example below) has the following data:

  • 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)

  • The scenario to be scored

    let store = restaf.initStore(); /* (1) */ store.logon(payload) .then ( () => example()) .catch(err) => console.log(e)

    async function example() {

    let {session} = await restaflib.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 restaflib.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

  1. caslScore method will load the model if it is not in memory
  2. If there are multiple models whose name only vary in case, the first one found will be used
  3. The casl code used for scoring can be found here

Setup

Web Applications

Include the following two script tags.

    <script src="https://unpkg.com/@sassoftware/restaf/dist/restaf.min.js"></script>

    <script src="https://unpkg.com/@sassoftware/restaflib/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 @sassoftware/restaf @sassoftware/restaflib
Clone this wiki locally