jwspecfit.redshift

Strong-line redshift fitting via brute-force grid evaluation.

This module implements fit_redshift(), which determines the redshift of a JWST NIRSpec spectrum by jointly fitting the amplitudes of a curated set of strong emission lines on a dense grid of trial redshifts. The algorithm is deliberately exhaustive (no early stopping), it gracefully skips lines that fall outside the spectrum’s wavelength range at the trial redshift, and it returns both a per-peak credible interval and a ranked list of secondary peaks so that aliasing failures are visible.

The per-z score is the chi^2 of a non-negative joint line fit plus a low-order polynomial continuum, computed via a single scipy.optimize.lsq_linear() call with mixed bounds.

Functions

fit_redshift(spec, *[, lines, z_min, z_max, ...])

Fit the redshift of spec by joint strong-line fitting on a z grid.

Classes

Peak(z, prob, dchi2, ci68, ci95, ...)

A single local minimum of the chi^2(z) curve, refined and ranked.

RedshiftResult(z_best, z_ci68, z_ci95, ...)

Output of fit_redshift().

class jwspecfit.redshift.Peak(z, prob, dchi2, ci68, ci95, n_lines_used, lines_used)[source]

Bases: object

A single local minimum of the chi^2(z) curve, refined and ranked.

Parameters:
ci68: tuple[float, float]
ci95: tuple[float, float]
dchi2: float
lines_used: list[str]
n_lines_used: int
prob: float
z: float
class jwspecfit.redshift.RedshiftResult(z_best, z_ci68, z_ci95, peaks, is_decisive, z_grid_coarse, chi2_coarse, P_z_coarse, z_grid_fine, chi2_fine, P_z_fine, lines_used, grating, spec)[source]

Bases: object

Output of fit_redshift().

Parameters:
P_z_coarse: ndarray
P_z_fine: ndarray
chi2_coarse: ndarray
chi2_fine: ndarray
grating: str | None
is_decisive: bool
lines_used: list[str]
peaks: list[Peak]
plot()[source]

Two-panel diagnostic: Delta chi^2(z) and the spectrum at z_best.

spec: Spectrum
z_best: float
z_ci68: tuple[float, float]
z_ci95: tuple[float, float]
z_grid_coarse: ndarray
z_grid_fine: ndarray
jwspecfit.redshift.fit_redshift(spec, *, lines=None, z_min=0.0, z_max=20.0, dz_coarse=None, prior=None, n_peaks=5, refine_dchi2=9.0, continuum_order=3, nonneg=True, min_lines_in_range=2, verbose=True)[source]

Fit the redshift of spec by joint strong-line fitting on a z grid.

Parameters:
  • spec (Spectrum) – Input spectrum. Must have valid wave_um, flux_ujy, err_ujy.

  • lines (list of str, optional) – Line names (keys of REST_LINES_A) to include. Defaults to DEFAULT_LINES.

  • z_min (float) – Bounds of the redshift search (default 0 to 20).

  • z_max (float) – Bounds of the redshift search (default 0 to 20).

  • dz_coarse (float, optional) – Coarse-grid step. Defaults to a value tied to the spectral resolving power (see _default_dz_coarse()).

  • prior (None | (lo, hi) | list[(lo, hi)] | callable) – Optional non-Gaussian prior over z. None means flat.

  • n_peaks (int) – Minimum number of peaks to refine and return.

  • refine_dchi2 (float) – All local minima within this Δχ² of the global minimum are refined, in addition to the top n_peaks.

  • continuum_order (int) – Order of the Legendre-polynomial continuum that is added to the per-z linear fit. Default 3.

  • nonneg (bool) – If True, line amplitudes are constrained to be non-negative (emission only). Strongly recommended.

  • min_lines_in_range (int) – Trial redshifts where fewer than this many default lines fall inside the spectrum get the continuum-only chi^2 baseline.

  • verbose (bool) – If True, log a few timing milestones at INFO level.

Return type:

RedshiftResult