specsr.data.datasets¶
Torch datasets over the paired spectra products.
Each row is one (low-resolution prism, medium-resolution grating) pair on a shared wavelength grid, plus the reference uncertainty, the catalogue redshift and provenance.
Normalisation is per spectrum: each spectrum is standardised by its own mean and standard deviation. That is deliberate — it carries no information between rows, so unlike a dataset-wide normalisation it cannot leak anything about the held-out split. The reference uncertainty is divided by the same scale so it stays in the normalised units the loss works in, and the per-row mean and scale are returned so predictions can be pushed back into physical units for evaluation and plotting.
Functions
|
Standardise one spectrum by its own mean and standard deviation. |
Classes
|
Paired spectra on a fixed grid, returned as a tuple. |
|
Paired low/high-resolution spectra from a built |
- class specsr.data.datasets.FixedGridSpectraDataset(*args, **kwargs)[source]¶
Bases:
DatasetPaired spectra on a fixed grid, returned as a tuple.
(low, high, high_err, z, high_mean, high_std)— the last two let a caller de-normalise back into physical HR units, which is what line fluxes and residuals mean anything in.This is the loader the training scripts use, and it lives here rather than in
train/so the normalisation exists once. It previously had a second copy intrain/sr1_best/train_sr1.py; the HR-error fix below then had to be applied twice, which is exactly the kind of duplication that lets one copy quietly keep a bug.PairedSpectrais the dict-returning equivalent and the better API for new code; this exists because the trained checkpoints were produced against the tuple form.
- class specsr.data.datasets.PairedSpectra(*args, **kwargs)[source]¶
Bases:
DatasetPaired low/high-resolution spectra from a built
.npzproduct.- Parameters:
npz_path (str | Path) – Path to a product written by the preprocessing pipeline.
normalize_flux (bool) – Standardise each spectrum by its own statistics (default). When off, fluxes are returned as stored but the per-row statistics are still computed and returned, so de-normalisation helpers keep working.
target_key (str) – Which reference to train against —
"flux_high"for the stitched grating spectrum, or"flux_high_smoothed"for its smoothed variant. The matching*_errarray is used as the reference uncertainty.dtype (torch.dtype) – Storage dtype.
float32halves memory against thefloat64the products are written in, which matters: at the log wavelength grid (~6.7k samples) a 52k-row product is several GB per array.
- Returns:
Each item is a dict so that call sites index by name rather than by
position. The previous tuple-based interface made it easy to silently
transpose two fields at a call site; a dict makes that a
KeyError.flux_low,flux_high,flux_high_errare(L,)tensors;zis a scalar tensor;flux_high_mean/flux_high_stdare theper-row de-normalisation statistics;
indexis the row index.
- REQUIRED = ('flux_low', 'z')¶
Arrays required to be present in a product.
- denormalize(x, idx)[source]¶
Map normalised flux back to the physical units of the reference.
- Parameters:
x (torch.Tensor)
- Return type: