Skip to content

Commit

Permalink
Merge branch 'formatting-and-lenting' of https://github.com/OSIPI/pypi
Browse files Browse the repository at this point in the history
…into formatting-and-lenting
  • Loading branch information
MohamedNasser8 committed Jun 5, 2024
2 parents 73e2283 + abbbbb5 commit bc35dcc
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 106 deletions.
1 change: 0 additions & 1 deletion docs/examples/aif/plot_aif_parker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""

import matplotlib.pyplot as plt

# %%
# Import necessary packages
import numpy as np
Expand Down
1 change: 0 additions & 1 deletion docs/examples/aif/plot_dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"""

import matplotlib.pyplot as plt

# %%
# Import necessary packages
import numpy as np
Expand Down
9 changes: 2 additions & 7 deletions docs/examples/tissue/plot_extended_tofts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"""

import matplotlib.pyplot as plt

# %%
# Import necessary packages
import numpy as np
Expand Down Expand Up @@ -44,13 +43,9 @@
# %%
# Comparing different discretization methods for an extracellular
# volume fraction of 0.2, Ktrans of 0.2 /min and vp of 0.05
ct = osipi.extended_tofts(
t, ca, Ktrans, ve, vp[0]
) # Defaults to Convolution
ct = osipi.extended_tofts(t, ca, Ktrans, ve, vp[0]) # Defaults to Convolution
plt.plot(t, ct, "b-", label="Convolution")
ct = osipi.extended_tofts(
t, ca, Ktrans, ve, vp[0], discretization_method="exp"
)
ct = osipi.extended_tofts(t, ca, Ktrans, ve, vp[0], discretization_method="exp")
plt.plot(t, ct, "g-", label="Exponential Convolution")
plt.title(f"Ktrans = {Ktrans} /min")
plt.xlabel("Time (sec)")
Expand Down
9 changes: 2 additions & 7 deletions docs/examples/tissue/plot_tofts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"""

import matplotlib.pyplot as plt

# %%
# Import necessary packages
import numpy as np
Expand Down Expand Up @@ -44,13 +43,9 @@
# %%
# Comparing different discretization methods for an extracellular
# volume fraction of 0.2 and Ktrans of 0.2 /min
ct = osipi.tofts(
t, ca, Ktrans=Ktrans[1], ve=ve
) # Defaults to Convolution
ct = osipi.tofts(t, ca, Ktrans=Ktrans[1], ve=ve) # Defaults to Convolution
plt.plot(t, ct, "b-", label="Convolution")
ct = osipi.tofts(
t, ca, Ktrans=Ktrans[1], ve=ve, discretization_method="exp"
)
ct = osipi.tofts(t, ca, Ktrans=Ktrans[1], ve=ve, discretization_method="exp")
plt.plot(t, ct, "g-", label="Exponential Convolution")
plt.title(f"Ktrans = {Ktrans[1]} /min")
plt.xlabel("Time (sec)")
Expand Down
1 change: 0 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# https://www.sphinx-doc.org/en/master/usage/configuration.html

import os

# -- Project information ---------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
import sys
Expand Down
1 change: 0 additions & 1 deletion docs/source/generated/examples/aif/plot_aif_parker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""

import matplotlib.pyplot as plt

# %%
# Import necessary packages
import numpy as np
Expand Down
1 change: 0 additions & 1 deletion docs/source/generated/examples/aif/plot_dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"""

import matplotlib.pyplot as plt

# %%
# Import necessary packages
import numpy as np
Expand Down
9 changes: 2 additions & 7 deletions docs/source/generated/examples/tissue/plot_extended_tofts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"""

import matplotlib.pyplot as plt

# %%
# Import necessary packages
import numpy as np
Expand Down Expand Up @@ -44,13 +43,9 @@
# %%
# Comparing different discretization methods for an extracellular
# volume fraction of 0.2, Ktrans of 0.2 /min and vp of 0.05
ct = osipi.extended_tofts(
t, ca, Ktrans, ve, vp[0]
) # Defaults to Convolution
ct = osipi.extended_tofts(t, ca, Ktrans, ve, vp[0]) # Defaults to Convolution
plt.plot(t, ct, "b-", label="Convolution")
ct = osipi.extended_tofts(
t, ca, Ktrans, ve, vp[0], discretization_method="exp"
)
ct = osipi.extended_tofts(t, ca, Ktrans, ve, vp[0], discretization_method="exp")
plt.plot(t, ct, "g-", label="Exponential Convolution")
plt.title(f"Ktrans = {Ktrans} /min")
plt.xlabel("Time (sec)")
Expand Down
9 changes: 2 additions & 7 deletions docs/source/generated/examples/tissue/plot_tofts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"""

import matplotlib.pyplot as plt

# %%
# Import necessary packages
import numpy as np
Expand Down Expand Up @@ -44,13 +43,9 @@
# %%
# Comparing different discretization methods for an extracellular
# volume fraction of 0.2 and Ktrans of 0.2 /min
ct = osipi.tofts(
t, ca, Ktrans=Ktrans[1], ve=ve
) # Defaults to Convolution
ct = osipi.tofts(t, ca, Ktrans=Ktrans[1], ve=ve) # Defaults to Convolution
plt.plot(t, ct, "b-", label="Convolution")
ct = osipi.tofts(
t, ca, Ktrans=Ktrans[1], ve=ve, discretization_method="exp"
)
ct = osipi.tofts(t, ca, Ktrans=Ktrans[1], ve=ve, discretization_method="exp")
plt.plot(t, ct, "g-", label="Exponential Convolution")
plt.title(f"Ktrans = {Ktrans[1]} /min")
plt.xlabel("Time (sec)")
Expand Down
8 changes: 2 additions & 6 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ def activate():
os.makedirs(venv_dir, exist_ok=True)
venv.create(venv_dir, with_pip=True)
windows = (
(sys.platform == "win32")
or (sys.platform == "win64")
or (os.name == "nt")
(sys.platform == "win32") or (sys.platform == "win64") or (os.name == "nt")
)
if windows:
return os.path.join(venv_dir, "Scripts", "activate")
Expand All @@ -66,9 +64,7 @@ def install():
"""Install requirements to a virtual environment."""

logging.info("Installing requirements...")
os.system(
activate() + " && " + "py -m pip install -r requirements.txt"
)
os.system(activate() + " && " + "py -m pip install -r requirements.txt")


if __name__ == "__main__":
Expand Down
14 changes: 3 additions & 11 deletions src/osipi/_aif.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import numpy as np


def aif_parker(
t: np.ndarray, BAT: float = 0.0, Hct: float = 0.0
) -> np.ndarray:
def aif_parker(t: np.ndarray, BAT: float = 0.0, Hct: float = 0.0) -> np.ndarray:
"""AIF model as defined by Parker et al (2005)
Args:
Expand Down Expand Up @@ -53,19 +51,13 @@ def aif_parker(
# A1/(SD1*sqrt(2*PI)) * exp(-(t_offset-m1)^2/(2*var1))
# A1 = 0.833, SD1 = 0.055, m1 = 0.171
gaussian1 = 5.73258 * np.exp(
-1.0
* (t_offset - 0.17046)
* (t_offset - 0.17046)
/ (2.0 * 0.0563 * 0.0563)
-1.0 * (t_offset - 0.17046) * (t_offset - 0.17046) / (2.0 * 0.0563 * 0.0563)
)

# A2/(SD2*sqrt(2*PI)) * exp(-(t_offset-m2)^2/(2*var2))
# A2 = 0.336, SD2 = 0.134, m2 = 0.364
gaussian2 = 0.997356 * np.exp(
-1.0
* (t_offset - 0.365)
* (t_offset - 0.365)
/ (2.0 * 0.132 * 0.132)
-1.0 * (t_offset - 0.365) * (t_offset - 0.365) / (2.0 * 0.132 * 0.132)
)
# alpha*exp(-beta*t_offset) / (1+exp(-s(t_offset-tau)))
# alpha = 1.064, beta = 0.166, s = 37.772, tau = 0.482
Expand Down
36 changes: 10 additions & 26 deletions src/osipi/_tissue.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ def tofts(
"""
if not np.allclose(np.diff(t), np.diff(t)[0]):
warnings.warn(
(
"Non-uniform time spacing detected. Time array may be"
" resampled."
),
("Non-uniform time spacing detected. Time array may be" " resampled."),
stacklevel=2,
)

Expand All @@ -90,9 +87,7 @@ def tofts(
# Convert units
Ktrans = Ktrans / 60 # from 1/min to 1/sec

if (
discretization_method == "exp"
): # Use exponential convolution
if discretization_method == "exp": # Use exponential convolution
# Shift the AIF by the arterial delay time (if not zero)
if Ta != 0:
f = interp1d(
Expand Down Expand Up @@ -134,9 +129,7 @@ def tofts(
else:
# Resample at the smallest spacing
dt = np.min(np.diff(t))
t_resampled = np.linspace(
t[0], t[-1], int((t[-1] - t[0]) / dt)
)
t_resampled = np.linspace(t[0], t[-1], int((t[-1] - t[0]) / dt))
ca_func = interp1d(
t,
ca,
Expand All @@ -158,9 +151,7 @@ def tofts(

# Discard unwanted points and make sure time spacing
# is correct
ct_resampled = (
convolution[0 : len(t_resampled)] * t_resampled[1]
)
ct_resampled = convolution[0 : len(t_resampled)] * t_resampled[1]

# Restore time grid spacing
ct_func = interp1d(
Expand Down Expand Up @@ -251,10 +242,7 @@ def extended_tofts(

if not np.allclose(np.diff(t), np.diff(t)[0]):
warnings.warn(
(
"Non-uniform time spacing detected. Time array may be"
" resampled."
),
("Non-uniform time spacing detected. Time array may be" " resampled."),
stacklevel=2,
)

Expand All @@ -265,9 +253,7 @@ def extended_tofts(
# Convert units
Ktrans = Ktrans / 60 # from 1/min to 1/sec

if (
discretization_method == "exp"
): # Use exponential convolution
if discretization_method == "exp": # Use exponential convolution
# Shift the AIF by the arterial delay time (if not zero)
if Ta != 0:
f = interp1d(
Expand Down Expand Up @@ -311,9 +297,7 @@ def extended_tofts(
else:
# Resample at the smallest spacing
dt = np.min(np.diff(t))
t_resampled = np.linspace(
t[0], t[-1], int((t[-1] - t[0]) / dt)
)
t_resampled = np.linspace(t[0], t[-1], int((t[-1] - t[0]) / dt))
ca_func = interp1d(
t,
ca,
Expand All @@ -335,9 +319,9 @@ def extended_tofts(

# Discard unwanted points, make sure time spacing is
# correct and add vp*ca term for extended model
ct_resampled = convolution[
0 : len(t_resampled)
] * t_resampled[1] + (vp * ca_resampled)
ct_resampled = convolution[0 : len(t_resampled)] * t_resampled[1] + (
vp * ca_resampled
)

# Restore time grid spacing
ct_func = interp1d(
Expand Down
40 changes: 10 additions & 30 deletions tests/test_tissue.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,52 +30,38 @@ def test_tissue_tofts():
t = np.arange(0, 6 * 60, 1)
ca = osipi.aif_parker(t)
ct = osipi.tofts(t, ca, Ktrans=0.6, ve=0.2, Ta=60.0)
assert (
np.min(np.where(ct > 0.0)) - np.min(np.where(ca > 0.0)) - 1
) * 1 == 60.0
assert (np.min(np.where(ct > 0.0)) - np.min(np.where(ca > 0.0)) - 1) * 1 == 60.0

# 4. Test that the discretization options give almost the same result -
# time step must be very small
t = np.arange(0, 6 * 60, 0.01)
ca = osipi.aif_parker(t)
ct_conv = osipi.tofts(t, ca, Ktrans=0.6, ve=0.2)
ct_exp = osipi.tofts(
t, ca, Ktrans=0.6, ve=0.2, discretization_method="exp"
)
ct_exp = osipi.tofts(t, ca, Ktrans=0.6, ve=0.2, discretization_method="exp")
assert np.allclose(ct_conv, ct_exp, rtol=1e-4, atol=1e-3)

# 5. Test that the ratio of the area under the ct and ca curves is
# approximately the extracellular volume
t = np.arange(0, 6 * 60, 1)
ca = osipi.aif_parker(t)
ct_conv = osipi.tofts(t, ca, Ktrans=0.6, ve=0.2)
ct_exp = osipi.tofts(
t, ca, Ktrans=0.6, ve=0.2, discretization_method="exp"
)
assert math.isclose(
np.trapz(ct_conv, t) / np.trapz(ca, t), 0.2, abs_tol=1e-1
)
assert math.isclose(
np.trapz(ct_exp, t) / np.trapz(ca, t), 0.2, abs_tol=1e-1
)
ct_exp = osipi.tofts(t, ca, Ktrans=0.6, ve=0.2, discretization_method="exp")
assert math.isclose(np.trapz(ct_conv, t) / np.trapz(ca, t), 0.2, abs_tol=1e-1)
assert math.isclose(np.trapz(ct_exp, t) / np.trapz(ca, t), 0.2, abs_tol=1e-1)

# 6. Test specific use cases
t = np.arange(0, 6 * 60, 1)
ca = osipi.aif_parker(t)
ct_conv = osipi.tofts(t, ca, Ktrans=0, ve=0.2)
assert np.count_nonzero(ct_conv) == 0

ct_exp = osipi.tofts(
t, ca, Ktrans=0, ve=0.2, discretization_method="exp"
)
ct_exp = osipi.tofts(t, ca, Ktrans=0, ve=0.2, discretization_method="exp")
assert np.count_nonzero(ct_exp) == 0

ct_conv = osipi.tofts(t, ca, Ktrans=0.6, ve=0)
assert np.count_nonzero(ct_conv) == 0

ct_exp = osipi.tofts(
t, ca, Ktrans=0.6, ve=0, discretization_method="exp"
)
ct_exp = osipi.tofts(t, ca, Ktrans=0.6, ve=0, discretization_method="exp")
assert np.count_nonzero(ct_exp) == 0


Expand All @@ -98,12 +84,8 @@ def test_tissue_extended_tofts():
# from the AIF by the specified offset time
t = np.arange(0, 6 * 60, 1)
ca = osipi.aif_parker(t)
ct = osipi.extended_tofts(
t, ca, Ktrans=0.6, 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
ct = osipi.extended_tofts(t, ca, Ktrans=0.6, 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. Test that the discretization options give almost the same result -
# time step must be very small
Expand All @@ -129,9 +111,7 @@ def test_tissue_extended_tofts():
0.2 + 0.3,
abs_tol=1e-1,
)
assert math.isclose(
np.trapz(ct_exp, t) / np.trapz(ca, t), 0.2 + 0.3, abs_tol=1e-1
)
assert math.isclose(np.trapz(ct_exp, t) / np.trapz(ca, t), 0.2 + 0.3, abs_tol=1e-1)

# 6. Test specific use cases
t = np.arange(0, 6 * 60, 1)
Expand Down

0 comments on commit bc35dcc

Please sign in to comment.