bayspec.model package

Subpackages

Submodules

bayspec.model.model module

Spectral model base classes and the composition algebra.

Model defines the functional contract (func(E, T, O)), parameter and config dictionaries, convolution with detector responses, and posterior-aware evaluators. Additive/Multiplicative/ Mathematic are thin base classes that fix the type tag, and CompositeModel implements the arithmetic (+, -, *, /) plus the convolution call operator (conv(add) etc.) used to build multi-component expressions.

class bayspec.model.model.Additive[source]

Bases: Model

Base for additive photon-flux components; type is locked to 'add'.

property type

Model type tag, always 'add'.

class bayspec.model.model.CompositeModel(m1, op, m2)[source]

Bases: Model

Binary combination of two models under +/-/*///().

The composite’s type is inferred from type_operation via tdict; invalid combinations raise ValueError. Duplicate component names are made unique with a numeric suffix.

property comment

Concatenated per-component comments, one line per component.

property expr

Parenthesized expression assembled from the two operands.

func(E, T=None, O=None)[source]

Evaluate the composite by dispatching on op.

For '()', m1 is called with O=m2 so convolution-style operators receive the nested model.

Raises:

ValueError – If op is not recognized.

property mdicts

Merged component mapping from both operands.

property tdict

Lookup table mapping '<t1><op><t2>' strings to the composite type.

A value of False marks an illegal combination.

property type

Derived composite type looked up in tdict.

Raises:
  • ValueError – If the operand-type pair is not an allowed combination.

  • AssertionError – If either operand has an unknown type tag.

type_operation()[source]

Return the resulting type for the current (m1, op, m2) triple.

Raises:
  • ValueError – If the operand-type pair is not a legal combination.

  • AssertionError – If either operand has an unknown type tag.

class bayspec.model.model.FrozenConst(value)[source]

Bases: Mathematic

Frozen scalar used to wrap numeric literals in composite expressions.

func(E=None, T=None, O=None)[source]

Return the stored constant regardless of E, T, O.

class bayspec.model.model.Mathematic[source]

Bases: Model

Base for dimensionless mathematical components; type is 'math'.

property type

Model type tag, always 'math'.

class bayspec.model.model.Model[source]

Bases: object

Base class for a spectral model component.

Subclasses override func() with the model’s analytical form and populate config (Cfg entries – frozen values like redshift) and params (Par entries – fittable quantities). The type tag – 'add', 'mul', 'conv' or 'math' – drives which derived spectra (phtspec, flxspec, ergspec, nouspec) are available and how composition is type-checked.

expr

Short expression used in tables and composite names.

type

One of 'add', 'mul', 'conv', 'math'.

comment

Free-form description.

config

OrderedDict of configuration Cfg entries.

params

OrderedDict of fit Par entries.

property all_config

List of per-config rows with component, label, and value.

property all_params

List of per-parameter rows with value, prior, posterior, and frozen flag.

at_par(theta)[source]

Write every free parameter value from the 1-indexed sequence theta.

best_ci_ergflux(emin, emax, ngrid, time=None)[source]
best_ci_ergflux_ratio(erange1, erange2, ngrid, time=None)[source]
best_ci_ergspec(E, T=None)[source]
best_ci_flxspec(E, T=None)[source]
best_ci_nouspec(E, T=None)[source]
best_ci_phtflux(emin, emax, ngrid, time=None)[source]
best_ci_phtflux_ratio(erange1, erange2, ngrid, time=None)[source]
best_ci_phtspec(E, T=None)[source]
best_ergflux(emin, emax, ngrid, time=None)[source]
best_ergflux_ratio(erange1, erange2, ngrid, time=None)[source]
best_ergspec(E, T=None)[source]
best_flxspec(E, T=None)[source]
best_nouspec(E, T=None)[source]
best_phtflux(emin, emax, ngrid, time=None)[source]
best_phtflux_ratio(erange1, erange2, ngrid, time=None)[source]
best_phtspec(E, T=None)[source]
property cdicts

Mapping from expr to each component’s config dict.

property cfg

Flat SuperDict of every config parameter across components.

property cfg_info

Tabular Info view of every configuration parameter.

property conv_ctsrate

Convolved count rate per unit against the bound fit_to data.

property conv_ctsrate_f64

Same as conv_ctsrate but cast to float64 for statistics.

property conv_ctsspec

conv_ctsrate divided by bin width.

Type:

Convolved count density

property conv_rate_to_flux

Like rate_to_flux but using the convolved-model count rate.

conv_rate_to_fluxdensity(E=1, T=None, unit='Fv')[source]

Like rate_to_fluxdensity() but using convolved count rates.

property conv_re_ctsrate

Convolved count rate using the re-binned detector response.

property conv_re_ctsspec

Convolved count density on the re-binned response.

convolve_data(data)[source]

Convolve the model with every unit of a Data container.

Parameters:

data – A Data.

Returns:

A (ctsrate_list, ctsspec_list) tuple, one entry per unit.

convolve_dataunit(dataunit)[source]

Convolve the model with a single DataUnit.

Parameters:

dataunit – A DataUnit.

Returns:

A (ctsrate, ctsspec) tuple.

convolve_response(response, time=None)[source]

Convolve the model with a single Response and return count-rate/spec.

Parameters:
  • response – A Response.

  • time – Optional time broadcast across photon bins.

Returns:

A (ctsrate, ctsspec) tuple.

property cts_to_erg
property cts_to_flx
property cts_to_pht

Per-bin conversion factor from convolved counts to photon density.

re_cts_to_pht, cts_to_flx, re_cts_to_flx, cts_to_erg and re_cts_to_erg follow the same pattern for the photon/flux/ energy spectra on the full or re-binned grids.

ergflux(emin, emax, ngrid, time=None)[source]

Integrated energy flux over [emin, emax] in erg/cm²/s.

Raises:

TypeError – If the model type is not 'add'.

ergflux_ratio_sample(erange1, erange2, ngrid, time=None)[source]
ergflux_sample(emin, emax, ngrid, time=None)[source]
ergspec(E, T=None)[source]

Energy spectrum \(\nu F_\nu = E^2 \, N(E)\) in erg/cm²/s.

Raises:

TypeError – If the model type is not 'add'.

property ergspec_at_rsp
ergspec_sample(E, T=None)[source]
property fdicts

Mapping from expr to each component’s func callable.

property fit_to

Return the Data this model is bound to, or raise if unset.

Raises:

AttributeError – If no data has been assigned.

flxspec(E, T=None)[source]

Energy flux density \(F_\nu = E \, N(E)\) in erg/cm²/s/keV.

Raises:

TypeError – If the model type is not 'add'.

property flxspec_at_rsp
flxspec_sample(E, T=None)[source]
func(E, T=None, O=None)[source]

Evaluate the model at energies E and optional time T.

Additive models return a photon flux density in photons/cm²/s/keV; multiplicative and mathematical models return a dimensionless factor. O is a nested model argument used by convolution operators.

Parameters:
  • E – Scalar or array of energies in keV.

  • T – Optional time or time array.

  • O – Optional nested model passed down by a convolution.

Returns:

The model value at the given sampling; subclass-specific.

integ(egrid, tgrid, ngrid)[source]

Trapezoidal-integrate the model over each ngrid-point bin.

Parameters:
  • egrid – Flattened energy grid, shaped (nbin * ngrid,).

  • tgrid – Matching flattened time grid.

  • ngrid – Sub-grid size per bin used to reshape egrid.

Returns:

Integrated photon flux per bin.

Raises:

TypeError – If the model type is not 'add'.

property mdicts

Mapping from expr to component model; overridden by composites.

mean_ergflux(emin, emax, ngrid, time=None)[source]
mean_ergflux_ratio(erange1, erange2, ngrid, time=None)[source]
mean_ergspec(E, T=None)[source]
mean_flxspec(E, T=None)[source]
mean_nouspec(E, T=None)[source]
mean_phtflux(emin, emax, ngrid, time=None)[source]
mean_phtflux_ratio(erange1, erange2, ngrid, time=None)[source]
mean_phtspec(E, T=None)[source]

Photon spectrum evaluated at the posterior mean parameter vector.

The family {mean,median,best,best_ci,truth}_{pht,nou,flx,erg}spec, _{pht,erg}flux, and _{pht,erg}flux_ratio all follow the same pattern: set par from the named posterior summary, then evaluate the spectrum, integrated flux, or flux ratio. truth_* variants raise ValueError when any parameter lacks a truth value.

median_ergflux(emin, emax, ngrid, time=None)[source]
median_ergflux_ratio(erange1, erange2, ngrid, time=None)[source]
median_ergspec(E, T=None)[source]
median_flxspec(E, T=None)[source]
median_nouspec(E, T=None)[source]
median_phtflux(emin, emax, ngrid, time=None)[source]
median_phtflux_ratio(erange1, erange2, ngrid, time=None)[source]
median_phtspec(E, T=None)[source]
nouspec(E, T=None)[source]

Dimensionless factor applied by multiplicative/mathematical models.

Raises:

TypeError – If the model type is not 'mul' or 'math'.

nouspec_sample(E, T=None)[source]
property par

Flat SuperDict of every fit parameter across components.

property par_best

Per-parameter posterior best-fit sample (or frozen value).

property par_best_ci

Per-parameter best-fit confidence bounds (or frozen value).

property par_info

Tabular Info view of parameters with frozen ones tagged.

property par_mean

Per-parameter posterior mean (or frozen value when applicable).

property par_median

Per-parameter posterior median (or frozen value).

property par_sample

Summary statistics of the posterior parameter matrix.

property par_truth

Per-parameter truth value (or frozen value); may contain None.

property pdicts

Mapping from expr to each component’s params dict.

phtflux(emin, emax, ngrid, time=None)[source]

Integrated photon flux over [emin, emax] in photons/cm²/s.

Parameters:
  • emin – Lower energy bound in keV.

  • emax – Upper energy bound in keV.

  • ngrid – Number of log-spaced grid points.

  • time – Optional time broadcast across the grid.

Raises:

TypeError – If the model type is not 'add'.

phtflux_ratio_sample(erange1, erange2, ngrid, time=None)[source]
phtflux_sample(emin, emax, ngrid, time=None)[source]
phtspec(E, T=None)[source]

Photon flux density \(N(E)\) in photons/cm²/s/keV.

Raises:

TypeError – If the model type is not 'add'.

property phtspec_at_rsp

Photon spectrum sampled at the response channel midpoints.

Sibling properties re_phtspec_at_rsp, flxspec_at_rsp, re_flxspec_at_rsp, ergspec_at_rsp, re_ergspec_at_rsp follow the same pattern, using either the full or re-binned channel grid.

phtspec_sample(E, T=None)[source]

Return posterior summaries of the photon spectrum at (E, T).

Sibling methods nouspec_sample, flxspec_sample, ergspec_sample, phtflux_sample, ergflux_sample, phtflux_ratio_sample, and ergflux_ratio_sample follow the same pattern on their respective quantities.

property posterior_nsample

Number of posterior draws; equals 1 when every parameter is frozen.

property posterior_sample

(nsample, npar) matrix of posterior draws.

Frozen parameters are filled with their fixed value so the matrix is rectangular.

property pvalues

Tuple of current parameter values, preserving par order.

property rate_to_flux

Per-unit ratio of integrated energy flux to observed net count rate.

rate_to_fluxdensity(E=1, T=None, unit='Fv')[source]

Per-unit conversion from net count rate to flux density at (E, T).

Parameters:
  • E – Energy at which the flux density is evaluated.

  • T – Optional time.

  • unit'NE' (photon flux density), 'Fv' (energy flux density), or 'Jy'.

Returns:

A list of per-unit conversion factors.

Raises:

ValueError – If unit is not recognized.

property re_cts_to_erg
property re_cts_to_flx
property re_cts_to_pht
property re_ergspec_at_rsp
property re_flxspec_at_rsp
property re_phtspec_at_rsp
sample_statistic(sample)[source]

Summarize a draw matrix with mean, median, and 1/2/3-sigma intervals.

Parameters:

sample(nsample, ...) array of draws.

Returns:

Dict with keys mean, median, Isigma, IIsigma, IIIsigma.

save(savepath)[source]

Dump the config and parameter tables under savepath.

Parameters:

savepath – Directory path. Created if missing.

truth_ergflux(emin, emax, ngrid, time=None)[source]
truth_ergflux_ratio(erange1, erange2, ngrid, time=None)[source]
truth_ergspec(E, T=None)[source]
truth_flxspec(E, T=None)[source]
truth_nouspec(E, T=None)[source]
truth_phtflux(emin, emax, ngrid, time=None)[source]
truth_phtflux_ratio(erange1, erange2, ngrid, time=None)[source]
truth_phtspec(E, T=None)[source]
class bayspec.model.model.Multiplicative[source]

Bases: Model

Base for dimensionless multiplicative components; type is 'mul'.

property type

Model type tag, always 'mul'.

Module contents

Model subpackages: local components, astromodels bridges, and xspec wrappers.

Top-level re-exports are kept disabled so that importing bayspec.model does not pull in xspec/astromodels at import time. Import the relevant submodule explicitly when needed.