Skip to content

Util fit

thmd.util.fit

Modules:

Functions:

  • curve_intersect_shapely

    find the intersection points between 2 lines

  • polyfit

    Polynomial fitting for y = f(x) relation, which returns uncertainties of coefficients.

  • find_slope

    Compute slope of a linear relation y = A + B*x.

  • find_roots

    find roots for y = f(x) relation in polynomial form.

  • find_extrema

    find extrema points (der=0) for y = f(x) relation in polynomial form.

  • find_convergence

    y is a function of x, then find value of x that y converge with a tolerance < tol.

  • extrapolate

curve_intersect_shapely(line1, line2)

find the intersection points between 2 lines Args: line1 (array like): Nx2 arrays, contains data points of curve 1 line2 (array like): Nx2 arrays, contains data points of curve 2

Returns:

  • Points ( array like ) –

    Nx2 arrays, contains data points of intersection points

polyfit(x, y, deg=1, sigma_y=None, uncert=False, **kwargs)

Polynomial fitting for y = f(x) relation, which returns uncertainties of coefficients. The fitted polynomial(s) are in the form

p(x) = a0 + a1*x + a2*x^2 + ... + an*x^n

Parameters:

  • x,y (array) –

    1D array of x and y data

  • deg (int, default: 1 ) –

    degree of polynomial

  • sigma_y (array, default: None ) –

    1D array of standard deviation of y data. Use in weighted least square fitting.

  • uncert (bool, default: False ) –

    return uncertainties of coefficients. Defaults to False.

  • **kwargs

    additional arguments, adapt all args from np.polyfit

Returns:

  • pars ( array ) –

    1D array of coefficients

  • uncert ( array ) –

    1D array of standard deviation of coefficients

find_slope(x, y)

Compute slope of a linear relation y = A + B*x.

Parameters:

  • x (list) –

    a list/array of x value

  • y (list) –

    a list/array of y value

Return

slope (float): the slope to linear relation

find_roots(x, y, order=1)

find roots for y = f(x) relation in polynomial form.

Parameters:

  • x (list) –

    a list/array of x value

  • y (list) –

    a list/array of y value

  • order (int, default: 1 ) –

    order of polynomial. Defaults to 1.

Returns:

  • roots ( list ) –

    list of roots

find_extrema(x, y, order=2, retun_fit=False)

find extrema points (der=0) for y = f(x) relation in polynomial form.

Parameters:

  • x (list) –

    a list/array of x value

  • y (list) –

    a list/array of y value

  • order (int, default: 2 ) –

    order of polynomial. Defaults to 2.

  • retun_fit (bool, default: False ) –

    return fitted params. Defaults to False.

Returns:

  • p ( list ) –

    list of flex points.

find_convergence(x, y, tol=1e-06, grid_size=None, convergence_side='right')

y is a function of x, then find value of x that y converge with a tolerance < tol.

Parameters:

  • x (list) –

    a list/array of x values

  • y (list) –

    a list/array of y values

  • tol (float, default: 1e-06 ) –

    tolerance of the slope (dy/dx). Defaults to 1e-6.

  • grid_size (float, default: None ) –

    set grid size of x if need to interpolate finer data. Defaults to None.

  • convergence_side (str, default: 'right' ) –

    find the first x on the left or right side. Defaults to "right".

Returns:

  • best_x, best_y (float): found x and y value that y converge with a tolerance < tol.

Notes

Value df['dy'] = df['y'].diff() depends on grid size. Should use df['dy'] = df['y'].diff()/ df['x'].diff().

References
  1. https://docs.scipy.org/doc/scipy/tutorial/interpolate.html

extrapolate(x, y, left_side: list = None, right_side: list = None, grid_size=0.1) -> tuple[list, list]

curve_intersect

Functions:

curve_intersect_shapely(line1, line2)

find the intersection points between 2 lines Args: line1 (array like): Nx2 arrays, contains data points of curve 1 line2 (array like): Nx2 arrays, contains data points of curve 2

Returns:

  • Points ( array like ) –

    Nx2 arrays, contains data points of intersection points

___curve_intersect_numpy(curve1, curve2, degree=3, bounds=None)

find the intersection points between 2 curves

Parameters:

  • curve1 (array like) –

    Nx2 arrays in form (x,y), contains data points of curve 1

  • curve2 (array like) –

    Nx2 arrays in form (x,y), contains data points of curve 2

  • degree (int, default: 3 ) –

    degree of polynomial function to fit the curve

  • bounds (tuple, default: None ) –

    (min, max) of x, to select points in bounds

Returns:

  • Points ( array like ) –

    Nx2 arrays, contains data points of intersection points

Refs

[1] http://t.ly/2_a-K

fit_root

Functions:

  • polyfit

    Polynomial fitting for y = f(x) relation, which returns uncertainties of coefficients.

  • find_slope

    Compute slope of a linear relation y = A + B*x.

  • find_roots

    find roots for y = f(x) relation in polynomial form.

  • find_extrema

    find extrema points (der=0) for y = f(x) relation in polynomial form.

  • find_convergence

    y is a function of x, then find value of x that y converge with a tolerance < tol.

  • extrapolate

polyfit(x, y, deg=1, sigma_y=None, uncert=False, **kwargs)

Polynomial fitting for y = f(x) relation, which returns uncertainties of coefficients. The fitted polynomial(s) are in the form

p(x) = a0 + a1*x + a2*x^2 + ... + an*x^n

Parameters:

  • x,y (array) –

    1D array of x and y data

  • deg (int, default: 1 ) –

    degree of polynomial

  • sigma_y (array, default: None ) –

    1D array of standard deviation of y data. Use in weighted least square fitting.

  • uncert (bool, default: False ) –

    return uncertainties of coefficients. Defaults to False.

  • **kwargs

    additional arguments, adapt all args from np.polyfit

Returns:

  • pars ( array ) –

    1D array of coefficients

  • uncert ( array ) –

    1D array of standard deviation of coefficients

find_slope(x, y)

Compute slope of a linear relation y = A + B*x.

Parameters:

  • x (list) –

    a list/array of x value

  • y (list) –

    a list/array of y value

Return

slope (float): the slope to linear relation

find_roots(x, y, order=1)

find roots for y = f(x) relation in polynomial form.

Parameters:

  • x (list) –

    a list/array of x value

  • y (list) –

    a list/array of y value

  • order (int, default: 1 ) –

    order of polynomial. Defaults to 1.

Returns:

  • roots ( list ) –

    list of roots

find_extrema(x, y, order=2, retun_fit=False)

find extrema points (der=0) for y = f(x) relation in polynomial form.

Parameters:

  • x (list) –

    a list/array of x value

  • y (list) –

    a list/array of y value

  • order (int, default: 2 ) –

    order of polynomial. Defaults to 2.

  • retun_fit (bool, default: False ) –

    return fitted params. Defaults to False.

Returns:

  • p ( list ) –

    list of flex points.

find_convergence(x, y, tol=1e-06, grid_size=None, convergence_side='right')

y is a function of x, then find value of x that y converge with a tolerance < tol.

Parameters:

  • x (list) –

    a list/array of x values

  • y (list) –

    a list/array of y values

  • tol (float, default: 1e-06 ) –

    tolerance of the slope (dy/dx). Defaults to 1e-6.

  • grid_size (float, default: None ) –

    set grid size of x if need to interpolate finer data. Defaults to None.

  • convergence_side (str, default: 'right' ) –

    find the first x on the left or right side. Defaults to "right".

Returns:

  • best_x, best_y (float): found x and y value that y converge with a tolerance < tol.

Notes

Value df['dy'] = df['y'].diff() depends on grid size. Should use df['dy'] = df['y'].diff()/ df['x'].diff().

References
  1. https://docs.scipy.org/doc/scipy/tutorial/interpolate.html

extrapolate(x, y, left_side: list = None, right_side: list = None, grid_size=0.1) -> tuple[list, list]

___uncertainty_weighted_fit_linear(x, y, sigma_y=None)

Compute uncertainties in coefficients A and B of y = A + B*x from the weighted least square fitting.

Apply when the measured data y_i has different uncertainties sigma_y_i, the weights w_i = 1/(sigma_y_i^2).

Parameters:

  • x,y (array) –

    1D array of x and y data

  • sigma_y (array, default: None ) –

    1D array of standard deviation of y data. Use in weighted least square fitting.

Returns:

  • pars ( array ) –

    1D array of coefficients A and B

  • uncertainties ( tuple ) –

    1D array (sigma_intercept, sigma_slope) of standard deviation of A and B

References
  1. Taylor_1997_An introduction to error analysis: the study of uncertainties in physical measurements, page 198.
  2. https://numpy.org/doc/stable/reference/generated/numpy.polynomial.polynomial.polyfit.html

user_lmfit

Classes:

  • UserLmfit

    The class contains set of objective function of fitting use by LMFIT package

UserLmfit

The class contains set of objective function of fitting use by LMFIT package NOTEs: - defined function followed the convection of LMFIT: the first argument of the function is taken as the independent variable, held in independent_vars, and the rest of the functions positional arguments (and, in certain cases, keyword arguments – see below) are used for Parameter names. https://lmfit.github.io/lmfit-py/model.html - This Class defines curve-forms that are not vailable in LMFIT's built-in models

  • Attributes: swType : (default='RATIONAL') Type of witching function, r0, d0 : The r_0 parameter of the switching function

  • Methods: fFunc : compute & return value and derivation of sw function fDmax : estimate value of Dmax

Ex: func = thmd.CurveLib.Linear(x)

Methods:

Linear(x, a0, a1)

this func is available in LMFIT, just play as an example here

inverseTemperature(x, a, b)
ExpDecay(x, A, lambd)
sizeEffect(x, a, b)

system size-dependence on term N^(⅔

unNormalGaussian(x, amp, cen, sig)

The unNormalize Gaussian function

NormalGaussian(x, amp, cen, sig)

The Normalize Gaussian function

sum_2unNormalGaussian(x, amp1, amp2, cen1, cen2, sig1, sig2)

The sum of 2 Gaussian function

sum_3unNormalGaussian(x, amp1, amp2, amp3, cen1, cen2, cen3, sig1, sig2, sig3)

The sum of 3 Gaussian function

sum_4unNormalGaussian(x, amp1, amp2, amp3, amp4, cen1, cen2, cen3, cen4, sig1, sig2, sig3, sig4)

The sum of 4 Gaussian function

sum_5unNormalGaussian(x, amp1, amp2, amp3, amp4, amp5, cen1, cen2, cen3, cen4, cen5, sig1, sig2, sig3, sig4, sig5)

The sum of 5 Gaussian function

sum_2NormalGaussian(x, amp1, amp2, cen1, cen2, sig1, sig2)

The sum of 2 Gaussian function

sum_3NormalGaussian(x, amp1, amp2, amp3, cen1, cen2, cen3, sig1, sig2, sig3)

The sum of 3 Gaussian function

sum_4NormalGaussian(x, amp1, amp2, amp3, amp4, cen1, cen2, cen3, cen4, sig1, sig2, sig3, sig4)

The sum of 4 Gaussian function

sum_5NormalGaussian(x, amp1, amp2, amp3, amp4, amp5, cen1, cen2, cen3, cen4, cen5, sig1, sig2, sig3, sig4, sig5)

The sum of 5 Gaussian function

DoseResp(x, A1=-3.3, A2=-2.9, LOGx0=480000, p=1.2)

Dose-response curve with variable Hill slope given by parameter 'p'. Origin's Category: Pharmacology * Params: Names=A1,A2,LOGx0,p Meanings=bottom asymptote,top asymptote, center, hill slope Initiate params: pars = mod.make_params(A1=-3.3, A2=-2.9, LOGx0=480000, p=1.2)

BiDoseResp(x, A1=-3.3, A2=-2.9, LOGx01=175000, LOGx02=480000, h1=0.1, h2=0.2, p=0.5)

Biphasic Dose Response Function, Origin's Category: Pharmacology * Params: Names=A1, A2, LOGx01, LOGx02, h1, h2, p Meanings=Bottom, Top, 1st EC50, 2nd EC50, slope1, slope2, proportion Initiate params: pars = mod.make_params(A1=0, A2=100, LOGx01=-8, LOGx02=-4, h1=0.8, h2=1.2, p=0.5)

Carreau(x, A1=60, A2=3, t=3.0, a=2.2, n=0.3)

Carreau-Yasuda model to describe pseudoplastic flow with asymptotic viscosities at zero and infinite shear rates Origin's Category: Rheology * Params: Names = A1,A2,t,a,n >0 (lower bound) Meanings = zero shear viscosity,infinite shear viscosity,time constant,transition control factor,power index Initiate params: pars = mod.make_params(A1=-3.3, A2=-2.9, t=2.0, a=2.2, n=0.2)

Cross(x, A1=0.1, A2=3, t=1000, m=0.9)

Cross model to describe pseudoplastic flow with asymptotic viscosities at zero and infinite shear rates Origin's Category: Rheology * Params: Names = A1,A2,t,m >0 (lower bound) Meanings = zero shear viscosity,infinite shear viscosity,time constant,power index Initiate params: pars = mod.make_params(A1=0.1, A2=3, t=1000, m=0.9)

GammaCFD(x, y0, A1, a, b)

Gamma cumulative distribution function Origin's Category: Statistics * Params: Names = y0,A1,a,b (A1,a,b >0) Meanings = Offset,Amplitude,Shape,Scale Initiate params: pars = mod.make_params(A1=0.1, A2=3, t=1000, m=0.9)