CompositeModel

class sherpa.models.model.CompositeModel(name: str, parts: Sequence[Model])[source] [edit on github]

Bases: Model

Represent a model with composite parts.

This is the base class for representing expressions that combine multiple models and values.

Parameters:
  • name (str) – The name for the collection of models.

  • parts (sequence of Model objects) – The models.

parts
Type:

sequence of Model

Notes

Composite models can be iterated through to find their components:

>>> from sherpa.models.basic import Gauss1D, Polynom1D
>>> l1 = Gauss1D('l1')
>>> l2 = Gauss1D('l2')
>>> b = Polynom1D('b')
>>> mdl = l1 + (0.5 * l2) + b
>>> mdl
<BinaryOpModel model instance 'l1 + 0.5 * l2 + b'>
>>> for cpt in mdl:
...     print(type(cpt))
...
<class 'sherpa.models.model.BinaryOpModel'>
<class 'sherpa.models.basic.Gauss1D'>
<class 'sherpa.models.model.BinaryOpModel'>
<class 'sherpa.models.model.ArithmeticConstantModel'>
<class 'sherpa.models.basic.Gauss1D'>
<class 'sherpa.models.basic.Polynom1D'>

Attributes Summary

lpars

Return any linked parameters.

ndim

The dimensionality of the model, if defined, or None.

pars

Return the parameters of the model.

thawedparhardmaxes

The hard maximum values for the thawed parameters.

thawedparhardmins

The hard minimum values for the thawed parameters.

thawedparmaxes

The maximum limits of the thawed parameters.

thawedparmins

The minimum limits of the thawed parameters.

thawedpars

The thawed parameters of the model.

Methods Summary

cache_clear()

Clear the cache for each component.

cache_status()

Display the cache status of each component.

calc(p, *args, **kwargs)

Evaluate the model on a grid.

freeze()

Freeze any thawed parameters of the model.

get_center()

get_thawed_pars()

Return the thawed parameter objects.

guess(dep, *args, **kwargs)

Call guess on each component.

reset()

Reset the parameter values.

set_center(*args, **kwargs)

startup([cache])

Called before a model may be evaluated multiple times.

teardown()

Called after a model may be evaluated multiple times.

thaw()

Thaw any frozen parameters of the model.

Attributes Documentation

lpars

Return any linked parameters.

This only returns linked parameters that are not related to the model, and each parameter is not repeated.

Added in version 4.16.1.

See also

pars

Examples

By default there are no linked parameters:

>>> from sherpa.models.basic import Gauss2D
>>> mdl = Gauss2D("mdl")
>>> len(mdl.pars)
6
>>> mdl.lpars
()

Force the model to have identical xpos and ypos parameters. Since the linked parameter value (mdl.xpos) is part of the model it is not included in lpars:

>>> mdl.ypos = mdl.xpos
>>> len(mdl.pars)
6
>>> mdl.lpars
()

Add a link to allow the sigma term to be fit rather than FWHM. Since the linked parameter - here from the Const1D model - is not a part of the model it is included in lpars:

>>> import numpy as np
>>> from sherpa.models.basic import Const1D
>>> sigma = Const1D("sigma")
>>> mdl.fwhm = 2 * np.sqrt(2 * np.log(2)) * sigma.c0
>>> len(mdl.pars)
6
>>> mdl.lpars
(<Parameter 'c0' of model 'sigma'>,)
ndim: int | None = None

The dimensionality of the model, if defined, or None.

pars

Return the parameters of the model.

This does not include any linked parameters.

Changed in version 4.16.1: The pars field can no-longer be set directly. Individual elements can still be changed.

See also

lpars

thawedparhardmaxes

The hard maximum values for the thawed parameters.

The minimum and maximum range of the parameters can be changed with thawedparmins and thawedparmaxes but only within the range given by thawedparhardmins to thawparhardmaxes.

thawedparhardmins

The hard minimum values for the thawed parameters.

The minimum and maximum range of the parameters can be changed with thawedparmins and thawedparmaxes but only within the range given by thawedparhardmins to thawparhardmaxes.

thawedparmaxes

The maximum limits of the thawed parameters.

Get or set the maximum limits of the thawed parameters of the model as a list of numbers. If there are no thawed parameters then [] is used. The ordering matches that of the pars attribute.

See also

thawedpars, thawedarhardmaxes, thawedparmins

thawedparmins

The minimum limits of the thawed parameters.

Get or set the minimum limits of the thawed parameters of the model as a list of numbers. If there are no thawed parameters then [] is used. The ordering matches that of the pars attribute.

See also

thawedpars, thawedarhardmins, thawedparmaxes

thawedpars

The thawed parameters of the model.

Get or set the thawed parameters of the model as a list of numbers. If there are no thawed parameters then [] is used. The ordering matches that of the pars attribute.

Methods Documentation

cache_clear() None[source] [edit on github]

Clear the cache for each component.

cache_status() None[source] [edit on github]

Display the cache status of each component.

Information on the cache - the number of “hits”, “misses”, and “requests” - is displayed at the INFO logging level.

Examples

>>> mdl.cache_status()
 xsphabs.gal                size:    5  hits:   715  misses:   158  check=  873
 powlaw1d.pl                size:    5  hits:   633  misses:   240  check=  873
calc(p: Sequence[SupportsFloat], *args, **kwargs) ndarray [edit on github]

Evaluate the model on a grid.

Parameters:
  • p (sequence of numbers) – The parameter values to use. The order matches the pars field.

  • *args – The model grid. The values can be scalar or arrays, and the number depends on the dimensionality of the model and whether it is being evaluated over an integrated grid or at a point (or points).

  • **kwargs – Any model-specific values that are not parameters.

freeze() None [edit on github]

Freeze any thawed parameters of the model.

get_center() [edit on github]
get_thawed_pars() list[Parameter] [edit on github]

Return the thawed parameter objects.

This includes linked parameters, which complicates the min/max settings, since the range on the components of a linked parameter does not match that of the original parameter, which is an issue when the limits are exceeded.

Added in version 4.16.1.

guess(dep, *args, **kwargs)[source] [edit on github]

Call guess on each component.

At the moment there is no recognition of the full model expression - e.g. cpt1 * cpt2 and cpt1 + cpt2 would ideally have different scalings applied here.

Changed in version 4.17.0: Prior to 4.17.0 the guess method could not be called on composite models.

reset() None [edit on github]

Reset the parameter values.

Restores each parameter to the last value it was set to. This allows the parameters to be easily reset after a fit.

set_center(*args, **kwargs) [edit on github]
startup(cache: bool = False) None[source] [edit on github]

Called before a model may be evaluated multiple times.

Parameters:

cache (bool, optional) – Should a cache be used when evaluating the models.

See also

teardown

teardown() None[source] [edit on github]

Called after a model may be evaluated multiple times.

See also

startup

thaw() None [edit on github]

Thaw any frozen parameters of the model.

Those parameters that are marked as “always frozen” are skipped.