TableModel
- class sherpa.models.basic.TableModel(name='tablemodel')[source] [edit on github]
Bases:
ArithmeticModel
Tabulated values are linearly scaled and may be interpolated.
The
load
method is used to read in the tabular data. There are two different modes:both independent (x) and dependent (y) axes are set, in which case model evaluation will interpolate the requested grid onto the data, with the interpolation scheme controlled by the
method
attribute. Thefold
method does not need to be used.the x axis is set to
None
, in which case the grid used to evaluate the model must have the same size as the y values, and thefold
method must be sent the data object to be fit before a fit is made. In this case themethod
attribute is ignored.
The model values - the y argument to
load
- are multiplied by theampl
parameter of the model.- ampl
The linear scaling factor for the table values
Notes
The model has
ndim
set toNone
as it can be used for any dimension of data. However, this only makes sense for the second mode, that is when the x argument toload
isNone
. For the first mode, when x is notNone
, the model evaluation only uses the first component of the independent axes - so the low values for a Data1DInt object or the x0 values for a Data1D object.The model ignores the integrate setting.
Examples
If the x array is given to the
load
call then the model can interpolate from the requested grid onto the load data (the default is linear interpolation):>>> tm = TableModel() >>> tm.load([10, 20, 25, 30], [14, 12, 17, 18]) >>> tm.ampl = 10 >>> tm([15, 20, 27]) array([130., 120., 174.])
The
method
attribute can be changed to select a different interpolation scheme: it requires a function that accepts (xout, xin, yin) and returns yout which are the interpolated values of xin,yin onto the grid xout:>>> from sherpa.utils import neville >>> tm.method = neville >>> tm([15, 20, 27]) array([ 90. , 120. , 182.16])
The model can be used for data when the independent axis is either not useful - such as for an image mask - or the data does not have a meaningful independent axis, as in the the independent variable being an index for a star and the dependent axis is a property of each star. In this case the x argument to
load
is set toNone
, which means that no interpolation is used and that thefold
method must be used if the data has been filtered in any way, but it’s safest to always use it:>>> from sherpa.models.basic import TableModel >>> from sherpa.data import Data1D >>> from sherpa.fit import Fit >>> d = Data1D('data', [1, 2, 3, 4, 5], [1.2, .4, 2.2, .3, 1.]) >>> d.staterror = [.2, .2, .2, .2, .2] >>> tm1 = TableModel('tabmodel') >>> tm1.load(None, [.6, .2, 1.1, .2, .5]) >>> print(tm1.ampl.val) 1.0 >>> tm1.fold(d) >>> fit1 = Fit(d, tm1) >>> res1 = fit1.fit() >>> print(tm1.ampl.val) 1.9894736842102083
In this case the
fold
method is necessary, to ensure that the fit only uses the valid data bins:>>> import numpy as np >>> y = np.ma.masked_invalid([np.nan, np.nan, 2.2, .3, 1.]) >>> d = Data1D('data', [1, 2, 3, 4, 5], y) >>> d.staterror = [.2, .2, .2, .2, .2] >>> tm2 = TableModel('tabmodel') >>> tm2.load(None, [.6, .2, 1.1, .2, .5]) >>> tm2.fold(d) >>> print(tm2.ampl.val) 1.0 >>> fit2 = Fit(d, tm2) >>> res2 = fit2.fit() >>> print(tm2.ampl.val) 1.9866666666663104
The masking also holds if the notice or ignore method has been used on the dataset:
>>> d = Data1D('data', [1, 2, 3, 4, 5], [1.2, .4, 2.2, .3, 1]) >>> d.staterror = [.2, .2, .2, .2, .2] >>> d.ignore(xhi=2) >>> tm3 = TableModel('tabmodel') >>> tm3.load(None, [.6, .2, 1.1, .2, .5]) >>> tm3.fold(d) >>> print(tm3.ampl.val) 1.0 >>> fit3 = Fit(d, tm3) >>> res = fit3.fit() >>> print(tm3.ampl.val) 1.9866666666663104
Attributes Summary
The maximum size of the cache.
Return any linked parameters.
The interpolation method, used when x is not None in the load call.
The dimensionality of the model, if defined, or None.
Return the parameters of the model.
The hard maximum values for the thawed parameters.
The hard minimum values for the thawed parameters.
The maximum limits of the thawed parameters.
The minimum limits of the thawed parameters.
The thawed parameters of the model.
Methods Summary
apply
(outer, *otherargs, **otherkwargs)Clear the cache.
Display the cache status.
calc
(p, x0[, x1])Evaluate the model.
fold
(data)Ensure the model matches the data filter.
freeze
()Freeze any thawed parameters of the model.
Return the thawed parameter objects.
get_x
()Return the independent axis or None.
get_y
()Return the dependent axis or None.
guess
(dep, *args, **kwargs)Set an initial guess for the parameter values.
load
(x, y)Set the model values.
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
- cache = 5
The maximum size of the cache.
- 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
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'>,)
- method
The interpolation method, used when x is not None in the load call.
The method argument is a function that accepts arguments (xout, xin, yin) and returns the yout values from interpolating xout onto (xin, yin). The default is linear interpolation (sherpa.utils.linear_interp).
- 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
- 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.
See also
- 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.
See also
- 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.
See also
Methods Documentation
- apply(outer, *otherargs, **otherkwargs) [edit on github]
- cache_clear() None [edit on github]
Clear the cache.
- cache_status() None [edit on github]
Display the cache status.
Information on the cache - the number of “hits”, “misses”, and “requests” - is displayed at the INFO logging level.
Examples
>>> pl.cache_status() powlaw1d.pl size: 5 hits: 633 misses: 240 check= 873
- calc(p, x0, x1=None, *args, **kwargs)[source] [edit on github]
Evaluate the model.
The load method must have been called first. If both x and y were given then the model is interpolated onto the x0 grid, otherwise x0 is only checked to see if it has the right size (fold should have been called if the data has been filtered).
- fold(data)[source] [edit on github]
Ensure the model matches the data filter.
This should be called after load, to ensure that any existing filter is applied correctly during a fit. It is only necessary for models where x is None in the load call.
- Parameters:
data (sherpa.data.Data instance) – An object with a mask attribute.
- 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.
- get_x()[source] [edit on github]
Return the independent axis or None.
- get_y()[source] [edit on github]
Return the dependent axis or None.
- guess(dep, *args, **kwargs) [edit on github]
Set an initial guess for the parameter values.
Attempt to set the parameter values, and ranges, for the model to match the data values. This is intended as a rough guess, so it is expected that the model is only evaluated a small number of times, if at all.
- load(x, y)[source] [edit on github]
Set the model values.
- Parameters:
x (None or sequence) – The model values. It is expected that either both are given, and have the same number of elements, or that only y is set, although the model can be cleared by setting both to None. If x is given then the data is saved after being sorted into increasing order of x.
y (None or sequence) – The model values. It is expected that either both are given, and have the same number of elements, or that only y is set, although the model can be cleared by setting both to None. If x is given then the data is saved after being sorted into increasing order of x.
- 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 [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() None [edit on github]
Called after a model may be evaluated multiple times.
See also
- thaw() None [edit on github]
Thaw any frozen parameters of the model.
Those parameters that are marked as “always frozen” are skipped.