medrs
High-performance medical imaging I/O and processing library for Rust and Python.
Overview
medrs is designed for throughput-critical medical imaging workflows, particularly deep learning pipelines that process large 3D volumes. It provides:
- Fast NIfTI I/O: Memory-mapped reading, crop-first loading (read sub-volumes without loading entire files)
- Transform Pipeline: Lazy evaluation with automatic operation fusion and SIMD acceleration
- Random Augmentation: Reproducible, GPU-friendly augmentations for ML training
- Python Bindings: Zero-copy numpy views, direct PyTorch/JAX tensor creation
Installation
Python
Rust
[]
= "0.1"
Development
Quick Start
Python
# Load a NIfTI image
=
# Method chaining for transforms
=
# Load directly to PyTorch tensor (most efficient)
=
Rust
use nifti;
use ;
Transform Pipeline
Build composable transform pipelines with lazy evaluation and automatic optimization:
Python
# Create a reusable pipeline
=
# Apply to multiple images
=
=
Rust
use TransformPipeline;
let pipeline = new
.z_normalize
.clamp
.resample_to_shape;
let processed = pipeline.apply;
Random Augmentation
Reproducible augmentations for ML training with optional seeding:
Python
=
# Individual augmentations
=
=
=
=
=
=
# Combined augmentation (flip + noise + scale + shift)
=
Rust
use ;
// Individual augmentations
let flipped = random_flip?;
let noisy = random_gaussian_noise;
// Combined augmentation
let augmented = random_augment?;
Crop-First Loading
Load only the data you need - essential for training pipelines:
Python
# Load a 64^3 patch starting at position (32, 32, 32)
=
# Load with resampling and reorientation in one step
=
# Load directly to GPU tensor
=
Training Data Loader
High-performance patch extraction for training:
=
# Training loop
=
Available Transforms
Intensity Transforms
z_normalize()/z_normalization()- Zero mean, unit variancerescale()/rescale_intensity()- Scale to [min, max] rangeclamp()- Clamp values to range
Spatial Transforms
resample()/resample_to_spacing()- Resample to target spacingresample_to_shape()- Resample to target shapereorient()- Reorient to standard orientation (RAS, LPS, etc.)crop_or_pad()- Crop or pad to target shapeflip()- Flip along specified axes
Random Augmentation
random_flip()- Random axis flippingrandom_gaussian_noise()- Additive Gaussian noiserandom_intensity_scale()- Random intensity scalingrandom_intensity_shift()- Random intensity offsetrandom_rotate_90()- Random 90-degree rotationsrandom_gamma()- Random gamma correctionrandom_augment()- Combined augmentation pipeline
Performance
medrs uses several optimization strategies:
- SIMD: Trilinear interpolation uses AVX2/SSE for 8-way parallel processing
- Parallel Processing: Rayon-based parallelism for large volumes
- Lazy Evaluation: Transform pipelines compose operations before execution
- Memory Mapping: Large files are memory-mapped to avoid full loads
- Buffer Pooling: Reusable buffers reduce allocation overhead
Examples
See the examples/ directory for:
basic/- Loading, transforms, and savingintegrations/- PyTorch, MONAI, JAX integrationadvanced/- Async pipelines, custom transforms
Testing
# Rust tests
# Python tests
# Benchmarks
License
medrs is dual-licensed under MIT and Apache-2.0. See LICENSE for details.
Contributing
See CONTRIBUTING.md for guidelines.
Maintainer
Liam Chalcroft (liam.chalcroft.20@ucl.ac.uk)