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
 All Forums
 Origin Forum for Programming
 LabTalk Forum
 Multiple fittings

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

Screensize:
UserName:
Password:
Anti-Spam Code:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkUpload FileInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
Leif12 Posted - 01/31/2022 : 03:58:12 AM
OriginPro 2022 (64-bit) SR1 (Academic)

Dear Origin community,

the last issue I had on sorting data, was thankfully fully solved with your help.
Now I have the following issue and hope again for your advice:

I'd like to fit multiple data with a user-defined function (1st fit) and use the fitted parameters to calculate another data set (Y2), which is used for another user-defined fitting (2nd fit). Additionally, all the results in between need to be displayable (in columns).
The fitting itself worked well and I already created a theme. But I don't know how to import the fitted parameters of different fits directly in the worksheet (together with R^2) in specified columns. Finally, it should be possible to transfer this process to many different worksheets without a lot of effort.

I attached a simplified origin file, which should make the point more understandable. For additional simplification, the data in the example can be fit by a linear regression, but the final script should be applicable to a user-defined fit function with two fit parameters.

grey fields: data that needs to be fit
red fields: Obtained fitted parameters
X1, Y1 and X2: Raw data
a&b and k&n: parameters obtained from fitting
datasheets: the developed script should be applicable to all sheets (same structure)

Best regards
Leif

https://my.originlab.com/ftp/forum_and_kbase/Images/Fitting%20issue.opju
5   L A T E S T    R E P L I E S    (Newest First)
cpyang Posted - 06/05/2022 : 7:30:40 PM
If you can run code outside, like from Code Builder with a py file, then the following code will take XY data in column A and B and add fitted X and Y in the next two columns, and also add a comment to the last column to show the fitting parameters:


import originpro as op

wks = op.find_sheet()
model = op.NLFit('Line')
model.set_data(wks, 0, 1)
model.fit()
rr = model.result()
a = rr['A']
b = rr['B']
r2=rr['cod']
label = f'Slope={b:6.3}, R^2={r2:6.3}'
x = [1,2,3]
y = [a + b * v for v in x]
wks.from_list(2, x)
wks.from_list(3, y)
wks.set_label(3, label, 'C')



To use this in a column formula, setup worksheet with 4 columns, XY in first two and fit data X in column C, and resulting fit Y in column D. You can set recalculate to Auto so you can change the fit X in column C and see D update.

in Column D, Before Formula Script:

string str$;

str$, col(D)=py.myfit(col(C));
col(D)[C]$=str$;


Python Function:

import originpro as op

def myfit(fitx):
    '''T:F'''
    wks = op.find_sheet()
    model = op.NLFit('Line')
    model.set_data(wks, 0, 1)
    model.fit()
    rr = model.result()
    a = rr['A']
    b = rr['B']
    r2=rr['cod']
    label = f'Slope={b:6.3}, R^2={r2:6.3}'
    fity = [a + b * v for v in fitx]
    return label, fity




CP
Leif12 Posted - 06/05/2022 : 11:45:54 AM
Thanks, this helped me a lot since February.

Recently I needed this script again and was wondering how to access the fit y-data via python script and add them to a column. I did'n find anything suitable in the NLFit X-function link or https://www.originlab.com/doc/en/python/Examples/Analysis#Extract_Values_from_NLFit_Report_Tables link, which worked without creating report sheets.

The single fit parmeters could be accessed by
rr = model.result(),
but how would I add 2 additional columns in the script below for the indenpendent x-variable and fit y-variable?


import originpro as op
import numpy as np

def userFit(d1):
'''T:F'''
a = []
b = []
rsquare = []
for i in range(5):
wks = op.find_sheet()
model = op.NLFit('Line') // you can replace with your own function
model.set_data(wks, 0, i + 1)
model.fit()
rr = model.result()
a.append(rr['A'])
b.append(rr['B'])
rsquare.append(rr['cod'])
return a, b, rsquare


Best
Leif
YimingChen Posted - 02/02/2022 : 2:22:55 PM
1. Please check this page for the list. Internally Python is running the fit using the NLFit X-function.
https://www.originlab.com/doc/X-Function/ref/nlbegin#Details_of_the_nltree_TreeNode

2 and 3. For more complicated analysis like global fit, see if you can make an analysis template first and load the data into the template with Python. See the example:
https://www.originlab.com/doc/en/python/Examples/Analysis#Extract_Values_from_NLFit_Report_Tables

James
Leif12 Posted - 02/01/2022 : 08:08:23 AM
Thanks James, again.
This is exactly what I was looking for. The link is also very helpful.

However, 3 more question occur to me :
1. How did you know that you had to use 'cod' in rsquare.append(rr['cod']) for the R square result?
Is there a list with the labels (to prevent further questions)?

2.
Is there a way to perform a global fit via the pyhton script, where e.g. 5 simultaneous fits have one shared fit parameter and one individual fit parameter? (In Origin, this would be made by a user-defined theme, but I don't know how this can be included into the script?)

3.
Is there an elegant way to choose the x and y data for the fit from the worksheet, if those columns are distributed in repeating distances? (Since I'd like to perform this for a global fit with shared parameters, I don't think it's achievable with a loop.)

Best Leif
YimingChen Posted - 01/31/2022 : 4:00:07 PM
Please see the project in the attachment. I set a Python column function in col(H) for the calculation. You can refer to this page on nonlinear curve fitting with Python. Notice the recalculation of the fitting will be triggered by data changing of col(B).
https://www.originlab.com/doc/en/python/Examples/Analysis#Simple_Curve_Fit_to_XY_Data


import originpro as op
import numpy as np

def userFit(d1):
    '''T:F'''
    a = []
    b = []
    rsquare = []
    for i in range(5):
        wks = op.find_sheet()
        model = op.NLFit('Line') // you can replace with your own function
        model.set_data(wks, 0, i + 1)
        model.fit()
        rr = model.result()
        a.append(rr['A'])
        b.append(rr['B'])
        rsquare.append(rr['cod'])
    return a, b, rsquare


https://my.originlab.com/ftp/forum_and_kbase/Images/Fitting%20issue_YM.opju

James

The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000