Skip to content

Commit eb79166

Browse files
committed
Allow to pass a dict for the summarized fields, fix the doc rendering
1 parent ebe5c6b commit eb79166

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
- Allow to pass a Dict with field names and summary strategies to the `fields` parameter in the `Summarizer` constructor
8+
59
### Changed
610

711
- Pin jsonschema version to <4.18 until regresssions are fixed
812

13+
### Fixed
14+
15+
- Fix the documentation rendering of the `fields` parameter in the `Summarizer` constructor
16+
917
## [v1.8.2] - 2023-07-12
1018

1119
### Fixed

pystac/summaries.py

+29-15
Original file line numberDiff line numberDiff line change
@@ -130,36 +130,50 @@ class SummaryStrategy(Enum):
130130

131131
class Summarizer:
132132
"""The Summarizer computes summaries from values, following the definition of fields
133-
to summarize provided in a json file.
133+
to summarize.
134134
135-
For more information about the structure of the fields json file, see:
135+
The fields to summarize can be provided as a JSON file or as a dictionary of
136+
field names and SummaryStrategys. If nothing is provided, a default JSON file
137+
will be used.
136138
139+
For more information about the structure of the fields JSON file, see:
137140
https://github.com/stac-utils/stac-fields
138141
142+
The default JSON file used is a snapshot of the following file at the time of
143+
the pystac release:
144+
https://cdn.jsdelivr.net/npm/@radiantearth/stac-fields/fields-normalized.json
145+
139146
Args:
140-
fields (str): the path to the json file with field descriptions.
141-
If no file is passed, a default one will be used.
147+
fields: A string containing the path to the json file with field descriptions.
148+
Alternatively, a dict with the field names as keys and SummaryStrategys
149+
as values.
150+
If nothing is passed, a default file with field descriptions will be used.
142151
"""
143152

144153
summaryfields: Dict[str, SummaryStrategy]
145154

146-
def __init__(self, fields: Optional[str] = None):
147-
jsonfields = _get_fields_json(fields)
148-
self._set_field_definitions(jsonfields)
155+
def __init__(self, fields: Optional[Union[str, Dict[str, SummaryStrategy]]] = None):
156+
if isinstance(fields, dict):
157+
self._set_field_definitions(fields)
158+
else:
159+
jsonfields = _get_fields_json(fields)
160+
self._set_field_definitions(jsonfields["metadata"])
149161

150162
def _set_field_definitions(self, fields: Dict[str, Any]) -> None:
151163
self.summaryfields = {}
152-
for name, desc in fields["metadata"].items():
153-
if isinstance(desc, dict):
164+
for name, desc in fields.items():
165+
strategy: SummaryStrategy = SummaryStrategy.DEFAULT
166+
if isinstance(desc, SummaryStrategy):
167+
strategy = desc
168+
elif isinstance(desc, dict):
154169
strategy_value = desc.get("summary", True)
155170
try:
156-
strategy: SummaryStrategy = SummaryStrategy(strategy_value)
171+
strategy = SummaryStrategy(strategy_value)
157172
except ValueError:
158-
strategy = SummaryStrategy.DEFAULT
159-
if strategy != SummaryStrategy.DONT_SUMMARIZE:
160-
self.summaryfields[name] = strategy
161-
else:
162-
self.summaryfields[name] = SummaryStrategy.DEFAULT
173+
pass
174+
175+
if strategy != SummaryStrategy.DONT_SUMMARIZE:
176+
self.summaryfields[name] = strategy
163177

164178
def _update_with_item(self, summaries: Summaries, item: Item) -> None:
165179
for k, v in item.properties.items():

tests/test_summaries.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import unittest
33
from typing import Any
44

5-
from pystac.summaries import RangeSummary, Summaries, Summarizer
5+
from pystac.summaries import RangeSummary, Summaries, Summarizer, SummaryStrategy
66
from tests.utils import TestCases
77

88

@@ -30,6 +30,20 @@ def test_summary_custom_fields_file(self) -> None:
3030
self.assertIsNone(summaries_dict.get("eo:bands"))
3131
self.assertEqual(len(summaries_dict["proj:epsg"]), 1)
3232

33+
def test_summary_custom_fields_dict(self) -> None:
34+
coll = TestCases.case_5()
35+
spec = {
36+
"eo:bands": SummaryStrategy.DONT_SUMMARIZE,
37+
"proj:epsg": SummaryStrategy.ARRAY,
38+
}
39+
obj = Summarizer(spec)
40+
self.assertTrue("eo:bands" not in obj.summaryfields)
41+
self.assertEqual(obj.summaryfields["proj:epsg"], SummaryStrategy.ARRAY)
42+
summaries = obj.summarize(coll.get_items(recursive=True))
43+
summaries_dict = summaries.to_dict()
44+
self.assertIsNone(summaries_dict.get("eo:bands"))
45+
self.assertEqual(len(summaries_dict["proj:epsg"]), 1)
46+
3347
def test_summary_wrong_custom_fields_file(self) -> None:
3448
coll = TestCases.case_5()
3549
with self.assertRaises(FileNotFoundError) as context:

0 commit comments

Comments
 (0)