Getting started
Quickstart
A minimal GSPICE run fits a Gaussian model to simulated training spectra and predicts a hidden range of pixels on held-out test spectra without downloading external data. Install GSPICE first.
Simulate spectra
Section titled “Simulate spectra”A model learns from many spectra. gspice.simulate()
makes 3000 training spectra and 5 test spectra, each of 200 pixels.
import numpy as np
import gspice
rng = np.random.default_rng(2026)train = gspice.simulate(3000, 200, rng=rng)test = gspice.simulate(5, 200, rng=rng)Each call returns a gspice.Simulation of
continuum-normalized spectra. These three of its fields have the shape (number of
spectra, number of pixels):
fluxholds the flux, with noise.ivarholds the inverse variance of the noise, 1/σ².truthholds the flux without noise.
Fit a model
Section titled “Fit a model”GSPICE predicts pixels from a mean spectrum and a covariance between every pair of
pixels. gspice.fit() estimates both from the
training spectra.
model, clipped = gspice.fit(train.flux, train.ivar)fit returns a gspice.Model and clipped,
the mask of the pixels that it left out. What you get back
describes both.
Predict hidden pixels
Section titled “Predict hidden pixels”Suppose that pixels 80 to 109 of every test spectrum are bad. Mark them in a mask, and
gspice.infill() predicts them from the other
pixels.
mask = np.zeros(test.flux.shape, dtype=bool)mask[:, 80:110] = Trueresult = gspice.infill(model, test.flux, test.ivar, mask=mask)print(f"{result.replaced.sum()} pixels replaced")150 pixels replacedinfill replaced the 30 masked pixels of each of the 5 test spectra.
- observed flux
- masked
- prediction
- ±1σ
- ±2σ
- flux without noise
The plot shows the spectrum test.flux[3] after infill, at a signal-to-noise ratio of 113. Pixels 80 to 109 masked (30). Mean σ 0.009. RMS error 0.006. The error is the prediction minus the flux without noise.
What happened
Section titled “What happened”fit treated each training spectrum as one draw from a multivariate Gaussian
distribution over its 200 pixels. It estimated the mean and the covariance of that
distribution. The covariance records how the flux at each pixel varies with the flux at
every other pixel.
For each test spectrum, infill then computed the distribution of the masked pixels,
given the other 170 pixels, its
conditioning pixels. result is
a gspice.Repaired:
result.fluxholds the mean of that distribution at the masked pixels, and the input flux elsewhere.result.varholds its variance at the masked pixels, which includes the noise.
Next steps
Section titled “Next steps”- Prepare your spectra turns your own data into
flux,ivarandmask. - What you get back lists every field of
resultandmodel. - Find and repair artifacts finds artifacts that no mask marks.
conditioning pixels
The pixels whose values a prediction uses, : all pixels outside . The paper calls this set , and counts it.