Fits

Functions

Details

Collection of fitting functions

fits.demo_ransac()[source]

Find the best-fit circle in an image, using the RANSAC algorithm

fits.fit_circle(x, y)[source]

Determine the best-fit circle to given datapoints.

Parameters
xarray (N,)

x-values.

yarray (N,)

corresponding y-values.

Returns
centerarray (2,)

x/y coordinates of center of the circle

radiusfloat

Circle radius.

Examples

>>> r = 2
>>> center = np.r_[5,5]
>>> theta = np.r_[0:2*np.pi:10j]
>>> x = r*np.cos(theta)+center[0]
>>> y = r*np.sin(theta)+center[1]
>>> cFit,rFit = thLib.fits.fit_circle(x,y)
fits.fit_ellipse(x, y)[source]

Ellipse fit by Taubin’s Method

Parameters
xarray

x-coordinates of the ellipse points

yarray

y-coordinates of the ellipse points

Returns
Aarray

Ellipse parameters A = [a b c d e f] is the vector of algebraic parameters of thefitting ellipse: ax^2 + bxy + cy^2 +dx + ey + f = 0 The vector A is normed, so that ||A||=1.

Notes

Among fast non-iterative ellipse fitting methods, this is perhaps the most accurate and robust.

This method fits a quadratic curve (conic) to a set of points; if points are better approximated by a hyperbola, this fit will return a hyperbola. To fit ellipses only, use “Direct Ellipse Fit”.

Published in G. Taubin, “Estimation Of Planar Curves, Surfaces And Nonplanar Space Curves Defined By Implicit Equations, With Applications To Edge And Range Image Segmentation”, IEEE Trans. PAMI, Vol. 13, pages 1115-1138, (1991)

fits.fit_exp(tFit, yFit, plotFlag=False)[source]

Calculates best-fit parameters for the exponential decay to an offset. This can serve as an example for a general non-linear fit.

Parameters
tFitarray (N,)

Time values.

yFitarray (N,)

Function values

Returns
offsetfloat

Function offset/bias.

ampfloat

Amplitude of exponential function

taufloat

Decay time.

Examples

>>> t = np.arange(10)
>>> tau = 2.
>>> amp = 1.
>>> offset = 2.
>>> x = offset + amp*np.exp(-t/tau)    
>>> fitted =  thLib.fits.fit_exp(t,x)
fits.fit_line(x, y, alpha=0.05, newx=[], plotFlag=False)[source]

Linear regression fit.

Parameters
xndarray

Input / Predictor.

yndarray

Input / Estimator.

alphafloat

Confidence limit [default=0.05]

newxfloat or ndarray

Values for which the fit and the prediction limits are calculated (optional)

plotFlag: int, optional

1 = plot, 0 = no_plot [default]

Returns
afloat

Intercept

bfloat

Slope

cindarray

Lower and upper confidence interval for the slope

infodictionary

contains return information on - residuals - var_res - sd_res - alpha - tval - df

newylist(ndarray)

Predictions for (newx, newx-ciPrediction, newx+ciPrediction)

Notes

Example data and formulas are taken from D. Altman, “Practical Statistics for Medicine”

Examples

>>> x = np.r_[0:10:11j]
>>> y = x**2
>>> (a,b,(ci_a, ci_b),_) = thLib.fits.fit_line(x,y)    
Summary: a=-15.0000+/-12.4590, b=10.0000+/-2.1060
Confidence intervals: ci_a=(-27.4590 - -2.5410), ci_b=(7.8940 - 12.1060)
Residuals: variance = 95.3333, standard deviation = 9.7639
alpha = 0.050, tval = 2.2622, df=9
fits.fit_sin(tList, yList, freq)[source]

Fit a sine wave with a known frequency to a given set of data.

y = amplitude * sin(2*pi*freq * tList + phase*pi/180) + bias

Parameters
yListarray

datapoints

tListfloat

time base, in sec

freqfloat

in Hz

Returns
phasefloat

in degrees

amplitudefloat
biasfloat

Examples

>>> np.random.seed(1234)
>>> t = np.arange(0,10,0.1)
>>> x = 3 + 4*np.sin(2*np.pi*t + 5*np.pi/180) + np.random.randn(len(t))
>>> (phase, amp, offset) = thLib.fits.fit_sin(t, x, 1)
fits.regress(y, X, alpha=0.05, intercept=True)[source]

Multilinear regression and confidence intervals. Note that by default, an intercept is added to the design matrix!

Parameters
Xndarray (N,) or (N,p)

predictors at each of N observations

yndarray(N,)

observed responses

alphafloat, optional

Defines the 100*(1-alpha)% confidence level in ci. Default alpha=0.05

interceptboolean

If ‘True’, an intercept is automatically added to the design matrix. If ‘False’, the behavior is like the Matlab “regress” function, and no intercept is added.

Returns
fitfloat

best fit intercept and regression parameters

cindarray (2,)

confidence intervals for the coefficient estimates at the given alpha level (default: 95%-level)

See also

fit_line

Examples

>>> x = np.array([0, 0, 10, 10])
>>> y = np.array([1, 3, 11, 13])
>>> (fit,ci) = thLib.fits.regress(y,x)    
>>> X = np.random.randn(10,2)
>>> mat = np.hstack( (np.ones( (len(X),1) ), X) )
>>> pars = np.c_[[2, 4, 5]]
>>> y = mat.dot(pars)
>>> y += 0.1*np.random.randn(*y.shape)
>>> (fit,ci) = thLib.fits.regress(y, mat)