Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedNasser8 committed Nov 27, 2024
1 parent 3535067 commit c703ed6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/osipi/_tissue.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,11 @@ def two_compartment_exchange_model(
>>> plt.plot(t, ca, "r", t, ct, "g")
"""
if vp == 0:
E = 1 - np.exp(-PS / Fp)
Ktrans = E * Fp
return tofts(t, ca, Ktrans, ve, Ta, discretization_method="conv")

if not np.allclose(np.diff(t), np.diff(t)[0]):
warnings.warn(
("Non-uniform time spacing detected. Time array may be" " resampled."),
Expand Down Expand Up @@ -449,8 +454,17 @@ def two_compartment_exchange_model(

dt = np.min(np.diff(t)) / upsample_factor

# get concentration in plasma and EES
if Ta != 0:
f = interp1d(
t,
ca,
kind="linear",
bounds_error=False,
fill_value=0,
)
ca = (t > Ta) * f(t - Ta)

# get concentration in plasma and EES
Cp = dt * convolve(ca, irf_cp, mode="full", method="auto")[: len(t)]
Ce = dt * convolve(ca, irf_ce, mode="full", method="auto")[: len(t)]

Expand Down
28 changes: 27 additions & 1 deletion tests/test_tissue.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,33 @@ def test_tissue_extended_tofts():


def test_tissue_two_compartment_exchange_model():
pass
# 1. Basic operation of the function - test that the peak tissue
# concentration is less than the peak AIF
t = np.linspace(0, 6 * 60, 360)
ca = osipi.aif_parker(t)
ct = osipi.two_compartment_exchange_model(t, ca, Fp=10, PS=5, ve=0.2, vp=0.3)
assert np.round(np.max(ct)) < np.round(np.max(ca))

# 2. Basic operation of the function - test with non-uniform spacing of
# time array
t = np.geomspace(1, 6 * 60 + 1, num=360) - 1
ca = osipi.aif_parker(t)
ct = osipi.two_compartment_exchange_model(t, ca, Fp=10, PS=5, ve=0.2, vp=0.3)
assert np.round(np.max(ct)) < np.round(np.max(ca))

# 3. The offset option - test that the tissue concentration is shifted
# from the AIF by the specified offset time
t = np.arange(0, 6 * 60, 1)
ca = osipi.aif_parker(t)
ct = osipi.two_compartment_exchange_model(t, ca, Fp=10, PS=5, ve=0.2, vp=0.3, Ta=60.0)
assert (np.min(np.where(ct > 0.0)) - np.min(np.where(ca > 0.0)) - 1) * 1 == 60.0

# 4. Handle case where VP is 0, it should behave like the tofts model
t = np.arange(0, 6 * 60, 1)
ca = osipi.aif_parker(t)
ct = osipi.two_compartment_exchange_model(t, ca, Fp=10, PS=5, ve=0.2, vp=0)
ct_tofts = osipi.tofts(t, ca, Ktrans=3.93, ve=0.2)
assert np.allclose(ct, ct_tofts, rtol=1e-4, atol=1e-3)


if __name__ == "__main__":
Expand Down

0 comments on commit c703ed6

Please sign in to comment.