Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
rhugonnet committed Jun 8, 2024
1 parent 4e29c5e commit b0e2bff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
5 changes: 3 additions & 2 deletions geoutils/raster/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import numpy as np
import rasterio as rio
from scipy.interpolate import RectBivariateSpline, RegularGridInterpolator
from scipy.ndimage import map_coordinates, binary_dilation
from scipy.ndimage import binary_dilation, map_coordinates

from geoutils._typing import NDArrayNum, Number
from geoutils.raster.georeferencing import _coords, _outside_image, _res, _xy2ij

method_to_order = {"nearest": 0, "linear": 1, "cubic": 3, "quintic": 5, "slinear": 1, "pchip": 3, "splinef2d": 3}


def _interpn_interpolator(
coords: tuple[NDArrayNum, NDArrayNum],
values: NDArrayNum,
Expand All @@ -35,7 +36,7 @@ def _interpn_interpolator(
# Adding masking of NaNs for methods not supporting it
method_support_nan = method in ["nearest"]
order = method_to_order[method]
dist_nodata_spread = int(np.ceil(order/2))
dist_nodata_spread = int(np.ceil(order / 2))

# If NaNs are not supported
if not method_support_nan:
Expand Down
18 changes: 12 additions & 6 deletions tests/test_raster/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
from geoutils._typing import MArrayNum, NDArrayNum
from geoutils.misc import resampling_method_from_str
from geoutils.projtools import reproject_to_latlon
from geoutils.raster.raster import _default_nodata, _default_rio_attrs
from geoutils.raster.interpolate import method_to_order
from geoutils.raster.raster import _default_nodata, _default_rio_attrs

DO_PLOT = False

Expand Down Expand Up @@ -2273,7 +2273,7 @@ def test_interp_points__synthetic(self, tag_aop: str | None, shift_aop: bool) ->
assert all(~np.isfinite(raster_points_mapcoords_edge))
assert all(~np.isfinite(raster_points_interpn_edge))

@pytest.mark.parametrize("example", [landsat_b4_path, aster_dem_path])
@pytest.mark.parametrize("example", [landsat_b4_path, aster_dem_path]) # type: ignore
@pytest.mark.parametrize(
"method", ["nearest", "linear", "cubic", "quintic", "slinear", "pchip", "splinef2d"]
) # type: ignore
Expand Down Expand Up @@ -2320,8 +2320,10 @@ def test_interp_points__real(
order = 1
d = int(np.ceil(order / 2))
# No NaN propagation for linear
indices_nan = [(i0 + i, j0 + j) for i in np.arange(-d, d+1) for j in np.arange(-d, d+1) if (np.abs(i) + np.abs(j)) <= d]
i,j = list(zip(*indices_nan))
indices_nan = [
(i0 + i, j0 + j) for i in np.arange(-d, d + 1) for j in np.arange(-d, d + 1) if (np.abs(i) + np.abs(j)) <= d
]
i, j = list(zip(*indices_nan))
x, y = r.ij2xy(i, j)
vals = r.interp_points((x, y), method=method, force_scipy_function="map_coordinates")[0]
vals2 = r.interp_points((x, y), method=method, force_scipy_function="interpn")[0]
Expand All @@ -2337,7 +2339,12 @@ def test_interp_points__real(
# should be exactly the same

# We get the indexes of valid pixels just at the edge of NaNs
indices_edge = [(i0 + i, j0 + j) for i in np.arange(-d-1, d+2) for j in np.arange(-d-1, d+2) if (np.abs(i) + np.abs(j)) == d+1]
indices_edge = [
(i0 + i, j0 + j)
for i in np.arange(-d - 1, d + 2)
for j in np.arange(-d - 1, d + 2)
if (np.abs(i) + np.abs(j)) == d + 1
]
i, j = list(zip(*indices_edge))
x, y = r.ij2xy(i, j)
# And get their interpolated value
Expand All @@ -2362,7 +2369,6 @@ def test_interp_points__real(
assert np.allclose(vals, vals_near, equal_nan=False)
assert np.allclose(vals2, vals2_near, equal_nan=False)


def test_value_at_coords(self) -> None:
"""
Test that value at coords works as intended
Expand Down

0 comments on commit b0e2bff

Please sign in to comment.