The sherpa.models.template module

Create models that interpolate between table models.

A sherpa.models.basic.TableModel allows an array of values to be used as a model, where only the normalization is varied. The TemplateModel class lets users take a number of table models, associate them with one or more parameters, and the InterpolatingTemplateModel class allows for interpolation between these models, and hence parameter values. Users are expected to use the create_template_model routine to create the template model.

The aim is to allow one or more parameters to have values associated with numeric data, and then to vary the model based on the parameter values. For example, consider the case of two parameters - Temperature and Height - where there’s data for a set of these parameters:

Temperature

Height

Data (x,y pairs)

1

10

(1,10), (2,30), …

2

10

(1,20), (2,40), …

2

15

(1,12), (3,46), …

10

50

These data values can be converted to sherpa.models.basic.TableModel instances and then associated with the parameters with create_template_model. The model can then be evaluated to allow the Temperature and Height values to be fit to observed data. For users of XSPEC, this provides functionality similar to XSPEC table models.

Examples

The templates used here represent a single parameter, called alpha, where the values are for the independent axis values of [50, 100, 150, 250]:

alpha

values

5

1, 2, 3, 4

17

3, 7, 4, 8

25

10, 15, 0, 12

>>> import numpy as np
>>> from sherpa.models.template import create_template_model
>>> from sherpa.models.basic import TableModel
>>> parnames = ["alpha"]
>>> parvals = np.asarray([[5], [17], [25]])
>>> m1 = TableModel("m1")
>>> m2 = TableModel("m2")
>>> m3 = TableModel("m3")
>>> x = [50, 100, 150, 250]
>>> m1.load(x, [1, 2, 3, 4])
>>> m2.load(x, [3, 7, 4, 8])
>>> m3.load(x, [10, 15, 0, 12])
>>> tmps = [m1, m2, m3]
>>> mdl = create_template_model("model", parnames, parvals, tmps)

The model defaults to the first parameter value:

>>> print(mdl)
model
   Param        Type          Value          Min          Max      Units
   -----        ----          -----          ---          ---      -----
   model.alpha  thawed            5            5           25
>>> mdl(x)
array([1., 2., 3., 4.])

If the parameter is changed then the templates are weighted to create the output:

>>> mdl.alpha = 16
>>> mdl(x)
array([3.7, 7.8, 3.6, 8.4])

The model can also be interpolated onto a different X axis:

>>> mdl([70, 120, 200])
array([5.34, 6.12, 6.  ])

Classes

TemplateModel([name, pars, parvals, templates])

Combine TableModel instances.

InterpolatingTemplateModel(name, template_model)

Allow parameter interpolation for a TemplateModel.

KNNInterpolator(name, template_model[, k, order])

Use K-nearest neighbors interpolation for parameters.

Template(name, template_model[, k, order])

The Template class.

Functions

add_interpolator(name, interpolator, **kwargs)

Add the interpolator to the list.

create_template_model(modelname, names, ...)

Create a TemplateModel model class.

reset_interpolators()

Reset the list of interpolators to the default.

Class Inheritance Diagram

Inheritance diagram of TemplateModel, InterpolatingTemplateModel, KNNInterpolator, Template