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 NaNs in "longyearbyen_ddem" example #224

Merged
merged 13 commits into from
Nov 8, 2021
22 changes: 14 additions & 8 deletions tests/test_spatialstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,23 @@ def test_sample_multirange_variogram_default(self):

# Check the variogram estimation runs for a random state
df = xdem.spatialstats.sample_empirical_variogram(
values=diff.data, gsd=diff.res[0], subsample=50,
values=diff, subsample=50,
random_state=42, runs=2)

# With random state, results should always be the same
assert df.exp[0] == pytest.approx(6.11, 0.01)
# With a single run, no error can be estimated
assert all(np.isnan(df.err_exp.values))

# Check that all type of coordinate inputs work
# Only the array and the ground sampling distance
df = xdem.spatialstats.sample_empirical_variogram(
values=diff.data, gsd=diff.res[0], subsample=50,
random_state=42, runs=2)

# Test multiple runs
df2 = xdem.spatialstats.sample_empirical_variogram(
values=diff.data, gsd=diff.res[0], subsample=50,
values=diff, subsample=50,
random_state=42, runs=2, n_variograms=2)

# Check that an error is estimated
Expand All @@ -68,7 +74,7 @@ def test_sample_multirange_variogram_methods(self, subsample_method):

# Check the variogram estimation runs for several methods
df = xdem.spatialstats.sample_empirical_variogram(
values=diff.data, gsd=diff.res[0], subsample=50, random_state=42,
values=diff, subsample=50, random_state=42,
subsample_method=subsample_method)

assert not df.empty
Expand All @@ -87,29 +93,29 @@ def test_sample_multirange_variogram_args(self):
with pytest.warns(UserWarning):
# An argument only use by cdist with a pdist method
df = xdem.spatialstats.sample_empirical_variogram(
values=diff.data, gsd=diff.res[0], subsample=50, random_state=42,
values=diff, subsample=50, random_state=42,
subsample_method='pdist_ring', **cdist_args)

with pytest.warns(UserWarning):
# Same here
df = xdem.spatialstats.sample_empirical_variogram(
values=diff.data, gsd=diff.res[0], subsample=50, random_state=42,
values=diff, subsample=50, random_state=42,
subsample_method='cdist_equidistant', runs=2, **pdist_args)

with pytest.warns(UserWarning):
# Should also raise a warning for a nonsense argument
df = xdem.spatialstats.sample_empirical_variogram(
values=diff.data, gsd=diff.res[0], subsample=50, random_state=42,
values=diff, subsample=50, random_state=42,
subsample_method='cdist_equidistant', runs=2, **nonsense_args)

# Check the function passes optional arguments specific to pdist methods without warning
df = xdem.spatialstats.sample_empirical_variogram(
values=diff.data, gsd=diff.res[0], subsample=50, random_state=42,
values=diff, subsample=50, random_state=42,
subsample_method='pdist_ring', **pdist_args)

# Check the function passes optional arguments specific to cdist methods without warning
df = xdem.spatialstats.sample_empirical_variogram(
values=diff.data, gsd=diff.res[0], subsample=50, random_state=42,
values=diff, subsample=50, random_state=42,
subsample_method='cdist_equidistant', runs=2, **cdist_args)

def test_multirange_fit_performance(self):
Expand Down
7 changes: 5 additions & 2 deletions xdem/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import urllib.request
from distutils.dir_util import copy_tree

import numpy as np

import geoutils as gu
import xdem

Expand Down Expand Up @@ -94,9 +96,10 @@ def process_coregistered_examples(overwrite: bool =False):
nuth_kaab.fit(reference_raster.data, to_be_aligned_raster.data,
inlier_mask=inlier_mask, transform=reference_raster.transform)
aligned_raster = nuth_kaab.apply(to_be_aligned_raster.data, transform=reference_raster.transform)
Copy link
Member

Choose a reason for hiding this comment

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

This line and the few lines below could be simplified now that Coreg methods take Rasters as arguments and with the arithmetic overloading functions !

aligned_raster.data[~np.isfinite(aligned_raster)] = reference_raster.nodata

diff = gu.Raster.from_array((reference_raster.data - aligned_raster),
transform=reference_raster.transform, crs=reference_raster.crs)
diff = reference_raster - \
gu.Raster.from_array(aligned_raster.data, transform=reference_raster.transform, crs=reference_raster.crs, nodata=reference_raster.nodata)

# Save it so that future calls won't need to recreate the file
os.makedirs(os.path.dirname(FILEPATHS_PROCESSED['longyearbyen_ddem']), exist_ok=True)
Expand Down
2 changes: 1 addition & 1 deletion xdem/spatialstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ def sample_empirical_variogram(values: Union[np.ndarray, RasterType], gsd: float
"""
# First, check all that the values provided are OK
if isinstance(values, Raster):
coords = values.coords()
gsd = values.res[0]
values, mask = get_array_and_mask(values.data)
elif isinstance(values, (np.ndarray, np.ma.masked_array)):
values, mask = get_array_and_mask(values)
Expand Down