The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 LabTalk Forum
 Output from peak fitting
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

dafekare

USA
12 Posts

Posted - 06/22/2022 :  3:01:30 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Hi,

I am trying to write a script to automatically apply multimodal peak fitting to a set of xy data and it works, but the output does not give as much as information as when I do it manually. Here are the codes I wrote:

// Create a new workbook, and import the data
newbook;
string fname$ = "C:\Users\pt4673\Documents\SMPS\data analyses\0.05 PL-2L #11 20mM 51322.csv";
impasc;
// Set data filter for column 1, numeric type
wks.col1.filter = 1; // Add filter
wks.col1.filterx$ = Particle; // Set the variable "Particle size" to represent column 1
// Set filter condition, greater than 12
wks.col1.filter$ = "particle> 12";
// Run the worksheet filter;
wks.runfilter();
plotxy iy:=(1,2) plot:=201;
//Set the scale to Log10
layer.x.type = 2;
//Set the start value to 10 nm
layer.x.from = 10;
//Set the end value to 100 nm
layer.x.to = 100;
//Set the increment value
layer.x.inc = 10;
// Set symbol size
// %C is the active dataset
// Set symbol size to 5 (default is 9)
sec -p 1;
set %C -z 5;
//apply trimodal peak fitting with a gaussian distribution
fitpeaks t:=gauss np:=3;

When I run, it only provides the result titled "peak fitting code output 62222" attached. However, doing it manually yields nlpeakfit output (see nlpeakfit output attached) and a whole table which is more insightful than my code output. Please can you provide guidance on how to write the script for this nlpeakfit results? I assume this is non-linear peak fitting and it differs from my code.

Thanks a lot!



DAA

YimingChen

1664 Posts

Posted - 06/22/2022 :  3:53:56 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
You may want to refer to these pages on using nlfit related X-function to do the fitting:
https://www.originlab.com/doc/LabTalk/guide/Non-linear-Fitting
https://www.originlab.com/doc/LabTalk/examples/Curve-Fitting#Fit_Multiple_Peaks_with_Replica

It generates more detailed fitting statistics:
https://www.originlab.com/doc/en/X-Function/ref/nlbegin#Details_of_the_nltree_TreeNode

James
Go to Top of Page

dafekare

USA
12 Posts

Posted - 06/30/2022 :  11:28:07 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Excellent comment! Just what I needed to get my problem solved! However, I have some follow up questions:

1. I need to apply the non-linear peak fitting to different xy curves (same x, different ys). How can I automate this within the code I already wrote without copying and pasting the script with the updated y column number?

2. I need to create a table of the xc and standard error values reported in the result sheet after peak fitting. Where can I find the code to this command? see image attached.



DAA
Go to Top of Page

YimingChen

1664 Posts

Posted - 06/30/2022 :  2:35:41 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Please try the sample code below:

// Clear the project to start with empty space
doc -s; doc -n; {win -cd;}

// Import sample data into a new workbook
newbook;
string fname$ = system.path.program$ + "Samples\Curve Fitting\Multiple Peaks.dat";
impAsc;	
	
// get number of datasets
String wbk = %H;
int ndata = wks.ncols - 1;

// prepare summary sheet
newbook;
String wbkrslt = %H;
int npeaks = 3;
wks.ncols = npeaks * 2;
Stringarray names = {"x1","x1_error","x2","x2_error","x3","x3_error"}
loop(i, 1, wks.ncols) {
	wcol(i)[L]$ = names.getAt(i)$;  
}

// fit each dataset
loop(i, 1, ndata) {
	range aa = [%(wbk$)]1!(1, $(i + 1));
	nlbegin aa gauss tt replica:=$(npeaks - 1);
	nlfit;
	nlend;	
	%(wbkrslt$)!cell(i, 1) = tt.xc;
	%(wbkrslt$)!cell(i, 2) = tt.e_xc;
	%(wbkrslt$)!cell(i, 3) = tt.xc__2;
	%(wbkrslt$)!cell(i, 4) = tt.e_xc__2;
	%(wbkrslt$)!cell(i, 5) = tt.xc__3;
	%(wbkrslt$)!cell(i, 6) = tt.e_xc__3;
}
Go to Top of Page

dafekare

USA
12 Posts

Posted - 06/30/2022 :  4:32:29 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
The code is not quite doing the trick, because I need to filter the data first and adjust the graph scale. Please find my previous code below:
// Clear the project to start with empty space
doc -s; doc -n; {win -cd;}
// Create a new workbook, and import the data
newbook;
string fname$ = "C:\Users\pt4673\Documents\particle\SMPS\data analyses\0.05 PL-2L #11 20mM 51322.csv";
impasc;
// Set data filter for column 1, numeric type
wks.col1.filter = 1; // Add filter
wks.col1.filterx$ = Particle; // Set the variable "Particle size" to represent column 1
// Set filter condition, greater than 12
wks.col1.filter$ = "particle> 12";
// Run the worksheet filter;
wks.runfilter();
plotxy iy:=(1,2) plot:=201;
//Set the scale to Log10
layer.x.type = 2;
//Set the start value to 10 nm
layer.x.from = 10;
//Set the end value to 100 nm
layer.x.to = 100;
//Set the increment value
layer.x.inc = 10;
// Set symbol size
// %C is the active dataset
// Set symbol size to 5 (default is 9)
sec -p 1;
set %C -z 5;
//apply non-linear peak fitting with a gaussian distribution. specify peaks once dialog box pops up
nlfitpeaks iy:=<active> func:=Gauss useqp:=0

DAA

Edited by - dafekare on 06/30/2022 4:33:50 PM
Go to Top of Page

YimingChen

1664 Posts

Posted - 06/30/2022 :  5:11:16 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
You just need to plot out the original data and the fitted curve. Try the code below.

//Clear the project to start with empty space
doc -s; doc -n; {win -cd;}

// Import sample data into a new workbook
newbook;
string fname$ = system.path.program$ + "Samples\Curve Fitting\Multiple Peaks.dat";
impAsc;	
	
// get number of datasets
String wbk = %H;
int ndata = wks.ncols - 1;
range rsource = 1;

// prepare summary sheet
newbook;
String wbkrslt = %H;
int npeaks = 3;
wks.ncols = npeaks * 2;
Stringarray names = {"x1","x1_error","x2","x2_error","x3","x3_error"}
loop(i, 1, wks.ncols) {
	wcol(i)[L]$ = names.getAt(i)$;  
}

// prepare the fitted curve sheet
newbook;
String wbkplot = %H;
wks.ncols = ndata + 1;
range rx = 1;
rx = rsource;

// fit each dataset
loop(i, 1, ndata) {
	range aa = [%(wbk$)]1!(1, $(i + 1));
	nlbegin aa gauss tt replica:=$(npeaks - 1);
	nlfit;
	range ry = [%(wbkplot$)]1!wcol(i + 1);
	ry = fit(rx);
	ry[L]$ = fitted$(i);
	nlend;	
	%(wbkrslt$)!cell(i, 1) = tt.xc;
	%(wbkrslt$)!cell(i, 2) = tt.e_xc;
	%(wbkrslt$)!cell(i, 3) = tt.xc__2;
	%(wbkrslt$)!cell(i, 4) = tt.e_xc__2;
	%(wbkrslt$)!cell(i, 5) = tt.xc__3;
	%(wbkrslt$)!cell(i, 6) = tt.e_xc__3;
	
	// Plot both src and fit in the new graph
	range rs = [%(wbk$)]1!wcol(i+1);
	plotxy iy:=rs plot:=201 ogl:=[<new>];
	range rt = [%(wbkplot$)]1!wcol(i + 1);
	plotxy iy:=rt plot:=200 ogl:=[<active>] color:=color(red);	
}
Go to Top of Page

dafekare

USA
12 Posts

Posted - 07/01/2022 :  10:19:53 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks for the help, but the code is not yet solving the problem. I like the nlfitpeaks function better because it uses the Levenberg Marquadt iteration and I can choose the peaks so it's more precise. I also need to filter the data (remove if x < 12) because it can easily interfere with the main dataset of focus. I want to be able to:
1. do the fitting using nlfitpeaks
2. Pull the xc and standard error values for each xy output and place them in the same table so I can separately analyze.

DAA

Edited by - dafekare on 07/01/2022 10:24:39 AM
Go to Top of Page

YimingChen

1664 Posts

Posted - 07/01/2022 :  10:51:24 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
To extract the data from the report sheet, you can refer to this page:
https://www.originlab.com/doc/LabTalk/guide/Accessing-Metadata#Access_Report_Page_Tree
Then you can create your own summary sheet by following the script I provided. If you have more questions on customizing your script, please submit a support ticket with the link below:
https://www.originlab.com/restricted/support/op/newticket.aspx

Please also provide your serial number in the ticket. Thank you.

James
Go to Top of Page

dafekare

USA
12 Posts

Posted - 07/01/2022 :  11:25:36 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
The report page tree you sent is really helpful! Will follow through with ticket request if I need additional help. Thanks and happy 4th in advance. :)

DAA
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000