Skip to content

Commit 4d350c4

Browse files
processed pr comments
1 parent 56e0731 commit 4d350c4

File tree

4 files changed

+74
-19
lines changed

4 files changed

+74
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
### Changed
1717

18-
- `load_stac` will now take over the requested band names if there is a mismatch/problem with the extracted collection metadata ([#752](https://github.com/Open-EO/openeo-python-client/issues/752))
18+
- When the bands provided to `Connection.load_stac(..., bands=[...])` do not fully match the bands the client extracted from the STAC metadata, a warning will be triggered, but the provided band names will still be used during the client-side preparation of the process graph. This is a pragmatic approach to bridge the gap between differing interpretations of band detection in STAC. Note that this might produce process graphs that are technically invalid and might not work on other backends or future versions of the backend you currently use. It is recommended to consult with the provider of the STAC metadata and openEO backend on the correct and future-proof band names. ([#752](https://github.com/Open-EO/openeo-python-client/issues/752))
1919

2020
### Removed
2121

openeo/rest/datacube.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,12 @@ def load_stac(
450450
type="bands",
451451
label=None,
452452
)
453-
missing_bands = [b for b in bands if not metadata.band_dimension.contains_band(b)]
454-
if len(missing_bands) == 0:
453+
unknown_bands = [b for b in bands if not metadata.band_dimension.contains_band(b)]
454+
if len(unknown_bands) == 0:
455455
metadata = metadata.filter_bands(band_names=bands)
456456
else:
457457
logging.warning(
458-
f"Bands {missing_bands} are not available in the collection metadata. Using requested bands as is."
458+
f"The specified bands {bands} are not a subset of the bands {metadata.band_dimension.band_names} found in the STAC metadata (unknown bands: {unknown_bands}). Using specified bands as is."
459459
)
460460
metadata = metadata.rename_labels(dimension="bands", target=bands)
461461
except Exception as e:

tests/rest/test_connection.py

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2957,7 +2957,40 @@ def test_load_stac_no_cube_extension_temporal_dimension(self, con120, tmp_path,
29572957
cube = con120.load_stac(str(stac_path))
29582958
assert cube.metadata.temporal_dimension == TemporalDimension(name="t", extent=dim_extent)
29592959

2960-
def test_load_stac_band_filtering(self, con120, tmp_path, caplog):
2960+
@pytest.mark.parametrize(
2961+
"bands, expected_warning",
2962+
[
2963+
(
2964+
["B04"],
2965+
"The specified bands ['B04'] are not a subset of the bands ['B01', 'B02', 'B03'] found in the STAC metadata (unknown bands: ['B04']). Using specified bands as is.",
2966+
),
2967+
(
2968+
["B03", "B04", "B05"],
2969+
"The specified bands ['B03', 'B04', 'B05'] are not a subset of the bands ['B01', 'B02', 'B03'] found in the STAC metadata (unknown bands: ['B04', 'B05']). Using specified bands as is.",
2970+
),
2971+
(["B03", "B02"], None),
2972+
(["B01", "B02", "B03"], None),
2973+
],
2974+
)
2975+
def test_load_stac_band_filtering(self, con120, tmp_path, caplog, bands, expected_warning):
2976+
stac_path = tmp_path / "stac.json"
2977+
stac_data = StacDummyBuilder.collection(
2978+
summaries={"eo:bands": [{"name": "B01"}, {"name": "B02"}, {"name": "B03"}]}
2979+
)
2980+
# TODO #738 real request mocking of STAC resources compatible with pystac?
2981+
stac_path.write_text(json.dumps(stac_data))
2982+
2983+
caplog.set_level(logging.WARNING)
2984+
# Test with non-existing bands in the collection metadata
2985+
cube = con120.load_stac(str(stac_path), bands=bands)
2986+
assert cube.metadata.band_names == bands
2987+
if expected_warning is None:
2988+
assert caplog.text == ""
2989+
else:
2990+
assert expected_warning in caplog.text
2991+
caplog.clear()
2992+
2993+
def test_load_stac_band_filtering_no_requested_bands(self, con120, tmp_path):
29612994
stac_path = tmp_path / "stac.json"
29622995
stac_data = StacDummyBuilder.collection(
29632996
summaries={"eo:bands": [{"name": "B01"}, {"name": "B02"}, {"name": "B03"}]}
@@ -2968,25 +3001,25 @@ def test_load_stac_band_filtering(self, con120, tmp_path, caplog):
29683001
cube = con120.load_stac(str(stac_path))
29693002
assert cube.metadata.band_names == ["B01", "B02", "B03"]
29703003

2971-
cube = con120.load_stac(str(stac_path), bands=["B03", "B02"])
2972-
assert cube.metadata.band_names == ["B03", "B02"]
3004+
def test_load_stac_band_filtering_no_metadata(self, con120, tmp_path, caplog):
3005+
stac_path = tmp_path / "stac.json"
3006+
stac_data = StacDummyBuilder.collection()
3007+
# TODO #738 real request mocking of STAC resources compatible with pystac?
3008+
stac_path.write_text(json.dumps(stac_data))
3009+
3010+
cube = con120.load_stac(str(stac_path))
3011+
assert cube.metadata.band_names == []
29733012

29743013
caplog.set_level(logging.WARNING)
2975-
# Test with non-existing bands in the collection metadata
2976-
cube = con120.load_stac(str(stac_path), bands=["B04"])
2977-
assert cube.metadata.band_names == ["B04"]
2978-
expected_warning = "Bands ['B04'] are not available in the collection metadata. Using requested bands as is."
2979-
assert expected_warning in caplog.text
2980-
caplog.clear()
2981-
2982-
cube = con120.load_stac(str(stac_path), bands=["B03", "B04", "B05"])
2983-
assert cube.metadata.band_names == ["B03", "B04", "B05"]
2984-
expected_warning = (
2985-
"Bands ['B04', 'B05'] are not available in the collection metadata. Using requested bands as is."
3014+
cube = con120.load_stac(str(stac_path), bands=["B01", "B02"])
3015+
assert cube.metadata.band_names == ["B01", "B02"]
3016+
assert (
3017+
"The specified bands ['B01', 'B02'] are not a subset of the bands [] found in the STAC metadata (unknown bands: ['B01', 'B02']). Using specified bands as is."
3018+
in caplog.text
29863019
)
2987-
assert expected_warning in caplog.text
29883020
caplog.clear()
29893021

3022+
29903023
@pytest.mark.parametrize(
29913024
"bands",
29923025
[

tests/test_metadata.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ def test_band_dimension_band_index():
100100
bdim.band_index("yellow")
101101

102102

103+
def test_band_dimension_contains_band():
104+
bdim = BandDimension(
105+
name="spectral",
106+
bands=[
107+
Band("B02", "blue", 0.490),
108+
Band("B03", "green", 0.560),
109+
Band("B04", "red", 0.665),
110+
],
111+
)
112+
113+
# Test band names
114+
assert bdim.contains_band("B02")
115+
assert not bdim.contains_band("B05")
116+
117+
# Test indexes
118+
assert bdim.contains_band(0)
119+
assert not bdim.contains_band(4)
120+
121+
# Test common names
122+
assert bdim.contains_band("blue")
123+
assert not bdim.contains_band("yellow")
124+
103125
def test_band_dimension_band_name():
104126
bdim = BandDimension(
105127
name="spectral",

0 commit comments

Comments
 (0)