the lmfitdata class

This is a child class of nddata used for fitting.

It works with two other very useful packages lmfit – which does a very good job of setting up bounded fits – and sympy – which we can use to represent the function that we are fitting.

Bounds improve the likelihood that our fit will converge, while the symbolic representation both helps us to generate latex representations of the function that we are fitting and to improve performance and error estimates by automatically calculating the jacobian.

You simply wrap your existing nddata with lmfitdata, set the functional_form property to the equation you want to fit, and provide guesses and bounds with set_guess. The easiest way to get started is to just see the fitting examples in the example gallery.

class pyspecdata.lmfitdata.lmfitdata(*args, **kwargs)

Inherits from an nddata and enables curve fitting through use of a sympy expression.

The user creates a lmfitdata class object from an existing nddata class object, and on this lmfitdata object can define the functional_form() of the curve it would like to fit to the data of the original nddata. This functional form must be provided as a sympy expression, with one of its variables matching the name of the dimension that the user would like to fit to.

copy(**kwargs)

Return a full copy of this instance.

Because methods typically change the data in place, you might want to use this frequently.

Parameters:

data (boolean) –

Default to True. False doesn’t copy the data – this is for internal use, e.g. when you want to copy all the metadata and perform a calculation on the data.

The code for this also provides the definitive list of the nddata metadata.

eval(taxis=None)

Calculate the fit function along the axis taxis.

Parameters:

taxis (ndarray, int) –

if ndarray:

the new axis coordinates along which we want to calculate the fit.

if int:

number of evenly spaced points along the t-axis along the fit

Returns:

self – the fit function evaluated along the axis coordinates that were passed

Return type:

nddata

fit(use_jacobian=True)

actually run the fit

property function_string

A property of the myfitclass class which stores a string output of the functional form of the desired fit expression provided in func:functional_form in LaTeX format

property functional_form

A property of the myfitclass class which is set by the user, takes as input a sympy expression of the desired fit expression

gen_indices(this_set, set_to)

pass this this_set and this_set_to parameters, and it will return: indices,values,mask indices –> gives the indices that are forced values –> the values they are forced to mask –> p[mask] are actually active in the fit

guess()

Old code that we are preserving here – provide the guess for our parameters; by default, based on pseudoinverse

jacobian(pars, sigma=None)

cache the symbolic jacobian and/or use it to compute the numeric result

Note that, like residual, this is designed for use by lmfit, so that if you want to actually see the Jacobian, you need to pass something a bit more complicated, like this:

>>> jac = newfit.jacobian(newfit.fit_parameters).view(newfit.data.dtype)

which assumes that I have run the fit, and so have access to the fit parameters, and gives the complex view for complex data (since in a complex fit, we use view to treat real an imaginary parts the same)

latex()

show the latex string for the function, with all the symbols substituted by their values

output(*name)

give the fit value of a particular symbol, or a dictionary of all values.

Parameters:

name (str (optional)) – name of the symbol. If no name is passed, then output returns a dictionary of the resulting values.

Returns:

retval – Either a dictionary of all the values, or the value itself

Return type:

dict or float

pinvr_step(sigma=None)

Use regularized Pseudo-inverse to (partly) solve: \(-residual = f(\mathbf{p}+\Delta \mathbf{p})-f(\mathbf{p}) \approx \nabla f(\mathbf{p}) \cdot \Delta \mathbf{p}\)

residual(pars, sigma=None)

calculate the residual OR if data is None, return fake data

set_guess(*args, **kwargs)

set both the guess and the bounds

Parameters:

guesses (dict of dicts) –

each dict has a keyword giving the parameter and a value that comprises a dict with guesses (value) and/or constraints (min/max)

Can be passed either as the only argument, or a kwarg called guesses, or as the kwargs themselves (i.e. .set_guesses(**guesses))

For example (the last case:)

>>> d.set_guess(
>>>     param1=dict(value=1.0, min=0, max=10),
>>>     param2=dict(value=2.0, min=0, max=10))

settoguess()

a debugging function, to easily plot the initial guess

property transformed_data

If we do something like fit a lorentzian or voigt lineshape, it makes more sense to define our fit function in the time domain, but to calculate the residuals and to evaluate in the frequency domain. Therefore, we define a function self.residual_transform that accepts an nddata, and defines how the data is manipulated to move into the (e.g. frequency) residual domain.

Returns:

retval – just return the ndarray

Return type:

ndarray