From 068c731d3ba5723cf8cbf76c163e0dce9c2be0d0 Mon Sep 17 00:00:00 2001 From: Burak <68427259+burakekim@users.noreply.github.com> Date: Fri, 3 Jan 2025 20:07:45 +0100 Subject: [PATCH 1/4] handle Nones in get_label --- torchgeo/datasets/eurocrops.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/torchgeo/datasets/eurocrops.py b/torchgeo/datasets/eurocrops.py index 5f438143c87..06774c621b5 100644 --- a/torchgeo/datasets/eurocrops.py +++ b/torchgeo/datasets/eurocrops.py @@ -204,6 +204,10 @@ 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: + print(f"Feature does not contain the label '{self.label_name}'. Skip rendering.") + return 0 + while True: if hcat_code in self.class_map: return self.class_map[hcat_code] From 1b1838712a2b2d3c42a95dcb90545fbe5f61c869 Mon Sep 17 00:00:00 2001 From: Burak Date: Fri, 3 Jan 2025 20:50:48 +0100 Subject: [PATCH 2/4] make ruff hapy --- torchgeo/datasets/eurocrops.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/torchgeo/datasets/eurocrops.py b/torchgeo/datasets/eurocrops.py index 06774c621b5..bac2b03552a 100644 --- a/torchgeo/datasets/eurocrops.py +++ b/torchgeo/datasets/eurocrops.py @@ -205,9 +205,11 @@ def get_label(self, feature: 'fiona.model.Feature') -> int: # (Parent code is computed by replacing rightmost non-0 character with 0.) hcat_code = feature['properties'][self.label_name] if hcat_code is None: - print(f"Feature does not contain the label '{self.label_name}'. Skip rendering.") + print( + f"Feature does not contain the label '{self.label_name}'. Skip rendering." + ) return 0 - + while True: if hcat_code in self.class_map: return self.class_map[hcat_code] From 359066624648334d91b078c298917be8ad996d58 Mon Sep 17 00:00:00 2001 From: Burak Date: Fri, 3 Jan 2025 21:01:15 +0100 Subject: [PATCH 3/4] unit test for the win --- tests/datasets/test_eurocrops.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/datasets/test_eurocrops.py b/tests/datasets/test_eurocrops.py index 3b2d4fc63f7..63896e23090 100644 --- a/tests/datasets/test_eurocrops.py +++ b/tests/datasets/test_eurocrops.py @@ -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() From 459351072c17aa7348b79e88e5fb83183e23ebc1 Mon Sep 17 00:00:00 2001 From: Burak Date: Fri, 10 Jan 2025 00:00:36 +0100 Subject: [PATCH 4/4] remove print statement --- torchgeo/datasets/eurocrops.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/torchgeo/datasets/eurocrops.py b/torchgeo/datasets/eurocrops.py index bac2b03552a..8832905c2d6 100644 --- a/torchgeo/datasets/eurocrops.py +++ b/torchgeo/datasets/eurocrops.py @@ -205,9 +205,6 @@ def get_label(self, feature: 'fiona.model.Feature') -> int: # (Parent code is computed by replacing rightmost non-0 character with 0.) hcat_code = feature['properties'][self.label_name] if hcat_code is None: - print( - f"Feature does not contain the label '{self.label_name}'. Skip rendering." - ) return 0 while True: