Skip to content

Table models

XSPEC-style additive and multiplicative OGIP table models.

ATable, MTable, and ETable load the OGIP 92-009 FITS format and interpolate it over model parameters and requested energy bins. They remain differentiable with respect to model parameters and bin edges. Import them explicitly; they require a table path and therefore are not included in jaxspec.model.list.

ATable

Bases: _TableComponent, AdditiveComponent

Additive OGIP table model — the equivalent of XSPEC's atable{file}.

The tabulated spectra are per-bin integrated photon fluxes \(\left[\frac{\text{photons}}{\text{cm}^2\text{s}}\right]\); they are interpolated multilinearly over the parameter grid and redistributed onto the requested bins proportionally to overlap (integral preserving):

\[ \mathcal{M}(E_\min, E_\max) = \frac{\text{norm}}{1+z} \int_{E_\min (1+z)/E_{\text{scale}}}^{E_\max (1+z)/E_{\text{scale}}} \text{d}E ~ S_{\vec{p}}(E) \]

Constructor arguments

  • path: OGIP 92-009 table-model FITS file.
  • energy_band: optional (e_min, e_max) in keV cropping the tabulated energy bins at load time (cropped copies are cached and shared between instances). When fitting a redshift or energy scale, widen the band so \(E \times (1+z)/E_{\text{scale}}\) stays covered, or flux is silently lost.

Parameters

Read from the file's PARAMETERS extension: one fittable parameter per row, renamed to valid dotted-path segments (see table_parameters for the mapping), plus escale when the file sets ESCALE, z when it sets REDSHIFT, and norm (normalisation of the spectrum). Every parameter needs a prior entry (a fixed value is enough to freeze it).

Warning

Parameter values outside the tabulated grid are clipped to the nearest grid node (XSPEC raises a hard error instead) — keep priors inside the grid. Bins outside the tabulated energy range get zero flux (XSPEC ignores LOELIMIT/HIELIMIT for additive tables, and so does jaxspec). Energy flux uses a mid-bin approximation, like every purely bin-integrated component. The table is baked into the compiled program as a constant: instances built from the same file share one in-memory copy, but very large tables cost their full size once per compiled fit. Enable float64 (jax.config.update("jax_enable_x64", True)) when the per-bin fluxes span many decades: the rebinning cumulative sum saturates single precision and silently corrupts the faint tail otherwise.

Source code in src/jaxspec/model/tabulated.py
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
class ATable(_TableComponent, AdditiveComponent):
    r"""Additive OGIP table model — the equivalent of XSPEC's `atable{file}`.

    The tabulated spectra are per-bin integrated photon fluxes
    $\left[\frac{\text{photons}}{\text{cm}^2\text{s}}\right]$; they are interpolated
    multilinearly over the parameter grid and redistributed onto the requested bins
    proportionally to overlap (integral preserving):

    $$ \mathcal{M}(E_\min, E_\max) = \frac{\text{norm}}{1+z}
    \int_{E_\min (1+z)/E_{\text{scale}}}^{E_\max (1+z)/E_{\text{scale}}}
    \text{d}E ~ S_{\vec{p}}(E) $$

    !!! abstract "Constructor arguments"
        * `path`: OGIP 92-009 table-model FITS file.
        * `energy_band`: optional `(e_min, e_max)` in keV cropping the tabulated energy
          bins at load time (cropped copies are cached and shared between instances).
          When fitting a redshift or energy scale, widen the band so
          $E \times (1+z)/E_{\text{scale}}$ stays covered, or flux is silently lost.

    !!! abstract "Parameters"
        Read from the file's `PARAMETERS` extension: one fittable parameter per row,
        renamed to valid dotted-path segments (see `table_parameters` for the mapping),
        plus `escale` when the file sets `ESCALE`, `z` when it sets `REDSHIFT`, and
        `norm` (normalisation of the spectrum). Every parameter needs a prior entry
        (a fixed value is enough to freeze it).

    !!! warning
        Parameter values outside the tabulated grid are clipped to the nearest grid
        node (XSPEC raises a hard error instead) — keep priors inside the grid.
        Bins outside the tabulated energy range get zero flux (XSPEC ignores
        `LOELIMIT`/`HIELIMIT` for additive tables, and so does jaxspec). Energy flux
        uses a mid-bin approximation, like every purely bin-integrated component.
        The table is baked into the compiled program as a constant: instances built
        from the same file share one in-memory copy, but very large tables cost their
        full size once per compiled fit.
        Enable float64 (`jax.config.update("jax_enable_x64", True)`) when the per-bin
        fluxes span many decades: the rebinning cumulative sum saturates single
        precision and silently corrupts the faint tail otherwise.
    """

    def __init__(self, path: str | Path, energy_band: tuple[float, float] | None = None):
        self._init_table(path, energy_band)
        self.norm = nnx.Param(1.0)

    def integrated_continuum(self, e_low, e_high):
        table = self._table
        spectrum = self._interpolated_table_values()
        scale, one_plus_z = self._energy_scale()
        flux = redistribute(
            spectrum,
            jnp.asarray(table.e_edges[:-1]),
            jnp.asarray(table.e_edges[1:]),
            e_low * scale,
            e_high * scale,
        )
        return jnp.asarray(self.norm) * flux / one_plus_z

ETable

Bases: MTable

Exponential multiplicative OGIP table model — XSPEC's etable{file}.

Identical to MTable except the combined table value (additional parameters included) is mapped through

\[ \mathcal{M}(E) = e^{-S_{\vec{p}}(E)} \]

so the tabulated quantity is an optical depth (additional parameters combine linearly before the exponential, XSPEC's design point for column densities). LOELIMIT/HIELIMIT are final factors and are not exponentiated, matching XSPEC. See MTable for the constructor arguments, parameter discovery and edge-behaviour caveats.

Source code in src/jaxspec/model/tabulated.py
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
class ETable(MTable):
    r"""Exponential multiplicative OGIP table model — XSPEC's `etable{file}`.

    Identical to [`MTable`][jaxspec.model.tabulated.MTable] except the combined table
    value (additional parameters included) is mapped through

    $$ \mathcal{M}(E) = e^{-S_{\vec{p}}(E)} $$

    so the tabulated quantity is an optical depth (additional parameters combine
    linearly *before* the exponential, XSPEC's design point for column densities).
    `LOELIMIT`/`HIELIMIT` are final factors and are **not** exponentiated, matching
    XSPEC. See [`MTable`][jaxspec.model.tabulated.MTable] for the constructor
    arguments, parameter discovery and edge-behaviour caveats.
    """

    def _transform(self, values):
        return jnp.exp(-values)

MTable

Bases: _TableComponent, MultiplicativeComponent

Multiplicative OGIP table model — the equivalent of XSPEC's mtable{file}.

The tabulated values are dimensionless multiplicative factors; the factor applied to an output bin is their width-weighted average over the covered table bins:

\[ \mathcal{M}(E_\min, E_\max) = \frac{1}{E_\max - E_\min}\int_{E_\min}^{E_\max} \text{d}E ~ S_{\vec{p}}(E) \]

evaluated at \(E \times (1+z)/E_{\text{scale}}\) when the file declares redshift or energy-scale parameters (no \(1/(1+z)\) flux factor for multiplicative tables).

Constructor arguments

  • path: OGIP 92-009 table-model FITS file.
  • energy_band: optional (e_min, e_max) in keV cropping the tabulated energy bins at load time (cropped copies are cached and shared between instances). When fitting a redshift or energy scale, widen the band so \(E \times (1+z)/E_{\text{scale}}\) stays covered, or the out-of-range factor silently applies instead.

Parameters

Read from the file's PARAMETERS extension: one fittable parameter per row, renamed to valid dotted-path segments (see table_parameters for the mapping), plus escale when the file sets ESCALE and z when it sets REDSHIFT. Every parameter needs a prior entry (a fixed value is enough to freeze it).

Warning

Parameter values outside the tabulated grid are clipped to the nearest grid node (XSPEC raises a hard error instead) — keep priors inside the grid. Bins fully outside the tabulated energy range use LOELIMIT/HIELIMIT from the file when present, and 1.0 otherwise. A bin partially covering the low end of the table averages over its covered fraction; XSPEC 12.15.1 drops such bins entirely (asymmetrically with the high end) — jaxspec keeps the symmetric, integral-consistent behaviour. The table is baked into the compiled program as a constant: instances built from the same file share one in-memory copy. Enable float64 (jax.config.update("jax_enable_x64", True)) when the factors span many decades: the rebinning cumulative sum saturates single precision.

Source code in src/jaxspec/model/tabulated.py
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
class MTable(_TableComponent, MultiplicativeComponent):
    r"""Multiplicative OGIP table model — the equivalent of XSPEC's `mtable{file}`.

    The tabulated values are dimensionless multiplicative factors; the factor applied
    to an output bin is their width-weighted average over the covered table bins:

    $$ \mathcal{M}(E_\min, E_\max) =
    \frac{1}{E_\max - E_\min}\int_{E_\min}^{E_\max} \text{d}E ~ S_{\vec{p}}(E) $$

    evaluated at $E \times (1+z)/E_{\text{scale}}$ when the file declares redshift or
    energy-scale parameters (no $1/(1+z)$ flux factor for multiplicative tables).

    !!! abstract "Constructor arguments"
        * `path`: OGIP 92-009 table-model FITS file.
        * `energy_band`: optional `(e_min, e_max)` in keV cropping the tabulated energy
          bins at load time (cropped copies are cached and shared between instances).
          When fitting a redshift or energy scale, widen the band so
          $E \times (1+z)/E_{\text{scale}}$ stays covered, or the out-of-range factor
          silently applies instead.

    !!! abstract "Parameters"
        Read from the file's `PARAMETERS` extension: one fittable parameter per row,
        renamed to valid dotted-path segments (see `table_parameters` for the mapping),
        plus `escale` when the file sets `ESCALE` and `z` when it sets `REDSHIFT`.
        Every parameter needs a prior entry (a fixed value is enough to freeze it).

    !!! warning
        Parameter values outside the tabulated grid are clipped to the nearest grid
        node (XSPEC raises a hard error instead) — keep priors inside the grid.
        Bins fully outside the tabulated energy range use `LOELIMIT`/`HIELIMIT` from
        the file when present, and 1.0 otherwise. A bin partially covering the low end
        of the table averages over its covered fraction; XSPEC 12.15.1 drops such bins
        entirely (asymmetrically with the high end) — jaxspec keeps the symmetric,
        integral-consistent behaviour. The table is baked into the compiled program as
        a constant: instances built from the same file share one in-memory copy.
        Enable float64 (`jax.config.update("jax_enable_x64", True)`) when the factors
        span many decades: the rebinning cumulative sum saturates single precision.
    """

    def __init__(self, path: str | Path, energy_band: tuple[float, float] | None = None):
        self._init_table(path, energy_band)

    def _transform(self, values):
        """Hook mapping combined table values to the multiplicative factor."""
        return values

    def _out_of_range_factor(self, below, above):
        table = self._table
        lo = 1.0 if table.lo_limit is None else table.lo_limit
        hi = 1.0 if table.hi_limit is None else table.hi_limit
        return jnp.where(below, lo, jnp.where(above, hi, 1.0))

    def _factor(self, e_low, e_high, n_points=2):
        # Average over covered table widths rather than point-sampling the factor.
        table = self._table
        values = self._interpolated_table_values()
        table_lo = jnp.asarray(table.e_edges[:-1])
        table_hi = jnp.asarray(table.e_edges[1:])
        widths = jnp.asarray(np.diff(table.e_edges))

        scale, _ = self._energy_scale()
        lo, hi = e_low * scale, e_high * scale
        covered_integral = redistribute(values * widths, table_lo, table_hi, lo, hi)
        covered_width = redistribute(widths, table_lo, table_hi, lo, hi)
        in_range = covered_width > 0
        average = self._transform(covered_integral / jnp.where(in_range, covered_width, 1.0))
        # Limits are final factors, not optical depths for ETable.
        out_value = self._out_of_range_factor(hi <= table_lo[0], lo >= table_hi[-1])
        factor = jnp.where(in_range, average, out_value)
        # A clipped shift can collapse both bin edges.
        return jnp.where(e_high > e_low, factor, 0.0)

    def factor(self, energy):
        """Pointwise factor (step function over the table bins) for direct calls;
        the fit hot path goes through the exact bin-averaged ``_factor`` instead."""
        table = self._table
        values = self._interpolated_table_values()
        edges = jnp.asarray(table.e_edges)
        scaled = energy * self._energy_scale()[0]
        i = jnp.clip(jnp.searchsorted(edges, scaled, side="right") - 1, 0, values.shape[-1] - 1)
        out_value = self._out_of_range_factor(scaled < edges[0], scaled >= edges[-1])
        return jnp.where(
            (scaled >= edges[0]) & (scaled < edges[-1]), self._transform(values[i]), out_value
        )

factor(energy)

Pointwise factor (step function over the table bins) for direct calls; the fit hot path goes through the exact bin-averaged _factor instead.

Source code in src/jaxspec/model/tabulated.py
447
448
449
450
451
452
453
454
455
456
457
458
def factor(self, energy):
    """Pointwise factor (step function over the table bins) for direct calls;
    the fit hot path goes through the exact bin-averaged ``_factor`` instead."""
    table = self._table
    values = self._interpolated_table_values()
    edges = jnp.asarray(table.e_edges)
    scaled = energy * self._energy_scale()[0]
    i = jnp.clip(jnp.searchsorted(edges, scaled, side="right") - 1, 0, values.shape[-1] - 1)
    out_value = self._out_of_range_factor(scaled < edges[0], scaled >= edges[-1])
    return jnp.where(
        (scaled >= edges[0]) & (scaled < edges[-1]), self._transform(values[i]), out_value
    )