Skip to content

Commit

Permalink
Allow to pass a dict for the summarized fields, fix the doc rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Aug 1, 2023
1 parent ebe5c6b commit a079c46
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

## [Unreleased]

### Added

- Allow to pass a Dict with field names and summary strategies to the `fields` parameter in the `Summarizer` constructor

### Changed

- Pin jsonschema version to <4.18 until regresssions are fixed

### Fixed

- Fix the documentation rendering of the `fields` parameter in the `Summarizer` constructor

## [v1.8.2] - 2023-07-12

### Fixed
Expand Down
15 changes: 10 additions & 5 deletions pystac/summaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,20 @@ class Summarizer:
https://github.com/stac-utils/stac-fields
Args:
fields (str): the path to the json file with field descriptions.
If no file is passed, a default one will be used.
fields: A string containing the path to the json file with field descriptions.
Alternatively, a dict with the field names as keys and SummaryStrategy's
as values.
If nothing is passed, a default file with field descriptions will be used.
"""

summaryfields: Dict[str, SummaryStrategy]

def __init__(self, fields: Optional[str] = None):
jsonfields = _get_fields_json(fields)
self._set_field_definitions(jsonfields)
def __init__(self, fields: Optional[Union[str, Dict[str, SummaryStrategy]]] = None):
if isinstance(fields, dict):
self.summaryfields = fields
else:
jsonfields = _get_fields_json(fields)
self._set_field_definitions(jsonfields)

def _set_field_definitions(self, fields: Dict[str, Any]) -> None:
self.summaryfields = {}
Expand Down
15 changes: 14 additions & 1 deletion tests/test_summaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import unittest
from typing import Any

from pystac.summaries import RangeSummary, Summaries, Summarizer
from pystac.summaries import RangeSummary, Summaries, Summarizer, SummaryStrategy
from tests.utils import TestCases


Expand Down Expand Up @@ -30,6 +30,19 @@ def test_summary_custom_fields_file(self) -> None:
self.assertIsNone(summaries_dict.get("eo:bands"))
self.assertEqual(len(summaries_dict["proj:epsg"]), 1)

def test_summary_custom_fields_dict(self) -> None:
coll = TestCases.case_5()
spec = {
"eo:bands": SummaryStrategy.DONT_SUMMARIZE,
"proj:epsg": SummaryStrategy.ARRAY,
}
obj = Summarizer(spec)
self.assertEqual(obj.summaryfields, spec)
summaries = obj.summarize(coll.get_items(recursive=True))
summaries_dict = summaries.to_dict()
self.assertIsNone(summaries_dict.get("eo:bands"))
self.assertEqual(len(summaries_dict["proj:epsg"]), 1)

def test_summary_wrong_custom_fields_file(self) -> None:
coll = TestCases.case_5()
with self.assertRaises(FileNotFoundError) as context:
Expand Down

0 comments on commit a079c46

Please sign in to comment.