SpecStack

class deproject.specstack.SpecStack

Bases: object

Manipulate a stack of spectra in Sherpa.

This SpecStack class provides a number of wrappers around Sherpa routines that handle loading data, setting the source model, setting up the data to fit, such as: the noticed energy range, how to handle the background, extracting parameter values, and plotting data.

Attributes Summary

datasets

Information (dataset identifier, annulus, name) about the loaded data.

n_datasets

How many datasets are registered?

Methods Summary

dummyfunc(*args, **kwargs)

find_norm(shell)

Return the normalization value for the given shell.

find_parval(parname)

Return the value of the first parameter matching the name.

freeze(par)

Freeze the given parameter in each shell.

get_par(par)

Return the parameter value for each shell.

group(*args)

Apply the grouping for each data set.

ignore(*args)

Apply Sherpa ignore command to each dataset.

load_pha(specfile, annulus)

Load a pha file and add to the datasets for stacked analysis.

notice(*args)

Apply Sherpa notice command to each dataset.

plot_arf(*args, **kwargs)

plot_bkg(*args, **kwargs)

plot_bkg_chisqr(*args, **kwargs)

plot_bkg_delchi(*args, **kwargs)

plot_bkg_fit(*args, **kwargs)

plot_bkg_fit_delchi(*args, **kwargs)

plot_bkg_fit_resid(*args, **kwargs)

plot_bkg_model(*args, **kwargs)

plot_bkg_ratio(*args, **kwargs)

plot_bkg_resid(*args, **kwargs)

plot_bkg_source(*args, **kwargs)

plot_bkg_unconvolved(*args, **kwargs)

plot_chisqr(*args, **kwargs)

plot_data(*args, **kwargs)

plot_delchi(*args, **kwargs)

plot_fit(*args, **kwargs)

plot_fit_delchi(*args, **kwargs)

plot_fit_resid(*args, **kwargs)

plot_model(*args, **kwargs)

plot_order(*args, **kwargs)

plot_psf(*args, **kwargs)

plot_ratio(*args, **kwargs)

plot_resid(*args, **kwargs)

plot_source(*args, **kwargs)

print_window(*args, **kwargs)

Create a hardcopy version of each plot window.

set_par(par, val)

Set the parameter value in each shell.

subtract(*args)

Subtract the background from each dataset.

thaw(par)

Thaw the given parameter in each shell.

tie_par(par, base, *others)

Tie parameters in one or more shells to the base shell.

ungroup(*args)

Turn off the grouping for each data set.

unsubtract(*args)

Un-subtract the background from each dataset.

untie_par(par, *others)

Remove the parameter tie/link in the shell.

Attributes Documentation

datasets = None

Information (dataset identifier, annulus, name) about the loaded data.

n_datasets

How many datasets are registered?

This is not the same as the number of annuli.

Returns:

ndata – The number of datasets.

Return type:

int

Methods Documentation

dummyfunc(*args, **kwargs)
find_norm(shell)

Return the normalization value for the given shell.

This is limited to XSPEC-style models, where the parameter is called “norm”.

Parameters:

shell (int) – The shell number.

Returns:

norm – The normalization of the shell.

Return type:

float

Raises:

ValueError – If there is not one norm parameter for the shell.

See also

find_parval, set_par

find_parval(parname)

Return the value of the first parameter matching the name.

Parameters:

parname (str) – The parameter name. The case is ignored in the match, and the first match is returned.

Returns:

parval – The parameter value

Return type:

float

Raises:

ValueError – There is no match for the parameter.

See also

find_norm, set_par

Examples

>>> kt = dep.find_parval('kt')
freeze(par)

Freeze the given parameter in each shell.

Parameters:

par (str) – The parameter name, specified as <model_type>.<par_name>

See also

thaw, tie_par, untie_par

Examples

>>> dep.freeze('clus.abundanc')
get_par(par)

Return the parameter value for each shell.

Parameters:

par (str) – The parameter name, specified as <model_type>.<par_name>

Returns:

vals – The parameter values, in shell order.

Return type:

ndarray

Examples

>>> kts = dep.get_par('xsapec.kt')
group(*args)

Apply the grouping for each data set.

See also

ungroup

Examples

>>> dep.group()
ignore(*args)

Apply Sherpa ignore command to each dataset.

The filter is applied to each data set separately.

See also

notice

Examples

Restrict the analysis to those bins which fall in the range 0.5 to 7.0 keV, where the limits are not included in the noticed range. The call to notice is used to clear any existing filter.

>>> dep.notice(None, None)
>>> dep.ignore(None, 0.5)
>>> dep.ignore(7.0, None)
load_pha(specfile, annulus)

Load a pha file and add to the datasets for stacked analysis.

It is required that datasets for all annuli are loaded before the source model is created (to ensure that components are created for each annulus).

Parameters:
  • specfile (str or sherpa.astro.data.DataPHA object) – If a string, the name of the file containing the source spectrum, which must be in PHA format (the data is expected to be extracted on the PI column). If a DataPHA object, then this is used (and is assumed to contain any needed background data).

  • annulus (int) – The annulus number for the data.

Returns:

dataid – The Sherpa dataset identifier used for this spectrum.

Return type:

int

Examples

Load the data for four annuli from the files ‘ann1.pi’ to ‘ann4.pi’.

>>> dep.load_pha('ann1.pi', 0)
>>> dep.load_pha('ann2.pi', 1)
>>> dep.load_pha('ann3.pi', 2)
>>> dep.load_pha('ann4.pi', 3)

Load in the PHA files into Sherpa DataPHA objects, and then use these objects:

>>> s1 = ui.unpack_pha('src1.pi')
>>> s2 = ui.unpack_pha('src2.pi')
>>> s3 = ui.unpack_pha('src3.pi')
>>> dep.load_pha(s1, 0)
>>> dep.load_pha(s2, 1)
>>> dep.load_pha(s3, 2)
notice(*args)

Apply Sherpa notice command to each dataset.

The filter is applied to each data set separately.

See also

ignore

Examples

Restrict the analysis to those bins which fall in the range 0.5 to 7.0 keV, where the limits are included in the noticed range. The first call to notice is used to clear any existing filter.

>>> dep.notice(None, None)
>>> dep.notice(0.5, 7.0)
plot_arf(*args, **kwargs)
plot_bkg(*args, **kwargs)
plot_bkg_chisqr(*args, **kwargs)
plot_bkg_delchi(*args, **kwargs)
plot_bkg_fit(*args, **kwargs)
plot_bkg_fit_delchi(*args, **kwargs)
plot_bkg_fit_resid(*args, **kwargs)
plot_bkg_model(*args, **kwargs)
plot_bkg_ratio(*args, **kwargs)
plot_bkg_resid(*args, **kwargs)
plot_bkg_source(*args, **kwargs)
plot_bkg_unconvolved(*args, **kwargs)
plot_chisqr(*args, **kwargs)
plot_data(*args, **kwargs)
plot_delchi(*args, **kwargs)
plot_fit(*args, **kwargs)
plot_fit_delchi(*args, **kwargs)
plot_fit_resid(*args, **kwargs)
plot_model(*args, **kwargs)
plot_order(*args, **kwargs)
plot_psf(*args, **kwargs)
plot_ratio(*args, **kwargs)
plot_resid(*args, **kwargs)
plot_source(*args, **kwargs)
print_window(*args, **kwargs)

Create a hardcopy version of each plot window.

Parameters:
  • args – The arguments to be sent to the “create a hardcopy” routine. The first argument, if given, is assumed to be the file name and so will have the shell number added to it.

  • kwargs – Keyword arguments for the call.

Notes

This is not guaranteed to work properly for Matplotlib.

Examples

Create hardcopy versions of the data plots, called “data0”, “data1”, …

>>> dep.plot_data()
>>> dep.print_window('data')
set_par(par, val)

Set the parameter value in each shell.

Parameters:
  • par (str) – The parameter name, specified as <model_type>.<par_name>

  • val (float) – The parameter value.

See also

get_par, tie_par

Examples

>>> dep.set_par('xsapec.abundanc', 0.25)
subtract(*args)

Subtract the background from each dataset.

See also

unsubtract

Examples

>>> dep.substract()
thaw(par)

Thaw the given parameter in each shell.

Parameters:

par (str) – The parameter name, specified as <model_type>.<par_name>

See also

freeze, tie_par, untie_par

Examples

>>> dep.thaw('clus.abundanc')
tie_par(par, base, *others)

Tie parameters in one or more shells to the base shell.

This is a limited form of the Sherpa ability to link parameters, since it sets the parameter in the other shells to the same value as the parameter in the base shell. More complex situations will require direct calls to sherpa.astro.ui.link.

Parameters:
  • par (str) – The parameter specifier, as <model_type>.<par_name>.

  • base (int) – The base shell number.

  • *others (scalar) – The shell, or shells to link to the base shell.

See also

set_par, untie_par

Examples

Tie the temperature and abundance parameters in shell 9 to that in shell 8, so that any fits will set the shell 9 values to those used in shell 8 (so reducing the number of free parameters in the fit).

>>> dep.tie_par('xsapec.kt', 8, 9)
Tying xsapec_9.kT to xsapec_8.kT
>>> dep.tie_par('xsapec.abundanc', 8, 9)
Tying xsapec_9.Abundanc to xsapec_8.Abundanc

Tie three annuli together:

>>> dep.tie_par('xsapec.kt', 12, 13, 14)
Tying xsapec_13.kT to xsapec_12.kT
Tying xsapec_14.kT to xsapec_12.kT
ungroup(*args)

Turn off the grouping for each data set.

See also

group

Examples

>>> dep.ungroup()
unsubtract(*args)

Un-subtract the background from each dataset.

This can be useful when you want to compare the results to the “wstat” stat (a Poisson-based stat which includes the background data as a component and provides a goodness-of-fit estimate).

See also

subtract

Examples

>>> dep.unsubstract()
untie_par(par, *others)

Remove the parameter tie/link in the shell.

This is intended to remove links between shells created by tie_par, but will remove any links created by sherpa.astro.ui.link.

Parameters:
  • par (str) – The parameter specifier, as <model_type>.<par_name>.

  • *others (scalar) – The shell, or shells to un-tie/unlink.

See also

tie_par

Notes

It is safe to call on a parameter that is not tied or linked to another parameter.

Examples

Untie the abundance parameter in shell 9; that is, it is now free to vary independently in a fit.

>>> dep.untie_par('xsapec.abundanc', 9)
Untying xsapec_9.Abundanc

Untie multiple annuli:

>>> dep.untie_par('xsmekal.kt', 13, 14)
Untying xsmekal_13.kT
Untying xsmekal_14.kT