Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
rhugonnet committed Sep 2, 2023
1 parent 77e2e5d commit 8b08c0d
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 69 deletions.
79 changes: 59 additions & 20 deletions tests/test_coreg/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TestCoregClass:
# Create some 3D coordinates with Z coordinates being 0 to try the apply_pts functions.
points = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [0, 0, 0, 0]], dtype="float64").T

def test_init(self):
def test_init(self) -> None:
"""Test instantiation of Coreg"""

c = coreg.Coreg()
Expand Down Expand Up @@ -465,7 +465,14 @@ def test_pipeline(self) -> None:
# Assert that the combined vertical shift is 2
assert pipeline2.to_matrix()[2, 3] == 2.0

all_coregs = [coreg.VerticalShift(), coreg.NuthKaab(), coreg.ICP(), coreg.Deramp(), coreg.TerrainBias(), coreg.DirectionalBias()]
all_coregs = [
coreg.VerticalShift(),
coreg.NuthKaab(),
coreg.ICP(),
coreg.Deramp(),
coreg.TerrainBias(),
coreg.DirectionalBias(),
]

@pytest.mark.parametrize("coreg1", all_coregs) # type: ignore
@pytest.mark.parametrize("coreg2", all_coregs) # type: ignore
Expand All @@ -479,12 +486,23 @@ def test_pipeline_combinations__nobiasvar(self, coreg1: Coreg, coreg2: Coreg) ->
aligned_dem, _ = pipeline.apply(self.tba.data, transform=self.ref.transform, crs=self.ref.crs)
assert aligned_dem.shape == self.ref.data.squeeze().shape

all_coregs = [coreg.VerticalShift(), coreg.NuthKaab(), coreg.ICP(), coreg.Deramp(), coreg.TerrainBias(),
coreg.DirectionalBias()]
all_coregs = [
coreg.VerticalShift(),
coreg.NuthKaab(),
coreg.ICP(),
coreg.Deramp(),
coreg.TerrainBias(),
coreg.DirectionalBias(),
]

@pytest.mark.parametrize("coreg1", all_coregs) # type: ignore
@pytest.mark.parametrize("coreg2", [coreg.BiasCorr1D(bias_var_names=["slope"], fit_or_bin="bin"),
coreg.BiasCorr2D(bias_var_names=["slope", "aspect"], fit_or_bin="bin")]) # type: ignore
@pytest.mark.parametrize(
"coreg2",
[
coreg.BiasCorr1D(bias_var_names=["slope"], fit_or_bin="bin"),
coreg.BiasCorr2D(bias_var_names=["slope", "aspect"], fit_or_bin="bin"),
],
) # type: ignore
def test_pipeline_combinations__biasvar(self, coreg1: Coreg, coreg2: Coreg) -> None:
"""Test pipelines with all combinations of coregistration subclasses with bias variables"""

Expand All @@ -493,33 +511,54 @@ def test_pipeline_combinations__biasvar(self, coreg1: Coreg, coreg2: Coreg) -> N
bias_vars = {"slope": xdem.terrain.slope(self.ref), "aspect": xdem.terrain.aspect(self.ref)}
pipeline.fit(**self.fit_params, bias_vars=bias_vars)

aligned_dem, _ = pipeline.apply(self.tba.data, transform=self.ref.transform, crs=self.ref.crs, bias_vars=bias_vars)
aligned_dem, _ = pipeline.apply(
self.tba.data, transform=self.ref.transform, crs=self.ref.crs, bias_vars=bias_vars
)
assert aligned_dem.shape == self.ref.data.squeeze().shape

def test_pipeline__errors(self):
def test_pipeline__errors(self) -> None:
"""Test pipeline raises proper errors."""

pipeline = coreg.CoregPipeline([coreg.NuthKaab(), coreg.BiasCorr1D()])
with pytest.raises(ValueError, match=re.escape("No `bias_vars` passed to .fit() for bias correction step "
"<class 'xdem.coreg.biascorr.BiasCorr1D'> of the pipeline.")):
with pytest.raises(
ValueError,
match=re.escape(
"No `bias_vars` passed to .fit() for bias correction step "
"<class 'xdem.coreg.biascorr.BiasCorr1D'> of the pipeline."
),
):
pipeline.fit(**self.fit_params)


pipeline2 = coreg.CoregPipeline([coreg.NuthKaab(), coreg.BiasCorr1D(), coreg.BiasCorr1D()])
with pytest.raises(ValueError, match=re.escape("No `bias_vars` passed to .fit() for bias correction step <class 'xdem.coreg.biascorr.BiasCorr1D'> "
"of the pipeline. As you are using several bias correction steps requiring"
" `bias_vars`, don't forget to explicitly define their `bias_var_names` "
"during instantiation, e.g. BiasCorr1D(bias_var_names=['slope']).")):
with pytest.raises(
ValueError,
match=re.escape(
"No `bias_vars` passed to .fit() for bias correction step <class 'xdem.coreg.biascorr.BiasCorr1D'> "
"of the pipeline. As you are using several bias correction steps requiring"
" `bias_vars`, don't forget to explicitly define their `bias_var_names` "
"during instantiation, e.g. BiasCorr1D(bias_var_names=['slope'])."
),
):
pipeline2.fit(**self.fit_params)

with pytest.raises(ValueError, match=re.escape("When using several bias correction steps requiring `bias_vars` in a pipeline,"
"the `bias_var_names` need to be explicitly defined at each step's "
"instantiation, e.g. BiasCorr1D(bias_var_names=['slope']).")):
with pytest.raises(
ValueError,
match=re.escape(
"When using several bias correction steps requiring `bias_vars` in a pipeline,"
"the `bias_var_names` need to be explicitly defined at each step's "
"instantiation, e.g. BiasCorr1D(bias_var_names=['slope'])."
),
):
pipeline2.fit(**self.fit_params, bias_vars={"slope": xdem.terrain.slope(self.ref)})

pipeline3 = coreg.CoregPipeline([coreg.NuthKaab(), coreg.BiasCorr1D(bias_var_names=["slope"])])
with pytest.raises(ValueError, match=re.escape("Not all keys of `bias_vars` in .fit() match the `bias_var_names` defined during "
"instantiation of the bias correction step <class 'xdem.coreg.biascorr.BiasCorr1D'>: ['slope'].")):
with pytest.raises(
ValueError,
match=re.escape(
"Not all keys of `bias_vars` in .fit() match the `bias_var_names` defined during "
"instantiation of the bias correction step <class 'xdem.coreg.biascorr.BiasCorr1D'>: ['slope']."
),
):
pipeline3.fit(**self.fit_params, bias_vars={"ncc": xdem.terrain.slope(self.ref)})

def test_pipeline_pts(self) -> None:
Expand Down
15 changes: 9 additions & 6 deletions tests/test_coreg/test_biascorr.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def test_biascorr(self) -> None:
bcorr5 = biascorr.BiasCorr(bias_var_names=np.array(["slope", "ncc"]))
assert bcorr5._meta["bias_var_names"] == ["slope", "ncc"]


def test_biascorr__errors(self) -> None:
"""Test the errors that should be raised by BiasCorr."""

Expand Down Expand Up @@ -354,8 +353,10 @@ def test_biascorr1d(self) -> None:

# Raise error when variables don't match
with pytest.raises(
ValueError, match=re.escape("The keys of `bias_vars` do not match the `bias_var_names` defined during "
"instantiation: ['ncc'].")
ValueError,
match=re.escape(
"The keys of `bias_vars` do not match the `bias_var_names` defined during " "instantiation: ['ncc']."
),
):
bcorr1d2 = biascorr.BiasCorr1D(bias_var_names=["ncc"])
bias_vars_dict = {"elevation": self.ref}
Expand Down Expand Up @@ -391,8 +392,11 @@ def test_biascorr2d(self) -> None:

# Raise error when variables don't match
with pytest.raises(
ValueError, match=re.escape("The keys of `bias_vars` do not match the `bias_var_names` defined during "
"instantiation: ['elevation', 'ncc'].")
ValueError,
match=re.escape(
"The keys of `bias_vars` do not match the `bias_var_names` defined during "
"instantiation: ['elevation', 'ncc']."
),
):
bcorr2d2 = biascorr.BiasCorr2D(bias_var_names=["elevation", "ncc"])
bias_vars_dict = {"elevation": self.ref, "slope": xdem.terrain.slope(self.ref)}
Expand Down Expand Up @@ -539,7 +543,6 @@ def test_terrainbias(self) -> None:

assert tb._meta["bias_var_names"] == ["maximum_curvature"]


def test_terrainbias__synthetic(self) -> None:
"""Test the subclass TerrainBias."""

Expand Down
34 changes: 30 additions & 4 deletions xdem/coreg/affine.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,20 @@ def _fit_func(
transform: rio.transform.Affine,
crs: rio.crs.CRS,
weights: NDArrayf | None,
bias_vars: dict[str, NDArrayf] | None = None,
verbose: bool = False,
**kwargs: Any,
) -> None:
# FOR DEVELOPERS: This function needs to be implemented.
raise NotImplementedError("This step has to be implemented by subclassing.")

def _apply_func(
self, dem: NDArrayf, transform: rio.transform.Affine, crs: rio.crs.CRS, **kwargs: Any
self,
dem: NDArrayf,
transform: rio.transform.Affine,
crs: rio.crs.CRS,
bias_vars: dict[str, NDArrayf] | None = None,
**kwargs: Any,
) -> tuple[NDArrayf, rio.transform.Affine]:
# FOR DEVELOPERS: This function is only needed for non-rigid transforms.
raise NotImplementedError("This should have been implemented by subclassing")
Expand Down Expand Up @@ -340,6 +346,7 @@ def _fit_func(
transform: rio.transform.Affine,
crs: rio.crs.CRS,
weights: NDArrayf | None,
bias_vars: dict[str, NDArrayf] | None = None,
verbose: bool = False,
**kwargs: Any,
) -> None:
Expand Down Expand Up @@ -368,7 +375,12 @@ def _fit_func(
self._meta["vshift"] = vshift

def _apply_func(
self, dem: NDArrayf, transform: rio.transform.Affine, crs: rio.crs.CRS, **kwargs: Any
self,
dem: NDArrayf,
transform: rio.transform.Affine,
crs: rio.crs.CRS,
bias_vars: dict[str, NDArrayf] | None = None,
**kwargs: Any,
) -> tuple[NDArrayf, rio.transform.Affine]:
"""Apply the VerticalShift function to a DEM."""
return dem + self._meta["vshift"], transform
Expand Down Expand Up @@ -426,6 +438,7 @@ def _fit_func(
transform: rio.transform.Affine,
crs: rio.crs.CRS,
weights: NDArrayf | None,
bias_vars: dict[str, NDArrayf] | None = None,
verbose: bool = False,
**kwargs: Any,
) -> None:
Expand Down Expand Up @@ -575,6 +588,7 @@ def _fit_func(
transform: rio.transform.Affine,
crs: rio.crs.CRS,
weights: NDArrayf | None,
bias_vars: dict[str, NDArrayf] | None = None,
verbose: bool = False,
**kwargs: Any,
) -> None:
Expand All @@ -589,7 +603,12 @@ def _fit_func(
self._meta["func"] = fit_ramp

def _apply_func(
self, dem: NDArrayf, transform: rio.transform.Affine, crs: rio.crs.CRS, **kwargs: Any
self,
dem: NDArrayf,
transform: rio.transform.Affine,
crs: rio.crs.CRS,
bias_vars: dict[str, NDArrayf] | None = None,
**kwargs: Any,
) -> tuple[NDArrayf, rio.transform.Affine]:
"""Apply the deramp function to a DEM."""
x_coords, y_coords = _get_x_and_y_coords(dem.shape, transform)
Expand Down Expand Up @@ -652,6 +671,7 @@ def _fit_func(
transform: rio.transform.Affine,
crs: rio.crs.CRS,
weights: NDArrayf | None,
bias_vars: dict[str, NDArrayf] | None = None,
verbose: bool = False,
**kwargs: Any,
) -> None:
Expand Down Expand Up @@ -928,7 +948,12 @@ def _to_matrix_func(self) -> NDArrayf:
return matrix

def _apply_func(
self, dem: NDArrayf, transform: rio.transform.Affine, crs: rio.crs.CRS, **kwargs: Any
self,
dem: NDArrayf,
transform: rio.transform.Affine,
crs: rio.crs.CRS,
bias_vars: dict[str, NDArrayf] | None = None,
**kwargs: Any,
) -> tuple[NDArrayf, rio.transform.Affine]:
"""Apply the Nuth & Kaab shift to a DEM."""
offset_east = self._meta["offset_east_px"] * self._meta["resolution"]
Expand Down Expand Up @@ -1082,6 +1107,7 @@ def _fit_func(
transform: rio.transform.Affine,
crs: rio.crs.CRS,
weights: NDArrayf | None,
bias_vars: dict[str, NDArrayf] | None = None,
verbose: bool = False,
**kwargs: Any,
) -> None:
Expand Down
Loading

0 comments on commit 8b08c0d

Please sign in to comment.