From 71af711d50ea21923d725d9e0b75fe9f7b9f81ac Mon Sep 17 00:00:00 2001 From: Samuel Farrens Date: Tue, 14 Mar 2023 17:22:03 +0100 Subject: [PATCH 1/5] removed unnormalised Gaussian kernel option and corresponding test --- modopt/math/stats.py | 40 +++++++++++++++++---------------------- modopt/tests/test_math.py | 10 ---------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/modopt/math/stats.py b/modopt/math/stats.py index 3ac818a7..6bbbdef1 100644 --- a/modopt/math/stats.py +++ b/modopt/math/stats.py @@ -18,7 +18,7 @@ import_astropy = True -def gaussian_kernel(data_shape, sigma, norm='max'): +def gaussian_kernel(data_shape, sigma, norm="max"): """Gaussian kernel. This method produces a Gaussian kerenal of a specified size and dispersion. @@ -60,22 +60,19 @@ def gaussian_kernel(data_shape, sigma, norm='max'): """ if not import_astropy: # pragma: no cover - raise ImportError('Astropy package not found.') + raise ImportError("Astropy package not found.") - if norm not in {'max', 'sum', 'none'}: + if norm not in {"max", "sum"}: raise ValueError('Invalid norm, options are "max", "sum" or "none".') kernel = np.array( Gaussian2DKernel(sigma, x_size=data_shape[1], y_size=data_shape[0]), ) - if norm == 'max': + if norm == "max": return kernel / np.max(kernel) - elif norm == 'sum': - return kernel / np.sum(kernel) - - elif norm == 'none': + else: return kernel @@ -147,7 +144,7 @@ def mse(data1, data2): return np.mean((data1 - data2) ** 2) -def psnr(data1, data2, method='starck', max_pix=255): +def psnr(data1, data2, method="starck", max_pix=255): r"""Peak Signal-to-Noise Ratio. This method calculates the Peak Signal-to-Noise Ratio between two data @@ -202,23 +199,21 @@ def psnr(data1, data2, method='starck', max_pix=255): 10\log_{10}(\mathrm{MSE})) """ - if method == 'starck': - return ( - 20 * np.log10( - (data1.shape[0] * np.abs(np.max(data1) - np.min(data1))) - / np.linalg.norm(data1 - data2), - ) + if method == "starck": + return 20 * np.log10( + (data1.shape[0] * np.abs(np.max(data1) - np.min(data1))) + / np.linalg.norm(data1 - data2), ) - elif method == 'wiki': - return (20 * np.log10(max_pix) - 10 * np.log10(mse(data1, data2))) + elif method == "wiki": + return 20 * np.log10(max_pix) - 10 * np.log10(mse(data1, data2)) raise ValueError( 'Invalid PSNR method. Options are "starck" and "wiki"', ) -def psnr_stack(data1, data2, metric=np.mean, method='starck'): +def psnr_stack(data1, data2, metric=np.mean, method="starck"): """Peak Signa-to-Noise for stack of images. This method calculates the PSNRs for two stacks of 2D arrays. @@ -261,12 +256,11 @@ def psnr_stack(data1, data2, metric=np.mean, method='starck'): """ if data1.ndim != 3 or data2.ndim != 3: - raise ValueError('Input data must be a 3D np.ndarray') + raise ValueError("Input data must be a 3D np.ndarray") - return metric([ - psnr(i_elem, j_elem, method=method) - for i_elem, j_elem in zip(data1, data2) - ]) + return metric( + [psnr(i_elem, j_elem, method=method) for i_elem, j_elem in zip(data1, data2)] + ) def sigma_mad(input_data): diff --git a/modopt/tests/test_math.py b/modopt/tests/test_math.py index e44011c9..ea177b15 100644 --- a/modopt/tests/test_math.py +++ b/modopt/tests/test_math.py @@ -284,16 +284,6 @@ class TestStats: ] ), ), - ( - "none", - np.array( - [ - [0.05854983, 0.09653235, 0.05854983], - [0.09653235, 0.15915494, 0.09653235], - [0.05854983, 0.09653235, 0.05854983], - ] - ), - ), failparam("fail", None, raises=ValueError), ], ) From 8046d103e16979012b979b0fbac430d930b40a74 Mon Sep 17 00:00:00 2001 From: Samuel Farrens Date: Tue, 14 Mar 2023 18:20:55 +0100 Subject: [PATCH 2/5] Restrict scikit-image version for testing --- .github/workflows/ci-build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci-build.yml b/.github/workflows/ci-build.yml index cdfff840..3bf673ea 100644 --- a/.github/workflows/ci-build.yml +++ b/.github/workflows/ci-build.yml @@ -43,7 +43,7 @@ jobs: python -m pip install --upgrade pip python -m pip install -r develop.txt python -m pip install -r docs/requirements.txt - python -m pip install astropy scikit-image scikit-learn + python -m pip install astropy "scikit-image<0.20" scikit-learn python -m pip install tensorflow>=2.4.1 python -m pip install twine python -m pip install . From f8e5926efde17ec56cc4d5369709d05bcf000c6e Mon Sep 17 00:00:00 2001 From: Samuel Farrens Date: Tue, 14 Mar 2023 18:26:11 +0100 Subject: [PATCH 3/5] added fix for basic test suite --- .github/workflows/ci-build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci-build.yml b/.github/workflows/ci-build.yml index 3bf673ea..b24bea79 100644 --- a/.github/workflows/ci-build.yml +++ b/.github/workflows/ci-build.yml @@ -108,7 +108,7 @@ jobs: python --version python -m pip install --upgrade pip python -m pip install -r develop.txt - python -m pip install astropy scikit-image scikit-learn + python -m pip install astropy "scikit-image<0.20" scikit-learn python -m pip install . - name: Run Tests From 0137a3858b4463e1e7a2408e505da33367d22bfa Mon Sep 17 00:00:00 2001 From: Samuel Farrens Date: Wed, 15 Mar 2023 11:44:31 +0100 Subject: [PATCH 4/5] set behaviour for different astropy versions --- modopt/math/stats.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modopt/math/stats.py b/modopt/math/stats.py index 6bbbdef1..09871ca3 100644 --- a/modopt/math/stats.py +++ b/modopt/math/stats.py @@ -11,6 +11,8 @@ import numpy as np try: + from packaging import version + from astropy import __version__ as astropy_version from astropy.convolution import Gaussian2DKernel except ImportError: # pragma: no cover import_astropy = False @@ -72,6 +74,9 @@ def gaussian_kernel(data_shape, sigma, norm="max"): if norm == "max": return kernel / np.max(kernel) + elif version.parse(astropy_version) < version.parse("5.2"): + return kernel / np.sum(kernel) + else: return kernel From a961c3cf9e9b8ff4c91489aa840937afb5c27adc Mon Sep 17 00:00:00 2001 From: Samuel Farrens Date: Wed, 15 Mar 2023 13:51:11 +0100 Subject: [PATCH 5/5] updated docstring for gaussian_kernel --- modopt/math/stats.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/modopt/math/stats.py b/modopt/math/stats.py index 09871ca3..59bf6759 100644 --- a/modopt/math/stats.py +++ b/modopt/math/stats.py @@ -31,9 +31,8 @@ def gaussian_kernel(data_shape, sigma, norm="max"): Desiered shape of the kernel sigma : float Standard deviation of the kernel - norm : {'max', 'sum', 'none'}, optional - Normalisation of the kerenl (options are ``'max'``, ``'sum'`` or - ``'none'``, default is ``'max'``) + norm : {'max', 'sum'}, optional + Normalisation of the kerenl (options are ``'max'`` or ``'sum'``, default is ``'max'``) Returns -------