Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in CoregPipeline.fit() #450

Merged
merged 6 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/test_coreg/test_affine.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def test_tilt(self) -> None:
assert np.abs(np.mean(periglacial_offset)) < np.abs(np.mean(pre_offset))

# Check that the mean periglacial offset is low
assert np.abs(np.mean(periglacial_offset)) < 0.01
assert np.abs(np.mean(periglacial_offset)) < 0.02

def test_icp_opencv(self) -> None:
warnings.simplefilter("error")
Expand Down
38 changes: 38 additions & 0 deletions tests/test_coreg/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,44 @@ def test_coreg_add(self) -> None:
vshift5 = vshift3 + vshift3
assert vshift5.to_matrix()[2, 3] == vshift * 4

def test_pipeline_consistency(self) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to add tests for this!

"""Check that pipelines properties are respected: reflectivity, fusion of same coreg"""

# Test 1: Fusion of same coreg
# Many vertical shifts
many_vshifts = coreg.VerticalShift() + coreg.VerticalShift() + coreg.VerticalShift()
many_vshifts.fit(**self.fit_params, random_state=42)
aligned_dem, _ = many_vshifts.apply(self.tba.data, transform=self.ref.transform, crs=self.ref.crs)

# The last steps should have shifts of EXACTLY zero
assert many_vshifts.pipeline[1]._meta["vshift"] == pytest.approx(0, abs=10e-5)
assert many_vshifts.pipeline[2]._meta["vshift"] == pytest.approx(0, abs=10e-5)

# Many horizontal + vertical shifts
many_nks = coreg.NuthKaab() + coreg.NuthKaab() + coreg.NuthKaab()
many_nks.fit(**self.fit_params, random_state=42)
aligned_dem, _ = many_nks.apply(self.tba.data, transform=self.ref.transform, crs=self.ref.crs)

# The last steps should have shifts of NEARLY zero
assert many_nks.pipeline[1]._meta["vshift"] == pytest.approx(0, abs=0.02)
assert many_nks.pipeline[1]._meta["offset_east_px"] == pytest.approx(0, abs=0.02)
assert many_nks.pipeline[1]._meta["offset_north_px"] == pytest.approx(0, abs=0.02)
assert many_nks.pipeline[2]._meta["vshift"] == pytest.approx(0, abs=0.02)
assert many_nks.pipeline[2]._meta["offset_east_px"] == pytest.approx(0, abs=0.02)
assert many_nks.pipeline[2]._meta["offset_north_px"] == pytest.approx(0, abs=0.02)

# Test 2: Reflectivity
# Those two pipelines should give almost the same result
nk_vshift = coreg.NuthKaab() + coreg.VerticalShift()
vshift_nk = coreg.VerticalShift() + coreg.NuthKaab()

nk_vshift.fit(**self.fit_params, random_state=42)
aligned_dem, _ = nk_vshift.apply(self.tba.data, transform=self.ref.transform, crs=self.ref.crs)
vshift_nk.fit(**self.fit_params, random_state=42)
aligned_dem, _ = vshift_nk.apply(self.tba.data, transform=self.ref.transform, crs=self.ref.crs)

assert np.allclose(nk_vshift.to_matrix(), vshift_nk.to_matrix(), atol=10e-1)


class TestBlockwiseCoreg:
ref, tba, outlines = load_examples() # Load example reference, to-be-aligned and mask.
Expand Down
2 changes: 1 addition & 1 deletion xdem/coreg/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1516,7 +1516,7 @@ def fit(

main_args_fit = {
"reference_dem": ref_dem,
"dem_to_be_aligned": tba_dem,
"dem_to_be_aligned": tba_dem_mod,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch !

"inlier_mask": inlier_mask,
"transform": out_transform,
"crs": crs,
Expand Down
Loading