Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 6 additions & 3 deletions core/eolearn/core/eodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,19 +352,22 @@ def __setitem__(

return self.__setattr__(FeatureType(feature_type).value, value, feature_name=feature_name)

def __delitem__(self, feature: Tuple[FeatureType, str]) -> None:
def __delitem__(self, feature: FeatureSpec) -> None:
"""Deletes the selected feature.

:param feature: EOPatch feature
"""
self._check_tuple_key(feature)
feature_type, feature_name = feature
del self[feature_type][feature_name]
if feature_type in [FeatureType.BBOX, FeatureType.TIMESTAMP]:
self.reset_feature_type(feature_type)
else:
del self[feature_type][feature_name]

@staticmethod
def _check_tuple_key(key: tuple) -> None:
"""A helper function that checks a tuple, which should hold (feature_type, feature_name)."""
if len(key) != 2:
if not isinstance(key, (tuple, list)) or len(key) != 2:
raise ValueError(f"Given element should be a tuple of (feature_type, feature_name), but {key} found.")

def __eq__(self, other: object) -> bool:
Expand Down
4 changes: 3 additions & 1 deletion core/eolearn/tests/test_eodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,18 @@ def test_simplified_feature_operations() -> None:
(FeatureType.MASK, "ones"),
(FeatureType.MASK_TIMELESS, "threes"),
(FeatureType.META_INFO, "beep"),
(FeatureType.BBOX, None),
],
)
def test_delete_existing_feature(feature: Tuple[FeatureType, str], mini_eopatch: EOPatch) -> None:
def test_delete_existing_feature(feature: FeatureSpec, mini_eopatch: EOPatch) -> None:
old = mini_eopatch.copy(deep=True)

del mini_eopatch[feature]
assert feature not in mini_eopatch

for old_feature in old.get_features():
if old_feature != feature:
# this also works for BBox :D
assert_array_equal(old[old_feature], mini_eopatch[old_feature])


Expand Down