How swagnercarena/paltax works
paltax is a Python scientific-computing package for simulation-based inference on strong gravitational lensing images. Its architecture is organized around three main concerns: configurable image simulation, neural posterior training, and reusable domain-specific physics models.
At a high level, the system looks like this:
1. A user selects a training configuration and an input/simulation configuration.
2. The training entrypoint loads those configs and starts an inference training run in a chosen work directory.
3. During training, data are generated on the fly by the simulation pipeline rather than loaded as a fixed dataset.
4. The generated images and labels flow into a neural inference model, including sequential neural posterior estimation variants.
5. Trained models and training outputs are written to the work directory.
Main runtime entrypoints
• paltax/main.py is the primary CLI-style entrypoint described in the README. It takes a work directory and a training config file.
• paltax/train.py, paltax/train_nf.py, and paltax/train_snpe.py appear to be the main training orchestration modules, with separate paths for generic training, normalizing flow training, and SNPE-specific training.
• paltax/hierarchical_inference.py suggests an additional inference mode for population-level or multi-stage inference beyond single-image posterior estimation.
Core subsystems
1. Configuration layer
This repo uses file-based configuration as a first-class architectural boundary.
• paltax/InputConfigs/ contains simulation/input configuration presets such as:
- baseline input configs
- PSF-focused configs
- weak-dark-matter-related configs
- “wide prior” or weighted variants
• paltax/TrainConfigs/ contains training preset definitions such as:
- constant, linear, exponential schedules
- different training scales like 50k, 500k, 5M
- model families such as MAF, ResNet, NPE, SNPE
- architecture-size variants like res_18, res_34, maf_large, etc.
Architecturally, these configs separate experiment definition from code. The codebase is designed so the same training pipeline can be reused with different science assumptions, priors, and network choices.
2. Physics and simulation layer
This is the domain-specific heart of the package. It is split into small modules, each representing a physical component of a strong-lensing scene.
• paltax/cosmology_utils.py
• paltax/lens_models.py
• paltax/source_models.py
• paltax/psf_models.py
• paltax/los.py for line-of-sight effects
• paltax/subhalos.py for substructure
• paltax/nfw_functions.py and paltax/power_law.py for mass-profile calculations
• paltax/power_spectrum.py for statistical structure generation
• paltax/image_simulation.py as the integrator that turns physical parameters into simulated images
This layer is the main boundary between astrophysical modeling and machine learning. It likely assembles:
• lens mass distribution
• source galaxy light profile
• point-spread function
• line-of-sight and subhalo perturbations
• cosmology-dependent distance and scale calculations
The output of this layer is synthetic imaging data suitable for training inference networks.
3. Data input and batching layer
• paltax/input_pipeline.py is the interface between simulation and training. Given the repository’s emphasis on on-the-fly generation, this module likely handles:
- constructing batches
- sampling parameters from priors
- calling the simulation code
- packaging image/parameter pairs into training-ready tensors or arrays
This is an important architectural seam: it decouples the scientific simulator from the model training loop.
4. Learning and inference layer
This layer implements the neural inference machinery.
• paltax/models.py is the umbrella model definition layer.
• paltax/maf_flow.py is a normalizing-flow implementation focused on Masked Autoregressive Flow style density estimation.
• paltax/train_nf.py suggests support for training normalizing-flow models directly.
• paltax/train_snpe.py suggests Sequential Neural Posterior Estimation training, which is a key focus of the project.
• paltax/hierarchical_inference.py likely supports inference over multiple observations or a higher-level latent structure.
The repository’s architecture appears to support multiple inference families under one training framework:
• direct posterior estimation
• normalizing-flow density modeling
• sequential posterior refinement
• possibly hierarchical extensions
5. Shared utilities
• paltax/utils.py contains shared helpers used across simulation, training, and evaluation.
• The corresponding tests show that this module is part of the stable internal API.
6. Test and validation layer
The repository is heavily unit-tested, with tests co-located by module:
• *_test.py files exist for most major modules
There are also bundled test fixtures under:
• paltax/test_files/
• datasets/hst_psf/
These fixtures are part of the reproducibility/verification boundary. They allow deterministic testing of simulation outputs, PSF handling, and training-related behavior without requiring external downloads.
Data and external assets
The repo includes small data assets that materially affect the architecture:
• datasets/hst_psf/ contains empirical HST PSF arrays and a test catalog.
• notebooks/data/slacs_catalog.csv and related notebook data are used for exploratory analysis or demonstration.
• paltax/test_files/*.npy, .npz, .h5 are test fixtures.
These assets indicate the package is not purely algorithmic; it also ships curated scientific inputs and reference outputs.
Development and distribution tooling
• setup.py, MANIFEST.in, and requirements.txt show the package is meant for installation and distribution via pip.
• paltax/requirements.txt likely pins package-level runtime dependencies.
• .github/workflows/ci.yml indicates GitHub Actions-based continuous integration.
• pytest.ini and the many test modules show pytest is the main test runner.
Notebooks and documentation are non-runtime but important for user workflows
• notebooks/GenerateImages.ipynb is a tutorial for generating images from an input config.
• notebooks/papers/2404_14487/ contains notebook code used to regenerate publication figures.
• docs/figures/ holds documentation assets.
These sit outside the core runtime path but demonstrate the intended workflow: experiment via configs, generate simulations, train inference models, then analyze results in notebooks.
A useful mental model for an architecture diagram
You can think of paltax as four stacked layers:
• Experiment definition layer: InputConfigs, TrainConfigs
• Physics simulation layer: cosmology, lenses, sources, PSF, LOS, subhalos, image synthesis
• Learning layer: training orchestration, normalizing flows, SNPE, hierarchical inference
• Packaging/validation layer: CLI entrypoint, tests, datasets, notebooks, CI
The most important directional data flow is:
config files → simulator/input pipeline → synthetic images and latent parameters → training/inference models → outputs in a work directory.
The main architectural boundary is between the physical simulator and the neural inference code. The simulator produces scientifically meaningful training examples; the learning layer consumes them without embedding astrophysical details directly into the training logic.
Main entry points: image_simulation.py Image sim · input_pipeline.py Input pipe · train.py Training · main.py CLI entry
Components 18
Configs
Experiment presets that define simulation assumptions and training behavior.
- Input configs · Simulation and prior presets for different science assumptions and input variants.
paltax/InputConfigs - Train configs · Training presets spanning schedules, model families, and scale variants.
paltax/TrainConfigs
Physics
Domain models that generate strong-lensing images from astrophysical parameters.
- Cosmology · Distance and scale calculations used by the simulator.
paltax/cosmology_utils.py - Lens model · Mass-model calculations for the foreground lens.
paltax/lens_models.py - Source model · Background source-light models for simulated scenes.
paltax/source_models.py - PSF model · Point-spread-function handling for imaging forward models.
paltax/psf_models.py - Line of sight · Line-of-sight perturbations and propagation effects.
paltax/los.py - Subhalos · Substructure modeling for perturbed lensing scenes.
paltax/subhalos.py - Image sim · Integrator that assembles physics components into synthetic images.
paltax/image_simulation.py
Learning
Training and inference code that consumes simulated batches.
Runtime
Entrypoints, orchestration, and shared utilities.
- CLI entry · Primary command-style entrypoint that launches runs from a work directory and config.
paltax/main.py - Training · Generic training orchestration for simulation-based inference.
paltax/train.py - NF train · Training path specialized for normalizing-flow models.
paltax/train_nf.py - SNPE train · Sequential neural posterior estimation training path.
paltax/train_snpe.py - Hierarchical · Higher-level inference mode for population or multi-stage settings.
paltax/hierarchical_inference.py - Utils · Cross-cutting helper functions used across the package.
paltax/utils.py - Input pipe · On-the-fly batch construction from priors and simulation calls.
paltax/input_pipeline.py
Connections 20
- CLI entry → Training: launches · Main entrypoint starts the generic training pipeline.
- CLI entry → NF train: launches · Main entrypoint can route to the normalizing-flow training path.
- CLI entry → SNPE train: launches · Main entrypoint can route to the SNPE training path.
- Training → Input pipe: uses · Training consumes generated batches from the input pipeline.
- NF train → MAF flow: trains · NF training optimizes the flow-based density estimator.
- SNPE train → Models: trains · SNPE training uses the shared model layer for inference networks.
- Training → Models: uses · Generic training wires simulation outputs into neural models.
- Input pipe → Image sim: calls · The input pipeline generates synthetic images on the fly.
- Input pipe → Input configs: reads · Pipeline behavior is driven by simulation/input presets.
- Training → Train configs: reads · Training orchestration is configured by training presets.
- Image sim → Cosmology: depends on · Simulation uses cosmology calculations for physical scaling.
- Image sim → Lens model: assembles · The simulator includes the lens mass model.
- Image sim → Source model: assembles · The simulator includes the source-light model.
- Image sim → PSF model: assembles · The simulator convolves scenes with a PSF model.
- Image sim → Line of sight: assembles · The simulator can include line-of-sight effects.
- Image sim → Subhalos: assembles · The simulator can include subhalo perturbations.
- Image sim → Utils: uses · Simulation relies on shared helper logic.
- SNPE train → Input pipe: uses · SNPE also consumes on-the-fly simulated batches.
- Hierarchical → Models: uses · Hierarchical inference reuses the core model stack.
- Hierarchical → Input pipe: uses · Hierarchical inference depends on the same data-generation seam.