space-ml-lab · Project P2 · Technical report

Unsupervised Anomaly Detection in Low-Resolution Spectra: A Self-Supervised Autoencoder with Isolation-Forest Latent Scoring

Draft — July 2026 · Developed for SPHEREx; demonstrated on SDSS DR17 · space-ml-lab/projects/p2-spherex-anomaly

Abstract

The most scientifically valuable objects in a spectroscopic survey are, by definition, those that fit no template. We present a compact, fully local pipeline for unsupervised discovery: a self-supervised autoencoder learns a 16-dimensional representation of real spectra, and anomalies are ranked by combining the reconstruction error with an Isolation Forest on the latent embeddings. The method is designed for SPHEREx (NASA, launched March 2025), whose all-sky near-infrared spectra are a natural target but whose extracted-spectrum catalogues are not yet public; we therefore develop and validate the identical pipeline on 4,990 genuine SDSS DR17 optical spectra as a documented stand-in. Without using labels, the latent space spontaneously organises by object class, and the anomaly ranking is strongly enriched in extreme-emission-line objects: 96% of the top-100 anomalies carry emission-line/broad-line subclasses versus 30.9% of the parent sample — a $3.1\times$ enrichment. All data are real.

Data-status note. SPHEREx is the intended target. As verified in July 2026, its public Quick Release (QR2) at IRSA exposes only per-band spectral images, not extracted spectra; assembling a spectrum requires forced spectrophotometry across ~102 channels. Gaia DR3 XP spectra (the closest NIR analogue) were retrievable but far too slow ($\sim$4.7 s/source). We therefore demonstrate on SDSS DR17 — the canonical testbed for this method — with the retrieval, preprocessing, autoencoder, and scoring identical to what would run on SPHEREx. Only the data source differs; this is stated transparently throughout.

1. Introduction

Supervised classification can only recover the classes it was trained on. For discovery — rare transients, blends, mis-calibrated sources, or genuinely new spectral types — the interesting objects are precisely those absent from any label set, so a classifier cannot surface them. Unsupervised anomaly detection takes the opposite stance: it models the empirical distribution of "typical" spectra and quantifies deviation from it, an approach that has repeatedly proven productive on spectroscopic surveys [4][3][7]. This is especially timely for SPHEREx, which measures a low-resolution spectrum ($0.75$–$5\,\mu$m, 102 channels) at every point on the sky and whose source catalogues are not yet released — untouched ground for a representation-learning / anomaly layer [1].

2. Data

The final sample is drawn from SDSS DR17 [5] SpecObj requiring zWarning=0, snMedian>5, sciencePrimary=1; a modular-hash ordering scatters the selection across plates and sub-surveys. Each spectrum is resampled by linear interpolation onto a common grid of 512 points uniform in $\log_{10}\lambda$ over 3850–9000 Å. The cached sample (data/spectra.npz) contains 4,990 spectra spanning $z\in[-0.002, 6.9]$:

classcountfraction
GALAXY2,83056.7%
STAR1,50030.1%
QSO66013.2%

3. Methods

Let a spectrum be a flux vector $x\in\mathbb{R}^{D}$ with $D=512$.

3.1 Preprocessing

To separate spectral shape from brightness, each spectrum is divided by its mean flux, $\tilde{x}=x/\bar{x}$; each wavelength bin is then standardised across the sample,

$$x^{\mathrm{std}}_{ij}=\frac{\tilde{x}_{ij}-\mu_j}{\sigma_j},\qquad \mu_j=\tfrac{1}{N}\sum_i \tilde{x}_{ij},\quad \sigma_j^2=\tfrac{1}{N}\sum_i(\tilde{x}_{ij}-\mu_j)^2.$$

3.2 Self-supervised autoencoder

An autoencoder $f_\theta=g\circ h$ compresses each standardised spectrum to a latent code $z=h(x)\in\mathbb{R}^{d}$ with $d=16$ and reconstructs $\hat{x}=g(z)$, trained purely self-supervised (no labels) to minimise the mean-squared reconstruction loss

$$\mathcal{L}(\theta)=\frac{1}{N}\sum_{i=1}^{N}\big\lVert x_i-\hat{x}_i\big\rVert_2^2.$$

The encoder is $512\to256\to128\to16$ and the decoder its mirror, with ReLU activations; Adam ($\text{lr}=10^{-3}$, weight decay $10^{-5}$), 200 epochs, batch size 256, on the Apple-MPS backend (seconds of wall-clock). Final train / validation MSE: 0.127 / 0.196 (standardised units).

3.3 Anomaly scores

Reconstruction error. $e_i=\tfrac{1}{D}\lVert x_i-\hat{x}_i\rVert_2^2$ — large $e_i$ means the model, trained on the typical population, cannot represent this spectrum (an outlier in data space).

Isolation Forest in latent space [2]. 300 random trees are fit to $\{z_i\}$; anomalies are isolated in fewer splits, i.e. shorter expected path length. The score is

$$s(z)=2^{-\,\mathbb{E}[h(z)]\,/\,c(n)},\qquad c(n)=2H(n-1)-\frac{2(n-1)}{n},$$

with $H$ the harmonic number; $s\to1$ indicates a strong anomaly.

Combined ranking. The two independent signals are converted to robust median/MAD $z$-scores and summed, $C_i=Z(e_i)+Z(s(z_i))$ with $Z(v)=(v-\mathrm{median}(v))/(1.4826\,\mathrm{MAD}(v))$. Using one detector in data space and one in latent space makes the ranking robust to the failure modes of either alone.

4. Results

4,990
SDSS spectra
16-D
latent space
100
top-2% anomalies
3.1×
emission-line enrichment

Emergent class structure (unsupervised). Although no labels were used in training, a t-SNE [6] projection of the 16-D latent space cleanly separates stars, galaxies, and quasars into distinct regions — direct evidence that the autoencoder learned a physically meaningful representation. The flagged anomalies concentrate in the sparse, peripheral regions (the outskirts of the quasar locus and the galaxy–quasar transition), exactly where an anomaly detector should place them.

t-SNE of the latent space coloured by SDSS class with anomalies highlighted
Figure 1. t-SNE projection of the 16-D autoencoder latent space, coloured by SDSS class (unused in training). The classes self-organise; the top-2% anomalies (red rings) and top-12 (gold stars) sit in sparse, peripheral regions.

What the anomalies are. Of the 100 top-ranked anomalies, 68 are galaxies and 32 are quasars, with no ordinary stars; 96 of 100 carry an emission-line or broad-line subclass (STARBURST, STARFORMING, BROADLINE, AGN), versus 30.9% of the full sample — a $3.1\times$ enrichment. This is physically sensible: the bulk population is dominated by smooth absorption/continuum spectra, so the objects most unlike the bulk are those with towering [O III], H$\alpha$, [O II] or broad quasar lines.

Top-ranked anomalous spectra versus the typical population
Figure 2. The 12 top-ranked anomalies (red) against the sample median and 16–84% band (grey). Their emission lines rise many times above the typical continuum and shift redward with redshift, as expected for extreme starburst/star-forming galaxies and broad-line quasars.
Reconstruction-error distribution
Figure 3. The sharply peaked reconstruction-error distribution with a long high-error tail. The 98th-percentile threshold (dashed) isolates that tail as the anomaly set.

4.1 Top-ranked anomalies

rankclasssubclass$z$recon. err
1GALAXYSTARBURST0.0645.29
2GALAXYSTARBURST0.0333.92
3GALAXYSTARBURST0.0844.03
7GALAXYSTARFORMING0.0131.60
12QSOBROADLINE0.4200.23

Full ranking of all 4,990 sources in outputs/anomaly_ranking.csv.

5. Caveats

6. Reproducibility

cd projects/p2-spherex-anomaly
python3 src/fetch_sdss.py            # query + download + resample -> data/spectra.npz
python3 src/anomaly_detect_local.py  # autoencoder + isolation forest -> figures + ranking

Deterministic (seeded); runs locally on Apple M2 / MPS.


References

  1. Doré, O., et al. (SPHEREx Collaboration). SPHEREx: An All-Sky Near-Infrared Spectral Survey. NASA/JPL. spherex.caltech.edu — IRSA: irsa.ipac.caltech.edu/Missions/spherex.html.
  2. Liu, F. T., Ting, K. M., & Zhou, Z.-H. (2008). Isolation Forest. IEEE ICDM, 413–422. doi:10.1109/ICDM.2008.17.
  3. Portillo, S. K. N., Parejko, J. K., Vergara, J. R., & Connolly, A. J. (2020). Dimensionality Reduction of SDSS Spectra with Variational Autoencoders. AJ 160, 45. doi:10.3847/1538-3881/ab9644.
  4. Baron, D., & Poznanski, D. (2017). The weirdest SDSS galaxies: results from an outlier detection algorithm. MNRAS 465, 4530–4555. doi:10.1093/mnras/stw3021.
  5. Abdurro'uf, et al. (2022). The Seventeenth Data Release of the Sloan Digital Sky Surveys (DR17). ApJS 259, 35. doi:10.3847/1538-4365/ac4414.
  6. van der Maaten, L., & Hinton, G. (2008). Visualizing Data using t-SNE. JMLR 9, 2579–2605.
  7. A search for anomalies in the DESI spectra with variational autoencoders (2025). arXiv:2506.17376.