Optimization & Simulation functions#

Fitting spectra#

class pyspex.spex.fit.Fit#

This is the parent class of all fit related commands.

Variables:
  • stat (basestring) – Fit statistics.

  • cstat (float) – C-statistics value.

  • chisq (float) – Chi-squared value.

  • wstat (float) – W-statistics value (not recommended).

  • nfree (int) – Degrees of freedom.

  • cstatexp (float) – Expected C-statistics value (C-stat only).

  • cstatrms (float) – RMS uncertainty on expected C-stat value.

  • ann_rt (float) – Simulated annealing: rt

  • ann_eps (float) – Simulated annealing: eps

  • ann_t (float) – Simulated annealing: t

  • ann_vm (float) – Simulated annealing: vm

  • ann_ns (int) – Simulated annealing: ns

  • ann_max (int) – Simulated annealing: max evaluations

  • ann_print (int) – Simulated annealing: print flag

fit(niter=100)#

Execute the SPEX fit command. The maximum number of iterations (niter) can be optionally set.

Parameters:

niter (int) – Number of fit iterations.

fit_print(status)#

Print each fit iteration to the console (default is True). The status variable can be either True or False, which means printing is on or off, respectively.

Parameters:

status (bool) – Set fit to high verbosity (True is yes, False is no).

get_method()#

Get the current type of statistics being used in the fit, for example chi2, cstat or wstat.

get_statistic()#

Get the current type of statistics being used in the fit, for example chi2, cstat or wstat.

set_ann(param, value)#

Set the simulating annealing method parameters.

Parameters:
  • param (str) – Type of annealing parameter (rt, t, eps, vm, ns, max, or print).

  • value (float) – Value of the parameter (will be converted to the nearest int if necessary).

set_method(meth)#

Set the desired fit statistics.

Parameters:

stat (str) – Abbreviation for the fit statistics to be used. For example: ‘csta’, ‘chi2’, ‘wsta’.

set_statistic(stat)#

Set the desired fit statistics.

Parameters:

stat (str) – Abbreviation for the fit statistics to be used. For example: ‘csta’, ‘chi2’, ‘wsta’.

set_statistic_inst(stat, inst, reg)#

Set the desired fit statistics per instrument and region.

Parameters:
  • stat (str) – Abbreviation for the fit statistics to be used. For example: ‘csta’, ‘chi2’, ‘wsta’.

  • inst (int) – Instrument number.

  • reg (int) – Region number.

show()#

Print the fit statistics to the terminal.

update()#

Get the most recent statistics values from SPEX.

Error calculation#

class pyspex.spex.fit.Error#

Class to calculate errors for free fit parameters.

Variables:
  • sector (int) – Sector number of parameter

  • component (int) – Component number of parameter

  • parameter (str) – Parameter name

  • value (float) – Parameter value

  • lerr (float) – Lower error boundary

  • uerr (float) – Upper error boundary

  • lc (bool) – Is there a lower C-stat or chi**2 value found?

  • cmin (float) – Lowest C-stat or chi**2 value

  • pmin (float) – Parameter value for which a better C-stat or Chi**2 was found

  • dchi (float) – Delta C-stat or chi**2 to optimize for

  • calculated (bool) – Is the error calculated?

error(isect, icomp, name, dchi=None)#

Calculate the error value for a particular parameter.

Parameters:
  • isect (int) – Sector number of the parameter.

  • icomp (int) – Component number of the parameter.

  • name (str) – Parameter name.

  • dchi (float) – (Optional) \(\Delta\chi^2\) value to optimize for (Default: 1.0, 68% errors)

get_value()#

Convenience function to return the parameter value and the errors.

Returns:

A tuple with the parameter value, lower error and upper error.

Return type:

tuple

set_dchi(dchi)#

Set the delta c-stat or delta chi**2 value that the error search should optimize for. The default value is 1.0.

Parameters:

dchi (float) – \(\Delta\chi^2\) value to optimize for (Default: 1.0, 68% errors)

start(value)#

Set the starting value for the error calculation.

Parameters:

value (float) – Starting value for the error calculation.

Simulating spectra#

class pyspex.spex.fit.Simulate#

Class to simulate spectra.

set_bnoise(status)#

Add Poisson noise to the background spectrum (status is True or False).

Parameters:

status (bool) – Add Poisson noise to the simulated background spectrum.

set_instrument(i1, i2, c1, c2)#

Define the range of instruments to simulate.

Parameters:

irange – Instrument range to simulate (default all)

set_noise(status)#

Add Poisson noise to the source spectrum (status is True or False).

Parameters:

status (bool) – Add Poisson noise to the simulated source spectrum.

set_random_seed(rand, seed)#

Set the random seed to an integer value.

Parameters:
  • rand (bool) – Should the seed be randomly set?

  • seed (int) – Set the random seed for the simulation.

set_syserr(src, bkg)#

Add a systematic error to the source spectrum (src) and to the background spectrum (bkg).

Parameters:
  • src (float) – Add a systematic error to the source spectrum.

  • bkg (float) – Add a systematic error to the background spectrum.

simulate(extime, ssys=None, bsys=None, noise=None, bnoise=None, seed=None)#

Simulate a spectrum for exposure time extime and optionally with a number of options.

Parameters:
  • extime (float) – Exposure time to simulate.

  • ssys (float) – (Optional) Add a systematic error to the source spectrum (Default 0).

  • bsys (float) – (Optional) Add a systematic error to the background spectrum (Default 0).

  • noise (bool) – (Optional) Add Poisson noise to the simulated source spectrum (Default True).

  • bnoise (bool) – (Optional) Add Poisson noise to the simulated background spectrum (Default False).

  • seed (int) – (Optional) Set the random seed for the simulation (Default: system clock).

simulate_exposure(extime)#

Simulate the spectrum for the provided exposure time.

Parameters:

extime (float) – Exposure time to simulate.