# benbillot/spatial_regularisation architecture

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

- Interactive diagram: https://gitdiagram.com/benbillot/spatial_regularisation
- Repository: https://github.com/benbillot/spatial_regularisation

## Overview

1. **Repository purpose**
   - This is a Python 3.12 research codebase for **keypoint-based image registration** with **spatial regularisation**.
   - The core goal is to train and evaluate unsupervised **affine or rigid** registration models, then apply them to either **pairwise scans** or **time series**.

2. **Top-level split: reusable package vs runnable experiments**
   - `spatial_regularisation/` contains the reusable library code.
   - `scripts/` contains experiment-specific entry points showing how the library is used for two application settings:
     - **foetal time-series rigid tracking**
     - **adult brain MRI affine registration**

3. **Main execution paths**
   - The repo has two major runtime flows:
     - **Training flow:** data loading → augmentation → model forward pass → keypoint/moment computation → registration + regularisation losses → optimisation.
     - **Inference flow:** load trained model → run prediction on scan pairs or time series → generate aligned outputs and registration results.

4. **Model subsystem**
   - `networks.py` and `equivariant_unet_physical_units.py` define the neural registration models and related building blocks.
   - The naming suggests the code supports architectures that produce keypoints or transformation parameters while being aware of **physical voxel spacing / units**, which is important for geometry-consistent registration.
   - `__init__.py` exposes the package as a single importable module boundary.

5. **Training subsystem**
   - `training.py` is the orchestration layer for optimisation.
   - It is responsible for wiring together:
     - input pipelines
     - model instantiation
     - loss computation
     - training loops and evaluation during training
   - The readme indicates the training code is specifically for **unsupervised** registration, so the supervision signal comes from image similarity plus the proposed regularisers rather than ground-truth transforms.

6. **Prediction / evaluation subsystem**
   - `predict.py` handles standard pairwise prediction and testing on **fixed/moving scan pairs**.
   - `predict_time_series.py` handles longitudinal data, where each later frame is aligned back to the **first frame**.
   - This is a distinct architectural boundary: pairwise registration and temporal tracking are implemented separately because their input structure and output aggregation differ.

7. **Loss and regularisation subsystem**
   - `losses.py` implements the three proposed **spatial regularisation terms** from the paper.
   - These losses are a central architectural component, because they sit between the registration model and optimisation, shaping the learned keypoints/transforms.
   - They are complemented by `spatial_moments.py`, which computes the **first- and second-order moments** of volumetric fields used by those losses.

8. **Geometry and measurement layer**
   - `spatial_moments.py` is the main geometric utility layer.
   - Its role is to turn volumetric outputs into interpretable spatial statistics, which are then consumed by the regularisation terms.
   - This is the main bridge between image-space model outputs and the paper’s moment-based regularisation objective.

9. **Data handling and augmentation**
   - `loaders.py` provides dataset ingestion and batching.
   - `augmenters.py` provides data augmentation for training.
   - Together they form the input boundary of the system: raw scans/time series enter here before any model or loss code sees them.

10. **Utility layer**
   - `utils.py` likely contains shared helpers used across training, inference, and data handling.
   - This module acts as a cross-cutting support boundary rather than a primary subsystem.

11. **Experiment scripts as concrete application wiring**
   - `scripts/training_adult.py` and `scripts/predict_adult.py` show the adult MRI affine-registration pipeline.
   - `scripts/training_foetal.py` and `scripts/predict_foetal.py` show the foetal rigid tracking pipeline.
   - These scripts are important for architecture diagrams because they demonstrate how the same core library is configured differently for two datasets and two motion models.

12. **Data flow summary**
   - Input data enters through loaders, optionally passes through augmentation, then reaches the model.
   - The model produces registration-relevant outputs that are interpreted via spatial moments.
   - The losses combine image/registration objectives with the proposed spatial regularisation.
   - Inference flows bypass optimisation and instead return predicted alignments for pairwise or temporal evaluation.

13. **Technology and runtime boundaries**
   - The code is pure Python and depends on the packages listed in `requirements.txt`.
   - No external service, database, or web layer is implied by the repository structure.
   - The architecture is therefore a **local research ML pipeline**: training and inference are script-driven and package-backed, not service-oriented.

14. **What to show in an architecture diagram**
   - The key boxes are: **scripts → loaders/augmenters → networks → spatial moments/losses → training/predict**.
   - The important conceptual boundaries are: **data ingestion**, **modeling**, **geometry/regularisation**, and **execution mode** (training vs prediction, pairwise vs time series).

Main entry points: [`spatial_regularisation/training.py`](https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/training.py) (Training), [`spatial_regularisation/predict.py`](https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/predict.py) (Predict), [`spatial_regularisation/networks.py`](https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/networks.py) (Networks), [`spatial_regularisation/predict_time_series.py`](https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/predict_time_series.py) (Time-series predict)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_scripts["Scripts"]
  node_scripts_training_adult["Adult train<br/>entrypoint<br/>[training_adult.py]"]
  node_scripts_predict_adult["Adult predict<br/>entrypoint<br/>[predict_adult.py]"]
  node_scripts_training_foetal["Foetal train<br/>entrypoint<br/>[training_foetal.py]"]
  node_scripts_predict_foetal["Foetal predict<br/>entrypoint<br/>[predict_foetal.py]"]
end

subgraph group_core["Core package"]
  node_spatial_regularisation_init["Package API<br/>module boundary<br/>[__init__.py]"]
  node_loaders[("Loaders<br/>data pipeline<br/>[loaders.py]")]
  node_augmenters(("Augmenters<br/>augmentation<br/>[augmenters.py]"))
  node_training["Training<br/>orchestration<br/>[training.py]"]
  node_predict["Predict<br/>inference<br/>[predict.py]"]
  node_predict_time_series["Time-series predict<br/>inference"]
  node_networks["Networks<br/>model<br/>[networks.py]"]
  node_equivariant_unet_physical_units["Physical U-Net<br/>model block"]
  node_losses["Losses<br/>regularisation<br/>[losses.py]"]
  node_spatial_moments{{"Moments<br/>geometry<br/>[spatial_moments.py]"}}
  node_utils(("Utils<br/>shared helpers<br/>[utils.py]"))
end

node_scripts_training_adult -->|"configures"| node_training
node_scripts_predict_adult -->|"runs"| node_predict
node_scripts_training_foetal -->|"configures"| node_training
node_scripts_predict_foetal -->|"runs"| node_predict_time_series
node_training -->|"reads data"| node_loaders
node_training -->|"augments"| node_augmenters
node_training -->|"optimises"| node_networks
node_training -->|"computes"| node_losses
node_training -->|"measures"| node_spatial_moments
node_training -->|"uses"| node_utils
node_predict -->|"reads data"| node_loaders
node_predict -->|"infers"| node_networks
node_predict -->|"uses"| node_utils
node_predict_time_series -->|"reads data"| node_loaders
node_predict_time_series -->|"infers"| node_networks
node_predict_time_series -->|"uses"| node_utils
node_losses -->|"depends on"| node_spatial_moments
node_networks -->|"builds from"| node_equivariant_unet_physical_units
node_spatial_regularisation_init -.->|"exposes"| node_networks
node_spatial_regularisation_init -.->|"exposes"| node_training
node_spatial_regularisation_init -.->|"exposes"| node_predict

click node_scripts_training_adult "https://github.com/benbillot/spatial_regularisation/blob/main/scripts/training_adult.py"
click node_scripts_predict_adult "https://github.com/benbillot/spatial_regularisation/blob/main/scripts/predict_adult.py"
click node_scripts_training_foetal "https://github.com/benbillot/spatial_regularisation/blob/main/scripts/training_foetal.py"
click node_scripts_predict_foetal "https://github.com/benbillot/spatial_regularisation/blob/main/scripts/predict_foetal.py"
click node_spatial_regularisation_init "https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/__init__.py"
click node_loaders "https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/loaders.py"
click node_augmenters "https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/augmenters.py"
click node_training "https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/training.py"
click node_predict "https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/predict.py"
click node_predict_time_series "https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/predict_time_series.py"
click node_networks "https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/networks.py"
click node_equivariant_unet_physical_units "https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/equivariant_unet_physical_units.py"
click node_losses "https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/losses.py"
click node_spatial_moments "https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/spatial_moments.py"
click node_utils "https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/utils.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_scripts_training_adult,node_scripts_predict_adult,node_scripts_training_foetal,node_scripts_predict_foetal toneBlue
class node_spatial_regularisation_init,node_loaders,node_augmenters,node_training,node_predict,node_predict_time_series,node_networks,node_equivariant_unet_physical_units,node_losses,node_spatial_moments,node_utils toneAmber
```

## Components

### Scripts

Experiment entry points for adult and foetal workflows

- [Adult train](https://github.com/benbillot/spatial_regularisation/blob/main/scripts/training_adult.py): entrypoint · Adult brain MRI affine-registration training script · `scripts/training_adult.py`
- [Adult predict](https://github.com/benbillot/spatial_regularisation/blob/main/scripts/predict_adult.py): entrypoint · Adult brain MRI affine-registration inference script · `scripts/predict_adult.py`
- [Foetal train](https://github.com/benbillot/spatial_regularisation/blob/main/scripts/training_foetal.py): entrypoint · Foetal time-series rigid tracking training script · `scripts/training_foetal.py`
- [Foetal predict](https://github.com/benbillot/spatial_regularisation/blob/main/scripts/predict_foetal.py): entrypoint · Foetal time-series rigid tracking inference script · `scripts/predict_foetal.py`

### Core package

Reusable library for loading, modeling, losses, and execution

- [Package API](https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/__init__.py): module boundary · Public package boundary for the reusable registration code · `spatial_regularisation/__init__.py`
- [Loaders](https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/loaders.py): data pipeline · Dataset ingestion and batching for scans and sequences · `spatial_regularisation/loaders.py`
- [Augmenters](https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/augmenters.py): augmentation · Training-time data augmentation · `spatial_regularisation/augmenters.py`
- [Training](https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/training.py): orchestration · Unsupervised optimisation and training loop orchestration · `spatial_regularisation/training.py`
- [Predict](https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/predict.py): inference · Pairwise registration prediction and evaluation · `spatial_regularisation/predict.py`
- [Time-series predict](https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/predict_time_series.py): inference · Longitudinal alignment back to the first frame · `spatial_regularisation/predict_time_series.py`
- [Networks](https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/networks.py): model · Registration model definitions and building blocks · `spatial_regularisation/networks.py`
- [Physical U-Net](https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/equivariant_unet_physical_units.py): model block · Geometry-aware U-Net variant using physical voxel spacing · `spatial_regularisation/equivariant_unet_physical_units.py`
- [Losses](https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/losses.py): regularisation · Spatial regularisation terms and training objectives · `spatial_regularisation/losses.py`
- [Moments](https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/spatial_moments.py): geometry · First- and second-order moments for volumetric fields · `spatial_regularisation/spatial_moments.py`
- [Utils](https://github.com/benbillot/spatial_regularisation/blob/main/spatial_regularisation/utils.py): shared helpers · Cross-cutting helper functions · `spatial_regularisation/utils.py`

## Connections

- Adult train → Training: configures · Adult training script wires the reusable training pipeline
- Adult predict → Predict: runs · Adult inference script uses pairwise prediction
- Foetal train → Training: configures · Foetal training script wires the reusable training pipeline
- Foetal predict → Time-series predict: runs · Foetal inference script uses longitudinal prediction
- Training → Loaders: reads data · Training starts from dataset ingestion and batching
- Training → Augmenters: augments · Training applies augmentation before optimisation
- Training → Networks: optimises · Training instantiates and updates the registration model
- Training → Losses: computes · Training combines image and spatial regularisation losses
- Training → Moments: measures · Training derives spatial statistics for regularisation
- Training → Utils: uses · Training shares helpers across the pipeline
- Predict → Loaders: reads data · Pairwise prediction consumes fixed/moving scan pairs
- Predict → Networks: infers · Pairwise prediction applies the trained model
- Predict → Utils: uses · Prediction reuses shared helpers
- Time-series predict → Loaders: reads data · Time-series prediction consumes longitudinal sequences
- Time-series predict → Networks: infers · Time-series prediction applies the trained model
- Time-series predict → Utils: uses · Time-series prediction reuses shared helpers
- Losses → Moments: depends on · Regularisation terms consume moment computations
- Networks → Physical U-Net: builds from · Network code relies on the geometry-aware U-Net block
- Package API → Networks: exposes · Package boundary makes model modules importable
- Package API → Training: exposes · Package boundary makes orchestration importable
- Package API → Predict: exposes · Package boundary makes pairwise inference importable
