Skip to content

Commit

Permalink
added max line of 70
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedNasser8 committed Jun 4, 2024
1 parent 026bcdd commit f64bdb5
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 72 deletions.
22 changes: 7 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,14 @@ repos:
- id: end-of-file-fixer
- id: check-yaml

# - repo: https://github.com/pycqa/flake8
# rev: 6.0.0
# hooks:
# - id: flake8
# args: [ '--max-line-length=80' ]

- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black

- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.10.1
hooks:
- id: isort

- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v1.6.0
hooks:
- id: autopep8

- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
args: [--line-length=80]
1 change: 1 addition & 0 deletions docs/examples/aif/plot_aif_parker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

import matplotlib.pyplot as plt

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

import matplotlib.pyplot as plt

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

import matplotlib.pyplot as plt

# %%
# Import necessary packages
import numpy as np
Expand Down Expand Up @@ -42,8 +43,7 @@
# 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
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
1 change: 1 addition & 0 deletions docs/examples/tissue/plot_tofts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

import matplotlib.pyplot as plt

# %%
# Import necessary packages
import numpy as np
Expand Down
4 changes: 3 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
# For the full list of built-in configuration values, see the documentation:
# 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
import os

project = "osipi"
copyright = "2022, OSIPI"
author = "OSIPI"
Expand Down
1 change: 1 addition & 0 deletions docs/source/generated/examples/aif/plot_aif_parker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

import matplotlib.pyplot as plt

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

import matplotlib.pyplot as plt

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

import matplotlib.pyplot as plt

# %%
# Import necessary packages
import numpy as np
Expand Down Expand Up @@ -42,8 +43,7 @@
# 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
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
1 change: 1 addition & 0 deletions docs/source/generated/examples/tissue/plot_tofts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

import matplotlib.pyplot as plt

# %%
# Import necessary packages
import numpy as np
Expand Down
8 changes: 5 additions & 3 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

# logging root configuration
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)


Expand Down Expand Up @@ -52,8 +53,9 @@ 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 Down
6 changes: 4 additions & 2 deletions src/osipi/_aif.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ def aif_parker(t: np.ndarray, BAT: float = 0.0, Hct: float = 0.0) -> np.ndarray:
# 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))
Expand Down
6 changes: 3 additions & 3 deletions src/osipi/_convolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ def exp_conv(T: float, t: np.ndarray, a: np.ndarray) -> np.ndarray:
n = len(t)
f = np.zeros((n,))

x = (t[1: n - 1] - t[0: n - 2]) / T
da = (a[1: n - 1] - a[0: n - 2]) / x
x = (t[1 : n - 1] - t[0 : n - 2]) / T
da = (a[1 : n - 1] - a[0 : n - 2]) / x

E = np.exp(-x)
E0 = 1 - E
E1 = x - E0

add = a[0: n - 2] * E0 + da * E1
add = a[0 : n - 2] * E0 + da * E1

for i in range(0, n - 2):
f[i + 1] = E[i] * f[i] + add[i]
Expand Down
81 changes: 58 additions & 23 deletions src/osipi/_tissue.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ def tofts(
if discretization_method == "exp": # Use exponential convolution
# Shift the AIF by the arterial delay time (if not zero)
if Ta != 0:
f = interp1d(t, ca, kind="linear",
bounds_error=False, fill_value=0)
f = interp1d(
t,
ca,
kind="linear",
bounds_error=False,
fill_value=0,
)
ca = (t > Ta) * f(t - Ta)

Tc = ve / Ktrans
Expand All @@ -94,8 +99,13 @@ def tofts(

# Shift the AIF by the arterial delay time (if not zero)
if Ta != 0:
f = interp1d(t, ca, kind="linear",
bounds_error=False, fill_value=0)
f = interp1d(
t,
ca,
kind="linear",
bounds_error=False,
fill_value=0,
)
ca = (t > Ta) * f(t - Ta)

# Check if time data grid is uniformly spaced
Expand All @@ -104,26 +114,34 @@ def tofts(
convolution = np.convolve(ca, imp)

# Discard unwanted points and make sure time spacing is correct
ct = convolution[0: len(t)] * t[1]
ct = convolution[0 : len(t)] * t[1]
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, kind="quadratic", bounds_error=False, fill_value=0
t,
ca,
kind="quadratic",
bounds_error=False,
fill_value=0,
)
imp_func = interp1d(
t, imp, kind="quadratic", bounds_error=False, fill_value=0
t,
imp,
kind="quadratic",
bounds_error=False,
fill_value=0,
)
ca_resampled = ca_func(t_resampled)
imp_resampled = imp_func(t_resampled)
# Convolve impulse response with AIF
convolution = np.convolve(ca_resampled, imp_resampled)

# 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 @@ -215,8 +233,13 @@ def extended_tofts(
if discretization_method == "exp": # Use exponential convolution
# Shift the AIF by the arterial delay time (if not zero)
if Ta != 0:
f = interp1d(t, ca, kind="linear",
bounds_error=False, fill_value=0)
f = interp1d(
t,
ca,
kind="linear",
bounds_error=False,
fill_value=0,
)
ca = (t > Ta) * f(t - Ta)

Tc = ve / Ktrans
Expand All @@ -230,8 +253,13 @@ def extended_tofts(

# Shift the AIF by the arterial delay time (if not zero)
if Ta != 0:
f = interp1d(t, ca, kind="linear",
bounds_error=False, fill_value=0)
f = interp1d(
t,
ca,
kind="linear",
bounds_error=False,
fill_value=0,
)
ca = (t > Ta) * f(t - Ta)

# Check if time data grid is uniformly spaced
Expand All @@ -240,27 +268,34 @@ def extended_tofts(
convolution = np.convolve(ca, imp)

# Discard unwanted points, make sure time spacing is correct and add vp*ca term for extended model
ct = convolution[0: len(t)] * t[1] + (vp * ca)
ct = convolution[0 : len(t)] * t[1] + (vp * ca)
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, kind="quadratic", bounds_error=False, fill_value=0
t,
ca,
kind="quadratic",
bounds_error=False,
fill_value=0,
)
imp_func = interp1d(
t, imp, kind="quadratic", bounds_error=False, fill_value=0
t,
imp,
kind="quadratic",
bounds_error=False,
fill_value=0,
)
ca_resampled = ca_func(t_resampled)
imp_resampled = imp_func(t_resampled)
# Convolve impulse response with AIF
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
4 changes: 2 additions & 2 deletions tests/test_aif.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def test_aif_georgiou():
# Not implemented yet so need to raise an error
t = np.arange(0, 6 * 60, 1)
try:
ca = osipi.aif_georgiou(t)
except:
osipi.aif_georgiou(t)
except Exception as ex:
assert True
else:
assert False
Expand Down
Loading

0 comments on commit f64bdb5

Please sign in to comment.