hestia-earth-models 0.74.5__py3-none-any.whl → 0.74.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.

@@ -1,4 +1,4 @@
1
- from hestia_earth.schema import IndicatorMethodTier, TermTermType
1
+ from hestia_earth.schema import TermTermType
2
2
  from hestia_earth.utils.tools import flatten, safe_parse_float
3
3
 
4
4
  from hestia_earth.models.log import logRequirements, logShouldRun, debugValues
@@ -8,6 +8,8 @@ from hestia_earth.models.utils.indicator import _new_indicator
8
8
  from hestia_earth.models.utils.background_emissions import no_gap_filled_background_emissions
9
9
  from hestia_earth.models.utils.term import get_lookup_value
10
10
  from hestia_earth.models.utils.input import unique_background_inputs
11
+ from hestia_earth.models.utils.impact_assessment import get_product
12
+ from hestia_earth.models.utils.crop import get_landCover_term_id
11
13
  from . import MODEL
12
14
 
13
15
  REQUIREMENTS = {
@@ -29,8 +31,7 @@ REQUIREMENTS = {
29
31
  RETURNS = {
30
32
  "Indicator": [{
31
33
  "value": "",
32
- "inputs": "",
33
- "methodTier": "background"
34
+ "inputs": ""
34
35
  }]
35
36
  }
36
37
  LOOKUPS = {
@@ -38,14 +39,12 @@ LOOKUPS = {
38
39
  "organicFertiliser": "backgroundEmissionsResourceUseDefaultValue"
39
40
  }
40
41
  MODEL_KEY = 'default_resourceUse'
41
- TIER = IndicatorMethodTier.BACKGROUND.value
42
42
 
43
43
 
44
- def _indicator(term_id: str, value: float, input: dict):
45
- indicator = _new_indicator(term_id, MODEL)
44
+ def _indicator(term_id: str, value: float, input: dict, land_cover_id: str):
45
+ indicator = _new_indicator(term_id, MODEL, land_cover_id)
46
46
  indicator['value'] = value
47
47
  indicator['inputs'] = [input]
48
- indicator['methodTier'] = TIER
49
48
  return indicator
50
49
 
51
50
 
@@ -53,8 +52,12 @@ def _default_value(input: dict):
53
52
  return safe_parse_float(get_lookup_value(input.get('term', {}), LOOKUPS['organicFertiliser']), default=None)
54
53
 
55
54
 
56
- def _run_input(impact: dict):
55
+ def _run_input(impact: dict, land_cover_id: str):
57
56
  required_resourceUse_term_ids = background_emissions_in_system_boundary(impact, TermTermType.RESOURCEUSE)
57
+ # remove landTransformation as need `previousLandCover` to be valid
58
+ required_resourceUse_term_ids = [
59
+ v for v in required_resourceUse_term_ids if not v.startswith('landTransformation')
60
+ ]
58
61
 
59
62
  def run(input: dict):
60
63
  input_term = input.get('input').get('term')
@@ -62,14 +65,14 @@ def _run_input(impact: dict):
62
65
  value = input.get('default-value-from-lookup')
63
66
 
64
67
  for emission_id in required_resourceUse_term_ids:
65
- logShouldRun(impact, MODEL, term_id, True, methodTier=TIER, model_key=MODEL_KEY, emission_id=emission_id)
68
+ logShouldRun(impact, MODEL, term_id, True, model_key=MODEL_KEY, emission_id=emission_id)
66
69
  debugValues(impact, model=MODEL, term=emission_id,
67
70
  value=value,
68
71
  coefficient=1,
69
72
  input=term_id)
70
73
 
71
74
  return [
72
- _indicator(term_id, value, input_term) for term_id in required_resourceUse_term_ids
75
+ _indicator(term_id, value, input_term, land_cover_id) for term_id in required_resourceUse_term_ids
73
76
  ]
74
77
 
75
78
  return run
@@ -95,18 +98,21 @@ def _should_run(impact: dict):
95
98
  ])
96
99
  ]
97
100
 
98
- should_run = all([bool(valid_inputs)])
101
+ landCover_term_id = get_landCover_term_id(get_product(impact).get("term", {}))
102
+
103
+ should_run = all([bool(valid_inputs), landCover_term_id])
99
104
 
100
105
  for input in inputs:
101
106
  term_id = input.get('input').get('term', {}).get('@id')
102
107
 
103
108
  logRequirements(impact, model=MODEL, term=term_id, model_key=MODEL_KEY,
104
- **_omit(input, ['input', 'input-value']))
105
- logShouldRun(impact, MODEL, term_id, should_run, methodTier=TIER, model_key=MODEL_KEY)
109
+ **_omit(input, ['input', 'input-value']),
110
+ landCover_term_id=landCover_term_id)
111
+ logShouldRun(impact, MODEL, term_id, should_run, model_key=MODEL_KEY)
106
112
 
107
- return should_run, valid_inputs
113
+ return should_run, valid_inputs, landCover_term_id
108
114
 
109
115
 
110
116
  def run(impact: dict):
111
- should_run, grouped_inputs = _should_run(impact)
112
- return flatten(map(_run_input(impact), grouped_inputs)) if should_run else []
117
+ should_run, grouped_inputs, landCover_term_id = _should_run(impact)
118
+ return flatten(map(_run_input(impact, landCover_term_id), grouped_inputs)) if should_run else []
@@ -120,7 +120,7 @@ def _extract_site_data(cycle: dict, land_cover_id: dict):
120
120
  duration=cycle.get("siteDuration"),
121
121
  unused_duration=cycle.get("siteUnusedDuration"),
122
122
  country_id=site.get("country", {}).get("@id"),
123
- land_cover_id=land_cover_id or get_landCover_term_id_from_site_type(site.get("siteType", {}))
123
+ land_cover_id=land_cover_id or get_landCover_term_id_from_site_type(site.get("siteType"))
124
124
  )
125
125
 
126
126
  is_valid = _should_run_site_data(site_data)
@@ -145,7 +145,7 @@ def _extract_other_sites_data(cycle: dict, land_cover_id: dict):
145
145
  duration=duration,
146
146
  unused_duration=unused_duration,
147
147
  country_id=site.get("country", {}).get("@id"),
148
- land_cover_id=land_cover_id or get_landCover_term_id_from_site_type(site.get("siteType", {}))
148
+ land_cover_id=land_cover_id or get_landCover_term_id_from_site_type(site.get("siteType"))
149
149
  ) for (
150
150
  site,
151
151
  area,
@@ -12,7 +12,7 @@ from hestia_earth.models.utils import _omit, group_by
12
12
  from hestia_earth.models.utils.emission import _new_emission
13
13
  from hestia_earth.models.utils.site import valid_site_type
14
14
  from hestia_earth.models.utils.cycle import cycle_end_year
15
- from hestia_earth.models.utils.crop import get_crop_grouping_faostat_production, get_landCover_term_id
15
+ from hestia_earth.models.utils.crop import get_crop_lookup_value, get_landCover_term_id
16
16
  from hestia_earth.models.utils.completeness import _is_term_type_complete
17
17
  from hestia_earth.models.utils.term import get_lookup_value
18
18
  from hestia_earth.models.utils.lookup import get_region_lookup_value
@@ -59,6 +59,7 @@ LOOKUPS = {
59
59
  "crop": [
60
60
  "correspondingSeedTermIds",
61
61
  "cropGroupingFaostatProduction",
62
+ "cropGroupingFaostatProductionProxy",
62
63
  "global_economic_value_share",
63
64
  "landCoverTermId"
64
65
  ],
@@ -154,23 +155,38 @@ def _filter_emissions(cycle: dict):
154
155
  )), emissions_per_group
155
156
 
156
157
 
157
- def _evs(product: dict):
158
- return safe_parse_float(
159
- get_lookup_value(product.get('term', {}), 'global_economic_value_share', model=MODEL, model_key=MODEL_KEY),
160
- default=None
161
- ) or product.get('economicValueShare')
158
+ def _crop_data(product: dict, country_id: str, end_year: int):
159
+ term = product.get('term', {})
160
+ term_id = term.get('@id')
162
161
 
162
+ crop_grouping = get_crop_lookup_value(MODEL, term_id, term_id, 'cropGroupingFaostatProduction')
163
+ crop_grouping_proxy = get_crop_lookup_value(MODEL, term_id, term_id, 'cropGroupingFaostatProductionProxy')
163
164
 
164
- def _faostat_yield(country_id: str, end_year: int, product: dict):
165
- grouping = get_crop_grouping_faostat_production(MODEL, product.get('term', {}))
166
- value = extract_grouped_data_closest_date(get_region_lookup_value(
165
+ faostat_yield = safe_parse_float(extract_grouped_data_closest_date(get_region_lookup_value(
167
166
  'region-crop-cropGroupingFaostatProduction-yield.csv',
168
167
  country_id,
169
- grouping,
168
+ crop_grouping_proxy or crop_grouping,
170
169
  model=MODEL,
171
170
  model_key=MODEL_KEY
172
- ), end_year)
173
- return safe_parse_float(value, default=None)
171
+ ), end_year), default=None)
172
+
173
+ # when using proxy, we cannot use the lookup value as it won't correspond to the primary product
174
+ global_evs = None if crop_grouping_proxy else safe_parse_float(
175
+ get_lookup_value(product.get('term', {}), 'global_economic_value_share', model=MODEL, model_key=MODEL_KEY),
176
+ default=None
177
+ )
178
+ product_evs = product.get('economicValueShare')
179
+
180
+ return {
181
+ 'product-id': term_id,
182
+ 'seed-id': get_lookup_value(term, 'correspondingSeedTermIds', model=MODEL, model_key=MODEL_KEY) or None,
183
+ 'economicValueShare': global_evs or product_evs,
184
+ 'FAOSTAT-yield': faostat_yield,
185
+ 'landCover-id': get_landCover_term_id(term, model=MODEL, model_key=MODEL_KEY)
186
+ } | (
187
+ # skip using product yield if proxy is used
188
+ {} if crop_grouping_proxy else {'product-yield': list_sum(product.get('value'))}
189
+ )
174
190
 
175
191
 
176
192
  def _group_seed_inputs(inputs: list):
@@ -191,18 +207,7 @@ def _should_run(cycle: dict):
191
207
  country_id = cycle.get('site', {}).get('country', {}).get('@id')
192
208
 
193
209
  # only keep the crop products that map to a seed
194
- crop_products = [
195
- {
196
- 'product': product.get('term', {}).get('@id'),
197
- 'seed-id': get_lookup_value(
198
- product.get('term', {}), 'correspondingSeedTermIds', model=MODEL, model_key=MODEL_KEY) or None,
199
- 'economicValueShare': _evs(product),
200
- 'FAOSTAT-yield': _faostat_yield(country_id, end_year, product),
201
- 'product-yield': list_sum(product.get('value')),
202
- 'landCover-id': get_landCover_term_id(product.get('term', {}), model=MODEL, model_key=MODEL_KEY)
203
- }
204
- for product in crop_products
205
- ]
210
+ crop_products = [_crop_data(product, country_id, end_year) for product in crop_products]
206
211
  valid_crop_products = [
207
212
  value for value in crop_products if all([
208
213
  value.get('seed-id'),