Navigation

astropy.modeling provides a framework for representing models and performing
model evaluation and fitting. It currently supports 1-D and 2-D models and
fitting with parameter constraints.

It is designed to be easily extensible and flexible. Models do not reference
fitting algorithms explicitly and new fitting algorithms may be added without
changing the existing models (though not all models can be used with all
fitting algorithms due to constraints such as model linearity).

The goal is to eventually provide a rich toolset of models and fitters such
that most users will not need to define new model classes, nor special purpose
fitting routines (while making it reasonably easy to do when necessary).

Note

astropy.modeling is currently a work-in-progress, and thus it is likely
there will still be API changes in later versions of Astropy. Backwards
compatibility support between versions will still be maintained as much as
possible, but new features and enhancements are coming in future versions.
If you have specific ideas for how it might be improved, feel free to let
us know on the astropy-dev mailing list or at
http://feedback.astropy.org

In this section, we look at a simple example of fitting a Gaussian to a
simulated dataset. We use the Gaussian1D
and Trapezoid1D models and the
LevMarLSQFitter fitter to fit the data:

importnumpyasnpimportmatplotlib.pyplotaspltfromastropy.modelingimportmodels,fitting# Generate fake datanp.random.seed(0)x=np.linspace(-5.,5.,200)y=3*np.exp(-0.5*(x-1.3)**2/0.8**2)y+=np.random.normal(0.,0.2,x.shape)# Fit the data using a box modelt_init=models.Trapezoid1D(amplitude=1.,x_0=0.,width=1.,slope=0.5)fit_t=fitting.LevMarLSQFitter()t=fit_t(t_init,x,y)# Fit the data using a Gaussiang_init=models.Gaussian1D(amplitude=1.,mean=0,stddev=1.)fit_g=fitting.LevMarLSQFitter()g=fit_g(g_init,x,y)# Plot the data with the best-fit modelplt.figure(figsize=(8,5))plt.plot(x,y,'ko')plt.plot(x,t(x),'b-',lw=2,label='Trapezoid')plt.plot(x,g(x),'r-',lw=2,label='Gaussian')plt.xlabel('Position')plt.ylabel('Flux')plt.legend(loc=2)

A list of models is provided in the Reference/API section. The fitting
framework includes many useful features that are not demonstrated here, such as
weighting of datapoints, fixing or linking parameters, and placing lower or
upper limits on parameters. For more information on these, take a look at the
Fitting Models to Data documentation.

In some cases it is necessary to describe many models of the same type but with
different sets of parameter values. This could be done simply by instantiating
as many instances of a Model as are needed. But that can
be inefficient for a large number of models. To that end, all model classes in
astropy.modeling can also be used to represent a model set which is a
collection of models of the same type, but with different values for their
parameters.

To instantiate a model set, use argument n_models=N where N is the
number of models in the set when constructing the model. The value of each
parameter must be a list or array of length N, such that each item in
the array corresponds to one model in the set:

This is equivalent to two Gaussians with the parameters amplitude=1,mean=0,stddev=0.1 and amplitude=2,mean=0,stddev=0.2 respectively. When
printing the model the parameter values are displayed as a table, with each row
corresponding to a single model in the set.

The number of models in a model set can be determined using the len builtin:

>>> len(g)2

Single models have a length of 1, and are not considered a model set as such.

When evaluating a model set, by default the input must be the same length as
the number of models, with one input per model:

>>> g([0,0.1])array([ 1. , 1.76499381])

The result is an array with one result per model in the set. It is also
possible to broadcast a single value to all models in the set:

>>> g(0)array([ 1., 2.])

Model sets are used primarily for fitting, allowing a large number of models of
the same type to be fitted simultaneously (and independently from each other)
to some large set of inputs. For example, fitting a polynomial to the time
response of each pixel in a data cube. This can greatly speed up the fitting
process, especially for linear models.

New in version 1.0: This feature is experimental and expected to see significant further
development, but the basic usage is stable and expected to see wide use.

While the Astropy modeling package makes it very easy to define new
models either from existing functions, or by writing a
Model subclass, an additional way to create new models is
by combining them using arithmetic expressions. This works with models built
into Astropy, and most user-defined models as well. For example, it is
possible to create a superposition of two Gaussians like so:

This model can be further combined with other models in new expressions. It is
also possible to define entire new model classes using arithmetic expressions
of other model classes. This allows general compound models to be created
without specifying any parameter values up front. This more advanced usage is
explained in more detail in the compound model documentation.

These new compound models can also be fitted to data, like most other models:

importnumpyasnpimportmatplotlib.pyplotaspltfromastropy.modelingimportmodels,fitting# Generate fake datanp.random.seed(42)g1=models.Gaussian1D(1,0,0.2)g2=models.Gaussian1D(2.5,0.5,0.1)x=np.linspace(-1,1,200)y=g1(x)+g2(x)+np.random.normal(0.,0.2,x.shape)# Now to fit the data create a new superposition with initial# guesses for the parameters:gg_init=models.Gaussian1D(1,0,0.1)+models.Gaussian1D(2,0.5,0.1)fitter=fitting.SLSQPLSQFitter()gg_fit=fitter(gg_init,x,y)# Plot the data with the best-fit modelplt.figure(figsize=(8,5))plt.plot(x,y,'ko')plt.plot(x,gg_fit(x),'r-',lw=2)plt.xlabel('Position')plt.ylabel('Flux')

This works for 1-D models, 2-D models, and combinations thereof, though there
are some complexities involved in correctly matching up the inputs and outputs
of all models used to build a compound model. You can learn more details in
the Compound Models documentation.

This subpackage provides a framework for representing models and
performing model evaluation and fitting. It supports 1D and 2D models
and fitting with parameter constraints. It has some predefined models
and fitting routines.

This module implements classes (called Fitters) which combine optimization
algorithms (typically from scipy.optimize) with statistic functions to perform
fitting. Fitters are implemented as callable classes. In addition to the data
to fit, the __call__ method takes an instance of
FittableModel as input, and returns a copy of the
model with its parameters determined by the optimizer.

Optimization algorithms, called “optimizers” are implemented in
optimizers and statistic functions are in
statistic. The goal is to provide an easy to extend
framework and allow users to easily create new fitters by combining statistics
with optimizers.