bxa.xspec package


bxa.xspec.gof module

Compute poisson-based GOF

bxa.xspec.gof.build(options, k)[source]
bxa.xspec.gof.gen_choices(cpart, k)[source]
bxa.xspec.gof.calc_multigof(data, model)[source]
bxa.xspec.gof.group_adapt(data, nmin=10)[source]
bxa.xspec.gof.write_multigof(filename, data, model, skip_plot_ifeq=None)[source]
bxa.xspec.gof.write_gof(prefix, gof_avg, gof_total)[source]

bxa.xspec.priors module

BXA (Bayesian X-ray Analysis) for Xspec

Copyright: Johannes Buchner (C) 2013-2020


bxa.xspec.priors.create_uniform_prior_for(model, par)[source]

Use for location variables (position) The uniform prior gives equal weight in non-logarithmic scale.

bxa.xspec.priors.create_jeffreys_prior_for(model, par)[source]

deprecated, use create_loguniform_prior_for instead.

bxa.xspec.priors.create_loguniform_prior_for(model, par)[source]

Use for scale variables (order of magnitude) The Jeffreys prior gives equal weight to each order of magnitude between the minimum and maximum value. Flat in logarithmic scale

bxa.xspec.priors.create_gaussian_prior_for(model, par, mean, std)[source]

Use for informed variables. The Gaussian prior weights by a Gaussian in the parameter.

bxa.xspec.priors.create_custom_prior_for(model, par, transform, aftertransform=<function <lambda>>)[source]

Pass your own prior weighting transformation


Creates a single prior transformation function from parameter transformations

bxa.xspec.qq module

Statistics and Plotting for quantile-quantile analysis for model discovery

bxa.xspec.qq.KSstat(data, model)[source]

Kolmogorov-Smirnov statistic: maximum deviance between data and model

bxa.xspec.qq.CvMstat(data, model)[source]

Cramér–von Mises statistic: Takes all deviances into account

bxa.xspec.qq.ADstat(data, model)[source]

Anderson-Darling statistic: Takes all deviances into account more weight on tails than CvM.

bxa.xspec.qq.qq_plot(bins, data, model, markers=[0.2, 1, 2, 5, 10], unit='', annotate=True)[source]

Create a quantile-quantile plot for model discovery (deviations in data from model).

  • bins: energies/channel

  • data: amount observed

  • model: amount predicted

  • markers: list of energies/channels (whichever the current plotting xaxis unit)

  • unit: unit to append to marker

  • annotate: add information to the plot


Xspec gets a bit confused if there are “.” in the filename So we replace . with _ in the filename. But if we replace it as part of the parent directory, we cannot write there, so this only alters the filename, not the entire path.

bxa.xspec.qq.qq(prefix, markers=5, annotate=True)[source]

Create a quantile-quantile plot for model discovery (deviations in data from model).

The current data and model is used, so call set_best_fit(analyzer, transformations) before, to get the qq plot at the best fit.

  • markers: list of energies/channels (whichever the current plotting xaxis unit) or number of equally spaced markers between minimum+maximum.

  • annotate: add information to the plot

bxa.xspec.sinning module

Binning routines for plotting

bxa.xspec.sinning.group_adapt(xdata, ydata, xlo, xhi, nmin=20)[source]

Adaptive grouping into nmin count bins

bxa.xspec.sinning.binning(outputfiles_basename, bins, widths, data, models)[source]

Bins the data for plotting. Using the gof module, computes a Poisson goodness-of-fit range, i.e. ranges where the model must lie. This is done for multiple binning sizes simultaneously.


  • marked_binned: data points binned to contain 10 counts a sequence ready to be passed to matplotlib.pyplot.errorbar

  • modelrange: range allowed by the data ready to be passed to matplotlib.pyplot.fill_between

  • and statistics (GoF measure)

outputfiles_basename is not used.

bxa.xspec.solver module

BXA (Bayesian X-ray Analysis) for Xspec

Copyright: Johannes Buchner (C) 2013-2020

class bxa.xspec.solver.XSilence[source]

Bases: object

Context for temporarily making xspec quiet.


Create a single prior transformation function from a list of transformations for each parameter. This assumes the priors factorize.

bxa.xspec.solver.store_chain(chainfilename, transformations, posterior, fit_statistic)[source]

Writes a MCMC chain file in the same format as the Xspec chain command.

bxa.xspec.solver.set_parameters(transformations, values)[source]

Set current parameters.

class bxa.xspec.solver.BXASolver(transformations, prior_function=None, outputfiles_basename='chains/', resume_from=None)[source]

Bases: object

Run the Bayesian analysis.

The nested sampling package UltraNest is used under the hood.

If prior is None, uniform priors are used on the passed parameters. If parameters is also None, all thawed parameters are used.

  • transformations – List of parameter transformation definitions

  • prior_function – set only if you want to specify a custom, non-separable prior

  • outputfiles_basename – prefix for output filenames.

  • resume_from – prefix for output filenames of a previous run with similar posterior from which to resume

More information on the concept of prior transformations is available at https://johannesbuchner.github.io/UltraNest/priors.html

allowed_stats = ['cstat', 'cash', 'pstat']

Sets model to the best fit values.


returns -0.5 of the fit statistic.

run(sampler_kwargs={'resume': 'overwrite'}, run_kwargs={'Lepsilon': 0.1}, speed='safe', resume=None, n_live_points=None, frac_remain=None, Lepsilon=0.1, evidence_tolerance=None)[source]

Run nested sampling with ultranest.


arguments passed to ReactiveNestedSampler (see ultranest documentation)


arguments passed to ReactiveNestedSampler.run() (see ultranest documentation)

The following arguments are also available directly for backward compatibility:

  • resume – sets sampler_kwargs[‘resume’]=’resume’ if True, otherwise ‘overwrite’

  • n_live_points – sets run_kwargs[‘min_num_live_points’]

  • evidence_tolerance – sets run_kwargs[‘dlogz’]

  • Lepsilon – sets run_kwargs[‘Lepsilon’]

  • frac_remain – sets run_kwargs[‘frac_remain’]

create_flux_chain(spectrum, erange='2.0 10.0', nsamples=None)[source]

For each posterior sample, computes the flux in the given energy range.

The so-created chain can be combined with redshift information to propagate the uncertainty. This is especially important if redshift is a variable parameter in the fit (with some prior).

Returns erg/cm^2 energy flux (first column) and photon flux (second column) for each posterior sample.

posterior_predictions_convolved(component_names=None, plot_args=None, nsamples=400)[source]

Plot convolved model posterior predictions.

Also returns data points for plotting.

  • component_names – labels to use. Set to ‘ignore’ to skip plotting a component

  • plot_args – matplotlib.pyplot.plot arguments for each component

  • nsamples – number of posterior samples to use (lower is faster)

posterior_predictions_unconvolved(component_names=None, plot_args=None, nsamples=400, plottype='model')[source]

Plot unconvolved model posterior predictions.

  • component_names – labels to use. Set to ‘ignore’ to skip plotting a component

  • plot_args – matplotlib.pyplot.plot arguments for each component

  • nsamples – number of posterior samples to use (lower is faster)

posterior_predictions_plot(plottype, callback, nsamples=None)[source]

Internal Routine used by posterior_predictions_unconvolved, posterior_predictions_convolved

bxa.xspec.solver.standard_analysis(transformations, outputfiles_basename, skipsteps=[], **kwargs)[source]

Run a default analysis which produces nice plots.

Deprecated; copy the code of this function into your script and adjust to your needs.

  • runs nested sampling analysis, creates MCMC chain file

  • marginal probabilities (1d and 2d)

  • model posterior predictions + data overplotted, convolved

  • model posterior predictions unconvolved

  • quantile-quantile plot with statistics

  • prints out summary of parameters

  • prints out model evidence

Look at the source of this function to figure out how to do the individual parts. Copy them to your scripts and adapt them to your needs.

Module contents

BXA (Bayesian X-ray Analysis) for Xspec

Copyright: Johannes Buchner (C) 2013-2020

bxa.xspec.nested_run(transformations, prior_function=None, n_live_points=400, evidence_tolerance=0.5, outputfiles_basename='chains/', verbose=True, **kwargs)[source]

deprecated. Use BXASolver instead.