- Python 100%
| bin | ||
| patients | ||
| src | ||
| .gitignore | ||
| .python-version | ||
| config.toml | ||
| main.py | ||
| pyproject.toml | ||
| README.md | ||
| uv.lock | ||
GBM-REG
MRI preprocessing, segmentation, and slab-to-volume registration tools for aligning post-mortem glioblastoma slabs with in-vivo full-brain MRI.
The pipeline is configured with TOML files. It can preprocess raw NIfTI scans, segment in-vivo and ex-vivo images, initialise candidate slab positions, and run affine registration over those candidates.
This pipeline was developed with use of LLMs in a pair-programming fashion. All code has been manually reviewed and edited to align with goals.
Repository Layout
.
├── main.py # Runs preprocessing, segmentation, and registration
├── config.toml # Global directories and pipeline settings
├── patients/ # Per-patient scan manifests
├── src/
│ ├── preprocess/ # Bias correction, orientation, skull stripping, normalisation
│ ├── segment/ # SynthSeg, Swin UNETR, and GMM segmentation helpers
│ ├── registration/ # Slab initialisation, masks, metrics, and affine registration
│ └── visualise/ # Plotting and overlay utilities
└── bin/ # Small helper scripts
Generated data is intentionally ignored by Git. Local inputs, model weights, intermediate volumes, and registration outputs should live under directories such as data/, models/, prep/, preprocessed/, outputs/, temp/, or render/.
Data Model
Each patient has a TOML file in patients/. The tracked examples show the expected structure:
PreMortem,PreSurgery, andPostSurgerysections map MRI contrasts such ast1,t1c,t2, andflairto source NIfTI paths.PostMortemcontains one section per slab series. Each slab definest1,t2, andflips, whereflipslists axes to flip during preprocessing.
Preprocessed outputs are organised as:
<preprocessed>/<patient>/<timepoint>/<contrast>.nii.gz
<preprocessed>/<patient>/PostMortem/<slab>/<contrast>.nii.gz
Registration outputs are written below:
<registered>/registration/
With the default config.toml, <preprocessed> is ./prep and <registered> is ./outputs.
Installation
This project uses uv and Python 3.13.
uv sync
Some stages also depend on external neuroimaging tools or model files:
hd-betor FSL BET for skull stripping, depending onskull_strip_algorithm.- FreeSurfer SynthSeg (
mri_synthseg) for in-vivo label generation. - Swin UNETR model weights at
./models/swinUNETR.ptunlessconfig.tomlpoints elsewhere. - CUDA if
segmentation.device = "cuda"; set it to"cpu"for CPU inference.
Configuration
Edit config.toml before running the pipeline.
Important sections:
[dirs]: locations for temp files, preprocessed scans, patient manifests, and registration outputs.[pipeline]: toggles forpreprocess,segment, andregister.[preprocessing]: orientation, bias correction, skull stripping, masking, and normalisation options.[segmentation]: SynthSeg input contrast, Swin UNETR weights, device, and ex-vivo segmentation settings.[registration]: registration input type, A-P candidate spacing, candidate limits, and dry-run style switches.
For quick registration checks, set:
[registration]
max_positions = 5
skip_registration = true
Use masks_only = true when you only want masks and metadata.
Running
Run every enabled stage for every patient manifest:
uv run python main.py --config config.toml
Run preprocessing for one patient:
uv run python -m src.preprocess.run --config config.toml --patient patients/IM008.toml
Run segmentation for one preprocessed patient:
uv run python -m src.segment.run --config config.toml --patient IM008
Run registration for one preprocessed patient:
uv run python -m src.registration.run --config config.toml --patient IM008
Pipeline Summary
- Preprocessing reorients images, applies N4 bias correction, registers contrasts within each timepoint, creates masks, and normalises intensities.
- Segmentation creates in-vivo tissue and tumour labels, then segments post-mortem slabs into tissue classes.
- Registration searches A-P slab positions, builds candidate slab volumes and masks, computes no-registration metrics, and optionally runs affine registration for intensity or segmentation inputs.
- Visualisation utilities generate overlays and metrics plots for manual inspection.
Notes
- Patient manifests may contain site-specific absolute paths. Keep private source data outside Git and update local manifests as needed.
- Registration and segmentation outputs can be large; keep them in ignored output directories.
- The top-level
main.pysends success or failure notifications throughsrc.misc.send_ntfywhen configured.