simulfit
- sherpa.astro.ui.simulfit(id: IdType | None = None, *otherids: IdType, **kwargs) None
Fit a model to one or more data sets.
Use forward fitting to find the best-fit model to one or more data sets, given the chosen statistic and optimization method. The fit proceeds until the results converge or the number of iterations exceeds the maximum value (these values can be changed with
set_method_opt). An iterative scheme can be added usingset_iter_methodto try and improve the fit. The final fit results are displayed to the screen and can be retrieved withget_fit_results.Changed in version 4.17.1: The parameter
record_stepswas added to keep parameter values of each iteration with the fit results.Changed in version 4.17.0: The outfile parameter can now be sent a Path object or a file handle instead of a string.
- Parameters:
id (int or str, optional) – The data set that provides the data. If not given then all data sets with an associated model are fit simultaneously.
*otherids (int or str, optional) – Other data sets to use in the calculation.
outfile (str, Path, IO object, or None, optional) – If set, then the fit results will be written to a file with this name. The file contains the per-iteration fit results.
clobber (bool, optional) – This flag controls whether an existing file can be overwritten (
True) or if it raises an exception (False, the default setting). This is only used ifoutfileis set to a string or Path object.record_steps (bool, optional) – If
True, then the parameter values and statistic value are recorded at each iteration in an array in theFitResultsobject that you can obtain withget_fit_results.
- Raises:
sherpa.utils.err.FitErr – If
filenamealready exists andclobberisFalse.
See also
confEstimate parameter confidence intervals using the confidence method.
contour_fitContour the fit to a data set.
covarEstimate the confidence intervals using the confidence method.
freezeFix model parameters so they are not changed by a fit.
get_fit_resultsReturn the results of the last fit.
plot_fitPlot the fit results (data, model) for a data set.
image_fitDisplay the data, model, and residuals for a data set in the image viewer.
set_statSet the statistical method.
set_methodChange the optimization method.
set_method_optChange an option of the current optimization method.
set_full_modelDefine the convolved model expression for a data set.
set_iter_methodSet the iterative-fitting scheme used in the fit.
set_modelSet the model expression for a data set.
show_fitSummarize the fit results.
thawAllow model parameters to be varied during a fit.
Notes
If outfile is sent a file handle then it is not closed by this routine.
The screen output from the fit can be controlled with the
SherpaVerbositycontext manager, as shown in the examples.Examples
Simultaneously fit all data sets with models and then store the results in the variable fres:
>>> fit() >>> fres = get_fit_results()
Fit just the data set ‘img’:
>>> fit('img')
Simultaneously fit data sets 1, 2, and 3:
>>> fit(1, 2, 3)
Fit the dataset ‘jet’ and keep the values for each parameter in every optimization step (this example assumes that the model for the data set ‘jet’ has a parameter called ‘const1d.c0’):
>>> fit('jet', record_steps=True) >>> fres = get_fit_results() >>> for row in fres.record_steps: ... print(f"{row['nfev']} {row['statistic']:8.6e} {row['const1d.c0']:6.4f}") [... output here ...]
Fit data set ‘jet’ and write the fit results to the text file ‘jet.fit’, over-writing it if it already exists:
>>> fit('jet', outfile='jet.fit', clobber=True)
Store the per-iteration values in a StringIO object and extract the data into the variable txt (this avoids the need to create a file):
>>> from io import StringIO >>> out = StringIO() >>> fit(outfile=out) >>> txt = out.getvalue()
The messages from
fituse the standard Sherpa logging infrastructure, and so can be ignored by usingSherpaVerbosity:>>> from sherpa.utils.logging import SherpaVerbosity >>> with SherpaVerbosity("WARN"): ... fit() ... >>> fres = get_fit_results()