Skip to content

Commit

Permalink
Merge pull request #793 from sentinel-hub/empty-batch-fix
Browse files Browse the repository at this point in the history
Fix SnowMaskTask when dealing with empty patches
  • Loading branch information
zigaLuksic authored Jun 13, 2024
2 parents efa49d4 + eadd54b commit de1501d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
4 changes: 2 additions & 2 deletions eolearn/coregistration/coregistration.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ def register(
warp_matrix,
warp_mode,
criteria,
valid_mask, # type: ignore[arg-type]
valid_mask,
self.gauss_kernel_size,
)
except cv2.error as cv2err:
except cv2.error as cv2err: # pylint: disable=catching-non-exception
warnings.warn(f"Could not calculate the warp matrix: {cv2err}", EORuntimeWarning)

return warp_matrix
Expand Down
6 changes: 5 additions & 1 deletion eolearn/features/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ def spatially_resize_image(
if resize_library is ResizeLib.CV2:
resize_function = partial(cv2.resize, dsize=size, interpolation=resize_method.get_cv2_method(data.dtype))
else:
resize_function = partial(_pil_resize_ndarray, size=size, method=resize_method.get_pil_method())
resize_function = partial(
_pil_resize_ndarray, # type: ignore[arg-type]
size=size,
method=resize_method.get_pil_method(),
)

resized_data = _apply_to_spatial_axes(resize_function, data, spatial_axes)

Expand Down
3 changes: 2 additions & 1 deletion eolearn/mask/snow_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def _apply_dilation(self, snow_masks: np.ndarray) -> np.ndarray:
"""Apply binary dilation for each mask in the series"""
if self.disk_size > 0:
disk = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (self.disk_size, self.disk_size))
snow_masks = np.array([cv2.dilate(mask.astype(np.uint8), disk) for mask in snow_masks])
dilated_masks = np.array([cv2.dilate(mask.astype(np.uint8), disk) for mask in snow_masks])
snow_masks = dilated_masks.reshape(snow_masks.shape) # edge case where data is empty
return snow_masks.astype(bool)

def execute(self, eopatch: EOPatch) -> EOPatch:
Expand Down
12 changes: 11 additions & 1 deletion tests/mask/test_snow_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import numpy as np
import pytest

from eolearn.core import FeatureType
from eolearn.core import EOPatch, FeatureType
from eolearn.mask import SnowMaskTask


Expand All @@ -33,3 +33,13 @@ def test_snow_coverage(task, result, test_eopatch):
snow_pixels = np.sum(output, axis=(1, 2, 3))
assert np.sum(snow_pixels) == result[0], "Sum of snowy pixels does not match"
assert snow_pixels[-4] == result[1], "Snowy pixels on specified frame do not match"


def test_snow_empty_eopatch(test_eopatch):
_, h, w, c = test_eopatch.data["BANDS-S2-L1C"].shape
empty_eopatch = EOPatch(bbox=test_eopatch.bbox, timestamps=[])
empty_eopatch.data["BANDS-S2-L1C"] = np.array([], dtype=np.float32).reshape((0, h, w, c))

task = SnowMaskTask((FeatureType.DATA, "BANDS-S2-L1C"), [2, 3, 7, 11], mask_name="TEST_SNOW_MASK")
resulting_eopatch = task(empty_eopatch) # checks if the task runs without errors
assert resulting_eopatch.mask["TEST_SNOW_MASK"].shape == (0, h, w, 1)

0 comments on commit de1501d

Please sign in to comment.