Interactive Curve Fitting – GUI Tools

This module contains the interactive GUI curve-fitting tools. They are based on Traits and TraitsGUI. Plotting is provided through the Chaco 2D plotting library , and, optionally, Mayavi for 3D plotting. The available models are those registered by the pymodelmit.core.register_model() mechanism.

FitGui – Interactive 1D model-fitting

Reference

class pymodelfit.fitgui.FitGui(xdata=None, ydata=None, weights=None, model=None, include_models=None, exclude_models=None, fittype=None, **traits)[source]

Bases: traits.has_traits.HasTraits

This class represents the fitgui application state.

Parameters:
  • xdata (array-like) – the first dimension of the data to be fit
  • ydata (array-like) – the second dimension of the data to be fit
  • weights

    The weights to apply to the data. Statistically interpreted as inverse errors (not inverse variance). May be any of the following forms:

    • None for equal weights
    • an array of points that must match ydata
    • a 2-sequence of arrays (xierr,yierr) such that xierr matches the xdata and yierr matches ydata
    • a function called as f(params) that returns an array of weights that match one of the above two conditions
  • model (None, string, or pymodelfit.core.FunctionModel1D instance.) – the initial model to use to fit this data
  • include_models – With exclude_models, specifies which models should be available in the “new model” dialog (see models.list_models for syntax).
  • exclude_models – With include_models, specifies which models should be available in the “new model” dialog (see models.list_models for syntax).
  • fittype (string) – The fitting technique for the initial fit (see pymodelfit.core.FunctionModel).

kwargs are passed in as any additional traits to apply to the application.

dataChanged()[source]

Updates the application state if the fit data are altered - the GUI will know if you give it a new data array, but not if the data is changed in-place.

getModelInitStr()[source]

Generates a python code string that can be used to generate a model with parameters matching the model in this FitGui.

Returns:initializer string
getModelObject()[source]

Gets the underlying object representing the model for this fit.

Returns:The pymodelfit.core.FunctionModel1D object.
weightsChanged()[source]

Updates the application state if the weights/error bars for this model are changed - the GUI will automatically do this if you give it a new set of weights array, but not if they are changed in-place.

pymodelfit.fitgui.fit_data(*args, **kwargs)[source]

Fit a 2d data set using the FitGui interface. A GUI application instance must already exist (e.g. interactive mode of ipython). This function is modal and will block until the user hits “OK” or “Cancel” - if non-blocking behavior is desired, create a FitGui object and call FitGui.edit_traits().

The following forms for input arguments are accepted:

  • fit_data(xdata,ydata)

  • fit_data(xdata,ydata,model)

  • fit_data(model)

    This form requires a FunctionModel1D object that includes data

Parameters:
  • xdata (array-like) – the first dimension of the data to be fit
  • ydata (array-like) – the second dimension of the data to be fit
  • model (None, string, or pymodelfit.core.FunctionModel1D instance) – the initial model to use to fit this data

kwargs are passed into the fitgui initializer

Returns:The model or None if fitting is cancelled or no model is assigned in the GUI.

Examples

>>> from numpy.random import randn
>>> fit_data(randn(100),randn(100)) 

This will bring up 100 normally-distributed points with no initial fitting model.

>>> from numpy.random import randn
>>> fit_data(randn(100),randn(100),'linear') 

This will bring up 100 normally-distributed points with a best-fit linear model.

>>> from numpy.random import randn
>>> fit_data(randn(100),randn(100),'linear',weights=rand(100)) 

This will bring up 100 normally-distributed points with a best-fit linear model with the points weighted by uniform random values.

>>> from numpy import tile
>>> from numpy.random import randn,rand
>>> fit_data(randn(100),randn(100),'linear',weights=tile(rand(100),2).reshape((2,10)),fittype='yerr') 

This will bring up 100 normally-distributed points with a linear model with the points weighted by a uniform random number (interpreted as inverse error) fit using the yerr algorithm instead of the default least-squares.

MultiFitGui – Interactive fitting for multiple 1D models

Note that the MultiFitGui requires Mayavi due to the need for 3D plotting.

Reference

class pymodelfit.multifitgui.MultiFitGui(data, names=None, models=None, weights=None, dofits=True, **traits)[source]

Bases: traits.has_traits.HasTraits

data should be c x N where c is the number of data columns/axes and N is the number of points

Parameters:
  • data (sequence of c equal-length arrays (length N)) – The data arrays
  • names (sequence of strings, length c) – Names
  • models (sequence of models, length c-1) – The models to fit for each pair either as strings or astroypsics.models.ParametricModel objects.
  • weights (array-like of size N or None) – the weights for each point or None for no weights
  • dofits (bool) – If True, the data will be fit to the models when the object is created, otherwise the models will be passed in as-is (or as created).

extra keyword arguments get passed in as new traits (r[finmask],m[finmask],l[finmask]),names=’rh,Mh,Lh’,weights=w[finmask],models=models,dofits=False)

weightsChanged()[source]
pymodelfit.multifitgui.fit_data_multi(data, names=None, weights=None, models=None)[source]

fit a data set consisting of a variety of curves simultaneously. A GUI application instance must already exist (e.g. interactive mode of ipython)

returns a tuple of models e.g. [xvsy,xvsz]