DiffeoMorph is a medical image registration and computational anatomy framework implemented in Python. It provides non-rigid Large Deformation Diffeomorphic Metric Mapping (LDDMM), Stationary Velocity Field (SVF) Lie group scaling-and-squaring exponential integration, Sobolev/fluid vector regularizers, deformation Jacobian determinant analysis, Tensor-Based Morphometry (TBM), and automated Alzheimer's Disease / Mild Cognitive Impairment (MCI) hippocampal atrophy and ventriculomegaly clinical staging.
-
Lie Group SVF Diffeomorphic Registration: Parameterizes spatial transformations on the Lie algebra of smooth velocity fields
$\phi = \exp(v)$ , ensuring invertible, topology-preserving diffeomorphisms with guaranteed absence of topological singularities ($\det(D\phi) > 0$ ). -
Scaling and Squaring Integration: Recursive doubling algorithm computing
$\exp(v)$ and its inverse$\exp(-v)$ at machine precision with$O(\log N)$ grid composition steps. -
Sobolev & Fluid-Mechanical Regularization: Gaussian and Helmholtz smoothing operators acting in Reproducing Kernel Hilbert Space (RKHS)
$V$ to enforce spatial smoothness and prevent grid tearing. - Contrast-Robust Similarity Metrics: Mean Squared Error (MSE) and Local Normalized Cross-Correlation (LNCC) with analytical variational driving forces for intra-modality and cross-contrast MRI (T1, T2, FLAIR).
-
Tensor-Based Morphometry (TBM): Point-wise deformation gradient tensor
$D\phi(x)$ , Jacobian determinant$|J_\phi(x)|$ , and continuous log-Jacobian field integration for sub-voxel volumetric contraction/expansion quantification. - ADNI-Aligned Alzheimer's Staging: Automated clinical biomarker analysis extracting annualized hippocampal atrophy rates and ventricular ex-vacuo expansion thresholds to stratify normal aging, prodromal MCI, and probable Alzheimer's dementia.
flowchart TD
A["Baseline MRI Scan I<sub>0</sub><br/>(T1-weighted 3D Volume)"] --> C["Diffeomorphic SVF Optimizer"]
B["Follow-up MRI Scan I<sub>1</sub><br/>(Longitudinal 3D Volume)"] --> C
C --> D["Stationary Velocity Field v(x)<br/>Lie Algebra Vector Space"]
D --> E["Scaling & Squaring Engine<br/>φ = exp(v), φ<sup>-1</sup> = exp(-v)"]
E --> F["Warped Volume<br/>I<sub>1</sub> ∁ exp(-v)"]
F --> G["Variational Similarity Metric<br/>(LNCC / MSE Loss + Gradient)"]
G --> H["Fluid RKHS Smoother<br/>Sobolev Velocity Update δv"]
H --> C
E --> I["Deformation Gradient Dφ(x)"]
I --> J["Jacobian Determinant |J<sub>φ</sub>(x)|<br/>Topology Audit: &det;(Dφ) > 0"]
J --> K["Tensor-Based Morphometry (TBM)<br/>Log-Jacobian Field log|J<sub>φ</sub>|"]
K --> L["Anatomical ROI Segmentation<br/>(Hippocampus, Ventricles, Cortex)"]
L --> M["Clinical Alzheimer Evaluator<br/>Annualized Atrophy & ADNI Staging"]
A diffeomorphism
The solution is given by the Lie group exponential mapping:
The inverse diffeomorphism is obtained symmetrically via negative velocity:
To evaluate
The full diffeomorphism is reconstructed through
where
v(x) ---------> [ / 2^N ] ---------> Id + v/2^N
|
+--------------------------------------------+
| Squaring Loop (k = N ... 1):
v
phi_{k-1} = phi_k o phi_k (Spatial Composition)
|
+------> exp(v) [Diffeomorphism with det(D phi) > 0]
The registration problem seeks the velocity field
The variational image gradient with respect to displacement produces the Demons-like optical flow driving force:
$$F(x) = \left(I_{moving}(\phi^{-1}(x)) - I_{fixed}(x)\right) \cdot \nabla \leftI_{moving} \circ \phi^{-1}\right$$
The force is smoothed with a Gaussian spatial kernel
The local volumetric expansion or compression factor is determined by the Jacobian determinant of the spatial transformation:
-
$|J_\phi(x)| > 1$ : Local tissue expansion (e.g., ventricular enlargement in hydrocephalus). -
$0 < |J_\phi(x)| < 1$ : Local tissue contraction (e.g., hippocampal atrophy). -
$|J_\phi(x)| \le 0$ : Topological folding / singularity (forbidden in diffeomorphisms).
The total deformed volume of an anatomical region
git clone https://github.com/ZengLLQ/diffeo-morph.git
cd diffeo-morph
pip install -e .For development and test dependencies:
pip install -e ".[dev]"# 1. Inspect CLI help
diffeo-morph --help
# 2. Execute non-rigid SVF registration
diffeo-morph register --size 24 --iterations 30 --lr 0.4 --sigma 1.2
# 3. Evaluate Alzheimer's Disease longitudinal morphometry staging
diffeo-morph alzheimer --atrophy-severity 1.2 --interval-months 12.0======================================================================
DiffeoMorph: Longitudinal Brain MRI Morphometry & TBM Report
======================================================================
REGIONAL MORPHOMETRIC ATROPHY ANALYSIS:
Hippocampus Volume Change : -5.80% (Annualized: -5.80%/yr)
Mean Hippocampal log|J| : -0.060 (Local tissue contraction)
Ventricular Expansion : +7.40% (Ex-vacuo hydrocephalus)
Mean Ventricular log|J| : +0.071
----------------------------------------------------------------------
DIAGNOSTIC STAGING EVALUATION:
Staging Classification : PROBABLE_ALZHEIMERS_DISEASE
Diagnostic Confidence : 88.0%
Pathology Summary : Clinically significant hippocampal volume loss (-5.8%/yr) consistent with typical Alzheimer's disease pathology.
----------------------------------------------------------------------
CLINICAL MANAGEMENT RECOMMENDATION:
Action Plan : Initiate disease-modifying anti-amyloid therapy consultation and cognitive monitoring.
======================================================================
import numpy as np
from diffeo_morph.geometry import create_identity_grid, warp_image
from diffeo_morph.registration import SVFRegistrationOptimizer
from diffeo_morph.clinical import TensorBasedMorphometry, AlzheimerEvaluator
# 1. Load or Generate 3D Brain Volumes (Baseline vs 1-Year Follow-up)
dim = 24
baseline_scan = np.random.rand(dim, dim, dim)
followup_scan = np.random.rand(dim, dim, dim)
hippocampus_mask = np.zeros((dim, dim, dim), dtype=bool)
hippocampus_mask[8:14, 8:14, 8:14] = True
# 2. Configure SVF Diffeomorphic Optimizer
optimizer = SVFRegistrationOptimizer(
learning_rate=0.4,
fluid_sigma=1.2,
elastic_weight=0.01,
metric="mse",
)
# 3. Execute Registration (Follow-up -> Baseline)
result = optimizer.register(
moving=followup_scan,
fixed=baseline_scan,
num_iterations=30,
)
print(f"Topology Foldings: {result.num_foldings} (Strictly Diffeomorphic: {result.is_diffeomorphic})")
# 4. Perform Tensor-Based Morphometry (TBM)
hippo_stats = TensorBasedMorphometry.analyze_roi(
result.jacobian_determinant,
hippocampus_mask,
region_name="Hippocampus",
)
print(f"Hippocampal Volume Change: {hippo_stats.percent_volume_change:+.2f}%")
print(f"Mean log-Jacobian: {hippo_stats.mean_log_jacobian:+.4f}")The test suite validates coordinate grids, Lie group scaling and squaring inversion consistency, analytical Jacobian determinants, and non-rigid registration convergence:
pytest -vtests/test_cli.py::test_cli_help PASSED
tests/test_cli.py::test_cli_register PASSED
tests/test_cli.py::test_cli_alzheimer PASSED
tests/test_grid.py::test_create_identity_grid_2d_and_3d PASSED
tests/test_grid.py::test_warp_image_identity PASSED
tests/test_grid.py::test_warp_image_displacement_shift PASSED
tests/test_grid.py::test_compose_transformations_identity PASSED
tests/test_jacobian.py::test_identity_grid_jacobian PASSED
tests/test_jacobian.py::test_folding_detection PASSED
tests/test_jacobian.py::test_log_jacobian PASSED
tests/test_lie_algebra.py::test_scaling_and_squaring_zero_velocity PASSED
tests/test_lie_algebra.py::test_scaling_and_squaring_inverse_consistency PASSED
tests/test_metrics.py::test_mean_squared_error PASSED
tests/test_metrics.py::test_local_normalized_cross_correlation PASSED
tests/test_morphometry.py::test_tbm_roi_analysis PASSED
tests/test_morphometry.py::test_alzheimer_evaluator_staging PASSED
tests/test_registration.py::test_2d_registration_convergence PASSED
tests/test_registration.py::test_3d_registration PASSED
============================= 18 passed in 2.37s ==============================
- Beg, M. F., Miller, M. I., Trouvé, A., & Younes, L. (2005). Computing large deformation metric mappings via geodesic flows of diffeomorphisms. International Journal of Computer Vision, 61(2), 139-157.
- Ashburner, J. (2007). A fast diffeomorphic image registration algorithm. NeuroImage, 38(1), 95-113.
- Arsigny, V., Commowick, O., Pennec, X., & Ayache, N. (2006). A log-Euclidean framework for statistics on diffeomorphisms. Medical Image Computing and Computer-Assisted Intervention (MICCAI), 924-931.
- Jack, C. R., et al. (2010). Brain atrophy rates in normal aging, mild cognitive impairment, and Alzheimer's disease: A longitudinal MRI study. The Lancet Neurology, 9(12), 1192-1203.
This project is licensed under the MIT License - see the LICENSE file for details.