SiPAT Interface

From Eigenvector Research Documentation Wiki
Revision as of 14:47, 20 December 2011 by imported>Jeremy (→‎Regression Model Predictions)
Jump to navigation Jump to search

Introduction

Eigenevctor Research's PLS_Toolbox and Siemens' SiPAT product can be used together for deployment of PLS_Toolbox or Solo multivariate analysis models in process control applications. This integration utilizes Mathworks' Matlab and functionality built into SiPAT to run custom Matlab functions. These functions can include calls to PLS_Toolbox functions.

The following discusses the PLS_Toolbox-specific configuration and provides example m-files which can be used to quickly get SiPAT and PLS_Toolbox integrated.

Licensing

Note that the PLS_Toolbox license requires that, unless special site licensing arrangements are made, you must obtain a separate PLS_Toolbox license for each instance of any PLS_Toolbox code you wish to deploy. Special licensing options are available. See the Eigenvector Website for more information.

Installation and Basic Configuration

SiPAT Configuration

The user is directed to the Siemens documentation for details on configuration of SiPAT for use with Matlab. The discussion below describes some of the PLS_Toolbox-specific considerations of this configuration.

Matlab and PLS_Toolbox Configuration

With Matlab already installed (as per The Mathworks installation instructions), PLS_Toolbox can be copied onto the target computer using any of the available file types as described in the standard installation instructions. The primary differences from the standard installation will be that you will create two special files and place these files into the PLS_Toolbox/utilities folder on the target computer:

  1. Create a text file named evrilicense.lic and put your license code (available from the Eigenvector Research download page) as a single line in that file. You can also obtain a evrilicense.lic file from the Eigenvector Helpdesk.
  2. Create an empty text file named evrinetwork.lic. This file will instruct PLS_Toolbox to ignore installation errors and run without warnings (necessary when calling PLS_Toolbox functions from within SiPAT.)

Specific Model and M-file Configuration

The SiPAT interface into Matlab provides for the specification of a data file to load and a Matlab function to execute (along with specific input and output configuration information.)

In the examples given here, the data file will be in the Matlab MAT format and will contain the model to apply. The Matlab function will be given in the Matlab m-file format and will contain the specific instructions for applying the model and returning the results to SiPAT. In these examples, it is assumed there is only one model that is being applied per each method (no special calibration transfer or other pre-transformation steps being used).

Creation of the Model MAT File

The model you wish to apply to new data should be saved from Matlab or Solo into a MAT file as the one and only item stored in the MAT file. In the Analysis Window, this is done by selecting the menu item: File > Save Model and using the WorkspaceBrowser_ImportingData#To_save_imported_data_to_a_.mat_file save dialog to specify a filename and an item name. This will save the model into the given filename with the specified item name. Although the MAT file name can be any standard filename, it will make m-file construction easier if the item name used is always the same in all saved models. In the example here, we will assume that the item is called "model". If a different name is used, the SiPAT-specific configuration comments in the m-file (see below) will need to be modified to indicate to SiPAT the name used for the model.

If saving the model from the Matlab command line, the following command can be used (assuming the model to use is currently named "model" in your Matlab workspace) :

 save myfile.mat model

Creation of the Function M-file

The second part to the SiPAT/PLS_Toolbox interface is a Matlab m-file which contains a function definition (Note: The contents of this m-file must actually be a Matlab function, meaning it must contain a function header line as shown in the scripts below. It cannot be a "script" in the strict Matlab definition of that term which implies code that is not wrapped inside a function definition.)

Three example m-files are given below: one for use with any of the regression model types (PLS, PCR, MLR, CLS, SVM, LWR, etc), one for use with classification model types (PLSDA, KNN, SIMCA, SVMDA/SVM-C), and one for use with principal component analysis (PCA) models. Each of these m-files assumes that the input is a single vector of values (passed by SiPAT) and each returns two or three values which correspond to the predictions from the model.

These functions all also assume that the input data will be two columns where the first column contains axis scale information for the variables (such as wavelength, m/z, time, etc) and the second column is the actual measured data. If this does not fit the type of data being passed by SiPAT (e.g. no axisscale information), the initial lines:

    %convert second column of input data into a dataset (if not appropriate column, change next line)
    x = data(:,2);
    x = dataset(double(x'));

    %Assume first column is axisscale information. If not true, comment out the next line
    x.axisscale{2} = double(data(:,1));

should be converted as necessary to handle the input data. For example, if only a single column of values is being passed, the following can be substituted in for the above code:

    %convert column of input data into a dataset
    x = dataset(double(data'));

Regression Model Predictions

The following m-file contents are appropriate for use with regression model types:

%START SIPAT
%<CONFIG>
%   <MODEL>REGRESSION</MODEL>
%   <PREFIX></PREFIX>
%   <SUFFIX></SUFFIX>
%   <INPUTS>
%       <INPUT Name="data" XDataType="MultiValue" YDataType="Single" />
%   </INPUTS>
%   <OUTPUTS>
%       <OUTPUT Name="y" XDataType="SingleValue" YDataType="Double" />
%       <OUTPUT Name="q_x" XDataType="SingleValue" YDataType="Double" />
%       <OUTPUT Name="H_x" XDataType="SingleValue" YDataType="Double" />
%   </OUTPUTS>
%   <FUNCTION>[y,q_x,H_x]=regpred(data,model)</FUNCTION>
%</CONFIG>
%END SIPAT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Regression using PLS_toobox from Eigenvector
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This function calculates the responses y and the Q-residuals
%(q_x) and Hotellings T2 (H_x) corresponding to the input variables data
%(data) using a standard model structure.
%
%Before using this function, make sure you loaded the following data into
%the workspace:
%  model: Model structure used in PLS_Toolbox/Solo including all pretreatment.

function [y,q_x,H_x] = regpred(data,model)

try
    %convert second column of input data into a dataset (if not appropriate column, change next line)
    x = data(:,2);
    x = dataset(double(x'));

    %Assume first column is axisscale information. If not true, comment out the next line
    x.axisscale{2} = double(data(:,1));

    %make a prediction
    opts =[];
    opts.plots='none';
    opts.display='off';
    pred_x = feval(lower(model.modeltype),x,model,opts);

    %return prediction in y  
    y = double(pred_x.pred{2});
    if isfield(pred_x,'ssqresiduals') 
        q_x = double(pred_x.ssqresiduals{1,1}./pred_x.detail.reslim{1});
        H_x = double(pred_x.tsqs{1}./pred_x.detail.tsqlim{1});
    else
        q_x = 0;
        H_x = 0;
    end

catch
    %errors are saved to the following file (change location as desired)
    fid=fopen('C:\sipat_error_sout.txt','w');
    fwrite(fid,encode(lasterror));
    fwrite(fid,encode(evalin('base','whos')));
    fclose(fid);
end

Classification Model Predictions

The following m-file contents are appropriate for use with classification model types:

%START SIPAT
%<CONFIG>
%   <MODEL>CLASSIFICATION</MODEL>
%   <PREFIX></PREFIX>
%   <SUFFIX></SUFFIX>
%   <INPUTS>
%       <INPUT Name="data" XDataType="MultiValue" YDataType="Single" />
%   </INPUTS>
%   <OUTPUTS>
%       <OUTPUT Name="y" XDataType="SingleValue" YDataType="Double" />
%       <OUTPUT Name="q_x" XDataType="SingleValue" YDataType="Double" />
%       <OUTPUT Name="H_x" XDataType="SingleValue" YDataType="Double" />
%   </OUTPUTS>
%   <FUNCTION>[y,q_x,H_x]=classpred(data,model)</FUNCTION>
%</CONFIG>
%END SIPAT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Classification using PLS_toobox from Eigenvector
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This function calculates the numerical class assignment, the Q-residuals
%(q_x) and Hotellings T2 (H_x) corresponding to the input variables data
%(data) using a standard model structure.
%
%Before using this function, make sure you loaded the following data into
%the workspace:
%  model: Model structure used in PLS_Toolbox/Solo including all pretreatment.

function [y,q_x,H_x] = classpred(data,model)

try
    %convert second column of input data into a dataset (if not appropriate column, change next line)
    x = data(:,2);
    x = dataset(double(x'));

    %Assume first column is axisscale information. If not true, comment out the next line
    x.axisscale{2} = double(data(:,1));

    %make a prediction
    opts =[];
    opts.plots='none';
    opts.display='off';
    pred_x = feval(lower(model.modeltype),x,model,opts);

    %return prediction in y  
    y = double(pred_x.classification.mostprobable); 
    if isfield(pred_x,'ssqresiduals') 
        q_x = double(pred_x.ssqresiduals{1,1}./pred_x.detail.reslim{1});
        H_x = double(pred_x.tsqs{1}./pred_x.detail.tsqlim{1});
    else
        q_x = 0;
        H_x = 0;
    end

catch
    %errors are saved to the following file (change location as desired)
    fid=fopen('C:\sipat_error_sout.txt','w');
    fwrite(fid,encode(lasterror));
    fwrite(fid,encode(evalin('base','whos')));
    fclose(fid);
end