hestia-earth-models 0.47.5__py3-none-any.whl → 0.49.0__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 (40) hide show
  1. hestia_earth/models/agribalyse2016/fuelElectricity.py +33 -18
  2. hestia_earth/models/agribalyse2016/machineryInfrastructureDepreciatedAmountPerCycle.py +1 -1
  3. hestia_earth/models/blonkConsultants2016/{co2ToAirSoilCarbonStockChangeManagementChange.py → co2ToAirAboveGroundBiomassStockChangeLandUseChange.py} +1 -1
  4. hestia_earth/models/deRuijterEtAl2010/nh3ToAirCropResidueDecomposition.py +1 -1
  5. hestia_earth/models/faostat2018/product/price.py +1 -1
  6. hestia_earth/models/faostat2018/seed.py +1 -1
  7. hestia_earth/models/haversineFormula/transport/distance.py +27 -8
  8. hestia_earth/models/impact_assessment/freshwaterWithdrawalsDuringCycle.py +5 -1
  9. hestia_earth/models/impact_assessment/product/economicValueShare.py +2 -1
  10. hestia_earth/models/ipcc2006/aboveGroundCropResidueRemoved.py +1 -1
  11. hestia_earth/models/ipcc2006/aboveGroundCropResidueTotal.py +1 -1
  12. hestia_earth/models/ipcc2006/belowGroundCropResidue.py +2 -2
  13. hestia_earth/models/ipcc2006/n2OToAirCropResidueDecompositionIndirect.py +1 -1
  14. hestia_earth/models/ipcc2006/n2OToAirExcretaDirect.py +1 -1
  15. hestia_earth/models/ipcc2006/n2OToAirExcretaIndirect.py +1 -1
  16. hestia_earth/models/ipcc2006/n2OToAirInorganicFertiliserDirect.py +1 -1
  17. hestia_earth/models/ipcc2006/n2OToAirInorganicFertiliserIndirect.py +1 -1
  18. hestia_earth/models/ipcc2006/n2OToAirOrganicFertiliserDirect.py +1 -1
  19. hestia_earth/models/ipcc2006/n2OToAirOrganicFertiliserIndirect.py +1 -1
  20. hestia_earth/models/ipcc2019/aboveGroundCropResidueTotal.py +1 -1
  21. hestia_earth/models/ipcc2019/belowGroundCropResidue.py +1 -1
  22. hestia_earth/models/ipcc2019/n2OToAirCropResidueDecompositionDirect.py +1 -1
  23. hestia_earth/models/koble2014/aboveGroundCropResidue.py +1 -1
  24. hestia_earth/models/mocking/search-results.json +240 -192
  25. hestia_earth/models/pooreNemecek2018/aboveGroundCropResidueTotal.py +1 -1
  26. hestia_earth/models/pooreNemecek2018/belowGroundCropResidue.py +1 -1
  27. hestia_earth/models/pooreNemecek2018/saplings.py +1 -1
  28. hestia_earth/models/site/cationExchangeCapacityPerKgSoil.py +11 -6
  29. hestia_earth/models/site/organicCarbonPerHa.py +32 -15
  30. hestia_earth/models/utils/pesticideAI.py +1 -1
  31. hestia_earth/models/version.py +1 -1
  32. {hestia_earth_models-0.47.5.dist-info → hestia_earth_models-0.49.0.dist-info}/METADATA +7 -7
  33. {hestia_earth_models-0.47.5.dist-info → hestia_earth_models-0.49.0.dist-info}/RECORD +40 -40
  34. {hestia_earth_models-0.47.5.dist-info → hestia_earth_models-0.49.0.dist-info}/WHEEL +1 -1
  35. tests/models/blonkConsultants2016/{test_co2ToAirSoilCarbonStockChangeManagementChange.py → test_co2ToAirAboveGroundBiomassStockChangeLandUseChange.py} +1 -1
  36. tests/models/faostat2018/product/test_price.py +7 -1
  37. tests/models/site/test_cationExchangeCapacityPerKgSoil.py +6 -4
  38. tests/models/site/test_organicCarbonPerHa.py +6 -4
  39. {hestia_earth_models-0.47.5.dist-info → hestia_earth_models-0.49.0.dist-info}/LICENSE +0 -0
  40. {hestia_earth_models-0.47.5.dist-info → hestia_earth_models-0.49.0.dist-info}/top_level.txt +0 -0
@@ -3,6 +3,7 @@ Fuel and Electricity
3
3
 
4
4
  This model calculates fuel and electricity data from the number of hours each machine is operated for using.
5
5
  """
6
+ from functools import reduce
6
7
  from hestia_earth.schema import InputStatsDefinition, TermTermType
7
8
  from hestia_earth.utils.model import filter_list_term_type
8
9
  from hestia_earth.utils.tools import flatten, list_sum, non_empty_list
@@ -44,34 +45,49 @@ def _input(term_id: str, value: float, operation: dict):
44
45
  return input
45
46
 
46
47
 
48
+ def _operation_input(operation: dict):
49
+ input = operation.get('input', {})
50
+ return _input(input.get('id'), input.get('value') * operation.get('value'), operation.get('term', {}))
51
+
52
+
47
53
  def _run_operation(cycle: dict):
48
- def exec(operation: dict):
49
- input = operation.get('input', {})
50
- input_term = input.get('term', {})
51
- input_term_id = input_term.get('@id')
52
- coefficient = input.get('value')
53
- value = list_sum(operation.get('value', []))
54
+ def exec(operations: list):
55
+ input_term_id = operations[0].get('input').get('id')
56
+ values_logs = ';'.join([
57
+ f"id:{p.get('term').get('@id')}_value:{p.get('value')}_coefficient:{p.get('input').get('value')}"
58
+ for p in operations
59
+ ])
54
60
 
55
61
  debugValues(cycle, model=MODEL, term=input_term_id,
56
- operation=operation.get('@id'),
57
- value=value,
58
- coefficient=coefficient)
59
- logShouldRun(cycle, MODEL, input_term_id, True, model_key=MODEL_KEY, operation=operation.get('@id'))
62
+ values=values_logs)
63
+
64
+ logShouldRun(cycle, MODEL, input_term_id, True, model_key=MODEL_KEY)
60
65
 
61
- return _input(input_term.get('@id'), coefficient * value, operation.get('term', {}))
66
+ return list(map(_operation_input, operations))
62
67
  return exec
63
68
 
64
69
 
70
+ def _group_operations(operations: list):
71
+ def grouper(group: dict, operation: dict):
72
+ input_term_id = operation.get('input').get('id')
73
+ group[input_term_id] = group.get(input_term_id, [])
74
+ group[input_term_id].append(operation)
75
+ return group
76
+
77
+ return reduce(grouper, operations, {})
78
+
79
+
65
80
  def _should_run_operation(cycle: dict):
66
81
  def exec(practice: dict):
67
82
  term = practice.get('term', {})
68
83
  term_id = term.get('@id')
69
- value = list_sum(practice.get('value', []))
84
+ values = practice.get('value', [])
85
+ value = list_sum(values) if all([not isinstance(v, str) for v in values]) else 0 # str allowed for Practice
70
86
  has_value = value > 0
71
87
 
72
88
  coeffs = get_lookup_value(term, LOOKUPS['operation'], model=MODEL, model_key=MODEL_KEY)
73
89
  values = non_empty_list(coeffs.split(';')) if coeffs else []
74
- inputs = [{'term': {'@id': c.split(':')[0]}, 'value': float(c.split(':')[1])} for c in values]
90
+ inputs = [{'id': c.split(':')[0], 'value': float(c.split(':')[1])} for c in values]
75
91
  has_lookup_value = len(inputs) > 0
76
92
 
77
93
  logRequirements(cycle, model=MODEL, term=term_id, model_key=MODEL_KEY,
@@ -80,10 +96,7 @@ def _should_run_operation(cycle: dict):
80
96
 
81
97
  should_run = all([has_value, has_lookup_value])
82
98
  logShouldRun(cycle, MODEL, term_id, should_run, model_key=MODEL_KEY)
83
- return [{
84
- **practice,
85
- 'input': input
86
- } for input in inputs] if should_run else []
99
+ return [{'term': term, 'value': value, 'input': input} for input in inputs] if should_run else []
87
100
  return exec
88
101
 
89
102
 
@@ -105,4 +118,6 @@ def _should_run(cycle: dict):
105
118
 
106
119
  def run(cycle: dict):
107
120
  should_run, operations = _should_run(cycle)
108
- return list(map(_run_operation(cycle), operations)) if should_run else []
121
+ # group operations by input to show logs as table
122
+ grouped_operations = _group_operations(operations)
123
+ return flatten(map(_run_operation(cycle), grouped_operations.values())) if should_run else []
@@ -79,7 +79,7 @@ def _should_run(cycle: dict):
79
79
 
80
80
  logRequirements(cycle, model=MODEL, term=TERM_ID,
81
81
  site_type_valid=site_type_valid,
82
- term_type_incomplete=term_type_incomplete)
82
+ term_type_material_incomplete=term_type_incomplete)
83
83
 
84
84
  should_run = all([site_type_valid, term_type_incomplete])
85
85
  logShouldRun(cycle, MODEL, TERM_ID, should_run)
@@ -49,7 +49,7 @@ RETURNS = {
49
49
  "statsDefinition": "modelled"
50
50
  }]
51
51
  }
52
- TERM_ID = 'co2ToAirSoilCarbonStockChangeManagementChange'
52
+ TERM_ID = 'co2ToAirAboveGroundBiomassStockChangeLandUseChange'
53
53
  TIER = EmissionMethodTier.TIER_1.value
54
54
 
55
55
 
@@ -77,7 +77,7 @@ def _should_run(cycle: dict):
77
77
  ])
78
78
 
79
79
  logRequirements(cycle, model=MODEL, term=TERM_ID,
80
- term_type_complete=term_type_complete,
80
+ term_type_cropResidue_complete=term_type_complete,
81
81
  left_on_field_residue=left_on_field_residue,
82
82
  total_nitrogenContent=total_nitrogenContent)
83
83
 
@@ -67,7 +67,7 @@ def _lookup_data(
67
67
  data = get_table_value(lookup, 'termid', country_id, column_name(grouping))
68
68
  debugMissingLookup(lookup_name, 'termid', country_id, grouping, data,
69
69
  model=MODEL, term=term_id, key=MODEL_KEY)
70
- price = extract_grouped_data(data, str(year)) if year else extract_grouped_data(data, 'Average_price_per_tonne')
70
+ price = extract_grouped_data(data, str(year)) or extract_grouped_data(data, 'Average_price_per_tonne')
71
71
  return safe_parse_float(price, None)
72
72
 
73
73
 
@@ -69,7 +69,7 @@ def _should_run(cycle: dict):
69
69
  term_type_incomplete = _is_term_type_incomplete(cycle, TERM_ID)
70
70
 
71
71
  logRequirements(cycle, model=MODEL, term=TERM_ID,
72
- term_type_incomplete=term_type_incomplete,
72
+ term_type_other_incomplete=term_type_incomplete,
73
73
  has_products=has_products,
74
74
  product_ids=product_ids)
75
75
 
@@ -74,18 +74,37 @@ def _run_input(cycle: dict, site_country: dict):
74
74
  return exec
75
75
 
76
76
 
77
- def _should_run_input(site_country: str):
77
+ def _should_run_input(site_country_id: str):
78
78
  def exec(input: dict):
79
- input_country = input.get('country', {}).get('@id')
79
+ input_country = input.get('country', {}) or {}
80
+ input_country_id = input_country.get('@id')
81
+ input_country = download_hestia(input_country_id)
80
82
  has_transports = len(input.get('transport', [])) > 0
81
- should_run = all([has_transports, input_country, input_country != site_country])
83
+ should_run = input_country and all([
84
+ input_country.get('latitude'), input_country.get('latitude'),
85
+ has_transports, input_country_id != site_country_id
86
+ ])
82
87
  return should_run
83
88
  return exec
84
89
 
85
90
 
86
- def run(cycle: dict):
87
- site_country = cycle.get('site', {}).get('country', {}).get('@id')
88
- inputs = list(filter(_should_run_input(site_country), cycle.get('inputs', [])))
91
+ def _should_run(cycle: dict):
92
+ country = cycle.get('site', {}).get('country', {})
93
+ country_id = cycle.get('site', {}).get('country', {}).get('@id')
94
+ inputs = list(filter(_should_run_input(country_id), cycle.get('inputs', [])))
89
95
  # download full term to get coordinates only if there is anything to run
90
- site_country = download_hestia(site_country) if len(inputs) > 0 else site_country
91
- return non_empty_list(map(_run_input(cycle, site_country), inputs))
96
+ country = download_hestia(country_id) if len(inputs) > 0 else {}
97
+
98
+ # can only run if the site country has centroid coordinates
99
+ logRequirements(cycle, model=MODEL, term=None, key=MODEL_KEY,
100
+ latitude=country.get('latitude'),
101
+ longitude=country.get('latitude'),
102
+ has_inputs_transport=len(inputs) > 0)
103
+ should_run = all([country.get('latitude'), country.get('latitude'), len(inputs) > 0])
104
+ logShouldRun(cycle, MODEL, None, should_run, key=MODEL_KEY)
105
+ return should_run, country, inputs
106
+
107
+
108
+ def run(cycle: dict):
109
+ should_run, country, inputs = _should_run(cycle)
110
+ return non_empty_list(map(_run_input(cycle, country), inputs)) if should_run else []
@@ -36,7 +36,11 @@ RETURNS = {
36
36
  }
37
37
  LOOKUPS = {
38
38
  "crop": "cropGroupingFAO",
39
- "region": ["Conveyancing_Efficiency_Annual_crops", "Conveyancing_Efficiency_Permanent_crops"]
39
+ "region": [
40
+ "Conveyancing_Efficiency_Annual_crops",
41
+ "Conveyancing_Efficiency_Permanent_crops",
42
+ "Conveyancing_Efficiency_Perennial_crops"
43
+ ]
40
44
  }
41
45
  TERM_ID = 'freshwaterWithdrawalsDuringCycle'
42
46
 
@@ -26,7 +26,8 @@ MODEL_KEY = 'economicValueShare'
26
26
 
27
27
 
28
28
  def _run(impact: dict, product: dict):
29
- return {**impact.get('product'), MODEL_KEY: product.get(MODEL_KEY, 100)}
29
+ value = product.get(MODEL_KEY)
30
+ return {**impact.get('product'), MODEL_KEY: value} if value is not None else None
30
31
 
31
32
 
32
33
  def _should_run(impact: dict):
@@ -70,7 +70,7 @@ def _should_run(cycle: dict):
70
70
 
71
71
  logRequirements(cycle, model=MODEL, term=TERM_ID,
72
72
  has_products_with_dryMatter=has_products_with_dryMatter,
73
- term_type_incomplete=term_type_incomplete)
73
+ term_type_cropResidue_incomplete=term_type_incomplete)
74
74
 
75
75
  should_run = all([has_products_with_dryMatter, term_type_incomplete])
76
76
  logShouldRun(cycle, MODEL, TERM_ID, should_run)
@@ -81,7 +81,7 @@ def _should_run(cycle: dict):
81
81
  single_crop_product=single_crop_product,
82
82
  nb_products=len(products),
83
83
  dryMatter=dm_property.get('value'),
84
- term_type_incomplete=term_type_incomplete)
84
+ term_type_cropResidue_incomplete=term_type_incomplete)
85
85
 
86
86
  should_run = all([term_type_incomplete, single_crop_product, dm_property])
87
87
  logShouldRun(cycle, MODEL, TERM_ID, should_run)
@@ -22,7 +22,7 @@ REQUIREMENTS = {
22
22
  }
23
23
  }
24
24
  LOOKUPS = {
25
- "crop": ["Crop_residue_intercept", "Crop_residue_slope", "Ratio_Abv_to_Below_Grou_crop_residue"]
25
+ "crop": ["Crop_residue_intercept", "Crop_residue_slope", "IPCC_2019_Ratio_BGRes_AGRes"]
26
26
  }
27
27
  RETURNS = {
28
28
  "Product": [{
@@ -95,7 +95,7 @@ def _should_run(cycle: dict):
95
95
  single_crop_product=single_crop_product,
96
96
  nb_products=len(products),
97
97
  dryMatter=dm_property.get('value'),
98
- term_type_incomplete=term_type_incomplete)
98
+ term_type_cropResidue_incomplete=term_type_incomplete)
99
99
 
100
100
  should_run = all([term_type_incomplete, single_crop_product, dm_property])
101
101
  logShouldRun(cycle, MODEL, TERM_ID, should_run)
@@ -52,7 +52,7 @@ def _should_run(cycle: dict):
52
52
  no3_n=no3_n,
53
53
  nh3_n=nh3_n,
54
54
  nox_n=nox_n,
55
- term_type_complete=term_type_complete)
55
+ term_type_cropResidue_complete=term_type_complete)
56
56
 
57
57
  should_run = all([no3_n, nh3_n, nox_n]) or term_type_complete
58
58
  logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
@@ -52,7 +52,7 @@ def _should_run(cycle: dict):
52
52
 
53
53
  logRequirements(cycle, model=MODEL, term=TERM_ID,
54
54
  N_total=N_total,
55
- term_type_complete=term_type_complete)
55
+ term_type_excreta_complete=term_type_complete)
56
56
 
57
57
  should_run = any([N_total, term_type_complete])
58
58
  logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
@@ -68,7 +68,7 @@ def _should_run(cycle: dict):
68
68
 
69
69
  logRequirements(cycle, model=MODEL, term=TERM_ID,
70
70
  N_total=N_total,
71
- term_type_complete=term_type_complete)
71
+ term_type_excreta_complete=term_type_complete)
72
72
 
73
73
  should_run = any([N_total, term_type_complete])
74
74
  logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
@@ -52,7 +52,7 @@ def _should_run(cycle: dict):
52
52
 
53
53
  logRequirements(cycle, model=MODEL, term=TERM_ID,
54
54
  N_total=N_total,
55
- term_type_complete=term_type_complete)
55
+ term_type_fertiliser_complete=term_type_complete)
56
56
 
57
57
  should_run = any([N_total, term_type_complete])
58
58
  logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
@@ -68,7 +68,7 @@ def _should_run(cycle: dict):
68
68
 
69
69
  logRequirements(cycle, model=MODEL, term=TERM_ID,
70
70
  N_total=N_total,
71
- term_type_complete=term_type_complete)
71
+ term_type_fertiliser_complete=term_type_complete)
72
72
 
73
73
  should_run = any([N_total, term_type_complete])
74
74
  logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
@@ -55,7 +55,7 @@ def _should_run(cycle: dict):
55
55
 
56
56
  logRequirements(cycle, model=MODEL, term=TERM_ID,
57
57
  N_total=N_total,
58
- term_type_complete=term_type_complete)
58
+ term_type_fertiliser_complete=term_type_complete)
59
59
 
60
60
  should_run = any([N_total, term_type_complete])
61
61
  logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
@@ -68,7 +68,7 @@ def _should_run(cycle: dict):
68
68
 
69
69
  logRequirements(cycle, model=MODEL, term=TERM_ID,
70
70
  N_total=N_total,
71
- term_type_complete=term_type_complete)
71
+ term_type_fertiliser_complete=term_type_complete)
72
72
 
73
73
  should_run = any([N_total, term_type_complete])
74
74
  logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
@@ -75,7 +75,7 @@ def _should_run(cycle: dict):
75
75
 
76
76
  logRequirements(cycle, model=MODEL, term=TERM_ID,
77
77
  has_crop_products=has_crop_products,
78
- term_type_incomplete=term_type_incomplete)
78
+ term_type_cropResidue_incomplete=term_type_incomplete)
79
79
 
80
80
  should_run = all([term_type_incomplete, has_crop_products])
81
81
  logShouldRun(cycle, MODEL, TERM_ID, should_run)
@@ -81,7 +81,7 @@ def _should_run(cycle: dict):
81
81
 
82
82
  logRequirements(cycle, model=MODEL, term=TERM_ID,
83
83
  has_crop_products=has_crop_products,
84
- term_type_incomplete=term_type_incomplete)
84
+ term_type_cropResidue_incomplete=term_type_incomplete)
85
85
 
86
86
  should_run = all([term_type_incomplete, has_crop_products])
87
87
  logShouldRun(cycle, MODEL, TERM_ID, should_run)
@@ -119,7 +119,7 @@ def _should_run(cycle: dict):
119
119
  ecoClimateZone = get_ecoClimateZone(cycle)
120
120
 
121
121
  logRequirements(cycle, model=MODEL, term=TERM_ID,
122
- term_type_complete=term_type_complete,
122
+ term_type_cropResidue_complete=term_type_complete,
123
123
  N_crop_residue=N_crop_residue,
124
124
  ecoClimateZone=ecoClimateZone)
125
125
 
@@ -130,7 +130,7 @@ def _should_run(cycle: dict):
130
130
  should_run = all([has_aboveGroundCropResidueTotal, term_type_incomplete])
131
131
  for term_id in TERM_ID.split(','):
132
132
  logRequirements(cycle, model=MODEL, term=term_id,
133
- term_type_incomplete=term_type_incomplete,
133
+ term_type_cropResidue_incomplete=term_type_incomplete,
134
134
  has_aboveGroundCropResidueTotal=has_aboveGroundCropResidueTotal)
135
135
  logShouldRun(cycle, MODEL, term_id, should_run)
136
136