calc_model
- sherpa.astro.ui.calc_model(id: IdType | None = None, bkg_id: IdType | None = None) tuple[tuple[np.ndarray, ...], np.ndarray]
Calculate the per-bin model values.
The values are filtered and grouped based on the data and will use the analysis setting for PHA data, but not the other plot options (such as whether to display as a rate).
Added in version 4.17.0.
- Parameters:
id (int, str, or None, optional) – Use the source expression associated with this data set. If not given then the default identifier is used, as returned by
get_default_id
.bkg_id (int, str, or None, optional) – If set, use the model associated with the given background component rather than the source model.
- Returns:
xvals, yvals – The independent axis, which uses a tuple as the number of elements depends on the dimensionality and type of data. The units depends on the data type: for PHA data the X axis will be in the analysis units and Y axis will generally be counts.
- Return type:
tuple of ndarray, ndarray
See also
Examples
For a PHA dataset the independent axis is a pair of values, giving the low and high energies. The xlo and xhi values are in keV, and represent the low and high edges of each bin, and the yvals array is in counts.
>>> load_pha("3c273.pi") >>> set_analysis("energy") >>> notice(0.5, 6) >>> set_source(xsphabs.gal * powlaw1d.pl) >>> gal.nh = 0.1 >>> pl.gamma = 1.7 >>> pl.ampl = 2e-4 >>> xvals, yvals = calc_model() >>> xlo = xvals[0] >>> xhi = xvals[1]
The results can be compared to the model output in plot_fit to show agreement (note that calc_model returns grouped values, as used by plot_fit, whereas plot_model shows the ungrouped data):
>>> set_analysis("energy", type="rate", factor=0) >>> plot_fit() >>> plot_model(overplot=True, color="black", alpha=0.4) >>> xvals, yvals = calc_model() >>> elo, ehi = xvals >>> exposure = get_exposure() >>> plt.plot((elo + ehi) / 2, yvals / (ehi - elo) / exposure)
Changing the analysis setting changes the x values, as xvals2 is in Angstrom rather than keV (the model values are the same, although there may be small numerical differences that mean the values do not exactly match):
>>> set_analysis("wave") >>> xvals2, yvals2 = calc_model()
For 1D datasets the x axis is a single-element tuple:
>>> load_arrays(2, [1, 4, 7], [3, 12, 2]) >>> set_source(2, gauss1.gline) >>> gline.pos = 4.2 >>> gline.fwhm = 3 >>> gline.ampl = 12 >>> xvals, yvals = calc_model(2) >>> x = xvals[0] >>> x array([1, 4, 7]) >>> yvals array([ 0.51187072, 11.85303595, 1.07215839])