Skip to main content

dmipy-fit: Diffusion Microstructure Imaging in Python

dmipy-fit is a Python toolbox for biophysical modelling of diffusion MRI data. Given an acquisition scheme and a multi-compartment tissue model, it fits the model parameters voxel-by-voxel and returns interpretable microstructure maps — axon density and dispersion, diffusivities, cell size, volume fractions, T2, and more.

It models the diffusion signal plus the transverse-relaxation contrasts that ride on it — T2 and surface relaxivity — as composable, occupancy-gated factors that attach to any compartment. Every analytical model is validated effect-by-effect against the dmipy-sim Monte-Carlo simulator (the two share one free-waveform sequence/substrate interface; the dependency is one-directional, fit → sim).

from dmipy_fit.signal_models.gaussian_models import G1Ball
from dmipy_fit.signal_models.cylinder_models import C1Stick
from dmipy_fit.core.modeling_framework import MultiCompartmentModel

ball_stick = MultiCompartmentModel([G1Ball(), C1Stick()])
fit = ball_stick.fit(scheme, data, solver="jax")     # whole slice on GPU
intra_fraction = fit.fitted_parameters["partial_volume_1"]

The signal model

dmipy-fit models the measured signal as a sum of compartments, each carrying its diffusion attenuation times its (transverse) relaxation contrasts:

$$ S ;=; S_0 \sum_i f_i ; \underbrace{E^{\mathrm{diff}}i(b)}{\text{diffusion}} ; \underbrace{e^{-\mathrm{TE}/T_{2,i}}}_{T_2} ; \underbrace{\hat B^{\mathrm{surf}}i(\mathrm{TE})}{\text{surface relaxivity}} $$

Magnetisation is treated as fully transverse (ideal instantaneous pulses). Each non-diffusion effect is an occupancy-gated factor — a multiplicative attenuation that attaches to any compartment via OccupancyGatedModel, so any subset composes by listing more factors.

What's here

  • Signal models (signal_models/) — sticks/cylinders, sphere, Gaussian (ball/zeppelin), plane, capped cylinder, tissue-response; b-tensor / free-waveform aware.
  • Occupancy-gated factors (signal_models/attenuation.py) — OccupancyGatedModel + TransverseRelaxation, IntraPoreSurfaceRelaxivity, ExteriorSurfaceRelaxivity. Relaxation and surface relaxivity as composable add-ons to any compartment.
  • Fitting framework (core/) — MultiCompartmentModel, spherical-mean and spherical-harmonics frameworks, fitted-model properties.
  • Distributions (distributions/) — Watson / Bingham dispersion, Gamma diameter.
  • Optimizers — brute2fine, MIX, multi-tissue NNLS; CSD (Tournier / cvxpy / OSQP-JAX); IVIM / impulsed / 3-tissue custom optimizers.
  • GPU fitting (jax/) — diffusion signal models, multicompartment, CSD, DTI, fractions, vmap_fit; whole-slice fitting in seconds with solver="jax".
  • White matter (white_matter/):
    • build_white_matter_model() — a decoupled, diffusion-only canonical WM model (stick + zeppelin + stuck-myelin dot + optional CSF ball), each compartment gated with T2 + surface-relaxivity. Because the intra-pore and exterior surface factors differ per compartment, surface relaxivity introduces a b-independent signal weighting between intra and extra (true spin fractions ≠ apparent fractions) — a physical effect that plain stick+zeppelin models omit.
    • t2_spectrum_mwf() — standard regularised NNLS T2-spectrum myelin-water fraction (Whittall–MacKay / DECAES). Estimate MWF directly from a multi-echo T2 decay, including one generated by a dmipy-sim CPMG-style multi-TE series.
  • Rician noise model, b-tensor / free-waveform schemes, and a Monte-Carlo bridge to dmipy-sim.
  • Flagship example (examples/flagship_canonical_wm/) — the canonical WM signal computed both ways (this analytical model and the dmipy-sim Monte-Carlo), agreeing for diffusion and surface relaxivity; fully reproducible (regenerates the cached MC bit-exactly). The clearest entry point to what the two engines are for.

Installation

pip install dmipy-fit                    # core only
pip install "dmipy-fit[jax]"             # + JAX GPU fitting
pip install "dmipy-fit[jax,data,viz]"    # + bundled-data loaders + plotting
pip install "dmipy-fit[all]"             # everything

Myelin water fraction from a Monte-Carlo CPMG decay

dmipy_sim.simulate_cpmg returns the full echo train from a single walk; feed it to the NNLS T2-spectrum estimator:

import numpy as np
import dmipy_sim as ds
from dmipy_fit.white_matter.mwf import t2_spectrum_mwf

geom = ds.MyelinatedCylinder(inner_radius=2.5e-6, outer_radius=3.57e-6, orientation=(0, 0, 1),
    D_intra=1.7e-9, D_myelin_radial=0.1e-9, D_myelin_tangential=0.5e-9, D_extra=1.7e-9,
    T2_intra=0.080, T2_myelin=0.015, T2_extra=0.080)

TE, n_echoes = 8e-3, 32
wf = ds.cpmg(n_echoes=n_echoes, TE=TE, G_magnitude=0.0, bvecs=[[1, 0, 0]], n_t_per_echo=60)
S = np.asarray(ds.simulate_cpmg(40000, None, wf, geom)).ravel()      # (n_echoes,), one walk
echo_times = np.arange(1, n_echoes + 1) * TE
mwf, T2_grid, spectrum = t2_spectrum_mwf(S / S[0], echo_times)

Testing

pytest -q                       # analytical + numerical (dipy/MISST references)
pytest -q -m "not slow"         # skip the heavy GPU battery

Tests assert against analytical results, MISST/dipy references, or analytic ↔ Monte-Carlo parity against dmipy-sim.

License

Dual-licensed: GNU AGPL-3.0 for open-source use, or a commercial license for proprietary/closed use. See LICENSE and LICENSING.md (commercial: rutger.fick@dmrai-lab.org).

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dmipy_fit-2.1.0.tar.gz (614.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dmipy_fit-2.1.0-py3-none-any.whl (707.8 kB view details)

Uploaded Python 3

File details

Details for the file dmipy_fit-2.1.0.tar.gz.

File metadata

  • Download URL: dmipy_fit-2.1.0.tar.gz
  • Upload date:
  • Size: 614.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dmipy_fit-2.1.0.tar.gz
Algorithm Hash digest
SHA256 8bebfae5c37e5f07bc9928a22507675303adf4f93b6252816df447616296dceb
MD5 423dc67526a0ce25aebf23ec0a812c39
BLAKE2b-256 bef17665d0b5de208f78b7fd74a274be6e22da40c507b011ce8d02ac535a80ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for dmipy_fit-2.1.0.tar.gz:

Publisher: publish.yml on dmrai-lab/dmipy-fit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dmipy_fit-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: dmipy_fit-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 707.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dmipy_fit-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8982061269bce6c9d05e81565c84228f31eef6709be3540487a54cfcdfd79184
MD5 f32f8cf6e256fa526fa0b41bdb2f833f
BLAKE2b-256 560673621eaee5fa32144e2efe0c5af8bdbe97abc8f45389b475d61bf4ec31a0

See more details on using hashes here.

Provenance

The following attestation bundles were made for dmipy_fit-2.1.0-py3-none-any.whl:

Publisher: publish.yml on dmrai-lab/dmipy-fit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

2.3.0

2 files

2.2.0

2 files

This release

2.1.0 This release

2 files

2.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page