Metadata-Version: 2.4
Name: saltjax
Version: 0.4.0
Summary: Fast batched SALT2 / SALT3 lightcurve fitting with JAX, reproducing sncosmo
Author-email: Mickael Rigault <m.rigault@ipnl.in2p3.fr>
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/MickaelRigault/saltjax
Keywords: astronomy,astrophysics,supernovae,jax
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Astronomy
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.26
Requires-Dist: pandas
Requires-Dist: scipy
Requires-Dist: jax
Requires-Dist: sncosmo
Requires-Dist: extinction
Provides-Extra: docs
Requires-Dist: myst-nb; extra == "docs"
Requires-Dist: sphinx-book-theme; extra == "docs"
Requires-Dist: sphinx_copybutton; extra == "docs"
Requires-Dist: sphinx-design; extra == "docs"
Provides-Extra: tests
Requires-Dist: pytest; extra == "tests"
Requires-Dist: coverage; extra == "tests"
Requires-Dist: iminuit; extra == "tests"
Provides-Extra: progress
Requires-Dist: tqdm; extra == "progress"
Dynamic: license-file

[![PyPI](https://img.shields.io/pypi/v/saltjax.svg?style=flat-square)](https://pypi.org/project/saltjax/)
[![Documentation Status](https://readthedocs.org/projects/saltjax/badge/?version=latest)](https://saltjax.readthedocs.io/en/latest/?badge=latest)
[![codecov](https://codecov.io/gh/MickaelRigault/saltjax/branch/main/graph/badge.svg)](https://codecov.io/gh/MickaelRigault/saltjax)

# saltjax

Fast, batched lightcurve fitting with [JAX](https://docs.jax.dev), reproducing
[sncosmo](https://sncosmo.readthedocs.io)'s `fit_lc`, for SALT2 / SALT3 and any time-series source.

`saltjax.fit_lc` fits thousands of lightcurves at once with any SALT2, SALT3 or time-series sncosmo source
(e.g. `"salt2"`, `"salt3"`, `"hsiao"`, `"nugent-sn2p"`), with any sncosmo dust. It uses the same model as sncosmo
(same interpolation, model covariance and dust), and gives the same parameters and errors
(to ~1e-2 sigma at worst), much faster.

## Speed

On ~1000 simulated ZTF SNe Ia with Milky Way extinction (SFD map), on a laptop CPU (Apple M1 Max),
with the same model in both (SALT2 + Milky Way CCM89 dust):

| per SN | sncosmo `fit_lc` | saltjax, first call | saltjax, later call |
|---|---|---|---|
| `modelcov=False` | 58 ms | 9 ms (6x) | 2.4 ms (25x) |
| `modelcov=True` | 107 ms | 20 ms (5x) | 6.7 ms (16x) |

The first call of a session includes tabulation and JAX compilation. Dust costs nothing during the fit: host
dust (any law, per-SN E(B-V) and R_V) gives the same saltjax time, while sncosmo gets slower. See the
[speed](docs/examples/speed.ipynb) and [dust](docs/examples/dust.ipynb) notebooks.

## Install

```bash
pip install saltjax
```

## What can be fitted

One function, `saltjax.fit_lc`, for any combination of a **source** and **effects** supported by sncosmo below.
The source (a name or an instance) sets what is fitted:

| Source (sncosmo) | Fitted parameters |
|---|---|
| any SALT2 or SALT3 source: `"salt2"`, `"salt2-extended"`, `"salt2-h17"`, `"salt3"`, `"salt3-nir"`… | `t0`, `x0`, `x1`, `c` (+ model covariance) |
| any time-series source (`sncosmo.TimeSeriesSource`): `"hsiao"`, `"nugent-sn1a"`, `"nugent-sn2p"`, the SNANA core-collapse templates… | `t0`, `amplitude` |

**Effects**, for any source, given as in `sncosmo.Model` (`effects`, `effect_names`, `effect_frames`):

* any sncosmo dust law (`CCM89Dust`, `OD94Dust`, `F99Dust`), or any user-defined `sncosmo.PropagationEffect`
  that is a deterministic transmission in wavelength;
* in the observer frame (`"obs"`, e.g. Milky Way dust) or the rest frame (`"rest"`, e.g. host dust);
* with parameters set **per target**, from the columns `{name}{param}` of `targets` (e.g. `mwebv`, `hostebv`,
  `hostr_v`), else from the effect instance. They are fixed during the fit.

By default (no `effects`), Milky Way CCM89 dust is applied from the `mwebv` column, if present.

## Quick start

```python
import saltjax

# data: lightcurves, indexed by (target, observation), with columns
#       time (or mjd), band, flux, fluxerr, zp [, zpsys]
# targets: one row per target with z [, t0, mwebv]
results = saltjax.fit_lc(data, targets, source="salt2", modelcov=True, progress_bar=True)
results[["t0", "x0", "x1", "c", "t0_err", "x0_err", "x1_err", "c_err"]]

# any SALT2 or SALT3 source registered in sncosmo
results = saltjax.fit_lc(data, targets, source="salt3")
results = saltjax.fit_lc(data, targets, source="salt2", version="T23")

# any time-series source: fits t0 and amplitude
results = saltjax.fit_lc(data, targets, source="hsiao")
results = saltjax.fit_lc(data, targets, source="nugent-sn2p")
```

With effects, the same arguments whatever the source, e.g. Milky Way and host dust whose parameters are read per
target from the columns `mwebv`, `hostebv` and `hostr_v` of `targets`:

```python
import sncosmo

dust = dict(effects=[sncosmo.CCM89Dust(), sncosmo.F99Dust(r_v=2.0)],
            effect_names=["mw", "host"], effect_frames=["obs", "rest"])

results = saltjax.fit_lc(data, targets, source="salt3", **dust)
results = saltjax.fit_lc(data, targets, source="nugent-sn2p", **dust)
```

Options specific to a source family (e.g. `modelcov` for SALT) raise an error with the other family.

Check a time-series fit by eye (give the same effects as the fit, if any):

```python
template = saltjax.timeseries.TimeSeriesTemplate.from_name("nugent-sn2p")
template.show_fit(data, results, index=results.index[0], **dust)
```

`saltjax` is used by [skysurvey](https://github.com/MickaelRigault/skysurvey) through
`skysurvey.lcfit.fit_salt_jax(dataset)`.

## How it reproduces sncosmo

* **Interpolation.** sncosmo interpolates the SALT2 surfaces with a separable bicubic-convolution kernel. Band
  integration is linear, so `saltjax` band-integrates the surfaces at the native phase nodes
  and applies the *same* kernel in phase. This is exact.
* **Dust and other effects.** Their parameters are fixed per target, so their transmission is folded, exactly, into
  the band integrals of each target, at its own redshift, on sncosmo's wavelength grid. Any sncosmo dust law
  (CCM89, OD94, F99), in the observer or rest frame, with per-target E(B-V) and R_V.
* **Colour.** The colour law is written as an exact band-mean factor times a power series truncated below machine
  precision. Model fluxes equal sncosmo's to ~1e-15.
* **Redshift tables.** The model covariance is tabulated on a fine redshift grid. This is the only approximation:
  relative differences of ~1e-6 (median).
* **Model covariance.** `modelcov=True` follows `sncosmo.fit_lc`. The model covariance (a diagonal plus one block
  per band) is inverted exactly with the Woodbury identity.
* **Errors.** Errors are `2 H^-1`, with `H` the exact Hessian of the chi2 (as Minuit HESSE).
* **Time-series sources.** sncosmo interpolates them with a tensor-product spline, also linear in the template
  and separable in phase and wavelength: the band integral of each phase spline function is computed once per
  target, and the band flux at any phase is the amplitude times a sum over a few spline functions, exactly as sncosmo.

## Limitations

* SALT2 and SALT3 sources (`sncosmo.SALT2Source`, `sncosmo.SALT3Source`, e.g.
  `"salt2"`, `"salt2-extended"`, `"salt3"`, `"salt3-nir"`) and time-series
  sources (`sncosmo.TimeSeriesSource`, e.g. `"hsiao"`, `"nugent-sn2p"`).
  Other sources (`StretchSource`, `SNEMOSource`, `MLCS2k2Source`, `SUGARSource`) are not supported.
* Effects (dust) in the observer or rest frame only, with fixed parameters (not fitted); no random scattering
  effects (G10, C11).
* Time-series sources: no model covariance (as in sncosmo, where only SALT has one).
* AB magnitude system only.
* No parameter bounds.
* Fixed redshift.

## Acknowledgements

`saltjax` was developed by Mickael Rigault with the help of [Claude Code](https://claude.com/claude-code)
(Anthropic), an AI coding assistant, which contributed to the implementation, tests and documentation
(including support for all SALT2/SALT3 sources and for sncosmo dust effects).
