How rbonnetguerrini/ml4transients works
1. What this repo is
ML4transients is an end-to-end Python package for transient-vs-bogus classification, centered on LSST data products. It is not a single model repo; it spans data generation, cutout/lightcurve preparation, model training, inference, and post-hoc evaluation/interpretability.
2. Core pipeline boundary
The architecture is organized as a pipeline over astronomy data artifacts:
raw LSST difference-image/visit data → injected catalogs and cutouts → indexed lightcurve datasets and cross-match annotations → PyTorch training datasets → trained CNN/ensemble models → batch inference and evaluation outputs.
Most steps are driven by YAML configs and shell submit scripts.
3. Package layout in src/ML4transients
The library is split by subsystem:
- data_preparation: crossmatch, cutouts, lightcurves
- data_access: dataset loading and lazy access
- training: models, losses, trainers, PyTorch dataset wrappers
- evaluation: inference, metrics, visualizations, interpretability, lightcurve labeling
- injection: synthetic transient catalog generation
- utils.py: shared helpers
This mirrors the operational workflow and makes the main architecture easy to diagram as separate stages.
4. Data preparation subsystem
This is the heaviest external-boundary layer and depends on LSST-distributed tooling. It includes:
- run_cutout.py and batch submit scripts to produce image cutouts/features from selected visits
- run_lightcurves.py to extract and index time-series data into patch-based HDF5 files
- run_crossmatch.py to annotate/filter against external catalogs such as Gaia/MilliQuas
- noise perturbation and batch orchestration utilities for splitting large collections across jobs
The output is structured for downstream ML consumption, not for direct human use.
5. Injection subsystem
scripts/injection and src/ML4transients/injection generate synthetic source catalogs for training. The repo supports both host-associated and hostless transients, with band-specific catalogue generation and batch submission. This stage feeds the LSST pipeline and is a distinct source of supervised labels in an otherwise weakly/unsupervised data regime.
6. Dataset access boundary
DatasetLoader in data_access is the main abstraction for consuming prepared data. It lazily loads cutouts, lightcurves, features, and inference outputs from disk, and builds object/source ID indexing for lookup. This is the boundary between storage formats (HDF5, indices, per-visit artifacts) and model-facing code.
7. Training subsystem
Training is PyTorch-based (torch 2.0 per README) and includes multiple strategies:
- standard CNN training
- deep ensemble training
- co-teaching and asymmetric co-teaching
- stochastic co-teaching variants
- multichannel variants that use difference images plus coadded images
The models.py, losses.py, trainers.py, and pytorch_dataset.py modules separate network definition, objective functions, training loop logic, and data feeding. Hyperparameter search is supported through Bayesian optimization configs.
8. Inference and evaluation subsystem
evaluation/inference.py and scripts/evaluation/run_inference.py run model prediction over prepared data, often in batch mode. Evaluation extends beyond classification metrics into:
- UMAP/embedding-based interpretability
- uncertainty analysis
- visualizations for cutouts and lightcurves
- object-subset evaluation via ID lists
Results are written to disk and can be re-ingested for further analysis.
9. Lightcurve-specific workflow and SuperNNova integration
A separate lightcurve inference path exists around SuperNNova (SNN). The repo converts HDF5 patch-based lightcurves to CSV, runs SNN in a different environment, then writes ensemble predictions back into the original HDF5 files. This is a clear cross-system boundary: ML4transients manages the data staging and result reintegration, while SNN performs the actual sequence-model inference.
10. Operational model: scripts, configs, and batch jobs
The repo is designed for HPC/cluster execution rather than a single interactive app. scripts/ contains job submission wrappers for SLURM-style batch execution, and configs/ defines stage-specific YAMLs for data preparation, injection, training, and evaluation. The README explicitly distinguishes LSST-distrib environments for pipeline-related steps from a normal conda/PyTorch environment for training and evaluation.
11. Artifacts and persistence
The main persistent outputs are HDF5 datasets, CSV exports for SNN, config-derived intermediate files, model checkpoints/weights, TensorBoard logs, and HTML/lightcurve visualizations. This means the architecture is file- and job-oriented, with disk-based handoffs between subsystems rather than a central service or database.
12. How to diagram it
A good architecture diagram would show four main lanes:
data generation/preparation, ML training, inference/evaluation, and external catalog/SNN integration.
The key interfaces are the on-disk dataset formats, the DatasetLoader abstraction, and the config-driven batch scripts that connect each stage.
Main entry points: ML4transients ML4transients · inference.py Inference · scripts Batch scripts · dataset_loader.py Dataset loader
Components 28
Data prep
Raw LSST data is turned into cutouts, lightcurves, and crossmatch annotations.
- Crossmatch · Matches and filters sources against external catalogs.
src/ML4transients/data_preparation/crossmatch.py - Cutouts · Builds cutouts and features from selected visits.
src/ML4transients/data_preparation/cutouts.py - Lightcurves · Extracts and indexes patch-based lightcurve datasets.
src/ML4transients/data_preparation/lightcurves.py - Run cutout · Batch entrypoint for cutout generation.
scripts/data_preparation/run_cutout.py - Run lc · Batch entrypoint for lightcurve extraction.
scripts/data_preparation/run_lightcurves.py - Run crossmatch · Batch entrypoint for catalog crossmatching.
scripts/data_preparation/run_crossmatch.py - Noise perturb · Creates noisy variants and splits work across jobs.
scripts/data_preparation/compute_noise_perturbation.py
Training
PyTorch models, losses, datasets, and training loops.
- Models · CNN and ensemble model definitions, including multichannel variants.
src/ML4transients/training/models.py - Losses · Standard, co-teaching, and ensemble training losses.
src/ML4transients/training/losses.py - PyTorch dataset · Feeds prepared artifacts into the training loop.
src/ML4transients/training/pytorch_dataset.py - Trainers · Implements standard, ensemble, and co-teaching training strategies.
src/ML4transients/training/trainers.py - Train model · Command-line training runner for configured experiments.
scripts/training/train_model.py
Evaluation
Inference, metrics, interpretability, and visualization outputs.
- Inference · Runs batch prediction over prepared data and writes outputs.
src/ML4transients/evaluation/inference.py - Metrics · Computes classification and subset evaluation metrics.
src/ML4transients/evaluation/metrics.py - Interpretability · Embeddings, UMAP-style views, and uncertainty analysis.
src/ML4transients/evaluation/interpretability.py - Visualizations · Cutout and result visualizations for inspection.
src/ML4transients/evaluation/visualizations.py - LC labels · Generates lightcurve-specific labels and outputs.
src/ML4transients/evaluation/lc_labels.py - Run eval · Batch evaluation driver for metrics and outputs.
scripts/evaluation/run_evaluation.py - SNN convert · Converts HDF5 lightcurves to CSV for SuperNNova.
scripts/data_preparation/SNN/convert_lc_for_snn.py - SNN infer · Runs SuperNNova inference in a separate environment.
scripts/data_preparation/SNN/infer_snn.py - SNN save · Writes SNN predictions back into original HDF5 artifacts.
scripts/data_preparation/SNN/save_inference_snn.py
Injection
Synthetic transient catalog generation and batch submission.
- Injection catalog · Generates synthetic transient catalogs for training labels.
src/ML4transients/injection/injection.py - Gen injection · Creates band-specific injection catalogues for batch jobs.
scripts/injection/gen_injection_catalogue.py
Other components
- ML4transients · Top-level library namespace for the pipeline.
src/ML4transients - Utils · Common helper functions used across subsystems.
src/ML4transients/utils.py - Dataset loader · Lazily loads prepared artifacts and builds ID-based lookup.
src/ML4transients/data_access/dataset_loader.py - Configs · Stage-specific YAML configuration files for batch execution.
configs - Batch scripts · SLURM-style wrappers and orchestration scripts across pipeline stages.
scripts
Connections 38
- ML4transients → Utils: shared helpers · Common utilities support all subsystems.
- ML4transients → Crossmatch: data prep · Namespace contains data preparation modules.
- ML4transients → Cutouts: data prep · Namespace contains data preparation modules.
- ML4transients → Lightcurves: data prep · Namespace contains data preparation modules.
- ML4transients → Dataset loader: data access · Namespace contains dataset loading boundary.
- ML4transients → Models: training · Namespace contains model code.
- ML4transients → Losses: training · Namespace contains loss functions.
- ML4transients → PyTorch dataset: training · Namespace contains dataset wrappers.
- ML4transients → Trainers: training · Namespace contains training loops.
- ML4transients → Inference: evaluation · Namespace contains inference and scoring.
- ML4transients → Metrics: evaluation · Namespace contains metrics and analysis.
- ML4transients → Injection catalog: injection · Namespace contains synthetic catalog generation.
- Run cutout → Cutouts: calls · Script drives cutout generation.
- Run lc → Lightcurves: calls · Script drives lightcurve extraction.
- Run crossmatch → Crossmatch: calls · Script drives catalog annotation.
- Noise perturb → Cutouts: augments · Noise utilities operate on prepared image artifacts.
- Dataset loader → PyTorch dataset: feeds · Prepared artifacts are consumed by training datasets.
- Dataset loader → Inference: feeds · Prepared artifacts are consumed by batch inference.
- PyTorch dataset → Models: batches for · Dataset wrapper supplies model inputs.
- Models → Losses: optimized by · Model training uses configured losses.
- Trainers → Models: trains · Training loops fit the models.
- Train model → Trainers: launches · CLI entrypoint launches configured training.
- Run eval → Inference: launches · Batch driver runs inference and evaluation.
- Inference → Metrics: scores · Inference outputs are summarized by metrics.
- Inference → Interpretability: analyzes · Inference outputs feed interpretability analysis.
- Inference → Visualizations: renders · Inference outputs feed visual reports.
- Inference → LC labels: labels · Produces lightcurve-specific labels.
- Gen injection → Injection catalog: generates · Script creates synthetic injection catalogs.
- SNN convert → Dataset loader: reads · Reads prepared lightcurve artifacts for export.
- SNN convert → SNN infer: stages for · CSV export prepares external SNN inference.
- SNN infer → SNN save: returns to · External predictions are reintegrated into repository artifacts.
- Configs → Run cutout: configures · YAML configs drive batch prep stages.
- Configs → Train model: configures · YAML configs drive training runs.
- Configs → Run eval: configures · YAML configs drive evaluation runs.
- Batch scripts → Run cutout: orchestrates · Repository scripts orchestrate HPC execution.
- Batch scripts → Train model: orchestrates · Repository scripts orchestrate HPC execution.
- Batch scripts → Run eval: orchestrates · Repository scripts orchestrate HPC execution.
- Batch scripts → Gen injection: orchestrates · Repository scripts orchestrate HPC execution.