class xspec.Model(exprString, modName='', sourceNum=1, setPars=None)

Xspec model class.


  • __init__
  • __call__ (the ‘()’ operator)
  • energies
  • folded
  • setPars
  • show
  • showList
  • untie
  • values

Attributes (all are get-only)

  • expression

    The model expression string, using full component names.

  • name

    The model name, optional in Xspec. This is an empty string for un-named models.

  • <components>

    Model includes an attribute of type Component for every Xspec component in the model. The attribute name is the same as the full name of the Xspec component (ie. m=Model(“po”) produces an m.powerlaw attribute).

  • componentNames

    List of component name strings.

  • flux

    A tuple containing the results of the most recent flux calculation for this model.

    The tuple values are: (value, errLow, errHigh (in ergs/cm^2), value, errLow, errHigh (in photons)). This will be filled in after an AllModels.calcFlux() call ONLY when no spectra are loaded. Otherwise results are stored in the Spectrum objects.

  • lumin

    Same as flux but for luminosity calculations.

  • nParameters

    Number of parameters in Model object [int].

  • startParIndex

    Global index of the first parameter in this Model object [int].


Get a Parameter object from the Model.


parIdx: The parameter index number.

Regardless of the data group to which the Model object belongs, its parameters are numbered from 1 to nParameters.

Returns the specified Parameter object.

__init__(exprString, modName='', sourceNum=1, setPars=None)

Model constructor.

New model is automatically added to the AllModels container, with one Model object constructed (internally) for each data group to which the model applies. This function returns the Model object corresponding to the lowest numbered data group.


exprString: The model expression string.

Component names may be abbreviated.

modName: Optional name assigned to model.

Any whitespace in string will be removed. This is required if souce number is > 1.

sourceNum: Optional integer for model’s source number.

setPars: Optional initial values for the model’s parameters.

These may be sent in a tuple, list, or dictionary (or as a single float or string if only setting the first parameter). Examples:

# Create a model with all default parameter settings:
m1 = Model("gauss")

# Create phabs*powerlaw and initialize pars 1 and 3 to
#   something other than their default values.
m2 = Model("phabs*po", setPars={1:5.5, 3:".18,,.01,.02"})

# Create another model named 'b', and reset par 2 to 5.0:
m3 = Model("phabs*bbody", "b", setPars={2:5.0})

If any mistakes are made with the optional setPars parameter arguments, the model will be created using all default values.

You can always reset the parameters later with the Model.setPars() method, or directly through the Parameter object’s values attribute.


Get the Model object’s energies array for a given spectrum.


spectrumIndex: The spectrum index number.

If this is 0, it will return the energies array used by the default dummy response.

Returns a list of energy array elements, the size will be 1 larger than the corresponding flux array.

This will return the energies array as specified by the AllModels.setEnergies function if that has been used to override the response energies array.


Get the Model object’s folded flux array for a given spectrum.


spectrumIndex: The spectrum index number.

This number should be 0 if model is not presently applied to any spectra (ie. in the “off” state).

Returns a list of folded flux array elements.


Change the value of multiple parameters in a single function call.

This is a quick way to change multiple parameter values at a time since only a SINGLE model recalculation will be performed at the end. In contrast, when parameter values are changed through the individual parameter objects, the model is recalculated after EACH parameter change. (See also AllModels.setPars(), for changing multiple parameters belonging to multiple model objects.)


parVals: An arbitrary number of parameter values.

These may be listed singly (as floats or strings), or collected into tuple, list or dictionary containers. Dictionaries must be used if parameters are not in consecutive order, in which case the parameter index number is the dictionary key.

Examples: Assume we have a model object m1 with 5 parameters.

Simplest case: change only the parameter values (and not the auxiliary values, ‘sigma’, ‘min’, ‘bot’, etc.), and change them in consecutive order:

# Pass in 1 or more floats
m1.setPars(5.5, 7.83, 4.1e2)  # changes pars 1-3
m1.setPars(2.0, 1.3e-5, -.05, 6.34, 9.2)  # changes all 5 pars

Still changing only the parameter values, but skipping over some:

m1.setPars(.02, 4.4, {5:3.2e5})  # changes pars 1-2, 5
m1.setPars({2:3.0, 4:-1.2})  # changes pars 2, 4
m1.setPars({2:1.8}, 9.3, 5.32)  # changes pars 2, 3, 4

Now also change the auxiliary values for some of the parameters. Pass in a STRING containing “<val>,<sigma>,<min>,<bottom>,<top>, <max>” This uses the same syntax as Standard XSPEC’s “newpar” command. Aux values can be skipped by using multiple commas:

# This sets a new <val>, <sigma>, and <max> for parameter 1, and 
# a new <val> of 5.3 for parameter 2.
m1.setPars(".3,.01,,,,100", 5.3)

# This sets all new auxiliary values for parameter 3.
m1.setPars({3:".8 -.01 1e-4 1e-3 1e5 1e6"})

Display information for a single Model object.

static showList()

Show the list of all available XSPEC model components.


Remove links for all parameters in Model object


Get the Model object’s values array for a given spectrum.


spectrumIndex: The spectrum index number.

This number should be 0 if model is not presently applied to any spectra (ie. in the “off” state).

Returns the values array as a list.