Skip to main content

A Python library for IVIM diffusion MRI model fitting

Project description

ivimfit

ivimfit is a modular Python library for fitting Intravoxel Incoherent Motion (IVIM) diffusion MRI models.
It supports monoexponential ADC fitting, biexponential (free and segmented) models, as well as Bayesian inference using PyMC.

Designed for researchers and clinicians working with DWI/IVIM datasets, this package offers signal filtering, robust modeling, and visualization tools for parameter evaluation.


📦 Features

  • ✅ Monoexponential ADC fitting
  • ✅ Full biexponential model (nonlinear free fit)
  • ✅ Segmented biexponential model (2-step D + [f, D*])
  • ✅ Bayesian IVIM modeling using MCMC via PyMC
  • ✅ Full Triexponential model (fast and intermediate component calculation)
  • ✅ Optional exclusion of b = 0
  • ✅ Automatic filtering of b-values > 1000
  • ✅ R² calculation and signal-fit visualization utilities

🧳 License

This project is licensed under the MIT License - see the [LICENSE] file for details.

📥 Installation

pip install ivimfit .

import numpy as np
import matplotlib.pyplot as plt
from ivimfit.utils import plot_fit, calculate_r_squared
from ivimfit.adc import fit_adc, monoexp_model

b = np.array([0, 50, 100, 200, 400, 600, 800])
s = np.array([800, 654,543,423,328,236,121])
#ADC Calculation
adc = fit_adc(b, s)
r2 = calculate_r_squared(s / s[0], monoexp_model(b, adc))

fig, ax = plot_fit(b, s, monoexp_model, [adc], model_name=f"ADC Fit (R² = {r2:.4f})")
plt.show()

#Biexponential Fitting
from ivimfit.biexp import fit_biexp_free, biexp_model

b = np.array([0, 50, 100, 200, 400, 600, 800])
s = np.array([800, 654,543,423,328,236,121])

f, D, D_star = fit_biexp_free(b, s)
r2 = calculate_r_squared(s / s[0], biexp_model(b, f, D, D_star))

fig, ax = plot_fit(b, s, biexp_model, [f, D, D_star], model_name=f"Free Fit (R² = {r2:.4f})")
plt.show()
#Segmented Fitting
from ivimfit.segmented import fit_biexp_segmented, biexp_fixed_D_model

b = np.array([0, 50, 100, 200, 400, 600, 800])
s = np.array([800, 654,543,423,328,236,121])

f, D_fixed, D_star = fit_biexp_segmented(b, s)
r2 = calculate_r_squared(s / s[0], biexp_fixed_D_model(b, f, D_star, D_fixed))

fig, ax = plot_fit(
    b, s,
    lambda b_, f_, D_star_,D_fixed: biexp_fixed_D_model(b_, f_, D_star_, D_fixed),
    [f, D_star,D_fixed],
    model_name=f"Segmented Fit (R² = {r2:.4f})"
)
plt.show()
#Bayesian Approach
from ivimfit.bayesian import fit_bayesian
from ivimfit.biexp import biexp_model

b = np.array([0, 50, 100, 200, 400, 600, 800])
s = np.array([800, 654,543,423,328,236,121])

if __name__ == "__main__":
    f, D, D_star = fit_bayesian(b, s, draws=500, chains=2)
    r2 = calculate_r_squared(s / s[0], biexp_model(b, f, D, D_star))

    fig, ax = plot_fit(b, s, biexp_model, [f, D, D_star], model_name=f"Bayesian Fit (R² = {r2:.4f})")
    plt.show()
#Triexponential fitting
from triexp import fit_triexp_free, triexp_model
b = np.array([0, 50, 100, 200, 400, 600, 800])
s = np.array([800, 654,543,423,328,236,121])
f1, f2, D, D1_star, D2_star = fit_triexp_free(b, s)
pred = triexp_model(b, f1, f2, D, D1_star, D2_star)
r2 = calculate_r_squared(s / s[0], pred)

fig, ax = plot_fit(b, s, triexp_model, [f1, f2, D, D1_star, D2_star], model_name=f"Tri-exponential Fit (R² = {r2:.4f})")
plt.show()

Project details


Download files

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

Source Distribution

ivimfit-0.1.3.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

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

ivimfit-0.1.3-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file ivimfit-0.1.3.tar.gz.

File metadata

  • Download URL: ivimfit-0.1.3.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for ivimfit-0.1.3.tar.gz
Algorithm Hash digest
SHA256 75c16c67bdfd737d683992d7387be53f0ef046548242ef858cd5945894768a32
MD5 aad0cbb46e798ecd84c00ddc670c533f
BLAKE2b-256 9587bd3c1ffb086e022b24a41a45d1c18a7056351a1db3df0dad98f12c379cdb

See more details on using hashes here.

File details

Details for the file ivimfit-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: ivimfit-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for ivimfit-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0f3ffee6d23d7e3831c32798c7573f48fe3e89634ac60e4dfbbdaf89589ebf2b
MD5 07ba34dc95d6e4d692914890dec807b9
BLAKE2b-256 7e54015d966e11578ef499262a4e0e53b61195054666698f236d78e2bfef79da

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page