Skip to content

Commit c64f473

Browse files
authored
Merge pull request #386 from gro-intelligence/GAIA-29324
add new parameters to `get_area_weighted_series()`
2 parents b89fdf9 + 4e2b12f commit c64f473

File tree

7 files changed

+81
-20
lines changed

7 files changed

+81
-20
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ dist/
33
docs/_build/
44

55
.coverage
6+
.DS_Store
7+

docs/authentication.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ To work with the Gro API, you need an authentication token. This token needs to
1010
Retrieving a token
1111
==================
1212

13-
Note that your account needs to be activated for API access before you will be able to retrieve a token. See https://gro-intelligence.com/products/gro-api for more info regarding unlocking API access for your account.
13+
Note that your account needs to be activated for API access before you will be able to retrieve a token. Please contact your sale rep or Customer Success to activate your account for API access. If you have any questions, please email [email protected].
1414
Once you have API access enabled for your account, you may retrieve your token by following the steps below:
1515

1616

docs/faq.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ You must get an `authentication token <authentication#retrieving-a-token>`_ from
9898
Why am I getting a 401 Unauthorized error when I try to use my Gro username and login?
9999
--------------------------------------------------------------------------------------
100100

101-
A Gro account gives you access to the web application at app.gro-intelligence.com. API access is sold as an add-on product you need to be activated for. To learn more about getting an API account, contact our sales team using the link at `gro-intelligence.com/products/gro-api <https://www.gro-intelligence.com/products/gro-api>`_
101+
A Gro account gives you access to the web application at app.gro-intelligence.com. API access is sold as an add-on product you need to be activated for. To learn more about getting an API account, contact our sales team at support@gro-intelligence.com
102102

103103
Gro Models
104104
==========

groclient/client.py

+40-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
import time
77

8-
from typing import List, Union
8+
from typing import Dict, List, Optional, Union
99

1010
# Python3 support
1111
try:
@@ -1813,10 +1813,15 @@ def get_area_weighted_series(
18131813
series_name: str,
18141814
weight_names: List[str],
18151815
region_id: Union[int, List[int]],
1816+
aggregation: Optional[str] = None,
1817+
start_date: Optional[str] = None,
1818+
end_date: Optional[str] = None,
1819+
limit: Optional[Union[int, str]] = "all",
1820+
level: Optional[int] = None,
18161821
method: str = "sum",
18171822
latest_date_only: bool = False,
1818-
metadata: bool = False
1819-
):
1823+
metadata: bool = False,
1824+
) -> Dict[str, float]:
18201825
"""Compute weighted average on selected series with the given weights.
18211826
18221827
Returns a dictionary mapping dates to weighted values.
@@ -1832,13 +1837,33 @@ def get_area_weighted_series(
18321837
region_id: integer or list of integers
18331838
The region or regions for which the weighted series will be computed
18341839
Supported region levels are (1, 2, 3, 4, 5, 8)
1835-
method: str, optional
1836-
'sum' by default. Multi-crop weights can be calculated with either 'sum' or 'normalize' method.
1837-
latest_date_only: bool, optional
1838-
False by default. If True, will return a single key-value pair where the key is the latested date.
1840+
aggregation: str, optional
1841+
A string that can be parsed into components used to do the aggregation.
1842+
Format: {type}-{data_points/start_doy}
1843+
e.g. "total-7" returns 7 data point trailing totals
1844+
e.g. "ytd" returns year to date rolling cumulative totals for each year
1845+
in the series.
1846+
e.g. "ytd-5" returns the rolling cumulative totals for each year starting from
1847+
the 5th day of the year to the 4th day of the following year.
1848+
start_date: str, optional
1849+
A timestamp of the format 'YYYY-MM-DD', e.g. '2023-01-01'
1850+
end_date: str, optional
1851+
A timestamp of the format 'YYYY-MM-DD', e.g. '2023-01-01'
1852+
limit: Union[int, str], optional, default="all"
1853+
Will return the x latest data points within a given range. Example usage: `limit=1` returns
1854+
the latest data point.
1855+
A value of 'all' indicates returning all data within the specified range.
1856+
level: int, optional
1857+
The region level to which data will be aggregated before being returned.
1858+
If level not provided, the endpoint will aggregate to the level of each specified
1859+
region id.
1860+
method: str, optional, default="sum"
1861+
Multi-crop weights can be calculated with either 'sum' or 'normalize' method.
1862+
latest_date_only: bool, optional, default=False
1863+
If True, will return a single key-value pair where the key is the latested date.
18391864
e.g. {'2000-03-12': 0.221}
1840-
metadata: bool, optional
1841-
False by default. If True, will return the metadata for the given series and weights.
1865+
metadata: bool, optional, default=False
1866+
If True, will return the metadata for the given series and weights.
18421867
18431868
Returns
18441869
-------
@@ -1853,9 +1878,14 @@ def get_area_weighted_series(
18531878
series_name,
18541879
weight_names,
18551880
region_id,
1881+
aggregation,
1882+
start_date,
1883+
end_date,
1884+
limit,
1885+
level,
18561886
method,
18571887
latest_date_only,
1858-
metadata
1888+
metadata,
18591889
)
18601890

18611891
def get_area_weighting_weight_metadata(

groclient/client_test.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ def mock_get_descendant(
145145
childs = list(mock_entities["regions"].values())
146146

147147
if not include_historical or include_details:
148-
149148
if not include_historical:
150149
childs = [child for child in childs if not child["historical"]]
151150

@@ -182,7 +181,6 @@ def mock_get_ancestor(
182181
childs = list(mock_entities["regions"].values())
183182

184183
if not include_historical or include_details:
185-
186184
if not include_historical:
187185
childs = [child for child in childs if not child["historical"]]
188186

@@ -273,9 +271,14 @@ def mock_get_area_weighted_series(
273271
series_name,
274272
weight_names,
275273
region_id,
274+
aggregation,
275+
start_date,
276+
end_date,
277+
limit,
278+
level,
276279
method,
277280
latest_date_only,
278-
metadata
281+
metadata,
279282
):
280283
return {"2022-07-11": 0.715615, "2022-07-19": 0.733129, "2022-07-27": 0.748822}
281284

groclient/lib.py

+21-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import warnings
2323

2424
from pkg_resources import get_distribution, DistributionNotFound
25-
from typing import List, Union
25+
from typing import Dict, List, Optional, Union
2626

2727
try:
2828
# functools are native in Python 3.2.3+
@@ -111,7 +111,11 @@ def get_access_token(api_host, user_email, user_password, logger=None):
111111
accessToken : string
112112
113113
"""
114-
warnings.warn(f'get_access_token() is deprecated and will be removed on November 1, 2023.', DeprecationWarning, 2)
114+
warnings.warn(
115+
f"get_access_token() is deprecated and will be removed on November 1, 2023.",
116+
DeprecationWarning,
117+
2,
118+
)
115119
retry_count = 0
116120
if not logger:
117121
logger = get_default_logger()
@@ -312,7 +316,9 @@ def lookup(access_token, api_host, entity_type, entity_ids):
312316
try: # Convert iterable types like numpy arrays or tuples into plain lists
313317
entity_ids = list(entity_ids)
314318
return lookup_batch(access_token, api_host, entity_type, entity_ids)
315-
except TypeError: # Convert anything else, like strings or numpy integers, into plain integers
319+
except (
320+
TypeError
321+
): # Convert anything else, like strings or numpy integers, into plain integers
316322
entity_id = int(entity_ids)
317323
# If an integer is given, return only the dict with that id
318324
return lookup_single(access_token, api_host, entity_type, entity_id)
@@ -845,10 +851,15 @@ def get_area_weighted_series(
845851
series_name: str,
846852
weight_names: List[str],
847853
region_id: Union[int, List[int]],
854+
aggregation: Optional[str],
855+
start_date: Optional[str],
856+
end_date: Optional[str],
857+
limit: Optional[Union[int, str]],
858+
level: Optional[int],
848859
method: str,
849860
latest_date_only: bool,
850861
metadata: bool,
851-
):
862+
) -> Dict[str, float]:
852863
url = f"https://{api_host}/area-weighting"
853864
headers = {"authorization": "Bearer " + access_token}
854865
if isinstance(region_id, int):
@@ -857,9 +868,14 @@ def get_area_weighted_series(
857868
"seriesName": series_name,
858869
"weightNames": weight_names,
859870
"regionIds": region_id,
871+
"aggregation": aggregation,
872+
"startDate": start_date,
873+
"endDate": end_date,
874+
"limit": limit,
875+
"level": level,
860876
"method": method,
861877
"latestDateOnly": latest_date_only,
862-
"metadata": metadata
878+
"metadata": metadata,
863879
}
864880
resp = get_data(url, headers, params=params)
865881
return resp.json()

groclient/lib_test.py

+10
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,11 @@ def test_get_area_weighted_series(mock_requests_get):
11491149
"NDVI_8day",
11501150
["Barley (ha)", "Corn (ha)"],
11511151
1215,
1152+
None,
1153+
None,
1154+
None,
1155+
"all",
1156+
None,
11521157
"sum",
11531158
False,
11541159
False
@@ -1163,6 +1168,11 @@ def test_get_area_weighted_series(mock_requests_get):
11631168
"NDVI_8day",
11641169
["Barley (ha)", "Corn (ha)"],
11651170
[1215],
1171+
None,
1172+
None,
1173+
None,
1174+
"all",
1175+
None,
11661176
"sum",
11671177
False,
11681178
True

0 commit comments

Comments
 (0)