hestia-earth-aggregation 0.21.7__tar.gz → 0.21.9__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. {hestia_earth_aggregation-0.21.7/hestia_earth_aggregation.egg-info → hestia_earth_aggregation-0.21.9}/PKG-INFO +1 -1
  2. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/__init__.py +17 -19
  3. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/aggregate_cycles.py +16 -2
  4. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/aggregate_country_nodes.py +13 -3
  5. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/covariance.py +1 -1
  6. hestia_earth_aggregation-0.21.9/hestia_earth/aggregation/version.py +1 -0
  7. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9/hestia_earth_aggregation.egg-info}/PKG-INFO +1 -1
  8. hestia_earth_aggregation-0.21.9/tests/test_aggregation.py +60 -0
  9. hestia_earth_aggregation-0.21.7/hestia_earth/aggregation/version.py +0 -1
  10. hestia_earth_aggregation-0.21.7/tests/test_aggregation.py +0 -34
  11. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/LICENSE +0 -0
  12. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/MANIFEST.in +0 -0
  13. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/README.md +0 -0
  14. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/config/Cycle/processedFood.json +0 -0
  15. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/log.py +0 -0
  16. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/recalculate_cycles.py +0 -0
  17. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/__init__.py +0 -0
  18. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/aggregate_weighted.py +0 -0
  19. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/blank_node.py +0 -0
  20. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/combine.py +0 -0
  21. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/completeness.py +0 -0
  22. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/cycle.py +0 -0
  23. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/distribution.py +0 -0
  24. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/emission.py +0 -0
  25. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/group.py +0 -0
  26. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/input.py +0 -0
  27. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/lookup.py +0 -0
  28. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/management.py +0 -0
  29. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/measurement.py +0 -0
  30. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/practice.py +0 -0
  31. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/product.py +0 -0
  32. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/property.py +0 -0
  33. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/quality_score.py +0 -0
  34. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/queries.py +0 -0
  35. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/site.py +0 -0
  36. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/source.py +0 -0
  37. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/term.py +0 -0
  38. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth/aggregation/utils/weights.py +0 -0
  39. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth_aggregation.egg-info/SOURCES.txt +0 -0
  40. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth_aggregation.egg-info/dependency_links.txt +0 -0
  41. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth_aggregation.egg-info/requires.txt +0 -0
  42. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/hestia_earth_aggregation.egg-info/top_level.txt +0 -0
  43. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/setup.cfg +0 -0
  44. {hestia_earth_aggregation-0.21.7 → hestia_earth_aggregation-0.21.9}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hestia_earth_aggregation
3
- Version: 0.21.7
3
+ Version: 0.21.9
4
4
  Summary: HESTIA's aggregation engine.
5
5
  Home-page: https://gitlab.com/hestia-earth/hestia-aggregation-engine
6
6
  Author: HESTIA Team
@@ -16,20 +16,10 @@ def _mock_nb_distribution(include_distribution: bool):
16
16
  not include_distribution and logger.warning("Not generating distribution.")
17
17
 
18
18
 
19
- def _get_aggregate_function(country: dict, is_global: bool | None):
20
- if is_global is True and not is_global_country(country):
21
- raise ValueError(
22
- "country must be a region (e.g., World, Continent) when using global aggregation"
23
- )
24
-
19
+ def _get_aggregate_function(country: dict, filter_by_country: bool = True):
25
20
  return (
26
21
  aggregate_global
27
- if is_global
28
- or (
29
- # automatic detection
30
- is_global is None
31
- and is_global_country(country)
32
- )
22
+ if is_global_country(country) and filter_by_country
33
23
  else aggregate_country
34
24
  )
35
25
 
@@ -42,7 +32,7 @@ def aggregate(
42
32
  source: dict = None,
43
33
  include_distribution: bool = True,
44
34
  filter_by_country: bool = True,
45
- is_global: bool | None = None,
35
+ include_covariance: bool = True,
46
36
  ):
47
37
  """
48
38
  Aggregates data from HESTIA.
@@ -64,10 +54,9 @@ def aggregate(
64
54
  Include a `distribution` for each aggregated data point. Included by default.
65
55
  filter_by_country : bool
66
56
  If set to `False`, Cycles from all countries will be used.
67
- Only used for country-level aggregations.
68
- is_global : bool | None
69
- Run global aggregation, which will find aggregated Country-level data, and weight the result by country.
70
- If the parameter is not set, it will be inferred from the `country`.
57
+ When used with `Worl` or `Continent` aggregation, setting `False` will also use non-aggregated Cycles.
58
+ include_covariance : bool
59
+ Include the `covarianceMatrix`. Included by default.
71
60
 
72
61
  Returns
73
62
  -------
@@ -76,7 +65,9 @@ def aggregate(
76
65
  """
77
66
  # mock nb distributions depending on the parameter
78
67
  _mock_nb_distribution(include_distribution)
79
- aggregate_function = _get_aggregate_function(country=country, is_global=is_global)
68
+ aggregate_function = _get_aggregate_function(
69
+ country=country, filter_by_country=filter_by_country
70
+ )
80
71
 
81
72
  now = current_time_ms()
82
73
  logger.info(
@@ -88,7 +79,14 @@ def aggregate(
88
79
  end_year,
89
80
  )
90
81
  aggregations, countries = aggregate_function(
91
- country, product, source, start_year, end_year, filter_by_country
82
+ country,
83
+ product,
84
+ source,
85
+ start_year,
86
+ end_year,
87
+ include_distribution=include_distribution,
88
+ filter_by_country=filter_by_country,
89
+ include_covariance=include_covariance,
92
90
  )
93
91
  logger.info("time=%s, unit=ms", current_time_ms() - now)
94
92
  aggregations = (
@@ -91,6 +91,9 @@ def aggregate_country(
91
91
  start_year: int,
92
92
  end_year: int,
93
93
  filter_by_country: bool = True,
94
+ include_covariance: bool = True,
95
+ *args,
96
+ **kwargs,
94
97
  ) -> Tuple[list, list]:
95
98
  """
96
99
  Create 1 to many country-level aggregations.
@@ -130,7 +133,11 @@ def aggregate_country(
130
133
 
131
134
  # combine cycles into a "master" cycle with multiple values
132
135
  cycles_aggregated = aggregate_cycles(
133
- cycles=cycles, product=product, start_year=start_year, end_year=end_year
136
+ cycles=cycles,
137
+ product=product,
138
+ start_year=start_year,
139
+ end_year=end_year,
140
+ include_covariance=include_covariance,
134
141
  )
135
142
  if not cycles_aggregated:
136
143
  logger.info("2 - No aggregated cycles.")
@@ -181,7 +188,14 @@ def aggregate_country(
181
188
  log_memory_usage()
182
189
 
183
190
  country_cycle = (
184
- (country_cycle | generate_covariance_country(weights=weights))
191
+ (
192
+ country_cycle
193
+ | (
194
+ generate_covariance_country(weights=weights)
195
+ if include_covariance
196
+ else {}
197
+ )
198
+ )
185
199
  if country_cycle
186
200
  else None
187
201
  )
@@ -720,7 +720,13 @@ def _aggregate_sites(site_ids: List[str], start_year: int, end_year: int) -> Sit
720
720
  return site
721
721
 
722
722
 
723
- def aggregate_cycles(cycles: list, product: dict, start_year: int, end_year: int):
723
+ def aggregate_cycles(
724
+ cycles: list,
725
+ product: dict,
726
+ start_year: int,
727
+ end_year: int,
728
+ include_covariance: bool = True,
729
+ ):
724
730
  batches = range(0, len(cycles), _BATCH_SIZE)
725
731
  logger.info(f"Processing {len(cycles)} cycles in {len(batches)} batches.")
726
732
 
@@ -742,13 +748,17 @@ def aggregate_cycles(cycles: list, product: dict, start_year: int, end_year: int
742
748
  logger.info("Finished processing batch, combining all cycles...")
743
749
  log_memory_usage()
744
750
  # generate covariance on sub cycles
745
- covariances = {key: generate_covariance_cycles(key) for key in all_cycles.keys()}
751
+ covariances = (
752
+ {key: generate_covariance_cycles(key) for key in all_cycles.keys()}
753
+ if include_covariance
754
+ else {}
755
+ )
746
756
  all_cycles = {
747
757
  key: _combine_formatted_cycles(cycles=values)
748
758
  for key, values in all_cycles.items()
749
759
  }
750
760
  # add covariances to cycles once they have been combined
751
- cycles = [cycle | covariances[key] for key, cycle in all_cycles.items()]
761
+ cycles = [cycle | covariances.get(key, {}) for key, cycle in all_cycles.items()]
752
762
  # discard memory
753
763
  all_cycles = {}
754
764
 
@@ -136,7 +136,7 @@ def add_covariance_cycles(cycles: list, suffix: str):
136
136
 
137
137
 
138
138
  def _format_covariance_value(value):
139
- return None if pd.isna(value) else value
139
+ return None if any([pd.isna(value), value == -np.inf, value == np.inf]) else value
140
140
 
141
141
 
142
142
  def _format_covariance_matrix(values):
@@ -0,0 +1 @@
1
+ VERSION = "0.21.9"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hestia_earth_aggregation
3
- Version: 0.21.7
3
+ Version: 0.21.9
4
4
  Summary: HESTIA's aggregation engine.
5
5
  Home-page: https://gitlab.com/hestia-earth/hestia-aggregation-engine
6
6
  Author: HESTIA Team
@@ -0,0 +1,60 @@
1
+ import pytest
2
+ from unittest.mock import Mock, patch
3
+
4
+ from hestia_earth.aggregation.utils.term import DEFAULT_COUNTRY_ID
5
+ from hestia_earth.aggregation import aggregate
6
+
7
+ class_path = "hestia_earth.aggregation"
8
+
9
+ country_id = "GADM-FRA"
10
+ product = {}
11
+ start_year = 2000
12
+ end_year = 2009
13
+
14
+
15
+ @pytest.mark.parametrize(
16
+ "country,filter_by_country,called",
17
+ [
18
+ ({"@id": country_id}, True, True),
19
+ ({"@id": country_id}, False, True),
20
+ ({"@id": DEFAULT_COUNTRY_ID}, True, False),
21
+ ({"@id": DEFAULT_COUNTRY_ID}, False, True),
22
+ ],
23
+ )
24
+ @patch(f"{class_path}.aggregate_global", return_value=([], []))
25
+ @patch(f"{class_path}.aggregate_country", return_value=([], []))
26
+ def test_aggregate_country(
27
+ mock_aggregate: Mock,
28
+ mock_1: Mock,
29
+ country: dict,
30
+ filter_by_country: bool,
31
+ called: bool,
32
+ ):
33
+ aggregate(
34
+ country, product, start_year, end_year, filter_by_country=filter_by_country
35
+ )
36
+ assert mock_aggregate.called == called
37
+
38
+
39
+ @pytest.mark.parametrize(
40
+ "country,filter_by_country,called",
41
+ [
42
+ ({"@id": country_id}, True, False),
43
+ ({"@id": country_id}, False, False),
44
+ ({"@id": DEFAULT_COUNTRY_ID}, True, True),
45
+ ({"@id": DEFAULT_COUNTRY_ID}, False, False),
46
+ ],
47
+ )
48
+ @patch(f"{class_path}.aggregate_country", return_value=([], []))
49
+ @patch(f"{class_path}.aggregate_global", return_value=([], []))
50
+ def test_aggregate_global(
51
+ mock_aggregate: Mock,
52
+ mock_1: Mock,
53
+ country: dict,
54
+ filter_by_country: bool,
55
+ called: bool,
56
+ ):
57
+ aggregate(
58
+ country, product, start_year, end_year, filter_by_country=filter_by_country
59
+ )
60
+ assert mock_aggregate.called == called
@@ -1 +0,0 @@
1
- VERSION = "0.21.7"
@@ -1,34 +0,0 @@
1
- from unittest.mock import Mock, patch
2
-
3
- from hestia_earth.aggregation import aggregate
4
-
5
- class_path = "hestia_earth.aggregation"
6
-
7
-
8
- @patch(f"{class_path}.aggregate_global", return_value=([], []))
9
- @patch(f"{class_path}.aggregate_country", return_value=([], []))
10
- def test_aggregate(mock_aggregate_country: Mock, mock_aggregate_global: Mock):
11
- product = {}
12
- start_year = 2000
13
- end_year = 2009
14
-
15
- country = {"@id": "GADM-FRA"}
16
- aggregate(country, product, start_year, end_year)
17
- mock_aggregate_country.assert_called_once()
18
- mock_aggregate_global.assert_not_called()
19
-
20
- mock_aggregate_global.reset_mock()
21
- mock_aggregate_country.reset_mock()
22
-
23
- country = {"@id": "region-world"}
24
- aggregate(country, product, start_year, end_year, is_global=False)
25
- mock_aggregate_country.assert_called_once()
26
- mock_aggregate_global.assert_not_called()
27
-
28
- mock_aggregate_global.reset_mock()
29
- mock_aggregate_country.reset_mock()
30
-
31
- country = {"@id": "region-world"}
32
- aggregate(country, product, start_year, end_year)
33
- mock_aggregate_country.assert_not_called()
34
- mock_aggregate_global.assert_called_once()