Skip to content

Commit

Permalink
EuroCrops: handle Nones in get_label (#2499)
Browse files Browse the repository at this point in the history
* handle Nones in get_label

* make ruff hapy

* unit test for the win

* remove print statement
  • Loading branch information
burakekim authored Feb 3, 2025
1 parent 33a1fc7 commit 29b5773
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/datasets/test_eurocrops.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ def test_invalid_query(self, dataset: EuroCrops) -> None:
):
dataset[query]

def test_get_label_with_none_hcat_code(self, dataset: EuroCrops) -> None:
mock_feature = {'properties': {dataset.label_name: None}}
label = dataset.get_label(mock_feature)
assert label == 0, "Expected label to be 0 when 'EC_hcat_c' is None."

def test_integrity_error(self, dataset: EuroCrops) -> None:
dataset.zenodo_files = (('AA.zip', 'invalid'),)
assert not dataset._check_integrity()
3 changes: 3 additions & 0 deletions torchgeo/datasets/eurocrops.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ def get_label(self, feature: 'fiona.model.Feature') -> int:
# We go up the class hierarchy until there is a match.
# (Parent code is computed by replacing rightmost non-0 character with 0.)
hcat_code = feature['properties'][self.label_name]
if hcat_code is None:
return 0

while True:
if hcat_code in self.class_map:
return self.class_map[hcat_code]
Expand Down

0 comments on commit 29b5773

Please sign in to comment.