Skip to content
GSPICE

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.

A model learns from many spectra. gspice.simulate() makes 3000 training spectra and 5 test spectra, each of 200 pixels.

Python
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):

  • flux holds the flux, with noise.
  • ivar holds the inverse variance of the noise, 1/σ².
  • truth holds the flux without noise.

GSPICE predicts pixels from a mean spectrum and a covariance between every pair of pixels. gspice.fit() estimates both from the training spectra.

Python
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.

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.

Python
mask = np.zeros(test.flux.shape, dtype=bool)
mask[:, 80:110] = True
result = gspice.infill(model, test.flux, test.ivar, mask=mask)
print(f"{result.replaced.sum()} pixels replaced")
Text
150 pixels replaced

infill 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.

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.flux holds the mean of that distribution at the masked pixels, and the input flux elsewhere.
  • result.var holds its variance at the masked pixels, which includes the noise.