Sherpa Table Models
![[CXC Logo]](/ciao/imgs/cxc-logo.gif)
Sherpa Threads (CIAO 4.1)
[Python Syntax]
OverviewLast Update: 09 Sep 2009 - originally "Table Models" section of "Sherpa User Models" thread Synopsis: It may be necessary to fit data to a function that is not pre-packaged as a Sherpa model; to do so, we may write that function as a Sherpa 4.1 'table model'. Table models are those that contain data read from a file, and have only one parameter, normalization. When the table model is "calculated", the model returns the data - or more generally, some calculated values based on those data values. 'User models' may also be defined in Sherpa 4.1; these are models for which the user specifies the parameters and model calculation function. In this thread, examples of table models are shown; for user models, see the thread "Sherpa User Models." |
Table Models
In addition to the user model class, a table model class has been added to Sherpa; it stores the y-values from an input data file or Crate, and returns them as an array when the table model is asked to "calculate" y-values. The model has one parameter, "ampl", the amplitude; the y-values are multiplied by the amplitude.
The Sherpa astronomy UI package, "sherpa.astro.ui", contains a simple function, load_table_model, to read data from a file or Crate and assign it to an instance of the table model class; it uses functions already defined by the astronomy UI to read from file. First, it attempts to read the file as an image; if that does not work, it tries to read it as a so-called TABLE Crate (if the table model is to be defined by a set of arrays in Sherpa - as opposed to being read from a file - the arrays must first be read into a Crate and then assigned as the table model with load_table_model.) To create a Crate, the crates_contrib.utils contributed package must be loaded; refer to the make_table_crate ahelp file for details and examples. If used with CRATES, load_table_model can pass along Data Model syntax just as done by the load_image and load_table functions.
In this simple example, we load data from an image, and the resulting Sherpa data set is assigned to a table model:
sherpa> from sherpa.astro.ui import * sherpa> load_image(1, "image.fits") sherpa> load_table_model("mytable", "image.fits") sherpa> print(mytable) mytable Param Type Value Min Max Units ----- ---- ----- --- --- ----- mytable.ampl thawed 1 -1e+120 1e+120 sherpa> print(mytable._file) image.fits sherpa> print(mytable._y) [ 0., 1., 0., ..., 0., 0., 0.,] sherpa> set_model(mytable) sherpa> image_fit()
Here, an instance of the table model is created, and named "mytable". In this simplest example, it is assigned exactly the same data that is read in as the data to be fit. The print function shows that "mytable" has exactly one model parameter, "ampl", which by default is 1.
The model "mytable" also has two attributes, which are not model parameters. The attribute "file" is the name of the file from which the table data are read, and the attribute "y" simply stores the y-values read from that data file. As these are not parameters to be varied during a fit, they are not printed by the call "print(mytable)", which is meant to show a model's parameters.
The y-values which are read from the data file (and stored by the table model class) may be returned as an array when the table model is asked to "calculate" y-values. (Again, since this is not a parameter to be varied during a fit, the array of y-values is not printed by the call print(mytable)).
Then the table model is assigned as a model. The call to image_fit shows that data and model have identical values, and that the residuals are all zero, as expected.
History
| 09 Sep 2009 | originally "Table Models" section of "Sherpa User Models" thread |
![[ds9 image of table model fit to the data contained in 'image.dat']](2_small.png)
