xdem icon indicating copy to clipboard operation
xdem copied to clipboard

Lack shape consistency check in xdem.coreg fit_and_apply

Open adehecq opened this issue 6 months ago • 2 comments

There is no test in fit_and_apply to check that the DEM to coregister and the inlier_mask have the same shape. if that happens, this leads to an error downstream that is difficult to understand. Here is a MWE (an opportunity for me to test the new icrop 😄) :

import geoutils as gu
import numpy as np
import xdem

reference_dem = xdem.DEM(xdem.examples.get_path("longyearbyen_ref_dem"))
dem_to_be_aligned = xdem.DEM(xdem.examples.get_path("longyearbyen_tba_dem"))
glacier_outlines = gu.Vector(xdem.examples.get_path("longyearbyen_glacier_outlines"))

# We create a stable ground mask (not glacierized) to mark "inlier data".
inlier_mask = ~glacier_outlines.create_mask(reference_dem)

# crop the mask by a few pixels on each side
nrows, ncols = inlier_mask.shape
inlier_mask2 = inlier_mask.icrop((0, 0, ncols-2, nrows-2))
print(inlier_mask2.shape)
print(inlier_mask.shape)

# Try running coregistration
nuth_kaab = xdem.coreg.NuthKaab()
aligned_dem = nuth_kaab.fit_and_apply(reference_dem, dem_to_be_aligned, inlier_mask2)

returns the following error:

File /usr/local/Caskroom/miniconda/base/envs/xdem/lib/python3.12/site-packages/xdem/coreg/base.py:268, in _preprocess_coreg_fit_raster_raster(reference_dem, dem_to_be_aligned, inlier_mask, transform, crs, area_or_point) 265 raise ValueError("'dem_to_be_aligned' had only NaNs") 267 # Isolate all invalid values --> 268 invalid_mask = np.logical_or.reduce((~inlier_mask, ref_mask, tba_mask)) 270 if np.all(invalid_mask): 271 raise ValueError("All values of the inlier mask are NaNs in either 'reference_dem' or 'dem_to_be_aligned'.")

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part.

adehecq avatar Jul 04 '25 13:07 adehecq

@adehecq: Do you want to take this issue or we can take it ?

belletva avatar Aug 18 '25 16:08 belletva

No, go ahead if you have time!

adehecq avatar Oct 01 '25 09:10 adehecq