sncosmo.fit_lc(data=None, model=None, vparam_names=[], bounds=None, spectra=None, method='minuit', guess_amplitude=True, guess_t0=True, guess_z=True, minsnr=5.0, modelcov=False, verbose=False, maxcall=10000, phase_range=None, wave_range=None, warn=True)[source]

Fit model parameters to data by minimizing chi^2.

Ths function defines a chi^2 to minimize, makes initial guesses for t0 and amplitude, then runs a minimizer.

dataTable or ndarray or dict

Table of photometric data. Must include certain columns. See the “Photometric Data” section of the documentation for required columns.


The model to fit.


Model parameters to vary in the fit.

boundsdict, optional

Bounded range for each parameter. Keys should be parameter names, values are tuples. If a bound is not given for some parameter, the parameter is unbounded. The exception is t0: by default, the minimum bound is such that the latest phase of the model lines up with the earliest data point and the maximum bound is such that the earliest phase of the model lines up with the latest data point.

spectraSpectrum or list of Spectrum objects

A list of spectra to include in the fit.

guess_amplitudebool, optional

Whether or not to guess the amplitude from the data. If false, the current model amplitude is taken as the initial value. Only has an effect when fitting amplitude. Default is True.

guess_t0bool, optional

Whether or not to guess t0. Only has an effect when fitting t0. Default is True.

guess_zbool, optional

Whether or not to guess z (redshift). Only has an effect when fitting redshift. Default is True.

minsnrfloat, optional

When guessing amplitude and t0, only use data with signal-to-noise ratio (flux / fluxerr) greater than this value. Default is 5.

method{‘minuit’}, optional

Minimization method to use. Currently there is only one choice.

modelcovbool, optional

Include model covariance when calculating chisq. Default is False. If true, the fit is performed multiple times until convergence.

phase_range(float, float), optional

If given, discard data outside this range of phases. Note that the definition of phase varies between models: For example, phase=0 refers to explosion time in some models and time of peak B band flux in others.

New in version 1.5.0

wave_range(float, float), optional

If given, discard data with bandpass effective wavelengths outside this range.

New in version 1.5.0

verbosebool, optional

Print messages during fitting.

warnbool, optional

Issue a warning when dropping bands outside the wavelength range of the model. Default is True.

New in version 1.5.0


The optimization result represented as a Result object, which is a dict subclass with attribute access. Therefore, res.keys() provides a list of the attributes. Attributes are:

  • success: boolean describing whether fit succeeded.

  • message: string with more information about exit status.

  • ncall: number of function evaluations.

  • chisq: minimum chi^2 value.

  • ndof: number of degrees of freedom (len(data) - len(vparam_names)).

  • param_names: same as model.param_names.

  • parameters: 1-d ndarray of best-fit values (including fixed parameters) corresponding to param_names.

  • vparam_names: list of varied parameter names.

  • covariance: 2-d ndarray of parameter covariance; indicies correspond to order of vparam_names.

  • errors: OrderedDict of varied parameter uncertainties. Corresponds to square root of diagonal entries in covariance matrix.

  • nfit: number of times the fit was performed. Can be greater than one when model covariance, phase range or wavelength range is used. New in version 1.5.0.

  • data_mask: Boolean array the same length as data specifying whether each observation was used in the final fit. New in version 1.5.0.


A copy of the model with parameters set to best-fit values.


t0 guess: If t0 is being fit and guess_t0=True, the function will guess the initial starting point for t0 based on the data. The guess is made as follows:

  • Evaluate the time and value of peak flux for the model in each band given the current model parameters.

  • Determine the data point with maximum flux in each band, for points with signal-to-noise ratio > minsnr (default is 5). If no points meet this criteria, the band is ignored (for the purpose of guessing only).

  • For each band, compare model’s peak flux to the peak data point. Choose the band with the highest ratio of data / model.

  • Set t0 so that the model’s time of peak in the chosen band corresponds to the peak data point in this band.

amplitude guess: If amplitude (assumed to be the first model parameter) is being fit and guess_amplitude=True, the function will guess the initial starting point for the amplitude based on the data.

redshift guess: If redshift (z) is being fit and guess_z=True, the function will set the initial value of z to the average of the bounds on z.


The flatten_result function can be used to make the result a dictionary suitable for appending as rows of a table:

>>> from astropy.table import Table
>>> table_rows = []
>>> for sn in sne:
...     res, fitmodel = sncosmo.fit_lc(
...          sn, model, ['t0', 'x0', 'x1', 'c'])
...     table_rows.append(flatten_result(res))
>>> t = Table(table_rows)