5.9 Curve Fitting and Batch Processing with LabTalk

Summary

In this tutorial, there are a number of script examples that show how to import data, perform data manipulation, curve fit and batch process multiple sets of data in a loop.

What you will learn

This tutorial will show you how to:

  1. Import Data
  2. Perform Nonlinear Regression
  3. Perform Linear Regression
  4. Batch Process

Importing Data

Some LabTalk scripts you write will operate on data that has already been imported or entered in your worksheet.

Often times you may need to write LabTalk script to import selected data files.

Every file type supported by Origin for import has its own x-function. For example when you select the Data: Import from File:CSV menu option, choose your data file(s) in the dialog that opens and then make sure the Show Options Dialog checkbox is checked when you click OK, the Import and Export: impCSV dialog will open. The title of this dialog informs you of the name of the x-function used for import.

Another example, to import pCLAMP files use the imppClamp x-function.


Importing a *.dat File

To import an ASCII file, use the impASC x-function.

 string fname$ = system.path.program$ + "Samples\Curve Fitting\Multiple Gaussians.dat";
 impASC fname:=fname$;

ImpASC Options TreeNode

When you do an import from the menu, you have the option of opening up the Options dialog. The settings in this dialog are accessed by the Treenode variable type.

The impASC x-function has an input variable as follows:

 options: [in] 
   type=TreeNode
   default=<unassigned>
   description=Specify the import options in the dialog.

This example makes use of many advanced options of the impASC X-Function. Notice that there is only one semi-colon (following all options assignments) indicating that all are part of the call to impASC.

 string fn$=system.path.program$ + "Samples\Spectroscopy\HiddenPeaks.dat"; 
 impasc fname:=fn$ 
 options.ImpMode:=3                        /* start with a new book */
 options.Sparklines:=0                     /* turn off sparklines */
 options.Names.AutoNames:=0                /* turn off auto rename */
 options.Names.FNameToSht:=1               /* rename sheet to file name */
 options.Miscellaneous.LeadingZeros:=1;    /* remove leading zeros */

Fitting your data

Fitting in LabTalk requires the following three x-functions and used in that order:

  • nlbegin
  • nlfit
  • nlend

NLBEGIN x-function

Begin the fitting process. Define input data, type of fitting function, and input parameters.

This function has many input variables. The only one that has no default value is the func variable, so when you use nlbegin at the very least you need to specify your function. The function you specify will be used to fit the active XY Range, just like when you fit from the Analysis:Fitting:Nonlinear Curve Fit menu.

// initialize fitting the active plot using gauss model
nlbegin iy:=1 func:=gauss nltree:=tt; 
// initialize expgrow1 model to fit data with column 1 as X and column 2 as Y
nlbegin iy:=(1,2) func:=expgrow1 nltree:=tt;

The nltree variable is an important one:

 nltree: [in/out] 
 type=TreeNode
 default=nlt
 description=Tree containing the information of fitting such as parameter values, standard 
               error, etc.

NLFIT x-function

This function performs the fit calculations. It has two input variables:

Variables:

 n: [in] 
   type=int
   default=-1
   description=Number of Iterations
 method: [in] 
   type=int
   default=0
   description=Iteration Method
   allowed values: 0=lm:LM, 1=s:Simplex

Script Usage Examples:

 nlfit 0; // update fit cuves and recalculate chisqr
 nlfit 1; // do only one iteration
 nlfit; // fit until converge

NLEND x-function

This x-function ends the fitting process and outputs parameter values.

 nlend output:=1 autoupdate:=1

Gaussian Fitting Complete Example

// Begin non-linear fitting, taking input data from Column 1 (X) and 
// Column 2 (Y) of the active worksheet,
// specifying the fitting function as Gaussian, 
// and creating the input parameter tree named ParamTree:
nlbegin iy:=(1,2) func:=gauss nltree:=ParamTree;
// Optional: let the peak center be fixed at X = 5 
ParamTree.xc = 25;     // Assign the peak center an X-value of 5.
ParamTree.f_xc = 1;   // Fix the peak center (f_xc = 0 is unfixed).
// Perform the fit calculations:
nlfit;
// Optional: report results to the Script Window.
type Baseline y0 is $(ParamTree.y0),; 
type Peak Center is $(ParamTree.xc), and; 
type Peak width (FWHM) is $(ParamTree.w);
// end the fitting session without a Report Sheet
nlend;


Batch Process

One way to achieve batch processing is to loop over multiple files or datasets, and within the loop process each dataset by calling appropriate X-Functions and other script commands to perform the necessary data processing.

Batch Nonlinear Curve Fitting

The following example shows how to import 10 files and perform a curve fit operation and print out the fitting results:

 // Find all files using wild card
 string path$ = system.path.program$ + "Samples\Batch Processing";
 findFiles ext:="T*.csv";
 
 // Start a new book with no sheets
 newbook sheet:=0;
 // Loop over all files
 for(int iFile = 1; iFile <= fname.GetNumTokens(CRLF); iFile++)
 {
     // Get file name
     string file$ = fname.GetToken(iFile, CRLF)$;
     // Import file into a new sheet
     newsheet;
     impasc file$;
     // Perform gaussian fitting to col 2 of the current data
     nlbegin iy:=2 func:=gaussamp nltree:=myfitresult;
     // Just fit and end with no report
     nlfit;
     nlend;
     // Print out file name and results
     type "File Name: %(file$)";
     type "   Peak Center= $(myfitresult.xc)";	
     type "   Peak Height= $(myfitresult.A)";	
     type "   Peak Width=  $(myfitresult.w)";	
 }

Batch Linear Fit Analysis

//Select all files using wild card
 string strpath$ = system.path.program$ + "Samples\Curve Fitting";
 dlgfile group:="Sensor*.dat" init:=strpath$ multi:=1;
//Get the number of files 
 int nfiles = fname.getnumtokens(CRLF);
//Define a variable to store the slope for linear fit result
 double dslope; 
//Define a string variable to store the file name
 string strFile$;
//loop over all selected files
 loop(ii,1,nfiles)
 {
//Get the file name
  strFile$ = fname.GetToken(ii, CRLF)$;
//Import the file into new sheet
  impASC strFile$ options.ImpMode:=4;
//Use fitlr x-function to fit the dataset and output slope
  fitlr (1,2) b:=bslope;
//Print the result string to script window
  type -a "Slope for %(strFile$) is $(bslope)";
 }

It is also possible to create an Analysis Template in advance and call the batchProcess X-Function to do batch processing. By doing this, less coding work will be required.

Processing Each Dataset in a Loop

Open the project Samples\Data Manipulation\Setting Column Values.OPJ and switch to the Columns from Other Sheets subfolder.

We will subtract a value, which is the first value of the reference subtracted from the first value of the sample, from the entire sample data. There are multiple transducers, so we can perform this calculation in a loop.

 //Reference worksheet range
 range rReference = 1!;
 //Sample worksheet range
 range rSample = 2!;
 //Time data range
 range rTime = 2!col(Time);
 //create a new worksheet
 newsheet name:="Corrected Sample";
 //fill the first column in this new worksheet with the time data range
 wcol(1) = rTime;
 //loop over all Transducer columns
 loop(ii,2,rSample.ncols)
 {
 range rS=rSample!wcol(ii);
 range rR = rReference!wcol(ii);
 wcol(ii) = rS - ( rS[1] - rR[1] );
 }