Fit Data API¶
FitDataBuilder turns tiles and a DataConfig into FitData: normalised, model-ready JAX
arrays with sky positions and indices. FitDataBuilder is exported from the top-level
lvm_tools namespace; FitData lives in lvm_tools.fit_data.fit_data.
import jax.numpy as jnp
from lvm_tools import DataConfig, FitDataBuilder, LVMTile
tile = LVMTile.from_file("/data/observation.fits")
config = DataConfig.from_tiles(tile, λ_range=(6500.0, 6800.0))
fit_data = FitDataBuilder(tiles=tile, config=config).build()
# Spectra have shape (n_wavelengths, n_spaxels); average over the spaxels with data
weights = fit_data.mask.astype(jnp.float32)
mean_flux = jnp.sum(fit_data.flux * weights, axis=1) / jnp.sum(weights, axis=1)
# Spaxel positions in arcsec east and north of fit_data.sky_centre, and back to RA, Dec
x, y = fit_data.sky_x, fit_data.sky_y
ra, dec = fit_data.sky_to_radec(x, y)
print(f"Mean barycentric velocity: {float(fit_data.v_bary.mean()):.2f} km/s")
FitDataBuilder¶
lvm_tools.FitDataBuilder
dataclass
¶
Builds FitData from tiles and a config, and identifies the pair by a hash.
Attributes:
| Name | Type | Description |
|---|---|---|
tiles |
LVMTileLike
|
The data: one tile or a collection. |
config |
DataConfig
|
The processing config, usually from |
Example
build()
¶
Process the tiles with the config and package the result for fitting.
The steps are:
- Clip the data to the config's wavelength, RA and Dec ranges.
- Set the data the config excludes to NaN (NaNs, bad flux, fibre status, mask).
- Flatten the tile and spaxel dimensions into one spaxel dimension.
- Make the normalisation functions from the config's offsets and scales (the normalisation strategies are not applied again here).
Returns:
| Type | Description |
|---|---|
FitData
|
The data, ready for fitting. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no data are left after clipping or filtering, or a strategy is not supported by the filtering. |
hash()
¶
A SHA-256 hash identifying the config and the tiles.
It is computed from the config's attributes and each tile's metadata (filename, tile ID, exposure number and DRP version), both serialised as JSON with sorted keys. It identifies the inputs, not the data values.
Returns:
| Type | Description |
|---|---|
str
|
The hash, as a hexadecimal string. |
FitData¶
lvm_tools.fit_data.fit_data.FitData
dataclass
¶
LVM data ready for model fitting: normalised arrays, positions and indices as JAX arrays.
Usually made by FitDataBuilder.build. The data are held as a flattened xarray dataset
plus the normalisation functions, and each property computes its JAX array from them
when accessed.
The spectral arrays (flux, i_var, u_flux, lsf_σ, mask) have shape
(n_wavelengths, n_spaxels), and the per-spaxel arrays (sky_x, sky_y, the indices,
mjd, v_bary) have shape (n_spaxels,), where the spaxels of every tile are counted
together. Floating-point arrays are 32-bit unless JAX's 64-bit mode is on. Pixels that
were clipped or filtered out are NaN in the processed data; mask marks the others,
and the spectral properties replace the NaNs with fill values so they can be used in
a likelihood together with mask.
Sky positions are given as sky_x and sky_y: offsets in arcsec east and north of
sky_centre, on the tangent plane. sky_data packages them for spectracles. The
members that scale the coordinates into [-π, π] (α, δ, αδ_data, predict_α,
predict_δ, arcsec_to_model_domain) are deprecated and emit DeprecationWarning.
Attributes:
| Name | Type | Description |
|---|---|---|
processed_data |
Dataset
|
The clipped, filtered data, flattened to dimensions
|
normalise_flux |
Callable
|
Normalises a flux, |
predict_flux |
Callable
|
Turns a normalised flux back into erg cm⁻² s⁻¹ Å⁻¹. |
normalise_ivar |
Callable
|
Normalises an inverse variance (multiplies it by the flux scale squared). |
predict_ivar |
Callable
|
Turns a normalised inverse variance back into physical units. |
normalise_α |
Callable
|
Normalises a right ascension in degrees. |
normalise_δ |
Callable
|
Normalises a declination in degrees. |
sky_origin |
tuple[float, float] | None
|
The (RA, Dec) in degrees to measure sky offsets from, or |
The fields are, in order: processed_data, normalise_flux, predict_flux,
normalise_ivar, predict_ivar, normalise_α, the inverse of normalise_α,
normalise_δ, the inverse of normalise_δ, and sky_origin.
Example
Sky positions¶
Positions are offsets in arcsec on the tangent plane at sky_centre.
sky_x
property
¶
Each spaxel's offset east of sky_centre, in arcsec, shape (n_spaxels,).
The offsets are coordinates on the tangent plane at sky_centre (astropy's
SkyOffsetFrame), so they include the cos(Dec) factor. They are computed in
64-bit precision before being converted to a JAX array.
sky_y
property
¶
Each spaxel's offset north of sky_centre, in arcsec, shape (n_spaxels,).
Computed as for sky_x, on the tangent plane at sky_centre.
sky_centre
property
¶
The (RA, Dec) in degrees that sky_x and sky_y are measured from.
This is sky_origin if set, otherwise the centre of the spaxels' bounding box in RA
and Dec. The bounding box is found with RA measured relative to the first spaxel, so
a footprint that crosses RA = 0 is handled.
sky_to_radec(x, y)
¶
Convert offsets from sky_centre in arcsec to (RA, Dec) in degrees.
The inverse of sky_x and sky_y, e.g. to plot a map made on a grid of sky_x,
sky_y in RA and Dec.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
Offsets east of |
required |
y
|
ArrayLike
|
Offsets north of |
required |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray]
|
The right ascensions and declinations, in degrees (ICRS). |
with_sky_origin(ra, dec)
¶
The same data, with sky offsets measured from (RA, Dec) in degrees.
Use it e.g. to put several datasets on one frame. The origin is a choice of frame,
not part of the data, so FitDataBuilder.hash doesn't include it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ra
|
float
|
Right ascension of the origin, in degrees. |
required |
dec
|
float
|
Declination of the origin, in degrees. |
required |
Returns:
| Type | Description |
|---|---|
FitData
|
A copy with |
sky_data
property
¶
The spaxel positions for spectracles, as arcsec offsets from sky_centre.
A spectracles SpatialDataLVM with x = sky_x, y = sky_y, and the spaxel, tile,
IFU and fibre indices (spaxel_idx, tile_idx, ifu_idx, fibre_idx). Length
scales of spectracles fields are then in arcsec. Build the field's region with
spectracles.Domain.from_points(d.x, d.y). Requires the optional spectracles
dependency (pip install lvm_tools[spectracles]).
Raises:
| Type | Description |
|---|---|
ModuleNotFoundError
|
If spectracles could not be imported. |
Spectral data¶
Arrays of shape (n_wavelengths, n_spaxels), plus the wavelength grid.
flux
property
¶
Normalised flux, shape (n_wavelengths, n_spaxels), with NaNs (excluded pixels)
set to 0.
i_var
property
¶
Normalised inverse variance of flux, shape (n_wavelengths, n_spaxels), with
NaNs set to 1e-4.
u_flux
property
¶
Normalised flux uncertainty, i_var**-0.5 (before its NaNs are filled), shape
(n_wavelengths, n_spaxels), with NaNs set to 100.
Infinite values (where the inverse variance is 0) become the largest finite float,
as in jax.numpy.nan_to_num.
mask
property
¶
Which pixels hold data, shape (n_wavelengths, n_spaxels): True where the flux
is not NaN, i.e. the pixel survived clipping and filtering.
λ
property
¶
Wavelengths in Angstrom, shape (n_wavelengths,).
lsf_σ
property
¶
Gaussian standard deviation of the line spread function in Angstrom (not
normalised), shape (n_wavelengths, n_spaxels), with NaNs set to the median of the
rest.
Indices and per-spaxel properties¶
λ_idx
property
¶
Wavelength indices 0 .. n_wavelengths - 1.
spaxel_idx
property
¶
Spaxel indices 0 .. n_spaxels - 1, counting the spaxels of every tile together.
tile_idx
property
¶
Index of each spaxel's tile, shape (n_spaxels,): the tiles' exposure numbers
replaced by 0 .. n_tiles - 1, in increasing order of exposure number.
ifu_idx
property
¶
Index of each spaxel's IFU bundle, shape (n_spaxels,): the IFU labels replaced
by 0 .. n_ifus - 1, in sorted order of label.
fibre_idx
property
¶
Fibre identifier of each spaxel, shape (n_spaxels,). These are the slitmap's
fibre IDs, not re-indexed, so gaps from excluded fibres are kept.
mjd
property
¶
Modified Julian Date of the midpoint of each spaxel's exposure, shape
(n_spaxels,).
v_bary
property
¶
Barycentric velocity correction of each spaxel in km/s, shape (n_spaxels,).
Computed by get_v_barycentric for Las Campanas Observatory, at each spaxel's
position and exposure midpoint.
Deprecated coordinates¶
Deprecated
These members scale RA and Dec into [-π, π] and emit DeprecationWarning. Use the
arcsec offsets instead: sky_x for α, sky_y for δ, sky_data for αδ_data,
and sky_to_radec for predict_α and predict_δ. Positions from sky_data are
already in arcsec, so arcsec_to_model_domain isn't needed.
α
property
¶
Deprecated: use sky_x (arcsec).
Right ascension of each spaxel, normalised with normalise_α and mapped onto
[-π, π] (see to_π_domain), shape (n_spaxels,). Emits DeprecationWarning.
δ
property
¶
Deprecated: use sky_y (arcsec).
Declination of each spaxel, normalised with normalise_δ and mapped onto [-π, π]
(see to_π_domain), shape (n_spaxels,). Emits DeprecationWarning.
αδ_data
property
¶
Deprecated: use sky_data (arcsec).
The spaxel positions for spectracles as in sky_data, but with the coordinates of
α and δ (in [-π, π]) instead of arcsec offsets. Emits DeprecationWarning.
Raises:
| Type | Description |
|---|---|
ModuleNotFoundError
|
If spectracles could not be imported. |
predict_α(x)
¶
Deprecated: use sky_to_radec.
Convert right ascensions in the [-π, π] coordinates of α back to degrees. Emits
DeprecationWarning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Array
|
Values in the coordinates of |
required |
Returns:
| Type | Description |
|---|---|
Array
|
The right ascensions in degrees. |
predict_δ(x)
¶
Deprecated: use sky_to_radec.
Convert declinations in the [-π, π] coordinates of δ back to degrees. Emits
DeprecationWarning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Array
|
Values in the coordinates of |
required |
Returns:
| Type | Description |
|---|---|
Array
|
The declinations in degrees. |
arcsec_to_model_domain(arcsec)
¶
Deprecated: with sky_data, positions are already in arcsec.
Convert a length in arcsec into the [-π, π] coordinates of α, treating it as a
length in degrees of right ascension (without a cos(Dec) factor). Emits
DeprecationWarning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arcsec
|
float
|
The length, in arcsec. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The length in the coordinates of |
Helper functions¶
These are in lvm_tools.fit_data.fit_data; the [-π, π] mappings are used only by the
deprecated coordinates.
lvm_tools.fit_data.fit_data.to_jax_array(arr, dtype=np.float64)
¶
Convert an array (e.g. an xarray DataArray or its values) to a JAX array.
JAX's warning that a 64-bit dtype was truncated (when JAX's 64-bit mode is off) is silenced, so the result is 32-bit unless that mode is on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
DataArray
|
The array. |
required |
dtype
|
dtype
|
The dtype to request. |
float64
|
Returns:
| Type | Description |
|---|---|
Array
|
The JAX array. |
lvm_tools.fit_data.fit_data.to_π_domain(x)
¶
Map values from [0, 1] onto [-π, π]: x * 2π - π.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
Values in [0, 1]. |
required |
Returns:
| Type | Description |
|---|---|
ArrayLike
|
The values in [-π, π]. |
lvm_tools.fit_data.fit_data.from_π_domain(x)
¶
Map values from [-π, π] back onto [0, 1]: (x + π) / 2π. The inverse of to_π_domain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
Values in [-π, π]. |
required |
Returns:
| Type | Description |
|---|---|
ArrayLike
|
The values in [0, 1]. |