API/Reference

Bridge between Sherpa and Astropy modeling.

exception saba.UnsupportedPythonError[source]
class saba.SherpaFitter(optimizer='levmar', statistic='leastsq', estmethod='covariance')[source]

Sherpa Fitter for astropy models.

Parameters
optimizerstring

the name of a sherpa optimizer. posible options include simplex, levmar, moncar, neldermead

statisticstring

the name of a sherpa statistic. posible options include cash, wstat, cstat, chi2, chi2constvar, chi2datavar, chi2gehrels, chi2modvar, chi2xspecvar, leastsq

estmethodstring

the name of a sherpa estmethod. possible options include confidence, covariance, projection

__call__(self, models, x, y, z=None, xbinsize=None, ybinsize=None, err=None, bkg=None, bkg_scale=1, **kwargs)[source]

Fit the astropy model with a the sherpa fit routines.

Parameters
modelsastropy.modeling.FittableModel or list of astropy.modeling.FittableModel

model to fit to x, y, z

xarray or list of arrays

input coordinates (independent for 1D & 2D fits)

yarray or list of arrays

input coordinates (dependent for 1D fits or independent for 2D fits)

zarray or list of arrays (optional)

input coordinates (dependent for 2D fits)

xbinsizearray or list of arrays (optional)

an array of xbinsizes in x - this will be x -/+ (binsize / 2.0)

ybinsizearray or list of arrays (optional)

an array of xbinsizes in y - this will be y -/+ (ybinsize / 2.0)

errarray or list of arrays (optional)

an array of errors in dependent variable

bkgarray or list of arrays (optional)

this will act as background data

bkg_salefloat or list of floats (optional)

the scaling factor for the dataset if a single value is supplied it will be copied for each dataset

**kwargs :

keyword arguments will be passed on to sherpa fit routine

Returns
model_copyastropy.modeling.FittableModel or a list of models.

a copy of the input model with parameters set by the fitter

est_errors(self, sigma=None, maxiters=None, numcores=1, methoddict=None, parlist=None)[source]

Use sherpa error estimators based on the last fit.

Parameters
sigma: float

this will be set as the confidance interval for which the errors are found too.

maxiters: int

the maximum number of iterations the error estimator will run before giving up.

methoddict: dict

!not quite sure couldn’t figure this one out yet!

parlist: list

a list of parameters to find the confidance interval of if none are provided all free parameters will be estimated.

get_sampler(self, *args, **kwargs)[source]

This returns and instance of SherpaMCMC with it’s self as the fitter

An interface which makes use of sherpa’s MCMC(pyBLoCXS) functionality.

Parameters
fitter: a `SherpaFitter` instance.

used to caluate the fit statstics, must have been fit as the covariance matrix is used.

sampler: string

the name of a valid sherpa sampler.

walker: string

the name of a valid sherpa walker.

Inheritance diagram of SherpaFitter

class saba.SherpaMCMC(fitter, sampler='mh', walker='mh')[source]

An interface which makes use of sherpa’s MCMC(pyBLoCXS) functionality.

Parameters
fitter: a `SherpaFitter` instance.

used to caluate the fit statstics, must have been fit as the covariance matrix is used.

sampler: string

the name of a valid sherpa sampler.

walker: string

the name of a valid sherpa walker.

__call__(self, niter=200000)[source]

based on the sherpa.sim.get_draws

Parameters
niter: int

the number of samples you wish to draw.

Returns
stat_values: array(float)

the fit statistic of the draw

accepted: array(bool)

if the fit was accepted

parameters: dict

the parameter values for each draw

accepted

The stored list of bools if each draw was accepted or not.

set_prior(self, parameter, prior)[source]

Set the prior function to use with a parameter. The default prior used by the SherpaMCMC function call for each parameter is flat, varying between the minimum and maximum values of the parameter (as given by the min and max attributes of the parameter object).

Parameters
parsherpa.models.parameter.Parameter instance

A parameter of a model instance.

priorfunction or sherpa.models.model.Model instance

The function to use for a prior. It must accept a single argument and return a value of the same size as the input.

Examples

Create a function (lognorm) and use it as the prior the nH parameter

>> def lognorm(x):

sigma = 0.5 x0 = 20 dx = np.log10(x) - x0 norm = sigma / np.sqrt(2 * np.pi) return norm * np.exp(-0.5*dx*dx/(sigma*sigma))

>> mcmc.set_prior(‘nH’, lognorm)

set_sampler_options(self, opt, value)[source]

Set an option for the current MCMC sampler.

Parameters
optstr

The option to change. Use get_sampler to view the available options for the current sampler.

value :

The value for the option.

Notes

The options depend on the sampler. The options include:

defaultprior

Set to False when the default prior (flat, between the parameter’s soft limits) should not be used. Use set_prior to set the form of the prior for each parameter.

inv:

A bool, or array of bools, to indicate which parameter is on the inverse scale.

log:

A bool, or array of bools, to indicate which parameter is on the logarithm (natural log) scale.

original:

A bool, or array of bools, to indicate which parameter is on the original scale.

p_M:

The proportion of jumps generatd by the Metropolis jumping rule.

priorshape:

An array of bools indicating which parameters have a user-defined prior functions set with set_prior.

scale:

Multiply the output of covar by this factor and use the result as the scale of the t-distribution.

Examples

>> mcmc = SherpaMCMC(sfit) >> mcmc.set_sampler_opt(‘scale’, 3)

stat_values

The stored values for the fit statistic of each run.

Sherpa to Astropy Mapping

_images/saba_api_map.png