Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vovaf709 committed Dec 25, 2023
1 parent aa04268 commit dff0601
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
33 changes: 20 additions & 13 deletions imops/interp2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,22 @@ class Linear2DInterpolator(Linear2DInterpolatorCpp):
"""

def __init__(
self, points: np.ndarray, values: np.ndarray, num_threads: int = 1, triangles: np.ndarray = None, **kwargs
self,
points: np.ndarray,
values: np.ndarray = None,
num_threads: int = 1,
triangles: np.ndarray = None,
**kwargs,
):
if triangles is not None:
if isinstance(triangles, np.ndarray):
if triangles.ndim != 2 or triangles.shape[1] != 3 or triangles.shape[0] * 3 != triangles.size:
raise ValueError('Passed \"triangles\" argument has an incorrect shape')
raise ValueError('Passed `triangles` argument has an incorrect shape')

Check warning on line 57 in imops/interp2d.py

View check run for this annotation

Codecov / codecov/patch

imops/interp2d.py#L57

Added line #L57 was not covered by tests
else:
raise ValueError(f'Wrong type of \"triangles\" argument, expected np.ndarray. Got {type(triangles)}')
raise ValueError(f'Wrong type of `triangles` argument, expected np.ndarray. Got {type(triangles)}')

Check warning on line 59 in imops/interp2d.py

View check run for this annotation

Codecov / codecov/patch

imops/interp2d.py#L59

Added line #L59 was not covered by tests

if not isinstance(points, np.ndarray):
raise ValueError(f'Wrong type of `points` argument, expected np.ndarray. Got {type(points)}')

Check warning on line 62 in imops/interp2d.py

View check run for this annotation

Codecov / codecov/patch

imops/interp2d.py#L62

Added line #L62 was not covered by tests

super().__init__(points, num_threads, triangles)
self.kdtree = KDTree(data=points, **kwargs)
Expand All @@ -77,24 +85,23 @@ def __call__(self, points: np.ndarray, values: np.ndarray = None, fill_value: fl
new_values: np.ndarray
interpolated values at given points
"""
if values is not None:
if isinstance(values, np.ndarray):
if values.ndim > 1:
raise ValueError(f'Wrong shape of \"values\" argument, expected ndim=1. Got shape {values.shape}')
else:
raise ValueError(f'Wrong type of \"values\" argument, expected np.ndarray. Got {type(values)}')

self.values = self.values if values is None else values
self.values = values or self.values

if self.values is None:
raise ValueError('\"values\" argument was never passed neither in __init__ or __call__ methods')
raise ValueError('`values` argument was never passed neither in __init__ or __call__ methods')

if isinstance(self.values, np.ndarray):
if self.values.ndim > 1:
raise ValueError(f'Wrong shape of `values` argument, expected ndim=1. Got shape {self.values.shape}')

Check warning on line 95 in imops/interp2d.py

View check run for this annotation

Codecov / codecov/patch

imops/interp2d.py#L95

Added line #L95 was not covered by tests
else:
raise ValueError(f'Wrong type of `values` argument, expected np.ndarray. Got {type(self.values)}')

_, neighbors = self.kdtree.query(
points, 1, **{'workers': self.num_threads} if python_version()[:3] != '3.6' else {}
)

if not isinstance(points, np.ndarray):
raise ValueError(f'Wrong type of \"points\" argument, expected np.ndarray. Got {type(points)}')
raise ValueError(f'Wrong type of `points` argument, expected np.ndarray. Got {type(points)}')

Check warning on line 104 in imops/interp2d.py

View check run for this annotation

Codecov / codecov/patch

imops/interp2d.py#L104

Added line #L104 was not covered by tests

int_values = super().__call__(points, self.values, neighbors, fill_value)
return int_values
45 changes: 44 additions & 1 deletion tests/test_interp2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from imops.interp2d import Linear2DInterpolator


tests_root = Path(__file__).parent
np.random.seed(1337)


Expand All @@ -17,8 +18,16 @@ def num_threads(request):
return request.param


@pytest.fixture
def example():
x = load(tests_root / 'test_data' / 'arr_0.npy.gz')
int_points = np.transpose((np.isnan(x)).nonzero())
x_points = np.transpose((~np.isnan(x)).nonzero())

return x_points, int_points


def test_test_data(num_threads):
tests_root = Path(__file__).parent
for i in range(2):
x = load(tests_root / 'test_data' / f'arr_{i}.npy.gz')
int_points = np.transpose((np.isnan(x)).nonzero())
Expand Down Expand Up @@ -56,3 +65,37 @@ def test_test_data(num_threads):
assert (
delta_di.max() <= 1.5 and delta_ds.max() <= 1e-10 and delta_si.max() <= 1.5
), f'Failed with small case: arr_{i}'


def test_no_values(example):
x_points, int_points = example

with pytest.raises(ValueError):
Linear2DInterpolator(x_points)(int_points)


def test_bad_values_dtype(example):
x_points, int_points = example

with pytest.raises(ValueError):
Linear2DInterpolator(x_points, values=[1, 2, 3])(int_points)
with pytest.raises(ValueError):
Linear2DInterpolator(x_points)(int_points, values=[1, 2, 3])
with pytest.raises(ValueError):
Linear2DInterpolator(x_points)(int_points, values=np.ones((3, 3)))


def test_bad_values_lenght(example):
x_points, int_points = example

values = np.ones(len(x_points) + 10)
with pytest.raises(ValueError):
Linear2DInterpolator(x_points, values=values)(int_points)
with pytest.raises(ValueError):
Linear2DInterpolator(x_points)(int_points, values=values)

values = values[:-20]
with pytest.raises(ValueError):
Linear2DInterpolator(x_points, values=values)(int_points)
with pytest.raises(ValueError):
Linear2DInterpolator(x_points)(int_points, values=values)

0 comments on commit dff0601

Please sign in to comment.