WrightTools.fit module

Fitting tools.

class WrightTools.fit.ExpectationValue[source]

Bases: WrightTools.fit.Function

ExpectationValue.

__init__()[source]

Initialization.

evaluate(p, xi)[source]

Evaluate the function.

Parameters:
  • p (list of numbers) – Parameters.
  • xi (array-like) – Coordinates.
Returns:

Function evaluated at coordinates.

Return type:

numpy array

fit(*args, **kwargs)[source]

Do fit.

Parameters:
  • *args
  • **kwargs
guess(values, xi)[source]

Guess.

Parameters:
  • values (array-like) – Values.
  • xi (array-like) – Points.
class WrightTools.fit.Exponential[source]

Bases: WrightTools.fit.Function

Exponential.

__init__()[source]

Initialization.

evaluate(p, xi)[source]

Evaluate the function.

Parameters:
  • p (list of numbers) – Parameters.
  • xi (array-like) – Coordinates.
Returns:

Function evaluated at coordinates.

Return type:

numpy array

guess(values, xi)[source]

Guess.

Parameters:
  • values (array-like) – Values.
  • xi (array-like) – Points.
Returns:

Guessed parameters.

Return type:

list of numbers

class WrightTools.fit.Fitter(function, data, *args, **kwargs)[source]

Bases: object

Fit each slice of a Data object.

__init__(function, data, *args, **kwargs)[source]

Create a Fitter object.

Parameters:
run(channel=0, propagate_other_channels=True, verbose=True)[source]

Run.

Parameters:
  • channel (string or integer (optional)) – Name or index of channel. Default is 0.
  • propagate_other_channels (boolean (optional)) – Toggle inclusion of other channels (collapsed) in outs. Default is True.
  • verbose (boolean (optional)) – Toggle talkback. Default is True.
Returns:

outs

Return type:

WrightTools.data.Data object

class WrightTools.fit.Function(*args, **kwargs)[source]

Bases: object

Base class for all fit functions.

__init__(*args, **kwargs)[source]

Create a Function object.

Parameters:
  • *args
  • **kwargs
fit(*args, **kwargs)[source]

Do fit.

Parameters:
  • *args
  • **kwargs
residuals(p, *args)[source]

Get residuals.

Parameters:
  • p (list of numbers) – Parameters.
  • *args – values, points
Returns:

Residuals.

Return type:

numpy array

class WrightTools.fit.Gaussian[source]

Bases: WrightTools.fit.Function

Gaussian.

__init__()[source]

Initialization.

evaluate(p, xi)[source]

Evaluate the function.

Parameters:
  • p (list of numbers) – Parameters.
  • xi (array-like) – Coordinates.
Returns:

Function evaluated at coordinates.

Return type:

numpy array

guess(values, xi)[source]

Guess.

Parameters:
  • values (array-like) – Values.
  • xi (array-like) – Points.
Returns:

Guessed parameters.

Return type:

list of numbers

class WrightTools.fit.Moments(subtract_baseline=False)[source]

Bases: WrightTools.fit.Function

Moments.

__init__(subtract_baseline=False)[source]

Initialization.

evaluate(p, xi)[source]

Evaluate the function.

Parameters:
  • p (list of numbers) – Parameters.
  • xi (array-like) – Coordinates.
Returns:

Function evaluated at coordinates.

Return type:

numpy array

fit(*args, **kwargs)[source]

Do fit.

Parameters:
  • *args
  • **kwargs
guess(values, xi)[source]

Guess.

Parameters:
  • values (array-like) – Values.
  • xi (array-like) – Points.
Returns:

Guessed parameters.

Return type:

list of numbers

class WrightTools.fit.MultiPeakFitter(data, channel=0, name='', fittype=2, intensity_label='OD')[source]

Bases: object

Class which allows easy fitting and representation of aborbance data.

Currently only offers Gaussian and Lorentzian functions. Functions are paramaterized by FWHM, height, and center. Requires the use of WrightTools.Data objects.

fittype = 2 may be used to fit a spectrum to maximize smoothness of model remainder. fittype = 0 may be used to fit a spectrum to minimize amplitude of model remainder.

__init__(data, channel=0, name='', fittype=2, intensity_label='OD')[source]

Create a MultiPeakFitter object.

Parameters:
  • data (WrightTools.Data object) –
  • channel (int) – channel of data object which has the z values to fit
  • name (str) –
  • fittype (int) – specifies order of differentiation to fit to—currently 0, 1, 2 are available
  • intensity_label (str) –
build_funcs(x, params, kinds, diff_order=0)[source]

Build a new 1D function of many 1D function of various kinds.

Parameters:
  • x (array) – 1D array containing points over which to return new function
  • params (array) – 1D array of ordered parameters for new functions—length 3n
  • kinds (list) – List of kinds of functions to add—length n
  • diff_order (int) – Specifies order of differentiated function to return
Returns:

z

Return type:

array

convert(destination_units)[source]

Exposes wt.data.convert() method to convert units of data object.

Parameters:destination_units (str) –
encode_params(names, kinds, params)[source]

Encode parameters of fit into an ordered dict of dicts.

Parameters:
  • names (list) – list of strings of length n
  • kinds (list) – list of strings of length n
  • params (array) – 1D array of floats of length 3n
Returns:

dic

Return type:

ordered dict of dicts

extract_params(dic)[source]

Extract parameters from a dictionary.

Parameters:dic (ordered dictionary of dicts) – Must contain keys: ‘FWHM’, ‘intensity’, ‘x0’, and ‘kind’.
Returns:names, kinds, p0 – names : list kinds : list p0 : array
Return type:tuple
fit(verbose=True)[source]

Run the fitting algorithm.

Fitting method that takes data and guesses (class attributes) and instantiates/updates fit_results, diff_model, and remainder (class attributes),

Parameters:verbose (bool) – toggle talkback
fit_results

ordered dict of dict

diff_model

array – array offerring the final results of the fit in the native space of the fit

remainder

array – array offering the remainder (actual - fit) in the native space of the spectra

function(x, kind, FWHM, intensity, x0, diff_order=0)[source]

Return a peaked distribution over array x.

The peaked distributions are characterized by their FWHM, height, and center. The distributions are not normalized to the same value given the same parameters! Analytic derivatives are provided for sake of computational speed & accuracy.

Parameters:
  • x (array) – array of values to return function over
  • kind (str) – Kind of function to return. Includes ‘lorentzian’ and ‘gaussian’
  • FWHM (float) – Full width at half maximum of returned function
  • intensity (float) – Center intensity of returned function
  • x0 (float) – x value of center of returned function
  • diff_order (int) – order of differentiation to return. diff_order = 0 returns merely the function.
guess(guesses)[source]

Create guess library for use in fitting.

Parameters:guesses (ordered dict of dicts) – In order to play nicely with the function creation method, the internal dict, at minimum, should have entries specifying ‘kind’, ‘FWHM’, ‘intensity’, and ‘x0’. The contents of guesses are used in the fit method.
intensity_label_change(intensity_label)[source]

Change label present in plot method.

Parameters:intensity_label (str) –
plot()[source]

Plot fit results.

save(path='/home/docs/checkouts/readthedocs.org/user_builds/wrighttools/checkouts/master/examples', fit_params=True, figure=True, verbose=True)[source]

Save results and representation of fits.

Saved files are timestamped.

Parameters:
  • path (str) – file path to save to
  • fit_params (bool) – toggle to save fit parameters
  • figure (bool) – toggle to save figure
  • verbose (bool) – toggle talkback
class WrightTools.fit.TwoD_Gaussian[source]

Bases: WrightTools.fit.Function

TwoD_Gaussian.

__init__()[source]

Initialization.

evaluate(p, x, y)[source]

Evaluate the function.

Parameters:
  • p (list of numbers) – Parameters.
  • xi (array-like) – Coordinates.
Returns:

Function evaluated at coordinates.

Return type:

numpy array

fit(values, x, y, **kwargs)[source]

Do fit.

Parameters:
  • *args
  • **kwargs
guess(v, x, y)[source]

Guess.

Parameters:
  • values (array-like) – Values.
  • xi (array-like) – Points.
Returns:

Guessed parameters.

Return type:

list of numbers

residuals(p, *args)[source]

Get residuals.

Parameters:
  • p (list of numbers) – Parameters.
  • *args – values, points
Returns:

Residuals.

Return type:

numpy array

WrightTools.fit.get_baseline(values, deviations=3)[source]

Guess the baseline for a data set.

Returns the average of all points in values less than n deviations away from zero.

Obviously, works best for data in which signal is relatively sparse so that noise dominates the standard deviation.

As a fallback, returns the minimum of values.

Parameters:
  • values (array-like) – The values to find the baseline of.
  • deviations (integer (optional)) – The number of standard deviations away from zero to exclude.
Returns:

Baseline guess.

Return type:

float

WrightTools.fit.leastsqfitter(p0, datax, datay, function, verbose=False, cov_verbose=False)[source]

Conveniently call scipy.optmize.leastsq().

Returns fit parameters and their errors.

Parameters:
  • p0 (list) – list of guess parameters to pass to function
  • datax (array) – array of independent values
  • datay (array) – array of dependent values
  • function (function) – function object to fit data to. Must be of the callable form function(p, x)
  • verbose (bool) – toggles printing of fit time, fit params, and fit param errors
  • cov_verbose (bool) – toggles printing of covarience matrix
Returns:

  • pfit_leastsq (list) – list of fit parameters. s.t. the error between datay and function(p, datax) is minimized
  • perr_leastsq (list) – list of fit parameter errors (1 std)