Skip to main content

Tests Coverage PyPI Python Docs


spectracles

Continuous field models for integral field spectroscopy and beyond

What it does

Spectracles fits models to IFU data where line or spectrum properties (line flux or velocity, continuum, ...) vary continuously across the sky. These are modelled as fields, so they are defined at every sky position spanned by the data, not just at the spaxels.

More generically, Spectracles is a Python/JAX library for statistical models, and particularly for models containing latent fields. It is designed for large-scale problems.

Spectracles has been designed for ease-of-use, both within and beyond the IFU community. Models are modular, and may be built from scratch or composed of many sub-models, which is particularly useful for hierarchical models (like spectrospatial IFU models). Spectracles is compatible with most JAX-supported inference algorithms out-of-the-box, including large-scale variational algorithms (MGVI or geoVI via NIFTy.re) or NUTS (e.g. BlackJAX or NumPyro).

Example

In this example, we fit a positive-valued field using observations at 300 scattered points. The inference machinery in this case is BlackJAX:

import blackjax
import equinox as eqx
import jax
import jax.numpy as jnp
from jax import random

from spectracles import FourierGP, Matern32, Parameter, SpatialDataGeneric, build_model
from spectracles.inference import GaussianLikelihood, LatentSpace, Posterior
from spectracles.priors import LogNormal


class LineMap(eqx.Module):
    log_flux: FourierGP

    def __call__(self, data):
        return jnp.exp(self.log_flux(data))


kernel = Matern32(
    length_scale=Parameter(prior=LogNormal(0.5, 0.2)),
    variance=Parameter(prior=LogNormal(1.0, 0.5)),
)
model = build_model(LineMap, FourierGP(kernel, n_modes=(15, 15)))
latent = LatentSpace(model)  # the parameters to fit

xy = random.uniform(random.PRNGKey(0), (2, 300), minval=-3.0, maxval=3.0)
spaxels = SpatialDataGeneric(x=xy[0], y=xy[1], idx=jnp.arange(300))
truth = latent.sample_prior_latent(random.PRNGKey(1))  # fake data from a random draw
flux = latent.to_model(truth)(spaxels) + 0.05 * random.normal(random.PRNGKey(2), (300,))
ivar = jnp.full(300, 0.05**-2)

posterior = Posterior(latent, lambda m: m(spaxels), GaussianLikelihood(flux, ivar))

# Sample with NUTS
warmup = blackjax.window_adaptation(blackjax.nuts, posterior.log_density)
(state, params), _ = warmup.run(random.PRNGKey(3), latent.zeros(), num_steps=300)
nuts = blackjax.nuts(posterior.log_density, **params)


def step(state, key):
    state, _ = nuts.step(key, state)
    return state, state.position


_, samples = jax.lax.scan(step, state, random.split(random.PRNGKey(4), 300))

posterior.summary(samples, true_latents=truth)  # fitted parameters vs the truth
maps = posterior.draws(samples, to="model")(spaxels)  # the model for every sample
mean_map, std_map = maps.mean(), maps.std()

The docs go through this step by step, and the worked example fits the same data with MGVI, NUTS and a best fit.

Installation

pip install spectracles            # or: uv add spectracles
pip install "spectracles[nifty]"   # to use MGVI and geoVI
pip install blackjax               # to use NUTS

For SDSS-V LVM data, lvm_tools reads the DRP output (lazily, with dask) and gives you the data ready for spectracles: pip install "lvm-tools[spectracles]". The LVM data guide shows a full Hα fit.

To run on a GPU, build jax-finufft from source as its README describes (this needs FFTW). To work on spectracles itself, clone this repository and run uv sync with uv, which also builds jax-finufft from source.

Features

IFU:

  • Models including emission lines whose parameters are fields
  • Models for systematics, for example per-pointing or per-spaxel parameters

Fields:

  • Fast stationary Gaussian-process fields on arbitrary points via the equispaced Fourier representation (EFGP) and FINUFFT (through jax-finufft)
  • Squared exponential (RBF) and Matern 1/2, 3/2 and 5/2 kernels
  • Easy dense evaluation after fitting (i.e. between the data points)
  • Runs on GPU through JAX

Generic:

  • Parameter sharing: pass the same Parameter (or kernel, or whole field) to several components, e.g. one velocity field for every line, and it is fitted as one parameter
  • Priors and fixed parameters set by path or glob pattern, e.g. model.with_priors({"**.length_scale": LogNormal(0.5, 0.2), "log_flux.*.variance": "fixed"})
  • Fix parameters at chosen values for a fit with posterior.fix({"log_flux.kernel.length_scale": 0.3})
  • Works with any JAX inference algorithm: posterior.log_density is a plain JAX function, and to_nifty(posterior) hands the same posterior to NIFTy.re
  • Gaussian likelihoods that drop NaNs, zero inverse variances and masked pixels automatically: GaussianLikelihood(data, ivar, mask=good)
  • The model, or any sub-component, at every posterior sample: posterior.draws(samples, to="model").log_flux(spaxels).mean()
  • Fast mapping of any function over posterior or prior samples
  • PPL-like features including easy prior specification and sampling
  • Plots of the model structure, showing which parameters are shared where: model.plot_model_graph()
  • Saving and loading models
  • Readable summaries of models and fits
  • Optimisation schedules for staged best fits (experimental; see the docs)

Stability

From 1.0.0, spectracles follows semantic versioning: anything in spectracles, spectracles.priors, spectracles.inference or spectracles.nifty only changes in a major release, after a deprecation warning. spectracles.lvm_models and the optimisation schedules (OptimiserFrame) are still changing. See the roadmap for what's planned.

Documentation

tomhilder.github.io/spectracles

Citation

If you find this software useful, please cite the paper:

Hilder et al. (2026):

@article{Hilder2026,
    author        = {{Hilder}, Thomas and {Casey}, Andrew R. and {Dalcanton}, Julianne J. and
                     {Kreckel}, Kathryn and {Stutz}, Amelia M. and {Singh}, Amrita and
                     {Blanc}, Guillermo A. and {S{\'a}nchez}, Sebasti{\'a}n F. and
                     {M{\'e}ndez-Delgado}, J. E. and {Saydjari}, Andrew K. and
                     {Vargas-Herrera}, Luciano and {Drory}, Niv and {Bizyaev}, Dmitry and
                     {Fern{\'a}ndez-Trincado}, Jos{\'e} G. and {Rom{\'a}n-Z{\'u}{\~n}iga}, Carlos G. and
                     {Kollmeier}, Juna A. and {Johnston}, Evelyn J.},
    title         = {{Unified Spectrospatial Forward Models: Spatially Continuous Maps of Weak
                     Emission Lines in the Rosette Nebula with SDSS-V LVM}},
    journal       = {The Astrophysical Journal},
    year          = {2026},
    volume        = {997},
    number        = {2},
    eid           = {339},
    doi           = {10.3847/1538-4357/ae274b},
    eprint        = {2510.07395},
    archivePrefix = {arXiv},
    primaryClass  = {astro-ph.GA},
}

You should also cite JAX and Equinox.

If you also use NIFTy.re, NumPyro, BlackJAX, or any other inference machinery that is not part of Spectracles, make sure you cite the relevant works!

License

MIT

Other useful software

Spectracles has many possible applications, but there are also many other great libraries out there! Here are just a few that may be useful to users of Spectracles:

  • lvm_tools: reads and selects SDSS-V LVM data (lazily, with dask) and hands it to spectracles as SpatialDataLVM.
  • NIFTy.re: JAX library for Gaussian-process models and variational inference (MGVI, geoVI), widely used for large imaging problems. Spectracles can hand its posteriors to NIFTy.re.
  • GraphGP: scalable Gaussian processes in JAX using nearest-neighbour graphs (Vecchia's approximation), in linear time and memory at arbitrary points.
  • efgp_jax: JAX implementation of the equispaced Fourier Gaussian processes that Spectracles' fields are based on.
  • tinygp and GPJax: general-purpose Gaussian-process libraries in JAX.
  • pPXF: full-spectrum fitting of stellar and gas kinematics and stellar populations, usually spaxel by spaxel or bin by bin.
  • PowerBin (successor to VorBin): adaptive spatial binning of IFU data to a target signal-to-noise.

Metadata

Release files for spectracles 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spectracles 1.0.0
File Size Uploaded Provenance Actions
spectracles-1.0.0.tar.gz 89.8 kB With provenance Details for spectracles-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for spectracles 1.0.0
File Interpreter ABI Platform Actions
spectracles-1.0.0-py3-none-any.whl Python 3 none any Details for spectracles-1.0.0-py3-none-any.whl

Total release size: 198.2 kB

Release files / spectracles-1.0.0.tar.gz

Download URL spectracles-1.0.0.tar.gz
Size 89.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4c60115c57925a1637b8e2fa6a23819cba994f5b0f9c24dc02ac218d0eafd0e6
BLAKE2b-256 checksum
How to use checksums
94c5ec46eb697e9d3f582d5f249cc07c622bae73102697e77f77c3d70fa54446
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Release files / spectracles-1.0.0-py3-none-any.whl

Download URL spectracles-1.0.0-py3-none-any.whl
Size 108.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
276023bc2b586a85d252ff5ef720722250addf86a7b233848fdd18dbf08f07d7
BLAKE2b-256 checksum
How to use checksums
b3a85c875da73527d57690ee74575c9938482bede9488feca259ca2d031b77af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Security for spectracles 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for spectracles-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page