Synopsis
Fit a model to one or more background PHA data sets.
Syntax
fit_bkg(id: int | str | None = None, *otherids: int | str, **kwargs) No return value.
Description
Fit only the background components of PHA data sets. This can be used to find the best-fit background parameters, which can then be frozen before fitting the data, or to ensure that these parameters are well defined before performing a simultaneous source and background fit.
Examples
Example 1
Simultaneously fit all background data sets with models and then store the results in the variable fres:
>>> fit_bkg() >>> fres = get_fit_results()
Example 2
Fit the background for data sets 1 and 2, then do a simultaneous fit to the source and background data sets:
>>> fit_bkg(1, 2) >>> fit(1, 2)
Example 3
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_bkg(outfile=out) >>> txt = out.getvalue()
Example 4
The messages from `fit_bkg` use the standard Sherpa logging infrastructure, and so can be ignored by using `SherpaVerbosity` :
>>> from sherpa.utils.logging import SherpaVerbosity
>>> with SherpaVerbosity("WARN"):
... fit_bkg()
...
>>> fres = get_fit_results()PARAMETERS
The parameters for this function are:
| Parameter | Type information | Definition |
|---|---|---|
| id | int or str, optional | The data set that provides the background data. If not given then all data sets with an associated background model are fit simultaneously. |
| *otherids | sequence of 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 if `outfile` is 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 the `FitResults` object that you can obtain with `get_fit_results` . |
Notes
This is only for PHA data sets where the background is being modelled, rather than subtracted from the data.
The screen output from the fit can be controlled with the `SherpaVerbosity` context manager, as shown in the examples.
If outfile is sent a file handle then it is not closed by this routine.
Changes in CIAO
Changed in CIAO 4.18
The parameter record_steps was added to keep parameter values of each iteration with the fit results.
Changed in CIAO 4.17
The outfile parameter can now be sent a Path object or a file handle instead of a string.
Bugs
See the bugs pages on the Sherpa website for an up-to-date listing of known bugs.
See Also
- fitting
- fit
- utilities
- calc_bkg_stat