Skip to content

Commit

Permalink
Re-organize coreg.py (#329)
Browse files Browse the repository at this point in the history
* Add a CRS argument to all coreg.fit functions

* Replace old slope/aspect calculation with xdem implementation

* Add a CRS argument to all coreg.fit functions

* Replace old slope/aspect calculation with xdem implementation

* Replace subsampling with geoutils.spatial_tools implementation

* Improve testing subsampling for deramp coreg

* Remove duplicate of deramping

* Add function to calculate standard ddem stats

* Fix circular import in previous commit

* Ensure fit/apply always take transform+crs as input, update tests accordingly. Make non-resampling possible for NuthKaab.

* Fix issue with arg name in overloading. Remove optional type for transform/crs.

* Raise an error if unproject CRS is used for NuthKaab

* Remove unused functions

* Move the functions in a more logical order (nothing is changed)

* Remove dilate_mask option of  function

* Fix bug with  option not taken into account in CoregPipeline

* Fix bug with resampling option not taken into account in Coregpipeline

* Revert to old slope/aspect calculation, more efficient

* Add a function to create inlier_mask for coreg

* Add inout to ignored codespell errors

* Add tests for create_inlier_mask function

* Update dem_coregistration function to use create_inlier_mask

* Fix typo in previous commit and apply linting

* Fix issue iwth NMAD improperly calculated

* Add a filter to remove pixels with dh above an absolute threshold

* Update comment

* Implement resample=False in dem_coregistration

* Remove the hmode/vmode. Update docstring of dem_coregistration.

* Enable input src/ref dem as Raster and not only path to file

* Implement resample=False for BiasCorr

* Update tests for create_inlier_mask

* Make sure option resample=False is flagged as not implemented for BlockWiseCoreg

* Add tests for resample=False option

* Linting

* Update default coreg method and modify outputs of dem_coregistration

* Add test suite for dem_coregistration function

* Update pre-commit

* Fix mypy issue

* Account for PR comments

* Remove NaN forcing

* Linting

* Remove unnecessary type

* Rename fit_func

* Improve docstrings

* Simplify if statement

* Make resampling algo an optional argument for Coreg.apply

* Fix two bugs and add comment

* Update NuthKaab shift in tests

* Update tests in examples and coreg

* Update test_spatialstats values with new coreg that outputs ddem with less nodata

* Fix runtime warning in code example

* Fix coregistration code snippet

* Fix coreg example with new transform and crs argument of Coreg class

---------

Co-authored-by: rhugonne <romain.hugonnet@gmail.com>
  • Loading branch information
adehecq and rhugonnet authored Jan 31, 2023
1 parent 3ffc772 commit 2b68111
Show file tree
Hide file tree
Showing 8 changed files with 932 additions and 437 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repos:
hooks:
- id: codespell
args: [
'--ignore-words-list', 'nd,alos',
'--ignore-words-list', 'nd,alos,inout',
'--ignore-regex', '\bhist\b',
'--'
]
Expand Down
6 changes: 3 additions & 3 deletions docs/source/code/coregistration_plot_nuth_kaab.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
inlier_mask = ~outlines_1990.create_mask(dem_2009)

nuth_kaab = xdem.coreg.NuthKaab()
nuth_kaab.fit(dem_2009.data, dem_1990.data, transform=dem_2009.transform, inlier_mask=inlier_mask)
dem_coreg = nuth_kaab.apply(dem_1990.data, transform=dem_1990.transform)
nuth_kaab.fit(dem_2009, dem_1990, inlier_mask=inlier_mask)
dem_coreg = nuth_kaab.apply(dem_1990)

ddem_pre = (dem_2009.data - dem_1990.data).filled(np.nan).squeeze()
ddem_post = (dem_2009.data - dem_coreg).filled(np.nan).squeeze()
ddem_post = (dem_2009.data - dem_coreg.data).filled(np.nan).squeeze()

nmad_pre = xdem.spatialstats.nmad(ddem_pre[inlier_mask.squeeze()])
nmad_post = xdem.spatialstats.nmad(ddem_post[inlier_mask.squeeze()])
Expand Down
463 changes: 411 additions & 52 deletions tests/test_coreg.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def test_array_content(self, rst_and_truevals: tuple[Raster, NDArrayf]) -> None:

assert values == pytest.approx(truevals)

@pytest.mark.parametrize("rst_and_truenodata", [(ref_dem, 0), (tba_dem, 0), (ddem, 2316)]) # type: ignore
# Note: Following PR #329, no gaps on DEM edges after coregistration
@pytest.mark.parametrize("rst_and_truenodata", [(ref_dem, 0), (tba_dem, 0), (ddem, 0)]) # type: ignore
def test_array_nodata(self, rst_and_truenodata: tuple[Raster, int]) -> None:
"""Let's also check that the data arrays have always the same number of not finite values"""

Expand Down
6 changes: 3 additions & 3 deletions tests/test_spatialstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,9 @@ def test_sample_multirange_variogram_default(self) -> None:

# Check the variogram output is consistent for a random state
df = xdem.spatialstats.sample_empirical_variogram(values=self.diff, subsample=10, random_state=42)
assert df["exp"][15] == pytest.approx(23.517837524414062, abs=1e-3)
assert df["exp"][15] == pytest.approx(5.087792205810548, abs=1e-3)
assert df["lags"][15] == pytest.approx(5120)
assert df["count"][15] == 2
assert df["count"][15] == 5
# With a single run, no error can be estimated
assert all(np.isnan(df.err_exp.values))

Expand Down Expand Up @@ -1180,7 +1180,7 @@ def test_patches_method_loop_quadrant(self) -> None:
assert all(df.columns == ["nmad", "nb_indep_patches", "exact_areas", "areas"])

# Check the sampling is fixed for a random state
assert df["nmad"][0] == pytest.approx(1.8663623135417342, abs=1e-3)
assert df["nmad"][0] == pytest.approx(1.8392861049330378, abs=1e-3)
assert df["nb_indep_patches"][0] == 100
assert df["exact_areas"][0] == pytest.approx(df["areas"][0], rel=0.2)

Expand Down
Loading

0 comments on commit 2b68111

Please sign in to comment.