Software Development Kit (SDK)

From Eigenvector Research Documentation Wiki
Revision as of 13:49, 27 February 2023 by Bob (talk | contribs) (→‎C#)
Jump to navigation Jump to search

Solo_Predictor Software Development Kit (SDK) Overview

In order to facilitate communication with and operations in Solo_Predictor from external environments, Eigenvector Research provides a software development kit (SDK) for common application languages. At this time the SDK is available in Python. Matlab, Java, and C# ports are planned for later release. Contact the help desk at helpdesk@eigenvector.com to obtain the SDK package.

The SDK includes a number of methods that cover a signification portion of common usage for deploying an existing model with new data. A description of the methods - inputs, outputs, options - may be found in the above table. These methods will, for the most part, be common across all platforms and exceptions will be clearly noted.


SDK Methods

method function arguments Python returns MATLAB returns C# returns
getLastResponse() last response returned by Solo/Solo_Predictor, typically in XML format none string(plain or XML) string(plain or XML) string(plain or XML)
getLastError() last error generated in operations none string(plain) string(plain) string(plain)
clearVariables() clear all workspace variables none Boolean Boolean Boolean
listVariables() list of workspace variables none list string array string[]
applyModel() apply workspace variable mdl to workspace variable data none Boolean Boolean Boolean
setDataFile(pathString) load specified file (method argument) and convert to workspace variable data string - path to data file Boolean Boolean Boolean
setModelFile(pathString) load specified file (method argument) and convert to workspace variable mdl string - path to model file (.mat extension required) Boolean Boolean Boolean
setOutputFormat(formatString) specify output format for prediction results - choice of field->value object or XML string
Python: choice of "dict" or "xml" (case insensitive)
MATLAB: choice of "struct", "containers.Map", or "xml"
C#: choice of "dict" or "xml"
Boolean Boolean Boolean
setPort(portValue) specify communication port with Solo/Solo_Predictor integer or string (which can be converted to integer) in the range of 1024:65535; default value = 2211 Boolean Boolean Boolean
setIPAddress(IPAddressString) specify IP address to communicate with Solo/Solo_Predictor string with valid value for IP address; default value is 127.0.0.1 Boolean Boolean Boolean
getDataFile() return data file set by setDataFile none string(plain) string(plain) string(plain)
getModelFile() return model file set by setModelFile none string(plain) string(plain) string(plain)
getPort() return port value set by setPort/default value none integer integer integer
getIPAddress() return IP address set by setIPAddress/default value none string(plain) string(plain) string(plain)
getOutputFormat() return output format for model predictions as set by setOutputFormat/default value none string(plain) string(plain) string(plain)
getPredictionResults() return model prediction values as either field->value object or XML formatted dataset object none string(XML) or dict; empty string if error encountered string(XML), struct, or containers.Map string(XML) or Dictionary
getModelInfo() return info from loaded model none string(plain) string(plain) string(plain)
getPredictionResultsVarNames() fields of field->value object for prediction outputs none list string array string[]
getVersion(modeString) returns version information for Solo_Predictor string with value of "terse" or "full"; default value (no input) is "terse" string(plain) or dict string(plain) or containers.Map string(plain) or Dictionary
runIncludeFile(pathString) execute content of text file pathString containing valid Solo scripting commands string to text file containing valid Solo scripting commands string(XML) string(XML) string(XML)
getVersionSDK() return current version of SDK none string(plain) string(plain) string(plain)

A number of the methods return Boolean values indicating success or failure at completing the desired operation. When a return value of False is obtained, detail surrounding the nature of the error may be found from the .getLastError() method. It's important to note that communication errors with Solo_Predictor are not handled by the SDK. As such, your code for communicating with Solo_Predictor should include the platform-appropriate error-trapping procedures for such instances.

Supported Models

The following model types are supported by the SDK:

PCA
MCR
Regression models
PLS
PCR
LWR
SVM-R
MLR
CLS
ANN
XGB
Classification models
PLSDA
SIMCA
SVM-C
ANNDA
XGBDA
LREGDA
PARAFAC

Solo_Predictor Requirements

The target Solo_Predictor must be running and have had its default.xml file modified so its "keywordonly" tag set = 0. See keywordonly

Python Requirements

The Python version of the SDK has been tested using Python versions 3.7 and 2.7. The following table lists the versions of libraries used with the tested versions of Python:

library Python 3.7 Python 2.7
requests 2.24 2.24
bs4 4.9.1 4.9.1
numpy 1.19.2 1.16.6
lxml 4.5.2 4.5.2

C# Requirements

library Version
HtmlAgilityPack 1.11

Example

A working example is provided below with comments for many of the steps.

Configuration

Python

from evrisdk import EvriSdk
curInstance = EvriSdk()
curInstance.setIPAddress("127.0.0.1")
curInstance.setPort(2211)

MATLAB

curInstance = evrisdk()
curInstance.setIPAddress("127.0.0.1")
curInstance.setPort(2211)

C#

using evrisdk; // use in header
// then put the following in class instantiation
EvriSdk curInstance = new EvriSdk();
curInstance.setIPAddress("127.0.0.1");
curInstance.setPort(2211);

After creating an instance of the EvriSdk class, the next two lines set the IP address of the computer running Solo_Predictor (here using the localhost address) and the port. The latter may be configured with the argument as either an integer or a string. Note that these lines are somewhat redundant as the values provided are the default ones.

Solo_Predictor Workspace

retVal = curInstance.clearVariables()
variableList = curInstance.listVariables()

The .clearVariables() method will clear the Solo_Predictor workspace with a Boolean return indicating the success or failure of the operation. Verification of this step is accomplished from the .listVariables() method.

Loading Data and Model

The following code segment will a) load a data file, b) load a model file, c) get a list of the prediction outputs from the model, and d) return information on the model (model type, date constructed, etc.):

retVal        = curInstance.setDataFile(fullPathToDataFile)
retVal        = curInstance.setModelFile(fullPathToModelFile)
predVarList   = curInstance.getPredictionResultsVarNames()
modelInfo     = curInstance.getModelInfo()

C#

Make sure to have the data type of the returned variables:

bool retVal          = curInstance.setDataFile(fullPathToDataFile)
bool retVal          = curInstance.setModelFile(fullPathToModelFile)
var predVarList      = curInstance.getPredictionResultsVarNames()
string modelInfo     = curInstance.getModelInfo()

A few comments are in order:

  • data may be imported from any of the files supported by Solo_Predictor; see this page for importing data into Solo_Predictor
  • if data is imported from a Matlab file, the file may contain only one variable (at this time the SDK does not support importing specified variables from a Matlab file)
  • a Matlab file (file extension: .mat) is expected for loading a model file. Any other extension will result in an error
    • currently all EVRI model types are supported by the .setModelFile method except for calibration transfer and hierarchical models
  • the Python list output will contain the variables generated from from model.plotscores(psops), where psops is a structure created from
psops = plotscores('options');
psops.reducedstats = {'q' 't2'};

Apply Model and Return Results

Applying the model to the data and reviewing the outputs with some error trapping in the event the model application fails:

Python

retVal   = curInstance.applyModel()
if retVal:
    curInstance.setOutputFormat("dict")
    predResults = curInstance.getPredictionResults()
    for key in predResults:
        print(key, "=>", res[key])
else:
    print(curInstance.returnLastError())

In the above, each value of res[key] will be a numpy array containing as many elements as there are samples in the data which has been loaded.

MATLAB

The following code assumes the output format is containers.Map.

retVal   = curInstance.applyModel();
if retVal
    curInstance.setOutputFormat("containers.Map");
    predResults = curInstance.getPredictionResults();
    fnames = keys(predResults);
    for i=1:length(fnames)
        disp([fnames{i} "=>", num2str(predResults(fnames{i}))]);
    end
else
    disp(curInstance.returnLastError())
end

C#

The following code assumes the output format is dict.

bool retVal   = curInstance.applyModel();
if (retVal)
    {
    curInstance.setOutputFormat("dict");
    var pred = curInstance.getPredictionResults();
    foreach (var kvp in pred)
            {
                Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, string.Join(", ",kvp.Value));
                Console.WriteLine();
            }
    }
else
    {
    Console.WriteLine(curInstance.returnLastError())
    }

To look at the contents of the workspace:

variableList = curInstance.listVariables()

IF XML output format is specified - curInstance.setOutputFormat("xml") - then the variable predResults will be an XML formatted string of the dataset object output. As an example, when applying a PCA model built in the arch demo dataset using 3 PCs and applying it to a test sample, specifying XML format results gives:

<response>
  <result class="dataset">
    <name class="string" />
    <type class="string">data</type>
    <author class="string" />
    <date class="numeric" size="[1,6]">2020,9,3,14,47,53.553401</date>
    <moddate class="numeric" size="[1,6]">2020,9,3,14,47,53.593668</moddate>
    <imagesize class="numeric" size="[0,0]" />
    <imagemode class="numeric" size="[0,0]" />
    <data class="numeric" size="[1,8]">-0.00223280606154,-0.00145155625614,-0.00357717299812,9.42744403283e-05,1.63676200186e-05,5.71331078063e-05,1.91433195948e-06,0.847852727433</data>
    <label class="cell" size="[2,1]">
      <tr>
        <td class="string" />
      </tr>
      <tr>
        <td class="string">
          <sr>Scores on PC 1       </sr>
          <sr>Scores on PC 2       </sr>
          <sr>Scores on PC 3       </sr>
          <sr>Q Residuals          </sr>
          <sr>Hotelling T^2        </sr>
          <sr>Q Residuals Reduced  </sr>
          <sr>Hotelling T^2 Reduced</sr>
          <sr>KNN Score Distance   </sr>
        </td>
      </tr>
    </label>
    <labelname class="cell" size="[2,1]">
      <tr>
        <td class="string" />
      </tr>
      <tr>
        <td class="string" />
      </tr>
    </labelname>
    <axisscale class="cell" size="[2,1]"> 
    . . .

By contrast, specifying Python dict returns the following:

    {'KNN Score Distance': array([0.84785273]), 'Hotelling T^2 Reduced': array([1.91433196e-06]), 
    'Q Residuals': array([9.42744403e-05]), 'Scores on PC 1': array([-0.00223281]), 
    'Scores on PC 3': array([-0.00357717]), 'Scores on PC 2': array([-0.00145156]), 
    'Q Residuals Reduced': array([5.71331078e-05]), 'Hotelling T^2': array([1.636762e-05])}

The latter format provides only numerical results and the appropriate tags, in this case, the keys of the Python dict. The advantage of the former is that any metadata associated with mode 1 of the loaded data - labels, classes, axis scales - is copied to the prediction results output. However, parsing of the XML output falls to the end user. Output formats containers.Map or struct for Matlab, and dict for C# will generate similar results as what is shown above.

.runIncludeFile Method

For more advanced operations, a text file containing valid Solo_Predictor scripting commands may be used with the .runIncludeMethod. As an example,

retVal = curInstance.runIncludeFile(FullPathToIncludeScript)

returns

<response>
  <result class="numeric" size="[1,3]">-0.00223280606154,-0.00145155625614,-0.00357717299812</result>
  <error class="string" />
  <date class="string">Thu 03 Sep 2020 15:25:52</date>
</response>

for the following commands contained within the script file:

:clear
myMdl='FullPathToModelFile';
myData='FullPathToDataFile';
myPred=myMdl|myData;
myPred.scores;

As previously, parsing of the XML output is the responsibility of the end user.