From 42ae7da9de986eef1054b4470d2c1343f8bb6eef Mon Sep 17 00:00:00 2001 From: Romain Hugonnet Date: Wed, 20 Mar 2024 09:28:10 -0800 Subject: [PATCH] Fix tests with new geoutils --- tests/test_coreg/test_base.py | 9 --------- tests/test_dem.py | 8 +++++++- tests/test_vcrs.py | 15 ++++++++++++++- xdem/coreg/base.py | 6 ++---- xdem/dem.py | 2 +- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/tests/test_coreg/test_base.py b/tests/test_coreg/test_base.py index 26d56a97..15537b0a 100644 --- a/tests/test_coreg/test_base.py +++ b/tests/test_coreg/test_base.py @@ -98,15 +98,6 @@ def test_error_method(self) -> None: dem3 = dem1.copy() + np.random.random(size=dem1.size).reshape(dem1.shape) assert abs(vshiftcorr.error(dem1, dem3, transform=affine, crs=crs, error_type="std") - np.std(dem3)) < 1e-6 - def test_ij_xy(self, i: int = 10, j: int = 20) -> None: - """ - Test the reversibility of ij2xy and xy2ij, which is important for point co-registration. - """ - x, y = self.ref.ij2xy(i, j, offset="ul") - i, j = self.ref.xy2ij(x, y, shift_area_or_point=False) - assert i == pytest.approx(10) - assert j == pytest.approx(20) - @pytest.mark.parametrize("subsample", [10, 10000, 0.5, 1]) # type: ignore def test_get_subsample_on_valid_mask(self, subsample: float | int) -> None: """Test the subsampling function called by all subclasses""" diff --git a/tests/test_dem.py b/tests/test_dem.py index e5b0ebdc..578eaafd 100644 --- a/tests/test_dem.py +++ b/tests/test_dem.py @@ -236,6 +236,9 @@ def test_set_vcrs(self) -> None: assert dem.vcrs_grid == "us_nga_egm08_25.tif" # -- Test 2: we check with grids -- + # Most grids aren't going to be downloaded, so this warning can be raised + warnings.filterwarnings("ignore", category=UserWarning, message="Grid not found in *") + dem.set_vcrs(new_vcrs="us_nga_egm96_15.tif") assert dem.vcrs_name == "unknown using geoidgrids=us_nga_egm96_15.tif" assert dem.vcrs_grid == "us_nga_egm96_15.tif" @@ -312,13 +315,16 @@ def test_to_vcrs__equal_warning(self) -> None: # Compare to manually-extracted shifts at specific coordinates for the geoid grids egm96_chile = {"grid": "us_nga_egm96_15.tif", "lon": -68, "lat": -20, "shift": 42} egm08_chile = {"grid": "us_nga_egm08_25.tif", "lon": -68, "lat": -20, "shift": 42} - geoid96_alaska = {"grid": "us_noaa_geoid06_ak.tif", "lon": -145, "lat": 62, "shift": 17} + geoid96_alaska = {"grid": "us_noaa_geoid06_ak.tif", "lon": -145, "lat": 62, "shift": 15} isn93_iceland = {"grid": "is_lmi_Icegeoid_ISN93.tif", "lon": -18, "lat": 65, "shift": 68} @pytest.mark.parametrize("grid_shifts", [egm08_chile, egm08_chile, geoid96_alaska, isn93_iceland]) # type: ignore def test_to_vcrs__grids(self, grid_shifts: dict[str, Any]) -> None: """Tests grids to convert vertical CRS.""" + # Most grids aren't going to be downloaded, so this warning can be raised + warnings.filterwarnings("ignore", category=UserWarning, message="Grid not found in *") + # Using an arbitrary elevation of 100 m (no influence on the transformation) dem = DEM.from_array( data=np.array([[100, 100]]), diff --git a/tests/test_vcrs.py b/tests/test_vcrs.py index d9f2c61f..69287cdd 100644 --- a/tests/test_vcrs.py +++ b/tests/test_vcrs.py @@ -3,6 +3,7 @@ import pathlib import re +import warnings from typing import Any import numpy as np @@ -12,8 +13,8 @@ import xdem import xdem.vcrs - class TestVCRS: + def test_parse_vcrs_name_from_product(self) -> None: """Test parsing of vertical CRS name from DEM product name.""" @@ -66,6 +67,9 @@ def test_vcrs_from_crs(self, input_output: tuple[CRS, CRS]) -> None: def test_vcrs_from_user_input(self, vcrs_input: str | pathlib.Path | int | CRS) -> None: """Tests the function _vcrs_from_user_input for varying user inputs, for which it will return a CRS.""" + # Most grids aren't going to be downloaded, so this warning can be raised + warnings.filterwarnings("ignore", category=UserWarning, message="Grid not found in *") + # Get user input vcrs = xdem.dem._vcrs_from_user_input(vcrs_input) @@ -116,6 +120,9 @@ def test_vcrs_from_user_input__errors(self) -> None: def test_build_vcrs_from_grid(self, grid: str) -> None: """Test that vertical CRS are correctly built from grid""" + # Most grids aren't going to be downloaded, so this warning can be raised + warnings.filterwarnings("ignore", category=UserWarning, message="Grid not found in *") + # Build vertical CRS vcrs = xdem.vcrs._build_vcrs_from_grid(grid=grid) assert vcrs.is_vertical @@ -132,6 +139,9 @@ def test_build_vcrs_from_grid(self, grid: str) -> None: def test_build_ccrs_from_crs_and_vcrs(self, crs: CRS, vcrs_input: CRS | str) -> None: """Test the function build_ccrs_from_crs_and_vcrs.""" + # Most grids aren't going to be downloaded, so this warning can be raised + warnings.filterwarnings("ignore", category=UserWarning, message="Grid not found in *") + # Get the vertical CRS from user input vcrs = xdem.vcrs._vcrs_from_user_input(vcrs_input=vcrs_input) @@ -180,6 +190,9 @@ def test_build_ccrs_from_crs_and_vcrs__errors(self) -> None: def test_transform_zz(self, grid_shifts: dict[str, Any]) -> None: """Tests grids to convert vertical CRS.""" + # Most grids aren't going to be downloaded, so this warning can be raised + warnings.filterwarnings("ignore", category=UserWarning, message="Grid not found in *") + # Using an arbitrary elevation of 100 m (no influence on the transformation) zz = 100 xx = grid_shifts["lon"] diff --git a/xdem/coreg/base.py b/xdem/coreg/base.py index 42ea5881..ee532afa 100644 --- a/xdem/coreg/base.py +++ b/xdem/coreg/base.py @@ -133,9 +133,7 @@ def _residuals_df( arr_ = dem.data.astype(np.float32) # get residual error at the point on DEM. - i, j = dem.xy2ij( - df_shifted["E"].values, df_shifted["N"].values, op=np.float32, shift_area_or_point=("AREA_OR_POINT" in dem.tags) - ) + i, j = dem.xy2ij(df_shifted["E"].values, df_shifted["N"].values) # ndimage return dem_h = scipy.ndimage.map_coordinates(arr_, [i, j], order=1, mode="nearest", **kwargs) @@ -177,7 +175,7 @@ def _df_sampling_from_dem( mask = dem.data.mask # Get value - x, y = dem.ij2xy(i[~mask[i, j]], j[~mask[i, j]], offset=offset) + x, y = dem.ij2xy(i[~mask[i, j]], j[~mask[i, j]]) z = scipy.ndimage.map_coordinates( dem.data.astype(np.float32), [i[~mask[i, j]], j[~mask[i, j]]], order=order, mode="nearest" ) diff --git a/xdem/dem.py b/xdem/dem.py index 22d384dd..cd5e4251 100644 --- a/xdem/dem.py +++ b/xdem/dem.py @@ -296,7 +296,7 @@ def to_vcrs( # Transform elevation with new vertical CRS zz = self.data - xx, yy = self.coords(offset="center") + xx, yy = self.coords() zz_trans = _transform_zz(crs_from=src_ccrs, crs_to=dst_ccrs, xx=xx, yy=yy, zz=zz) # Update DEM