Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
rhugonnet committed Mar 7, 2024
1 parent 9b9e937 commit ff1cc4c
Show file tree
Hide file tree
Showing 7 changed files with 463 additions and 254 deletions.
12 changes: 8 additions & 4 deletions tests/test_coreg/test_affine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import copy
import warnings

import geopandas as gpd
import numpy as np
import pytest
import geopandas as gpd
import rasterio as rio
from geoutils import Raster, Vector
from geoutils.raster import RasterType
Expand Down Expand Up @@ -42,8 +42,9 @@ class TestAffineCoreg:
)
# Create some 3D coordinates with Z coordinates being 0 to try the apply functions.
points_arr = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [0, 0, 0, 0]], dtype="float64").T
points = gpd.GeoDataFrame(geometry=gpd.points_from_xy(x=points_arr[:, 0], y=points_arr[:, 1], crs=ref.crs),
data={"z": points_arr[:, 2]})
points = gpd.GeoDataFrame(
geometry=gpd.points_from_xy(x=points_arr[:, 0], y=points_arr[:, 1], crs=ref.crs), data={"z": points_arr[:, 2]}
)

def test_from_classmethods(self) -> None:

Expand Down Expand Up @@ -262,7 +263,10 @@ def test_nuth_kaab(self) -> None:
transformed_points = nuth_kaab.apply(self.points)

# Check that the x shift is close to the pixel_shift * image resolution
assert all(abs((transformed_points.geometry.x.values - self.points.geometry.x.values) - pixel_shift * self.ref.res[0]) < 0.1)
assert all(
abs((transformed_points.geometry.x.values - self.points.geometry.x.values) - pixel_shift * self.ref.res[0])
< 0.1
)
# Check that the z shift is close to the original vertical shift.
assert all(abs((transformed_points["z"].values - self.points["z"].values) + vshift) < 0.1)

Expand Down
30 changes: 20 additions & 10 deletions tests/test_coreg/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import warnings
from typing import Any, Callable

import geopandas as gpd
import geoutils as gu
import numpy as np
import geopandas as gpd
import pytest
import rasterio as rio
from geoutils import Raster, Vector
Expand Down Expand Up @@ -49,8 +49,9 @@ class TestCoregClass:
)
# Create some 3D coordinates with Z coordinates being 0 to try the apply functions.
points_arr = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [0, 0, 0, 0]], dtype="float64").T
points = gpd.GeoDataFrame(geometry=gpd.points_from_xy(x=points_arr[:, 0], y=points_arr[:, 1], crs=ref.crs),
data={"z": points_arr[:, 2]})
points = gpd.GeoDataFrame(
geometry=gpd.points_from_xy(x=points_arr[:, 0], y=points_arr[:, 1], crs=ref.crs), data={"z": points_arr[:, 2]}
)

def test_init(self) -> None:
"""Test instantiation of Coreg"""
Expand Down Expand Up @@ -415,8 +416,15 @@ def test_apply_resample(self, inputs: list[Any]) -> None:
"'crs' must be given if DEM is array-like.",
),
("dem1", "dem2", "dem2.transform", "None", "apply", "warns", "DEM .* overrides the given 'transform'"),
("None", "None", "None", "None", "fit", "error", "Input elevation data should be a raster, "
"an array or a geodataframe."),
(
"None",
"None",
"None",
"None",
"fit",
"error",
"Input elevation data should be a raster, " "an array or a geodataframe.",
),
("dem1 + np.nan", "dem2", "None", "None", "fit", "error", "'reference_dem' had only NaNs"),
("dem1", "dem2 + np.nan", "None", "None", "fit", "error", "'dem_to_be_aligned' had only NaNs"),
],
Expand Down Expand Up @@ -507,8 +515,9 @@ class TestCoregPipeline:
)
# Create some 3D coordinates with Z coordinates being 0 to try the apply functions.
points_arr = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [0, 0, 0, 0]], dtype="float64").T
points = gpd.GeoDataFrame(geometry=gpd.points_from_xy(x=points_arr[:, 0], y=points_arr[:, 1], crs=ref.crs),
data={"z": points_arr[:, 2]})
points = gpd.GeoDataFrame(
geometry=gpd.points_from_xy(x=points_arr[:, 0], y=points_arr[:, 1], crs=ref.crs), data={"z": points_arr[:, 2]}
)

@pytest.mark.parametrize("coreg_class", [coreg.VerticalShift, coreg.ICP, coreg.NuthKaab]) # type: ignore
def test_copy(self, coreg_class: Callable[[], Coreg]) -> None:
Expand Down Expand Up @@ -730,8 +739,9 @@ class TestBlockwiseCoreg:
)
# Create some 3D coordinates with Z coordinates being 0 to try the apply functions.
points_arr = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [0, 0, 0, 0]], dtype="float64").T
points = gpd.GeoDataFrame(geometry=gpd.points_from_xy(x=points_arr[:, 0], y=points_arr[:, 1], crs=ref.crs),
data={"z": points_arr[:, 2]})
points = gpd.GeoDataFrame(
geometry=gpd.points_from_xy(x=points_arr[:, 0], y=points_arr[:, 1], crs=ref.crs), data={"z": points_arr[:, 2]}
)

@pytest.mark.parametrize(
"pipeline", [coreg.VerticalShift(), coreg.VerticalShift() + coreg.NuthKaab()]
Expand Down Expand Up @@ -883,7 +893,7 @@ def rotation_matrix(rotation: float = 30) -> NDArrayf:

# Apply a rotation in the opposite direction
unrotated_dem = (
_apply_matrix_rst(rotated_dem, ref.transform, rotation_matrix(-rotation * 0.99), centroid=centroid) + 4.0
_apply_matrix_rst(rotated_dem, ref.transform, rotation_matrix(-rotation * 0.99), centroid=centroid) + 4.0
) # TODO: Check why the 0.99 rotation and +4 vertical shift were introduced.

diff = np.asarray(ref.data.squeeze() - unrotated_dem)
Expand Down
22 changes: 16 additions & 6 deletions tests/test_coreg/test_biascorr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import re
import warnings

import geopandas as gpd
import geoutils as gu
import numpy as np
import geopandas as gpd
import pytest
import scipy

Expand Down Expand Up @@ -504,8 +504,14 @@ def test_directionalbias__synthetic(self, fit_args, angle, nb_freq) -> None:
bias_elev = bias_dem.to_points(subsample=50000, pixel_offset="ul").ds.rename(columns={"b1": "z"})
else:
bias_elev = bias_dem
dirbias.fit(elev_fit_args["reference_elev"], to_be_aligned_elev=bias_elev, subsample=40000, random_state=42,
bounds_amp_wave_phase=bounds, niter=10)
dirbias.fit(
elev_fit_args["reference_elev"],
to_be_aligned_elev=bias_elev,
subsample=40000,
random_state=42,
bounds_amp_wave_phase=bounds,
niter=10,
)

# Check all fit parameters are the same within 10%
fit_params = dirbias._meta["fit_params"]
Expand Down Expand Up @@ -616,8 +622,13 @@ def test_terrainbias__synthetic(self, fit_args) -> None:
bias_elev = bias_dem.to_points(subsample=20000, pixel_offset="ul").ds.rename(columns={"b1": "z"})
else:
bias_elev = bias_dem
tb.fit(elev_fit_args["reference_elev"], to_be_aligned_elev=bias_elev, subsample=10000, random_state=42, bias_vars={
"maximum_curvature": maxc})
tb.fit(
elev_fit_args["reference_elev"],
to_be_aligned_elev=bias_elev,
subsample=10000,
random_state=42,
bias_vars={"maximum_curvature": maxc},
)

# Check high-order parameters are the same within 10%
bin_df = tb._meta["bin_dataframe"]
Expand All @@ -631,4 +642,3 @@ def test_terrainbias__synthetic(self, fit_args) -> None:
corrected_dem = tb.apply(bias_dem, bias_vars={"maximum_curvature": maxc})
# Need to standardize by the synthetic bias spread to avoid huge/small values close to infinity
assert np.nanvar((corrected_dem - self.ref) / np.nanstd(synthetic_bias)) < 0.01

8 changes: 7 additions & 1 deletion xdem/coreg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
Tilt,
VerticalShift,
)
from xdem.coreg.base import BlockwiseCoreg, Coreg, CoregPipeline, apply_matrix, invert_matrix # noqa
from xdem.coreg.base import ( # noqa
BlockwiseCoreg,
Coreg,
CoregPipeline,
apply_matrix,
invert_matrix,
)
from xdem.coreg.biascorr import ( # noqa
BiasCorr,
BiasCorr1D,
Expand Down
Loading

0 comments on commit ff1cc4c

Please sign in to comment.