Author |
Topic  |
|
dafekare
USA
12 Posts |
Posted - 06/22/2022 : 3:01:30 PM
|
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 |
|
dafekare
USA
12 Posts |
Posted - 06/30/2022 : 11:28:07 AM
|
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 |
 |
|
YimingChen
1664 Posts |
Posted - 06/30/2022 : 2:35:41 PM
|
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;
}
|
 |
|
dafekare
USA
12 Posts |
Posted - 06/30/2022 : 4:32:29 PM
|
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 |
 |
|
YimingChen
1664 Posts |
Posted - 06/30/2022 : 5:11:16 PM
|
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);
}
|
 |
|
dafekare
USA
12 Posts |
Posted - 07/01/2022 : 10:19:53 AM
|
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 |
 |
|
YimingChen
1664 Posts |
|
dafekare
USA
12 Posts |
Posted - 07/01/2022 : 11:25:36 AM
|
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 |
 |
|
|
Topic  |
|
|
|