JavaScript Predictor Object

From Eigenvector Documentation Wiki
Jump to: navigation, search


The JavaScript Predictor Object is freely available to developers who wish to create JavaScript applications that interface with Solo_Predictor as does the Solo Predictor Field Monitor.

The object provides basic methods to get a list of available models (from a specific folder on the server), then apply those models to data stored in a DataCache object in Solo_Predictor. Finally, it also provides access to raw data, and Q or Hotelling's T-squared contributions (if the underlying model type provides contributions).

Available Models

These properties and methods relate to the models that are available to Solo_Predictor and which of those should be applied to data when it is imported.


.modelFolder Sets or retrieves the folder (on the server, that is the computer running Solo_Predictor) which should be searched for models to apply. When updated, the modelList property (see below) is automatically updated.
.modelList [READ ONLY] An array of strings which define all valid files located in the modelFolder on the server. The contents of this list will be automatically updated when the modelFolder property is updated or when the updateModelList method is called.
.modelListError [READ ONLY] The error (if any) returned after the last updateModelList call. If no error occurred, this property will be empty.
.updateModelListCallback A function that should be executed when updateModelList is executed. The function should expect a single input consisting of the Predictor object itself. For example:[BR]
     obj.updateModelListCallback = function (myobj) {
       if (myobj.modelListError) throw new Error(myobj.modelListError);
       //Do something with myobj.modelList here...


.updateModelList Contacts the server and requests the current list of models (.MAT and .TXT files) in the specified modelFolder. When the server responds, the results are stored in the modelList and modelListError properties and finally a call is made to the function specified in updateModelListCallback. If any errors occurred, the modelList will be empty.

Applying Models to Data

The following properties and methods relate to applying models to data and retrieving those results. They all relate to the singular method applyModels.


These properties control the behavior of the applyModels method:

.selectedModels An array of strings which indicate the names of models which should be applied. The model names can be taken from the modelList property defined above. If this array is empty, no models will be applied. If more than one model is listed, all models will be applied and Solo_Predictor will attempt to join all the results into a single results matrix. Changing the selectedModels list will generally cause the most recent results to be cleared.
.datacacheblock Identifies which DataCache object block should be connected to the predictor as the source of data to which the models should be applied. Generally, this will be always be = 1, but some advanced applications might make use of additional DataCache blocks.
.applyModelsCallback Provides a function that will be executed once the applyModels call has been completed. The supplied function should expect a single input consisting of the Predictor object itself.

These properties are read-only and provide results from the most recent completed applyModels call:

.data [READ ONLY] The DataSet object-encoded contents that define the results of applying the model(s) to the specified data cache.
.lastApplyUpdate [READ ONLY] The JavaScript timestamp of when the last call to applyModels was completed.
.lastApplyError [READ ONLY] The text of any errors that occurred during the last call to applyModels. If no errors occurred in the last call, this property will be an empty string. Errors may be server-related errors (can not communicate) or model errors (e.g. data size does not match that expected by model)


.applyModels Checks the server for new data in the indicated DataCache and applies any models listed in selectedModels to the data. When model application is done, the data, lastApplyUpdate, and lastApplyError properties are populated, and then the applyModelsCallback function is called.

Note: If no new data has appeared in the DataCache since the last applyModels call and the list of selected models hasn't changed, then the lastApplyUpdate timestamp is updated but no call will be made to the applyModelsCallback function. That is, update calls which did not change the data or error properties will not trigger a call to applyModelsCallback. Internally this is known as a "nochange" response from Solo_Predictor and is intended to reduce load on the server and client when no new data or results are available.


Once the Predictor has successfully applied the model(s) to data, the JavaScript Predictor has access to raw data, Q contributions, and T contributions (Hotelling's T^2 contributions). These are all obtained by a call to the dataDrill method as described below:

.dataDrill(mode,items,resultCallback) Makes a request to retrieve the contributions or data for one or more data points. The inputs include:
  • mode = a single character string indicating what kind of contributions to retreive: 'd' = data, 't' = t contributions, 'q' = q contributions
  • items = an array of one or more sample numbers for which the contributions should be returned. Zero-indexed relative to the results returned in the rows of the data property.
  • resultCallback = a function which should be called with an input consisting of a single input containing the returned result.

The results will be an object with the fields:

  • result : an array of arrays with the requested data. The top level array contains one array for each requested index in "items".
  • error : a string representation any errors that occurred during the call. Often, if error is non-empty, the result property will be empty.
  • date : a string representation of the date and time the sever completed the request.


The following VERY SIMPLE example creates a predictor object, sets the folder location, selects a model, then applies it. All output is done via the evri.debugmsg method in utilities.js:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>JavaScript Predictor Example</title>
    <link rel="stylesheet" href="default.css" type="text/css">
    <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="js/jquery.cookie.js"></script>
    <script type="text/javascript" src="js/prettyprint.js"></script>
    <script type="text/javascript" src="login/js/utilities.js"></script>
    <script type="text/javascript" src="js/solopredobj.js"></script>
   <div id="information"></div>
   <script type="text/javascript">
      evri.debug = true;
      var obj = new Predictor();

      //define callback for when model list updates
      obj.updateModelListCallback = function (item) {
        evri.debugmsg("Model List");
        obj.selectedModels = obj.modelList[0];  //choose first model

      //define callback for when models are applied     
      obj.applyModelsCallback = function (item) {
        evri.debugmsg("Got Results");

        //now we have results, do a data-drill to grab data for item #0      
        item.dataDrill('d',[0],function(results) { evri.debugmsg(results) });       


      //choose model folder (which also does an updateModelList call)       
      obj.modelsFolder   = "C:/Models/";