From 29a406ac6a1c9b0f99133071ead27d7807589305 Mon Sep 17 00:00:00 2001 From: Mohamed Nasser Date: Wed, 5 Jun 2024 00:36:48 +0300 Subject: [PATCH] flake commented --- .pre-commit-config.yaml | 4 +-- docs/examples/tissue/plot_extended_tofts.py | 8 +++-- docs/examples/tissue/plot_tofts.py | 8 +++-- .../examples/tissue/plot_extended_tofts.py | 8 +++-- .../generated/examples/tissue/plot_tofts.py | 8 +++-- manage.py | 4 ++- src/osipi/_aif.py | 19 ++++++++--- src/osipi/_tissue.py | 32 +++++++++++++------ tests/test_tissue.py | 20 +++++++++--- 9 files changed, 82 insertions(+), 29 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c5880b2..a0fb973 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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] diff --git a/docs/examples/tissue/plot_extended_tofts.py b/docs/examples/tissue/plot_extended_tofts.py index e1a34f9..87f135c 100755 --- a/docs/examples/tissue/plot_extended_tofts.py +++ b/docs/examples/tissue/plot_extended_tofts.py @@ -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)") diff --git a/docs/examples/tissue/plot_tofts.py b/docs/examples/tissue/plot_tofts.py index 2726eb0..7eb8782 100755 --- a/docs/examples/tissue/plot_tofts.py +++ b/docs/examples/tissue/plot_tofts.py @@ -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)") diff --git a/docs/source/generated/examples/tissue/plot_extended_tofts.py b/docs/source/generated/examples/tissue/plot_extended_tofts.py index e1a34f9..87f135c 100755 --- a/docs/source/generated/examples/tissue/plot_extended_tofts.py +++ b/docs/source/generated/examples/tissue/plot_extended_tofts.py @@ -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)") diff --git a/docs/source/generated/examples/tissue/plot_tofts.py b/docs/source/generated/examples/tissue/plot_tofts.py index 2726eb0..7eb8782 100755 --- a/docs/source/generated/examples/tissue/plot_tofts.py +++ b/docs/source/generated/examples/tissue/plot_tofts.py @@ -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)") diff --git a/manage.py b/manage.py index 09e7f9f..8d6390a 100755 --- a/manage.py +++ b/manage.py @@ -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__": diff --git a/src/osipi/_aif.py b/src/osipi/_aif.py index ee76612..24fdfc9 100755 --- a/src/osipi/_aif.py +++ b/src/osipi/_aif.py @@ -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: @@ -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 @@ -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) @@ -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) diff --git a/src/osipi/_tissue.py b/src/osipi/_tissue.py index e23758d..c5f5d5e 100755 --- a/src/osipi/_tissue.py +++ b/src/osipi/_tissue.py @@ -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, ) @@ -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( @@ -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, @@ -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, ) @@ -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( @@ -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, @@ -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( diff --git a/tests/test_tissue.py b/tests/test_tissue.py index 898a334..74b963b 100755 --- a/tests/test_tissue.py +++ b/tests/test_tissue.py @@ -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 ) @@ -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 @@ -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