From 08637a9ffe175327d6412f9537e8741fe7266f72 Mon Sep 17 00:00:00 2001 From: Tuan Nguyen Date: Thu, 15 Sep 2022 10:06:26 +0200 Subject: [PATCH 1/5] geospatial aggregation to support output --- CHANGELOG.md | 4 ++++ cognite/client/_api/geospatial.py | 20 ++++++++++++++++--- cognite/client/_version.py | 2 +- pyproject.toml | 2 +- .../test_api/test_geospatial.py | 8 ++++++++ 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d42d8c6ddc..378360b1a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,10 @@ Changes are grouped as follows - `Fixed` for any bug fixes. - `Security` in case of vulnerabilities. +## [4.6.0] - 2022-09-15 +### Changed +- Change geospatial.aggregate_features to support `aggregate_output` + ## [4.5.2] - 2022-09-09 ### Fixed - Fixes the issue when updating transformations with new nonce credentials diff --git a/cognite/client/_api/geospatial.py b/cognite/client/_api/geospatial.py index c6ba001e0e..25a9313d6a 100644 --- a/cognite/client/_api/geospatial.py +++ b/cognite/client/_api/geospatial.py @@ -764,11 +764,12 @@ def stream_features( def aggregate_features( self, feature_type_external_id: str, - property: str, - aggregates: Sequence[str], + property: str = None, + aggregates: Sequence[str] = None, filter: Optional[Dict[str, Any]] = None, group_by: Sequence[str] = None, order_by: Sequence[OrderSpec] = None, + aggregate_output: Dict[str, Any] = None, ) -> FeatureAggregateList: """`Aggregate filtered features` @@ -780,6 +781,7 @@ def aggregate_features( aggregates (Sequence[str]): list of aggregates to be calculated group_by (Sequence[str]): list of properties to group by with order_by (Sequence[OrderSpec]): the order specification + aggregate_output (Dict[str, Any]): the aggregate output Returns: FeatureAggregateList: the filtered features @@ -794,17 +796,28 @@ def aggregate_features( ... feature_type_external_id="my_feature_type", ... feature=Feature(external_id="my_feature", temperature=12.4) ... ) - >>> res = c.geospatial.aggregate_features( + >>> res_deprecated = c.geospatial.aggregate_features( ... feature_type_external_id="my_feature_type", ... filter={"range": {"property": "temperature", "gt": 12.0}}, ... property="temperature", ... aggregates=["max", "min"], ... group_by=["category"], ... order_by=[OrderSpec("category", "ASC")] + ... ) # deprecated + >>> res = c.geospatial.aggregate_features( + ... feature_type_external_id="my_feature_type", + ... filter={"range": {"property": "temperature", "gt": 12.0}}, + ... group_by=["category"], + ... order_by=[OrderSpec("category", "ASC")], + ... aggregate_output={"min_temperature": {"min": {"property": "temperature"}}, + ... "max_temperature": {"max": {"property": "temperature"}} + ... } ... ) >>> for a in res: ... # loop over aggregates in different groups """ + if property or aggregates: + warnings.warn("property and aggregates are deprecated, use aggregate_output instead.", DeprecationWarning) resource_path = self._feature_resource_path(feature_type_external_id) + "/aggregate" cls = FeatureAggregateList order = None if order_by is None else [f"{item.property}:{item.direction}" for item in order_by] @@ -816,6 +829,7 @@ def aggregate_features( "aggregates": aggregates, "groupBy": group_by, "sort": order, + "output": aggregate_output }, ) return cls._load(res.json()["items"], cognite_client=self._cognite_client) diff --git a/cognite/client/_version.py b/cognite/client/_version.py index ce0a9858c2..9b139df561 100644 --- a/cognite/client/_version.py +++ b/cognite/client/_version.py @@ -1,2 +1,2 @@ -__version__ = "4.5.2" +__version__ = "4.6.0" __api_subversion__ = "V20220125" diff --git a/pyproject.toml b/pyproject.toml index 3f365b9fc2..77df40ed5a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [tool.poetry] name = "cognite-sdk" -version = "4.5.2" +version = "4.6.0" description = "Cognite Python SDK" readme = "README.md" diff --git a/tests/tests_integration/test_api/test_geospatial.py b/tests/tests_integration/test_api/test_geospatial.py index 0c398dc940..83ee26a07e 100644 --- a/tests/tests_integration/test_api/test_geospatial.py +++ b/tests/tests_integration/test_api/test_geospatial.py @@ -589,3 +589,11 @@ def test_aggregate_with_order_by(self, cognite_client, test_feature_type, test_f order_by=[OrderSpec("externalId", "ASC")], ) assert external_ids == [item.external_id for item in res_asc] + + def test_aggregate_output(self, cognite_client, test_feature_type, test_features): + res = cognite_client.geospatial.aggregate_features( + feature_type_external_id=test_feature_type.external_id, + filter={"range": {"property": "temperature", "gt": 12.0, "lt": 13.0}}, + aggregate_output={"count": {"count": {"property": "temperature"}}} + ) + assert res[0].count == 1 From 9cd0ce33a7e4bddc98adeb879b552dc54bba691f Mon Sep 17 00:00:00 2001 From: Tuan Nguyen Date: Thu, 15 Sep 2022 10:21:51 +0200 Subject: [PATCH 2/5] format --- cognite/client/_api/geospatial.py | 2 +- tests/tests_integration/test_api/test_geospatial.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cognite/client/_api/geospatial.py b/cognite/client/_api/geospatial.py index 25a9313d6a..eaa09c4816 100644 --- a/cognite/client/_api/geospatial.py +++ b/cognite/client/_api/geospatial.py @@ -829,7 +829,7 @@ def aggregate_features( "aggregates": aggregates, "groupBy": group_by, "sort": order, - "output": aggregate_output + "output": aggregate_output, }, ) return cls._load(res.json()["items"], cognite_client=self._cognite_client) diff --git a/tests/tests_integration/test_api/test_geospatial.py b/tests/tests_integration/test_api/test_geospatial.py index 83ee26a07e..6354f19764 100644 --- a/tests/tests_integration/test_api/test_geospatial.py +++ b/tests/tests_integration/test_api/test_geospatial.py @@ -594,6 +594,6 @@ def test_aggregate_output(self, cognite_client, test_feature_type, test_features res = cognite_client.geospatial.aggregate_features( feature_type_external_id=test_feature_type.external_id, filter={"range": {"property": "temperature", "gt": 12.0, "lt": 13.0}}, - aggregate_output={"count": {"count": {"property": "temperature"}}} + aggregate_output={"count": {"count": {"property": "temperature"}}}, ) assert res[0].count == 1 From ce100cb959637946209be531cc823a83f24e23b2 Mon Sep 17 00:00:00 2001 From: Tuan Nguyen Date: Mon, 26 Sep 2022 16:22:23 +0200 Subject: [PATCH 3/5] rename --- cognite/client/_api/geospatial.py | 8 ++++---- tests/tests_integration/test_api/test_geospatial.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cognite/client/_api/geospatial.py b/cognite/client/_api/geospatial.py index eaa09c4816..d3877458f5 100644 --- a/cognite/client/_api/geospatial.py +++ b/cognite/client/_api/geospatial.py @@ -769,7 +769,7 @@ def aggregate_features( filter: Optional[Dict[str, Any]] = None, group_by: Sequence[str] = None, order_by: Sequence[OrderSpec] = None, - aggregate_output: Dict[str, Any] = None, + output: Dict[str, Any] = None, ) -> FeatureAggregateList: """`Aggregate filtered features` @@ -781,7 +781,7 @@ def aggregate_features( aggregates (Sequence[str]): list of aggregates to be calculated group_by (Sequence[str]): list of properties to group by with order_by (Sequence[OrderSpec]): the order specification - aggregate_output (Dict[str, Any]): the aggregate output + output (Dict[str, Any]): the aggregate output Returns: FeatureAggregateList: the filtered features @@ -809,7 +809,7 @@ def aggregate_features( ... filter={"range": {"property": "temperature", "gt": 12.0}}, ... group_by=["category"], ... order_by=[OrderSpec("category", "ASC")], - ... aggregate_output={"min_temperature": {"min": {"property": "temperature"}}, + ... output={"min_temperature": {"min": {"property": "temperature"}}, ... "max_temperature": {"max": {"property": "temperature"}} ... } ... ) @@ -829,7 +829,7 @@ def aggregate_features( "aggregates": aggregates, "groupBy": group_by, "sort": order, - "output": aggregate_output, + "output": output, }, ) return cls._load(res.json()["items"], cognite_client=self._cognite_client) diff --git a/tests/tests_integration/test_api/test_geospatial.py b/tests/tests_integration/test_api/test_geospatial.py index 6354f19764..d04a51c60a 100644 --- a/tests/tests_integration/test_api/test_geospatial.py +++ b/tests/tests_integration/test_api/test_geospatial.py @@ -594,6 +594,6 @@ def test_aggregate_output(self, cognite_client, test_feature_type, test_features res = cognite_client.geospatial.aggregate_features( feature_type_external_id=test_feature_type.external_id, filter={"range": {"property": "temperature", "gt": 12.0, "lt": 13.0}}, - aggregate_output={"count": {"count": {"property": "temperature"}}}, + output={"count": {"count": {"property": "temperature"}}}, ) assert res[0].count == 1 From 3b1c5fe44f792d41c9fe8a7d2667a75cb93e064c Mon Sep 17 00:00:00 2001 From: Tuan Nguyen Date: Mon, 26 Sep 2022 16:40:59 +0200 Subject: [PATCH 4/5] fix deprecation message --- cognite/client/_api/geospatial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cognite/client/_api/geospatial.py b/cognite/client/_api/geospatial.py index d3877458f5..653df73167 100644 --- a/cognite/client/_api/geospatial.py +++ b/cognite/client/_api/geospatial.py @@ -817,7 +817,7 @@ def aggregate_features( ... # loop over aggregates in different groups """ if property or aggregates: - warnings.warn("property and aggregates are deprecated, use aggregate_output instead.", DeprecationWarning) + warnings.warn("property and aggregates are deprecated, use output instead.", DeprecationWarning) resource_path = self._feature_resource_path(feature_type_external_id) + "/aggregate" cls = FeatureAggregateList order = None if order_by is None else [f"{item.property}:{item.direction}" for item in order_by] From f9af48e386281573dac575a75246c7ca988d6672 Mon Sep 17 00:00:00 2001 From: Tuan Nguyen Date: Tue, 27 Sep 2022 13:48:06 +0200 Subject: [PATCH 5/5] improve example --- cognite/client/_api/geospatial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cognite/client/_api/geospatial.py b/cognite/client/_api/geospatial.py index 653df73167..d31b3e64f6 100644 --- a/cognite/client/_api/geospatial.py +++ b/cognite/client/_api/geospatial.py @@ -810,7 +810,7 @@ def aggregate_features( ... group_by=["category"], ... order_by=[OrderSpec("category", "ASC")], ... output={"min_temperature": {"min": {"property": "temperature"}}, - ... "max_temperature": {"max": {"property": "temperature"}} + ... "max_volume": {"max": {"property": "volume"}} ... } ... ) >>> for a in res: