About Chandra Archive Proposer Instruments & Calibration Newsletters Data Analysis HelpDesk Calibration Database NASA Archives & Centers Chandra Science Links

Skip the navigation links
Last modified: 09 Sep 2009
Hardcopy (PDF): A4 | Letter

Sherpa User Models

Sherpa Threads (CIAO 4.1)

[S-Lang Syntax]



Overview

Last Update: 09 Sep 2009 - "Table Models" section moved to a separate 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 Python, S-Lang, C, or Fortran function and assign it to a Sherpa 4.1 'user model'. User models are those for which the user specifies the parameters and model calculation function. 'Table models' can also be defined in Sherpa 4.1; these 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. In this thread, examples of user models are shown; for table models, see the thread "Sherpa Table Models."

Proceed to the HTML or hardcopy (PDF: A4 | letter) version of the thread.





User Models

Assigning an S-Lang Function to a User Model

Here, we consider the most simple example: a user model that contains the equation for a line, y = mx + b. To implement such a model in S-Lang, we would write the function as follows:

define myline(pars, x)
{
  return pars[0] * x + pars[1];
}

This function would then be stored in a S-Lang file, which we could call "myline.sl".

Next, we must import the new user function into a S-Lang fitting session. A script to load a user-written function into S-Lang, and then assign it to a user model, could be written as follows:

A plot of the user model fit to the data contained in 'foo.dat' might appear as shown Figure 1.

This script can be called from a S-Lang program, such as slsh, with fit results identical to those which would arise from the same user model established with Python (see the Python version of this thread).

The one difference to note is that in S-Lang, we must explicitly pass a reference to the function "myline", as the second argument to the load_user_model function. Therefore, S-Lang syntax requires that the second argument should be written as "&myline", not simply as "myline". (See the Python version of this thread for a detailed discussion of the functions load_user_model and add_user_pars).

The S-Lang user may also import a user-written S-Lang function into Python, assign it to an instance of the user model, and run the fitting session in Python. In the following example, PySL is used to take a user function written in S-Lang and call it from a previously defined Python user model class (as shown in the Python version of this thread):
from sherpa.astro.ui import *
from pysl import sl
import pysl.slsh
sl.require('myline')

load_data(1, "foo.dat")
plot_data()

load_user_model(sl.myline, "myl")
add_user_pars("myl", ["m","b"])

set_model(myl)

myl.m=30
myl.b=20

fit()
plot_fit()

The only difference between this example and defining a user model in Python is that PySL is used to import the user function into S-Lang, and PySL gives a reference so that the S-Lang function can be called from Python. But once load_user_model has been used to create an instance of the user model, the user model can be manipulated as any other pre-packaged Sherpa model.



History

12 Jan 2009 original version
09 Sep 2009 "Table Models" section moved to a separate thread.

Return to Threads Page: Top | All | Fitting
Hardcopy (PDF): A4 | Letter
Last modified: 09 Sep 2009


The Chandra X-Ray Center (CXC) is operated for NASA by the Smithsonian Astrophysical Observatory.
60 Garden Street, Cambridge, MA 02138 USA.    Email: cxcweb@head.cfa.harvard.edu
Smithsonian Institution, Copyright © 1998-2004. All rights reserved.