From abbbbb59247232162f2c9812cd9ef189d94b66f5 Mon Sep 17 00:00:00 2001 From: github-actions Date: Wed, 5 Jun 2024 12:25:26 +0000 Subject: [PATCH] Apply lint auto-fixes --- docs/examples/aif/plot_aif_parker.py | 1 - docs/examples/aif/plot_dummy.py | 1 - docs/examples/tissue/plot_extended_tofts.py | 9 +---- docs/examples/tissue/plot_tofts.py | 9 +---- docs/source/conf.py | 1 - .../generated/examples/aif/plot_aif_parker.py | 1 - .../generated/examples/aif/plot_dummy.py | 1 - .../examples/tissue/plot_extended_tofts.py | 9 +---- .../generated/examples/tissue/plot_tofts.py | 9 +---- manage.py | 8 +--- src/osipi/_aif.py | 14 ++----- src/osipi/_tissue.py | 36 +++++------------ tests/test_tissue.py | 40 +++++-------------- 13 files changed, 33 insertions(+), 106 deletions(-) diff --git a/docs/examples/aif/plot_aif_parker.py b/docs/examples/aif/plot_aif_parker.py index df58db5..87ad903 100755 --- a/docs/examples/aif/plot_aif_parker.py +++ b/docs/examples/aif/plot_aif_parker.py @@ -6,7 +6,6 @@ """ import matplotlib.pyplot as plt - # %% # Import necessary packages import numpy as np diff --git a/docs/examples/aif/plot_dummy.py b/docs/examples/aif/plot_dummy.py index 9dd4f1c..5dca336 100755 --- a/docs/examples/aif/plot_dummy.py +++ b/docs/examples/aif/plot_dummy.py @@ -7,7 +7,6 @@ """ import matplotlib.pyplot as plt - # %% # Import necessary packages import numpy as np diff --git a/docs/examples/tissue/plot_extended_tofts.py b/docs/examples/tissue/plot_extended_tofts.py index 5b8811f..6b41818 100755 --- a/docs/examples/tissue/plot_extended_tofts.py +++ b/docs/examples/tissue/plot_extended_tofts.py @@ -7,7 +7,6 @@ """ import matplotlib.pyplot as plt - # %% # Import necessary packages import numpy as np @@ -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)") diff --git a/docs/examples/tissue/plot_tofts.py b/docs/examples/tissue/plot_tofts.py index 1906d39..2736e9c 100755 --- a/docs/examples/tissue/plot_tofts.py +++ b/docs/examples/tissue/plot_tofts.py @@ -7,7 +7,6 @@ """ import matplotlib.pyplot as plt - # %% # Import necessary packages import numpy as np @@ -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)") diff --git a/docs/source/conf.py b/docs/source/conf.py index ee5f9f1..4511361 100755 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -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 diff --git a/docs/source/generated/examples/aif/plot_aif_parker.py b/docs/source/generated/examples/aif/plot_aif_parker.py index df58db5..87ad903 100755 --- a/docs/source/generated/examples/aif/plot_aif_parker.py +++ b/docs/source/generated/examples/aif/plot_aif_parker.py @@ -6,7 +6,6 @@ """ import matplotlib.pyplot as plt - # %% # Import necessary packages import numpy as np diff --git a/docs/source/generated/examples/aif/plot_dummy.py b/docs/source/generated/examples/aif/plot_dummy.py index 9dd4f1c..5dca336 100755 --- a/docs/source/generated/examples/aif/plot_dummy.py +++ b/docs/source/generated/examples/aif/plot_dummy.py @@ -7,7 +7,6 @@ """ import matplotlib.pyplot as plt - # %% # Import necessary packages import numpy as np diff --git a/docs/source/generated/examples/tissue/plot_extended_tofts.py b/docs/source/generated/examples/tissue/plot_extended_tofts.py index 5b8811f..6b41818 100755 --- a/docs/source/generated/examples/tissue/plot_extended_tofts.py +++ b/docs/source/generated/examples/tissue/plot_extended_tofts.py @@ -7,7 +7,6 @@ """ import matplotlib.pyplot as plt - # %% # Import necessary packages import numpy as np @@ -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)") diff --git a/docs/source/generated/examples/tissue/plot_tofts.py b/docs/source/generated/examples/tissue/plot_tofts.py index 1906d39..2736e9c 100755 --- a/docs/source/generated/examples/tissue/plot_tofts.py +++ b/docs/source/generated/examples/tissue/plot_tofts.py @@ -7,7 +7,6 @@ """ import matplotlib.pyplot as plt - # %% # Import necessary packages import numpy as np @@ -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)") diff --git a/manage.py b/manage.py index 5e3a2ed..a3107ae 100755 --- a/manage.py +++ b/manage.py @@ -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") @@ -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__": diff --git a/src/osipi/_aif.py b/src/osipi/_aif.py index cfe9135..c6ab1a8 100755 --- a/src/osipi/_aif.py +++ b/src/osipi/_aif.py @@ -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: @@ -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 diff --git a/src/osipi/_tissue.py b/src/osipi/_tissue.py index 9abec8a..3e9e13d 100755 --- a/src/osipi/_tissue.py +++ b/src/osipi/_tissue.py @@ -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, ) @@ -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( @@ -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, @@ -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( @@ -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, ) @@ -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( @@ -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, @@ -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( diff --git a/tests/test_tissue.py b/tests/test_tissue.py index c74019f..00ebfdd 100755 --- a/tests/test_tissue.py +++ b/tests/test_tissue.py @@ -30,18 +30,14 @@ 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 @@ -49,15 +45,9 @@ def test_tissue_tofts(): 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) @@ -65,17 +55,13 @@ 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 @@ -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 @@ -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)