# swagnercarena/paltax architecture

> Architecture diagram of the GitHub repository swagnercarena/paltax, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/swagnercarena/paltax
- Repository: https://github.com/swagnercarena/paltax

## Overview

`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: [`paltax/image_simulation.py`](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/image_simulation.py) (Image sim), [`paltax/input_pipeline.py`](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/input_pipeline.py) (Input pipe), [`paltax/train.py`](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/train.py) (Training), [`paltax/main.py`](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/main.py) (CLI entry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph "Configs"
  node_input_cfg["Input configs<br/>preset set"]
  node_train_cfg["Train configs<br/>preset set"]
end

subgraph "Physics"
  node_cosmo["Cosmology<br/>physics module<br/>[cosmology_utils.py]"]
  node_lens["Lens model<br/>physics module<br/>[lens_models.py]"]
  node_source["Source model<br/>physics module<br/>[source_models.py]"]
  node_psf["PSF model<br/>physics module<br/>[psf_models.py]"]
  node_los["Line of sight<br/>physics module<br/>[los.py]"]
  node_subhalos["Subhalos<br/>physics module<br/>[subhalos.py]"]
  node_image_sim["Image sim<br/>simulator"]
end

subgraph "Learning"
  node_models["Models<br/>model hub<br/>[models.py]"]
  node_maf["MAF flow<br/>density model<br/>[maf_flow.py]"]
end

subgraph "Runtime"
  node_main["CLI entry<br/>entrypoint<br/>[main.py]"]
  node_train["Training<br/>orchestrator<br/>[train.py]"]
  node_train_nf["NF train<br/>orchestrator<br/>[train_nf.py]"]
  node_train_snpe["SNPE train<br/>orchestrator<br/>[train_snpe.py]"]
  node_hier["Hierarchical<br/>inference mode"]
  node_utils["Utils<br/>shared helpers<br/>[utils.py]"]
  node_input_pipe["Input pipe<br/>data pipeline<br/>[input_pipeline.py]"]
end

subgraph "Assets"
end

node_main -->|"launches"| node_train
node_main -->|"launches"| node_train_nf
node_main -->|"launches"| node_train_snpe
node_train -->|"uses"| node_input_pipe
node_train_nf -->|"trains"| node_maf
node_train_snpe -->|"trains"| node_models
node_train -->|"uses"| node_models
node_input_pipe -->|"calls"| node_image_sim
node_input_pipe -->|"reads"| node_input_cfg
node_train -->|"reads"| node_train_cfg
node_image_sim -->|"depends on"| node_cosmo
node_image_sim -->|"assembles"| node_lens
node_image_sim -->|"assembles"| node_source
node_image_sim -->|"assembles"| node_psf
node_image_sim -->|"assembles"| node_los
node_image_sim -->|"assembles"| node_subhalos
node_image_sim -->|"uses"| node_utils
node_train_snpe -->|"uses"| node_input_pipe
node_hier -->|"uses"| node_models
node_hier -->|"uses"| node_input_pipe

click node_main "https://github.com/swagnercarena/paltax/blob/main/paltax/main.py"
click node_train "https://github.com/swagnercarena/paltax/blob/main/paltax/train.py"
click node_train_nf "https://github.com/swagnercarena/paltax/blob/main/paltax/train_nf.py"
click node_train_snpe "https://github.com/swagnercarena/paltax/blob/main/paltax/train_snpe.py"
click node_hier "https://github.com/swagnercarena/paltax/blob/main/paltax/hierarchical_inference.py"
click node_utils "https://github.com/swagnercarena/paltax/blob/main/paltax/utils.py"
click node_input_cfg "https://github.com/swagnercarena/paltax/tree/main/paltax/InputConfigs"
click node_train_cfg "https://github.com/swagnercarena/paltax/tree/main/paltax/TrainConfigs"
click node_cosmo "https://github.com/swagnercarena/paltax/blob/main/paltax/cosmology_utils.py"
click node_lens "https://github.com/swagnercarena/paltax/blob/main/paltax/lens_models.py"
click node_source "https://github.com/swagnercarena/paltax/blob/main/paltax/source_models.py"
click node_psf "https://github.com/swagnercarena/paltax/blob/main/paltax/psf_models.py"
click node_los "https://github.com/swagnercarena/paltax/blob/main/paltax/los.py"
click node_subhalos "https://github.com/swagnercarena/paltax/blob/main/paltax/subhalos.py"
click node_image_sim "https://github.com/swagnercarena/paltax/blob/main/paltax/image_simulation.py"
click node_input_pipe "https://github.com/swagnercarena/paltax/blob/main/paltax/input_pipeline.py"
click node_models "https://github.com/swagnercarena/paltax/blob/main/paltax/models.py"
click node_maf "https://github.com/swagnercarena/paltax/blob/main/paltax/maf_flow.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_input_cfg,node_train_cfg toneBlue
class node_cosmo,node_lens,node_source,node_psf,node_los,node_subhalos,node_image_sim toneAmber
class node_models,node_maf toneMint
class node_main,node_train,node_train_nf,node_train_snpe,node_hier,node_utils,node_input_pipe toneRose
```

## Components

### Configs

Experiment presets that define simulation assumptions and training behavior.

- [Input configs](https://github.com/swagnercarena/paltax/tree/HEAD/paltax/InputConfigs): preset set · Simulation and prior presets for different science assumptions and input variants. · `paltax/InputConfigs`
- [Train configs](https://github.com/swagnercarena/paltax/tree/HEAD/paltax/TrainConfigs): preset set · Training presets spanning schedules, model families, and scale variants. · `paltax/TrainConfigs`

### Physics

Domain models that generate strong-lensing images from astrophysical parameters.

- [Cosmology](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/cosmology_utils.py): physics module · Distance and scale calculations used by the simulator. · `paltax/cosmology_utils.py`
- [Lens model](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/lens_models.py): physics module · Mass-model calculations for the foreground lens. · `paltax/lens_models.py`
- [Source model](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/source_models.py): physics module · Background source-light models for simulated scenes. · `paltax/source_models.py`
- [PSF model](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/psf_models.py): physics module · Point-spread-function handling for imaging forward models. · `paltax/psf_models.py`
- [Line of sight](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/los.py): physics module · Line-of-sight perturbations and propagation effects. · `paltax/los.py`
- [Subhalos](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/subhalos.py): physics module · Substructure modeling for perturbed lensing scenes. · `paltax/subhalos.py`
- [Image sim](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/image_simulation.py): simulator · Integrator that assembles physics components into synthetic images. · `paltax/image_simulation.py`

### Learning

Training and inference code that consumes simulated batches.

- [Models](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/models.py): model hub · Umbrella neural model definitions and shared architecture wiring. · `paltax/models.py`
- [MAF flow](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/maf_flow.py): density model · Masked autoregressive flow implementation for posterior density estimation. · `paltax/maf_flow.py`

### Runtime

Entrypoints, orchestration, and shared utilities.

- [CLI entry](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/main.py): entrypoint · Primary command-style entrypoint that launches runs from a work directory and config. · `paltax/main.py`
- [Training](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/train.py): orchestrator · Generic training orchestration for simulation-based inference. · `paltax/train.py`
- [NF train](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/train_nf.py): orchestrator · Training path specialized for normalizing-flow models. · `paltax/train_nf.py`
- [SNPE train](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/train_snpe.py): orchestrator · Sequential neural posterior estimation training path. · `paltax/train_snpe.py`
- [Hierarchical](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/hierarchical_inference.py): inference mode · Higher-level inference mode for population or multi-stage settings. · `paltax/hierarchical_inference.py`
- [Utils](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/utils.py): shared helpers · Cross-cutting helper functions used across the package. · `paltax/utils.py`
- [Input pipe](https://github.com/swagnercarena/paltax/blob/HEAD/paltax/input_pipeline.py): data pipeline · On-the-fly batch construction from priors and simulation calls. · `paltax/input_pipeline.py`

## Connections

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