Skip to content

Commit

Permalink
Merge pull request #383 from gro-intelligence/GAIA-20785-add-metadata…
Browse files Browse the repository at this point in the history
…-flag-area-weighted-series

GAIA-20785 - Add metadata support on area weighted series
  • Loading branch information
prabesh-paudel authored Jul 21, 2023
2 parents f208327 + 3f6ed0c commit f38af39
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
4 changes: 4 additions & 0 deletions groclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,7 @@ def get_area_weighted_series(
region_id: Union[int, List[int]],
method: str = "sum",
latest_date_only: bool = False,
metadata: bool = False
):
"""Compute weighted average on selected series with the given weights.
Expand All @@ -1836,6 +1837,8 @@ def get_area_weighted_series(
latest_date_only: bool, optional
False by default. If True, will return a single key-value pair where the key is the latested date.
e.g. {'2000-03-12': 0.221}
metadata: bool, optional
False by default. If True, will return the metadata for the given series and weights.
Returns
-------
Expand All @@ -1852,6 +1855,7 @@ def get_area_weighted_series(
region_id,
method,
latest_date_only,
metadata
)

def get_area_weighting_weight_metadata(
Expand Down
1 change: 1 addition & 0 deletions groclient/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ def mock_get_area_weighted_series(
region_id,
method,
latest_date_only,
metadata
):
return {"2022-07-11": 0.715615, "2022-07-19": 0.733129, "2022-07-27": 0.748822}

Expand Down
2 changes: 2 additions & 0 deletions groclient/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ def get_area_weighted_series(
region_id: Union[int, List[int]],
method: str,
latest_date_only: bool,
metadata: bool,
):
url = f"https://{api_host}/area-weighting"
headers = {"authorization": "Bearer " + access_token}
Expand All @@ -855,6 +856,7 @@ def get_area_weighted_series(
"regionIds": region_id,
"method": method,
"latestDateOnly": latest_date_only,
"metadata": metadata
}
resp = get_data(url, headers, params=params)
return resp.json()
Expand Down
48 changes: 38 additions & 10 deletions groclient/lib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1108,39 +1108,67 @@ def test_get_area_weighting_weight_names(mock_requests_get):

@mock.patch("requests.get")
def test_get_area_weighted_series(mock_requests_get):
api_response = {
"2022-07-11": 0.715615,
"2022-07-19": 0.733129,
"2022-07-27": 0.748822,
api_response_with_metadata = {
"data_points": {
"2022-07-11": 0.715615,
"2022-07-19": 0.733129,
"2022-07-27": 0.748822
},
"series_description": {
"series_name": "dummy_series",
"item_id": 2,
"metric_id": 3,
"source_id": 4,
"frequency_id": 9,
"unit_id": 3,
"plain_words_name": "dummy",
"plain_words_source": "Gro"
},
"weight_description": [
{
"weight_name": "dummy weight",
"metric_id": 23,
"item_id": 41,
"source_id": 21,
"frequency_id": 12,
"unit_id": 31
}
]
}
initialize_requests_mocker_and_get_mock_data(mock_requests_get, api_response)
api_response_without_metadata = api_response_with_metadata["data_points"]
initialize_requests_mocker_and_get_mock_data(mock_requests_get, api_response_without_metadata)

expected_return = {
expected_return_without_metadata = {
"2022-07-11": 0.715615,
"2022-07-19": 0.733129,
"2022-07-27": 0.748822,
}
result = lib.get_area_weighted_series(
result_without_metadata = lib.get_area_weighted_series(
MOCK_TOKEN,
MOCK_HOST,
"NDVI_8day",
["Barley (ha)", "Corn (ha)"],
1215,
"sum",
False,
False
)
assert result == expected_return
assert result_without_metadata == expected_return_without_metadata

initialize_requests_mocker_and_get_mock_data(mock_requests_get, api_response_with_metadata)

result = lib.get_area_weighted_series(
result_with_metadata = lib.get_area_weighted_series(
MOCK_TOKEN,
MOCK_HOST,
"NDVI_8day",
["Barley (ha)", "Corn (ha)"],
[1215],
"sum",
False,
True
)
assert result == expected_return
expected_return_with_metadata = api_response_with_metadata
assert result_with_metadata == expected_return_with_metadata


@mock.patch("requests.post")
Expand Down

0 comments on commit f38af39

Please sign in to comment.