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 ivimfit.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.4.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.4-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ivimfit-0.1.4.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.4.tar.gz
Algorithm Hash digest
SHA256 de48bad14d7be66c1b457987138cadb2064024923039efc22000e5d0a2804233
MD5 6b396746b6647b3db2f51f652316004f
BLAKE2b-256 5bb045f66d906c449c3513a9f1e4f5b056c10a717358c38a1d7a93a303d1af32

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ivimfit-0.1.4-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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c6c8cb87d795142a6371e1c494175fa850da5ff26b1ab1b5471c28f3685868ef
MD5 0e2384ae967bf29d22cebb7dab630089
BLAKE2b-256 0125c012af472d4768ced44beb2a5c3eccb9e6bdd15d2b014f524dfc6b28b098

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