Skip to content

Commit

Permalink
Add delete instructions for bbox and timestamps (#545)
Browse files Browse the repository at this point in the history
* add delete instructions for bbox and timestamps

* fix code-flow issues and update types
  • Loading branch information
zigaLuksic committed Jan 20, 2023
1 parent d288629 commit dfada66
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
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

0 comments on commit dfada66

Please sign in to comment.