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 SnowMaskTask when dealing with empty patches #793

Merged
merged 9 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 eolearn/coregistration/coregistration.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def register(
warp_matrix,
warp_mode,
criteria,
valid_mask, # type: ignore[arg-type]
valid_mask,
self.gauss_kernel_size,
)
except cv2.error as cv2err: # pylint: disable=catching-non-exception
Expand Down
1 change: 1 addition & 0 deletions eolearn/features/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ 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:
# type: ignore[arg-type]
resize_function = partial(_pil_resize_ndarray, size=size, method=resize_method.get_pil_method())

resized_data = _apply_to_spatial_axes(resize_function, data, spatial_axes)
Expand Down
7 changes: 2 additions & 5 deletions eolearn/mask/snow_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,12 @@ 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:
bands = eopatch[self.bands_feature][..., self.band_indices]
if bands.shape[0] == 0:
eopatch[self.mask_feature] = np.zeros((*bands.shape[:-1], 1), dtype=bool)
return eopatch

with np.errstate(divide="ignore", invalid="ignore"):
# (B03 - B11) / (B03 + B11)
ndsi = (bands[..., 0] - bands[..., 3]) / (bands[..., 0] + bands[..., 3])
Expand Down
11 changes: 10 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 FeatureType, EOPatch
from eolearn.mask import SnowMaskTask


Expand All @@ -33,3 +33,12 @@ 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):
bands = test_eopatch.data["BANDS-S2-L1C"]
empty_bands_array = np.array([], dtype=bands.dtype).reshape((0, *bands.shape[1:]))
empty_eopatch = EOPatch(bbox=test_eopatch.bbox, timestamps=[], data={"BANDS-S2-L1C": empty_bands_array})
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] == 0
mlubej marked this conversation as resolved.
Show resolved Hide resolved
Loading