hestia-earth-models 0.61.5__py3-none-any.whl → 0.61.7__py3-none-any.whl

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.

Potentially problematic release.


This version of hestia-earth-models might be problematic. Click here for more details.

Files changed (26) hide show
  1. hestia_earth/models/cycle/input/hestiaAggregatedData.py +1 -1
  2. hestia_earth/models/ipcc2019/animal/weightAtMaturity.py +5 -1
  3. hestia_earth/models/ipcc2019/co2ToAirSoilOrganicCarbonStockChangeManagementChange.py +1 -1
  4. hestia_earth/models/ipcc2019/organicCarbonPerHa.py +12 -7
  5. hestia_earth/models/mocking/__init__.py +8 -11
  6. hestia_earth/models/mocking/mock_search.py +13 -14
  7. hestia_earth/models/mocking/search-results.json +293 -193
  8. hestia_earth/models/preload_requests.py +32 -0
  9. hestia_earth/models/schererPfister2015/pToDrainageWaterSoilFlux.py +1 -1
  10. hestia_earth/models/schererPfister2015/pToGroundwaterSoilFlux.py +1 -1
  11. hestia_earth/models/site/management.py +1 -1
  12. hestia_earth/models/site/soilMeasurement.py +11 -29
  13. hestia_earth/models/utils/aquacultureManagement.py +2 -2
  14. hestia_earth/models/utils/crop.py +24 -1
  15. hestia_earth/models/utils/cycle.py +0 -23
  16. hestia_earth/models/utils/lookup.py +6 -3
  17. hestia_earth/models/utils/site.py +29 -16
  18. hestia_earth/models/version.py +1 -1
  19. {hestia_earth_models-0.61.5.dist-info → hestia_earth_models-0.61.7.dist-info}/METADATA +8 -1
  20. {hestia_earth_models-0.61.5.dist-info → hestia_earth_models-0.61.7.dist-info}/RECORD +26 -25
  21. tests/models/site/test_soilMeasurement.py +2 -1
  22. tests/models/utils/{test_cycle.py → test_crop.py} +2 -2
  23. tests/models/utils/test_site.py +1 -1
  24. {hestia_earth_models-0.61.5.dist-info → hestia_earth_models-0.61.7.dist-info}/LICENSE +0 -0
  25. {hestia_earth_models-0.61.5.dist-info → hestia_earth_models-0.61.7.dist-info}/WHEEL +0 -0
  26. {hestia_earth_models-0.61.5.dist-info → hestia_earth_models-0.61.7.dist-info}/top_level.txt +0 -0
@@ -10,7 +10,7 @@ from hestia_earth.schema import TermTermType
10
10
  from hestia_earth.utils.model import find_primary_product, find_term_match, linked_node
11
11
 
12
12
  from hestia_earth.models.log import debugValues, logRequirements, logShouldRun
13
- from hestia_earth.models.utils.cycle import valid_site_type
13
+ from hestia_earth.models.utils.crop import valid_site_type
14
14
  from hestia_earth.models.utils.term import get_generic_crop
15
15
  from hestia_earth.models.utils.aggregated import (
16
16
  should_link_input_to_impact, link_inputs_to_impact, find_closest_impact, aggregated_end_date
@@ -91,7 +91,11 @@ def _should_run(cycle: dict):
91
91
  weightAtMaturity=lookup_value,
92
92
  liveweightPerHead=liveweightPerHead_value)
93
93
 
94
- should_run = all([country_id, value.get('value') is not None, lookup_value >= liveweightPerHead_value])
94
+ should_run = all([
95
+ country_id,
96
+ lookup_value is not None,
97
+ lookup_value is None or liveweightPerHead_value is None or lookup_value >= liveweightPerHead_value
98
+ ])
95
99
  logShouldRun(cycle, MODEL, term_id, should_run)
96
100
 
97
101
  return should_run
@@ -694,7 +694,7 @@ def _should_run(cycle: dict) -> tuple:
694
694
  """
695
695
  cycle_id = cycle.get("@id")
696
696
  site = get_site(cycle)
697
- cycles = related_cycles(site.get("@id"))
697
+ cycles = related_cycles(site)
698
698
 
699
699
  grouped_soc_stocks = _get_grouped_soc_stocks(site)
700
700
  grouped_soc_stock_changes = _calc_grouped_soc_stock_changes(grouped_soc_stocks)
@@ -1891,11 +1891,12 @@ def _get_carbon_sources_from_cycles(cycles: dict) -> list[CarbonSource]:
1891
1891
  inputs_and_products = non_empty_list(flatten(
1892
1892
  [cycle.get("inputs", []) + cycle.get("products", []) for cycle in cycles]
1893
1893
  ))
1894
+ crop_residue_terms = get_crop_residue_incorporated_or_left_on_field_terms()
1894
1895
 
1895
1896
  return non_empty_list([
1896
1897
  _iterate_carbon_source(node) for node in inputs_and_products
1897
1898
  if any([
1898
- node.get("term", {}).get("@id") in get_crop_residue_incorporated_or_left_on_field_terms(),
1899
+ node.get("term", {}).get("@id") in crop_residue_terms,
1899
1900
  node.get("term", {}).get("termType") in CARBON_SOURCE_TERM_TYPES
1900
1901
  ])
1901
1902
  ])
@@ -3236,9 +3237,10 @@ def _get_carbon_input_kwargs(
3236
3237
  cumulative_threshold=MIN_AREA_THRESHOLD
3237
3238
  )
3238
3239
 
3240
+ cover_crop_property_terms = get_cover_crop_property_terms()
3239
3241
  has_cover_crop = cumulative_nodes_match(
3240
3242
  lambda node: any(
3241
- get_node_property(node, term_id, False).get("value", False) for term_id in get_cover_crop_property_terms()
3243
+ get_node_property(node, term_id, False).get("value", False) for term_id in cover_crop_property_terms
3242
3244
  ),
3243
3245
  land_cover_nodes,
3244
3246
  cumulative_threshold=MIN_AREA_THRESHOLD
@@ -3515,7 +3517,7 @@ def _should_run(site: dict) -> tuple[bool, dict]:
3515
3517
  site_type = site.get("siteType", "")
3516
3518
  management_nodes = site.get("management", [])
3517
3519
  measurement_nodes = site.get("measurements", [])
3518
- cycles = related_cycles(site.get("@id"))
3520
+ cycles = related_cycles(site)
3519
3521
 
3520
3522
  has_management = len(management_nodes) > 0
3521
3523
  has_measurements = len(measurement_nodes) > 0
@@ -3768,14 +3770,16 @@ def _get_grouped_climate_measurements(grouped_measurements: dict) -> dict:
3768
3770
 
3769
3771
 
3770
3772
  def _get_grouped_irrigated_monthly(grouped_cycles: dict) -> dict:
3773
+ irrigated_terms = get_irrigated_terms()
3774
+
3771
3775
  return {
3772
3776
  year: {
3773
- _InventoryKey.IRRIGATED_MONTHLY: _get_irrigated_monthly(year, cycles)
3777
+ _InventoryKey.IRRIGATED_MONTHLY: _get_irrigated_monthly(year, cycles, irrigated_terms)
3774
3778
  } for year, cycles in grouped_cycles.items()
3775
3779
  }
3776
3780
 
3777
3781
 
3778
- def _get_irrigated_monthly(year: int, cycles: list[dict]) -> list[bool]:
3782
+ def _get_irrigated_monthly(year: int, cycles: list[dict], irrigated_terms: list[str]) -> list[bool]:
3779
3783
  # Get practice nodes and add "startDate" and "endDate" from cycle if missing.
3780
3784
  irrigation_nodes = non_empty_list(flatten([
3781
3785
  [
@@ -3793,7 +3797,7 @@ def _get_irrigated_monthly(year: int, cycles: list[dict]) -> list[bool]:
3793
3797
  return [
3794
3798
  cumulative_nodes_term_match(
3795
3799
  grouped_nodes.get(year, {}).get(month, []),
3796
- target_term_ids=get_irrigated_terms(),
3800
+ target_term_ids=irrigated_terms,
3797
3801
  cumulative_threshold=MIN_AREA_THRESHOLD
3798
3802
  ) for month in range(1, 13)
3799
3803
  ]
@@ -3862,12 +3866,13 @@ def _check_is_paddy_rice(cycles: list[dict]) -> bool:
3862
3866
  default_node_value=MIN_YIELD_THRESHOLD
3863
3867
  ) for cycle in cycles)
3864
3868
 
3869
+ reice_terms = get_upland_rice_crop_terms() + get_upland_rice_land_cover_terms()
3865
3870
  has_upland_rice_products = any(cumulative_nodes_term_match(
3866
3871
  filter_list_term_type(
3867
3872
  cycle.get("products", []) + cycle.get("practices", []),
3868
3873
  [TermTermType.CROP, TermTermType.FORAGE, TermTermType.LANDCOVER]
3869
3874
  ),
3870
- target_term_ids=get_upland_rice_crop_terms() + get_upland_rice_land_cover_terms(),
3875
+ target_term_ids=reice_terms,
3871
3876
  cumulative_threshold=MIN_YIELD_THRESHOLD,
3872
3877
  default_node_value=MIN_YIELD_THRESHOLD
3873
3878
  ) for cycle in cycles)
@@ -1,23 +1,20 @@
1
+ import os
1
2
  from hestia_earth.models.utils import measurement, site
2
3
 
3
4
  from .mock_search import mock as mock_search
4
5
 
6
+ CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
7
+ RESULTS_PATH = os.path.join(CURRENT_DIR, 'search-results.json')
5
8
 
6
- def enable_mock(node: dict = None):
7
- """
8
- Mock calls to Hestia API using pre-loaded search results.
9
9
 
10
- Parameters
11
- ----------
12
- node : dict
13
- Optional - The node used to run calculations. This is especially useful when running calculations on a Site.
14
- """
10
+ def enable_mock(filepath: str, node: dict = None):
15
11
  # apply mocks on search results
16
- mock_search()
17
- # skip fetch bibliography data
18
- measurement.include_source = lambda v, *args: v
12
+ mock_search(filepath)
19
13
 
20
14
  if node is not None:
15
+ # skip fetch bibliography data
16
+ measurement.include_source = lambda v, *args: v
17
+
21
18
  # mock related cycles to return the current node
22
19
  fake_node = {'@id': 'fake-cycle', **node}
23
20
  site.download_hestia = lambda *args: fake_node
@@ -1,13 +1,9 @@
1
- import os
2
1
  from inspect import getmembers, isfunction
3
2
  import json
4
3
  from hestia_earth.utils.tools import flatten
5
4
 
6
- from hestia_earth.models.log import logger
7
5
  from hestia_earth.models.utils import term
8
6
 
9
- CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
10
- RESULTS_PATH = os.path.join(CURRENT_DIR, 'search-results.json')
11
7
  IGNORE_FUNC = ['get_lookup_value', 'get_table_value']
12
8
 
13
9
 
@@ -49,24 +45,27 @@ def create_search_results():
49
45
  return list(map(_create_search_result, funcs))
50
46
 
51
47
 
52
- def _load_results():
53
- with open(RESULTS_PATH) as f:
48
+ def _load_results(filepath: str):
49
+ with open(filepath) as f:
54
50
  return json.load(f)
55
51
 
56
52
 
57
- def _find_search_result(query: dict):
58
- search_results = _load_results()
53
+ def _find_search_result(filepath: str, query: dict):
54
+ search_results = _load_results(filepath)
59
55
  res = next((n for n in search_results if n['query'] == query), {})
60
- logger.debug('mocking search result: %s', res)
61
56
  return res.get('results', [])
62
57
 
63
58
 
64
- def _fake_search(query: dict, *_a, **_b): return _find_search_result(query)
59
+ def _fake_search(filepath: str):
60
+ def mock(query: dict, *_a, **_b): return _find_search_result(filepath, query)
61
+ return mock
65
62
 
66
63
 
67
- def _fake_find_node(_n, query: dict, *_a, **_b): return _find_search_result(query)
64
+ def _fake_find_node(filepath: str):
65
+ def mock(_n, query: dict, *_a, **_b): return _find_search_result(filepath, query)
66
+ return mock
68
67
 
69
68
 
70
- def mock():
71
- term.search = _fake_search
72
- term.find_node = _fake_find_node
69
+ def mock(filepath: str):
70
+ term.search = _fake_search(filepath)
71
+ term.find_node = _fake_find_node(filepath)