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.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.

Variables:
  • expr – Short expression used in tables and composite names.

  • type – One of 'add', 'mul', 'conv', 'math'.

  • comment – Free-form description.

  • configOrderedDict of configuration Cfg entries.

  • paramsOrderedDict of fit Par entries.

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.

property mdicts

Mapping from expr to component model; overridden by composites.

property fdicts

Mapping from expr to each component’s func callable.

property cdicts

Mapping from expr to each component’s config dict.

property pdicts

Mapping from expr to each component’s params dict.

property cfg

Flat SuperDict of every config parameter across components.

property par

Flat SuperDict of every fit parameter across components.

property pvalues

Tuple of current parameter values, preserving par order.

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.

property cfg_info

Tabular Info view of every configuration parameter.

property par_info

Tabular Info view of parameters with frozen ones tagged.

save(savepath)[source]

Dump the config and parameter tables under savepath.

Parameters:

savepath – Directory path. Created if missing.

property fit_to

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

Raises:

AttributeError – If no data has been assigned.

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'.

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.

convolve_dataunit(dataunit)[source]

Convolve the model with a single DataUnit.

Parameters:

dataunit – A DataUnit.

Returns:

A (ctsrate, ctsspec) tuple.

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.

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_re_ctsrate

Convolved count rate using the re-binned detector response.

property conv_ctsspec

conv_ctsrate divided by bin width.

Type:

Convolved count density

property conv_re_ctsspec

Convolved count density on the re-binned response.

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.

property re_phtspec_at_rsp
property flxspec_at_rsp
property re_flxspec_at_rsp
property ergspec_at_rsp
property re_ergspec_at_rsp
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.

property re_cts_to_pht
property cts_to_flx
property re_cts_to_flx
property cts_to_erg
property re_cts_to_erg
property rate_to_flux

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

property conv_rate_to_flux

Like rate_to_flux but using the convolved-model 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.

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

Like rate_to_fluxdensity() but using convolved count rates.

phtspec(E, T=None)[source]

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

Raises:

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

nouspec(E, T=None)[source]

Dimensionless factor applied by multiplicative/mathematical models.

Raises:

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

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'.

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'.

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'.

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'.

at_par(theta)[source]

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

property par_mean

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

property par_median

Per-parameter posterior median (or frozen value).

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_truth

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

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_phtspec(E, T=None)[source]
best_phtspec(E, T=None)[source]
best_ci_phtspec(E, T=None)[source]
truth_phtspec(E, T=None)[source]
mean_nouspec(E, T=None)[source]
median_nouspec(E, T=None)[source]
best_nouspec(E, T=None)[source]
best_ci_nouspec(E, T=None)[source]
truth_nouspec(E, T=None)[source]
mean_flxspec(E, T=None)[source]
median_flxspec(E, T=None)[source]
best_flxspec(E, T=None)[source]
best_ci_flxspec(E, T=None)[source]
truth_flxspec(E, T=None)[source]
mean_ergspec(E, T=None)[source]
median_ergspec(E, T=None)[source]
best_ergspec(E, T=None)[source]
best_ci_ergspec(E, T=None)[source]
truth_ergspec(E, T=None)[source]
mean_phtflux(emin, emax, ngrid, time=None)[source]
median_phtflux(emin, emax, ngrid, time=None)[source]
best_phtflux(emin, emax, ngrid, time=None)[source]
best_ci_phtflux(emin, emax, ngrid, time=None)[source]
truth_phtflux(emin, emax, ngrid, time=None)[source]
mean_ergflux(emin, emax, ngrid, time=None)[source]
median_ergflux(emin, emax, ngrid, time=None)[source]
best_ergflux(emin, emax, ngrid, time=None)[source]
best_ci_ergflux(emin, emax, ngrid, time=None)[source]
truth_ergflux(emin, emax, ngrid, time=None)[source]
mean_phtflux_ratio(erange1, erange2, ngrid, time=None)[source]
median_phtflux_ratio(erange1, erange2, ngrid, time=None)[source]
best_phtflux_ratio(erange1, erange2, ngrid, time=None)[source]
best_ci_phtflux_ratio(erange1, erange2, ngrid, time=None)[source]
truth_phtflux_ratio(erange1, erange2, ngrid, time=None)[source]
mean_ergflux_ratio(erange1, erange2, ngrid, time=None)[source]
median_ergflux_ratio(erange1, erange2, ngrid, time=None)[source]
best_ergflux_ratio(erange1, erange2, ngrid, time=None)[source]
best_ci_ergflux_ratio(erange1, erange2, ngrid, time=None)[source]
truth_ergflux_ratio(erange1, erange2, ngrid, time=None)[source]
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.

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.

property par_sample

Summary statistics of the posterior parameter matrix.

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.

nouspec_sample(E, T=None)[source]
flxspec_sample(E, T=None)[source]
ergspec_sample(E, T=None)[source]
phtflux_sample(emin, emax, ngrid, time=None)[source]
ergflux_sample(emin, emax, ngrid, time=None)[source]
phtflux_ratio_sample(erange1, erange2, ngrid, time=None)[source]
ergflux_ratio_sample(erange1, erange2, ngrid, time=None)[source]
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.Multiplicative[source]

Bases: Model

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

property type

Model type tag, always 'mul'.

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.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.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 expr

Parenthesized expression assembled from the two operands.

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.

property comment

Concatenated per-component comments, one line per component.

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.

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.

property tdict

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

A value of False marks an illegal combination.

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.