Skip to content

Commit

Permalink
flake commented
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedNasser8 committed Jun 4, 2024
1 parent f64bdb5 commit 29a406a
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ repos:
# rev: 6.0.0
# hooks:
# - id: flake8
# args: [ '--max-line-length=80' ]
# args: [ '--max-line-length=70' ]

- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
args: [--line-length=80]
args: [--line-length=70]
8 changes: 6 additions & 2 deletions docs/examples/tissue/plot_extended_tofts.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@

# %%
# 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
8 changes: 6 additions & 2 deletions docs/examples/tissue/plot_tofts.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@

# %%
# 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: 6 additions & 2 deletions docs/source/generated/examples/tissue/plot_extended_tofts.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@

# %%
# 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
8 changes: 6 additions & 2 deletions docs/source/generated/examples/tissue/plot_tofts.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@

# %%
# 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
4 changes: 3 additions & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ 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
19 changes: 15 additions & 4 deletions src/osipi/_aif.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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 @@ -56,7 +58,10 @@ def aif_parker(t: np.ndarray, BAT: float = 0.0, Hct: float = 0.0) -> np.ndarray:
# 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 Expand Up @@ -113,7 +118,10 @@ def aif_georgiou(t: np.ndarray, BAT: float = 0.0) -> np.ndarray:
"""

msg = "This function is not yet implemented \n"
msg += "If you implement it yourself, please consider submitting it as an OSIPI code contribution"
msg += (
"If you implement it yourself, please consider submitting it"
" as an OSIPI code contribution"
)
raise NotImplementedError(msg)


Expand Down Expand Up @@ -156,5 +164,8 @@ def aif_weinmann(t: np.ndarray, BAT: float = 0.0) -> np.ndarray:
>>> plt.plot(t,ca)
"""
msg = "This function is not yet implemented \n"
msg += "If you implement it yourself, please consider submitting it as an OSIPI code contribution"
msg += (
"If you implement it yourself, please consider submitting it"
" as an OSIPI code contribution"
)
raise NotImplementedError(msg)
32 changes: 23 additions & 9 deletions src/osipi/_tissue.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ 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 @@ -77,7 +80,9 @@ 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 @@ -118,7 +123,9 @@ 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 Down Expand Up @@ -219,7 +226,10 @@ 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 @@ -230,7 +240,9 @@ 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 @@ -272,7 +284,9 @@ 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 @@ -293,9 +307,9 @@ def extended_tofts(
convolution = np.convolve(ca_resampled, imp_resampled)

# 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
20 changes: 15 additions & 5 deletions tests/test_tissue.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ def test_tissue_tofts():
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")
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
)
Expand All @@ -55,13 +59,17 @@ def test_tissue_tofts():
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 @@ -81,7 +89,9 @@ def test_tissue_extended_tofts():
# 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.extended_tofts(t, ca, Ktrans=0.6, ve=0.2, vp=0.3, Ta=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
Expand Down

0 comments on commit 29a406a

Please sign in to comment.