From bd9a81662c8fdf57105aa63e9bd545ace5b0e651 Mon Sep 17 00:00:00 2001 From: Alexey Belkov Date: Wed, 31 Jan 2024 14:07:53 +0300 Subject: [PATCH 1/9] microbug fix in interp2d --- imops/interp2d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imops/interp2d.py b/imops/interp2d.py index cb94dee8..976a60b6 100644 --- a/imops/interp2d.py +++ b/imops/interp2d.py @@ -90,7 +90,7 @@ def __call__(self, points: np.ndarray, values: np.ndarray = None, fill_value: fl new_values: np.ndarray interpolated values at given points """ - self.values = values or self.values + self.values = values if values is not None else self.values if self.values is None: raise ValueError('`values` argument was never passed neither in __init__ or __call__ methods') From f4678f827d3cddc5cfb77084203c048c67cf9e16 Mon Sep 17 00:00:00 2001 From: Alexey Belkov Date: Wed, 31 Jan 2024 14:30:36 +0300 Subject: [PATCH 2/9] remove rewriting values in interp2d --- imops/interp2d.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/imops/interp2d.py b/imops/interp2d.py index 976a60b6..ba43d481 100644 --- a/imops/interp2d.py +++ b/imops/interp2d.py @@ -81,7 +81,7 @@ def __call__(self, points: np.ndarray, values: np.ndarray = None, fill_value: fl points: np.ndarray 2-D array of data point coordinates to interpolate at values: np.ndarray - 1-D array of fp32/fp64 values to use at initial points. If passed, existing values will be rewritten + 1-D array of fp32/fp64 values to use at initial points. If passed, existing values will NOT be rewritten fill_value: float value to fill past edges @@ -90,16 +90,16 @@ def __call__(self, points: np.ndarray, values: np.ndarray = None, fill_value: fl new_values: np.ndarray interpolated values at given points """ - self.values = values if values is not None else self.values + x_values = values if values is not None else self.values - if self.values is None: + if x_values is None: raise ValueError('`values` argument was never passed neither in __init__ or __call__ methods') - if not isinstance(self.values, np.ndarray): - raise TypeError(f'Wrong type of `values` argument, expected np.ndarray. Got {type(self.values)}') + if not isinstance(x_values, np.ndarray): + raise TypeError(f'Wrong type of `values` argument, expected np.ndarray. Got {type(x_values)}') - if self.values.ndim > 1: - raise ValueError(f'Wrong shape of `values` argument, expected ndim=1. Got shape {self.values.shape}') + if x_values.ndim > 1: + raise ValueError(f'Wrong shape of `values` argument, expected ndim=1. Got shape {x_values.shape}') _, neighbors = self.kdtree.query( points, 1, **{'workers': self.num_threads} if python_version()[:3] != '3.6' else {} @@ -108,4 +108,4 @@ def __call__(self, points: np.ndarray, values: np.ndarray = None, fill_value: fl if not isinstance(points, np.ndarray): raise TypeError(f'Wrong type of `points` argument, expected np.ndarray. Got {type(points)}') - return super().__call__(points, self.values, neighbors, fill_value) + return super().__call__(points, x_values, neighbors, fill_value) From 6896bae66fe21bb61ca74fb73647f019d6d68ef1 Mon Sep 17 00:00:00 2001 From: Alexey Belkov Date: Thu, 1 Feb 2024 11:39:28 +0300 Subject: [PATCH 3/9] add test, version upd --- imops/__version__.py | 2 +- tests/test_interp2d.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/imops/__version__.py b/imops/__version__.py index 73f83151..37e9d6dd 100644 --- a/imops/__version__.py +++ b/imops/__version__.py @@ -1 +1 @@ -__version__ = '0.8.5' +__version__ = '0.8.6' diff --git a/tests/test_interp2d.py b/tests/test_interp2d.py index 0d2c1276..f2b032a1 100644 --- a/tests/test_interp2d.py +++ b/tests/test_interp2d.py @@ -74,6 +74,15 @@ def test_no_values(example): Linear2DInterpolator(x_points)(int_points) +def test_no_changes_in_values(example): + x_points, int_points = example + first_values = np.ones((x_points.shape[0], ), dtype=float) + second_values = 2.0 * np.ones((x_points.shape[0], ), dtype=float) + interpolator = Linear2DInterpolator(x_points, first_values) + interpolator(int_points, second_values) + assert np.all(interpolator.values == first_values), f'Failed with changes in self.values after __call__' + + def test_bad_values_dtype(example): x_points, int_points = example From bdf950c36b405cce1203114e528b020e562c8c4d Mon Sep 17 00:00:00 2001 From: Alexey Belkov Date: Thu, 1 Feb 2024 11:43:53 +0300 Subject: [PATCH 4/9] codestyle :kekw: --- tests/test_interp2d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_interp2d.py b/tests/test_interp2d.py index f2b032a1..5f97c07d 100644 --- a/tests/test_interp2d.py +++ b/tests/test_interp2d.py @@ -80,7 +80,7 @@ def test_no_changes_in_values(example): second_values = 2.0 * np.ones((x_points.shape[0], ), dtype=float) interpolator = Linear2DInterpolator(x_points, first_values) interpolator(int_points, second_values) - assert np.all(interpolator.values == first_values), f'Failed with changes in self.values after __call__' + assert np.all(interpolator.values == first_values), 'Failed with changes in self.values after __call__' def test_bad_values_dtype(example): From 0ef153389a8cf65ee0e7511fdc0db0d29702ec91 Mon Sep 17 00:00:00 2001 From: Philipenko Vladimir Date: Thu, 1 Feb 2024 11:59:31 +0300 Subject: [PATCH 5/9] codestyle 2 --- _build_utils.py | 2 +- tests/test_interp2d.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/_build_utils.py b/_build_utils.py index b236a685..e9cee776 100644 --- a/_build_utils.py +++ b/_build_utils.py @@ -13,7 +13,7 @@ class LazyImport(dict): # https://github.com/cython/cython/blob/6ad6ca0e9e7d030354b7fe7d7b56c3f6e6a4bc23/Cython/Compiler/ModuleNode.py#L773 def __init__(self, module_name): self.module_name = module_name - return super().__init__(self, description=self.__doc__) + super().__init__(self, description=self.__doc__) # Must be hashable due to # https://github.com/cython/cython/blob/6ad6ca0e9e7d030354b7fe7d7b56c3f6e6a4bc23/Cython/Compiler/Main.py#L307 diff --git a/tests/test_interp2d.py b/tests/test_interp2d.py index 5f97c07d..55d28221 100644 --- a/tests/test_interp2d.py +++ b/tests/test_interp2d.py @@ -76,8 +76,8 @@ def test_no_values(example): def test_no_changes_in_values(example): x_points, int_points = example - first_values = np.ones((x_points.shape[0], ), dtype=float) - second_values = 2.0 * np.ones((x_points.shape[0], ), dtype=float) + first_values = np.ones((x_points.shape[0],), dtype=float) + second_values = 2.0 * np.ones((x_points.shape[0],), dtype=float) interpolator = Linear2DInterpolator(x_points, first_values) interpolator(int_points, second_values) assert np.all(interpolator.values == first_values), 'Failed with changes in self.values after __call__' From 15dca7da4df2363a37c77e76a1e4eea526ea969f Mon Sep 17 00:00:00 2001 From: Philipenko Vladimir Date: Thu, 1 Feb 2024 12:02:13 +0300 Subject: [PATCH 6/9] bump actions --- .github/workflows/release.yml | 2 +- .github/workflows/test_build.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b59edf03..0ee32c90 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -42,7 +42,7 @@ jobs: steps: - uses: actions/checkout@v3 - - uses: actions/setup-python@v3 + - uses: actions/setup-python@v4 - name: Install cibuildwheel run: python -m pip install cibuildwheel==2.10.0 - name: Install gcc for mac diff --git a/.github/workflows/test_build.yml b/.github/workflows/test_build.yml index 25b05bed..587a129e 100644 --- a/.github/workflows/test_build.yml +++ b/.github/workflows/test_build.yml @@ -16,7 +16,7 @@ jobs: steps: - uses: actions/checkout@v3 - - uses: actions/setup-python@v3 + - uses: actions/setup-python@v4 - name: Install cibuildwheel run: python -m pip install cibuildwheel==2.10.0 - name: Install gcc for mac From aa0c6a914d642fd2fdf465d87963aa1f53d69ff3 Mon Sep 17 00:00:00 2001 From: Alexey Belkov Date: Thu, 1 Feb 2024 18:51:45 +0300 Subject: [PATCH 7/9] fix comments --- imops/interp2d.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/imops/interp2d.py b/imops/interp2d.py index ba43d481..e870be1b 100644 --- a/imops/interp2d.py +++ b/imops/interp2d.py @@ -81,7 +81,7 @@ def __call__(self, points: np.ndarray, values: np.ndarray = None, fill_value: fl points: np.ndarray 2-D array of data point coordinates to interpolate at values: np.ndarray - 1-D array of fp32/fp64 values to use at initial points. If passed, existing values will NOT be rewritten + 1-D array of fp32/fp64 values to use at initial points fill_value: float value to fill past edges @@ -90,16 +90,17 @@ def __call__(self, points: np.ndarray, values: np.ndarray = None, fill_value: fl new_values: np.ndarray interpolated values at given points """ - x_values = values if values is not None else self.values + if values is None: + values = self.values - if x_values is None: + if values is None: raise ValueError('`values` argument was never passed neither in __init__ or __call__ methods') - if not isinstance(x_values, np.ndarray): - raise TypeError(f'Wrong type of `values` argument, expected np.ndarray. Got {type(x_values)}') + if not isinstance(values, np.ndarray): + raise TypeError(f'Wrong type of `values` argument, expected np.ndarray. Got {type(values)}') - if x_values.ndim > 1: - raise ValueError(f'Wrong shape of `values` argument, expected ndim=1. Got shape {x_values.shape}') + if values.ndim > 1: + raise ValueError(f'Wrong shape of `values` argument, expected ndim=1. Got shape {values.shape}') _, neighbors = self.kdtree.query( points, 1, **{'workers': self.num_threads} if python_version()[:3] != '3.6' else {} @@ -108,4 +109,4 @@ def __call__(self, points: np.ndarray, values: np.ndarray = None, fill_value: fl if not isinstance(points, np.ndarray): raise TypeError(f'Wrong type of `points` argument, expected np.ndarray. Got {type(points)}') - return super().__call__(points, x_values, neighbors, fill_value) + return super().__call__(points, values, neighbors, fill_value) From bdf384817f665cc5baa27c29885f3ab3f1d5eb92 Mon Sep 17 00:00:00 2001 From: Alexey Belkov Date: Fri, 2 Feb 2024 00:42:40 +0300 Subject: [PATCH 8/9] more tests :billy_sleep: --- imops/interp2d.py | 14 ++++++++++---- tests/test_interp2d.py | 24 +++++++++++++++++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/imops/interp2d.py b/imops/interp2d.py index e870be1b..36c5daf8 100644 --- a/imops/interp2d.py +++ b/imops/interp2d.py @@ -53,11 +53,14 @@ def __init__( if triangles is not None: if not isinstance(triangles, np.ndarray): raise TypeError(f'Wrong type of `triangles` argument, expected np.ndarray. Got {type(triangles)}') - if triangles.ndim != 2 or triangles.shape[1] != 3 or triangles.shape[0] * 3 != triangles.size: + if triangles.ndim != 2 or triangles.shape[1] != 3: raise ValueError('Passed `triangles` argument has an incorrect shape') if not isinstance(points, np.ndarray): raise TypeError(f'Wrong type of `points` argument, expected np.ndarray. Got {type(points)}') + + if points.ndim != 2 or points.shape[1] != 2: + raise ValueError('Passed `points` argument has an incorrect shape') if values is not None: if not isinstance(values, np.ndarray): @@ -101,12 +104,15 @@ def __call__(self, points: np.ndarray, values: np.ndarray = None, fill_value: fl if values.ndim > 1: raise ValueError(f'Wrong shape of `values` argument, expected ndim=1. Got shape {values.shape}') + + if not isinstance(points, np.ndarray): + raise TypeError(f'Wrong type of `points` argument, expected np.ndarray. Got {type(points)}') + + if points.ndim != 2 or points.shape[1] != 2: + raise ValueError('Passed `points` argument has an incorrect shape') _, neighbors = self.kdtree.query( points, 1, **{'workers': self.num_threads} if python_version()[:3] != '3.6' else {} ) - if not isinstance(points, np.ndarray): - raise TypeError(f'Wrong type of `points` argument, expected np.ndarray. Got {type(points)}') - return super().__call__(points, values, neighbors, fill_value) diff --git a/tests/test_interp2d.py b/tests/test_interp2d.py index 55d28221..880d92e3 100644 --- a/tests/test_interp2d.py +++ b/tests/test_interp2d.py @@ -41,7 +41,6 @@ def test_test_data(num_threads): scipy_values = griddata(x_points, x_values, int_points, method='linear', fill_value=0.0) delta_ds = np.abs(delaunay_values - scipy_values) - # delta_di = np.abs(delaunay_values - imops_values) delta_si = np.abs(scipy_values - imops_values) assert delta_ds.max() <= 1e-10 and delta_si.max() <= 5, f'Failed with big case, arr_{i}' @@ -108,3 +107,26 @@ def test_bad_values_lenght(example): Linear2DInterpolator(x_points, values=values)(int_points) with pytest.raises(ValueError): Linear2DInterpolator(x_points)(int_points, values=values) + + +def test_bad_triangles_dtype(example): + x_points, _ = example + + with pytest.raises(TypeError): + Linear2DInterpolator(x_points, triangles=[1, 2, 3]) + with pytest.raises(ValueError): + Linear2DInterpolator(x_points, triangles=np.ones((3, 2))) + + +def test_bad_points_dtype(example): + x_points, _ = example + + values = np.ones((x_points.shape[0],)) + with pytest.raises(TypeError): + Linear2DInterpolator(points=[1, 2, 3]) + with pytest.raises(TypeError): + Linear2DInterpolator(points=x_points, values=values)(points=[1, 2, 3]) + with pytest.raises(ValueError): + Linear2DInterpolator(points=np.ones((3, 3))) + with pytest.raises(ValueError): + Linear2DInterpolator(points=x_points, values=values)(points=np.ones((3, 3))) From 5212e88f0fcb116c5e6ecdefb2589e70faf8021b Mon Sep 17 00:00:00 2001 From: Alexey Belkov Date: Fri, 2 Feb 2024 00:47:41 +0300 Subject: [PATCH 9/9] codestyle :angrypepe: --- imops/interp2d.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/imops/interp2d.py b/imops/interp2d.py index 36c5daf8..60d8d982 100644 --- a/imops/interp2d.py +++ b/imops/interp2d.py @@ -58,7 +58,7 @@ def __init__( if not isinstance(points, np.ndarray): raise TypeError(f'Wrong type of `points` argument, expected np.ndarray. Got {type(points)}') - + if points.ndim != 2 or points.shape[1] != 2: raise ValueError('Passed `points` argument has an incorrect shape') @@ -104,10 +104,10 @@ def __call__(self, points: np.ndarray, values: np.ndarray = None, fill_value: fl if values.ndim > 1: raise ValueError(f'Wrong shape of `values` argument, expected ndim=1. Got shape {values.shape}') - + if not isinstance(points, np.ndarray): raise TypeError(f'Wrong type of `points` argument, expected np.ndarray. Got {type(points)}') - + if points.ndim != 2 or points.shape[1] != 2: raise ValueError('Passed `points` argument has an incorrect shape')