hestia-earth-models 0.59.4__py3-none-any.whl → 0.59.6__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 (55) hide show
  1. hestia_earth/models/cycle/animal/milkYield.py +86 -0
  2. hestia_earth/models/cycle/endDate.py +50 -0
  3. hestia_earth/models/cycle/inorganicFertiliser.py +3 -2
  4. hestia_earth/models/cycle/liveAnimal.py +3 -0
  5. hestia_earth/models/cycle/milkYield.py +8 -3
  6. hestia_earth/models/cycle/pre_checks/__init__.py +1 -2
  7. hestia_earth/models/cycle/startDate.py +42 -0
  8. hestia_earth/models/cycle/utils.py +1 -1
  9. hestia_earth/models/faostat2018/liveweightPerHead.py +77 -41
  10. hestia_earth/models/faostat2018/product/price.py +30 -55
  11. hestia_earth/models/faostat2018/utils.py +10 -2
  12. hestia_earth/models/geospatialDatabase/potentialEvapotranspirationLongTermAnnualMean.py +2 -2
  13. hestia_earth/models/geospatialDatabase/potentialEvapotranspirationMonthly.py +9 -8
  14. hestia_earth/models/geospatialDatabase/precipitationMonthly.py +10 -8
  15. hestia_earth/models/geospatialDatabase/temperatureAnnual.py +2 -5
  16. hestia_earth/models/geospatialDatabase/temperatureLongTermAnnualMean.py +2 -3
  17. hestia_earth/models/geospatialDatabase/temperatureMonthly.py +8 -8
  18. hestia_earth/models/geospatialDatabase/utils.py +6 -1
  19. hestia_earth/models/haversineFormula/transport/distance.py +6 -3
  20. hestia_earth/models/ipcc2006/n2OToAirInorganicFertiliserIndirect.py +1 -1
  21. hestia_earth/models/ipcc2019/organicCarbonPerHa.py +89 -114
  22. hestia_earth/models/ipcc2019/pastureGrass.py +2 -1
  23. hestia_earth/models/linkedImpactAssessment/__init__.py +78 -43
  24. hestia_earth/models/mocking/search-results.json +244 -271
  25. hestia_earth/models/schmidt2007/h2SToAirWasteTreatment.py +58 -0
  26. hestia_earth/models/schmidt2007/n2OToAirWasteTreatmentDirect.py +58 -0
  27. hestia_earth/models/schmidt2007/nh3ToAirWasteTreatment.py +58 -0
  28. hestia_earth/models/site/management.py +107 -12
  29. hestia_earth/models/site/soilMeasurement.py +9 -9
  30. hestia_earth/models/utils/__init__.py +4 -1
  31. hestia_earth/models/utils/animalProduct.py +6 -4
  32. hestia_earth/models/utils/blank_node.py +6 -5
  33. hestia_earth/models/utils/product.py +9 -1
  34. hestia_earth/models/utils/term.py +0 -23
  35. hestia_earth/models/version.py +1 -1
  36. {hestia_earth_models-0.59.4.dist-info → hestia_earth_models-0.59.6.dist-info}/METADATA +1 -1
  37. {hestia_earth_models-0.59.4.dist-info → hestia_earth_models-0.59.6.dist-info}/RECORD +53 -43
  38. tests/models/cycle/animal/test_milkYield.py +43 -0
  39. tests/models/cycle/test_endDate.py +24 -0
  40. tests/models/cycle/test_startDate.py +22 -0
  41. tests/models/faostat2018/product/test_price.py +25 -45
  42. tests/models/faostat2018/test_liveweightPerHead.py +106 -42
  43. tests/models/ipcc2019/test_organicCarbonPerHa.py +12 -18
  44. tests/models/schmidt2007/test_h2SToAirWasteTreatment.py +45 -0
  45. tests/models/schmidt2007/test_n2OToAirWasteTreatmentDirect.py +45 -0
  46. tests/models/schmidt2007/test_nh3ToAirWasteTreatment.py +45 -0
  47. tests/models/site/test_management.py +24 -3
  48. tests/models/site/test_soilMeasurement.py +40 -21
  49. tests/models/utils/test_blank_node.py +71 -3
  50. tests/models/utils/test_term.py +1 -8
  51. hestia_earth/models/cycle/pre_checks/startDate.py +0 -52
  52. tests/models/cycle/pre_checks/test_startDate.py +0 -44
  53. {hestia_earth_models-0.59.4.dist-info → hestia_earth_models-0.59.6.dist-info}/LICENSE +0 -0
  54. {hestia_earth_models-0.59.4.dist-info → hestia_earth_models-0.59.6.dist-info}/WHEEL +0 -0
  55. {hestia_earth_models-0.59.4.dist-info → hestia_earth_models-0.59.6.dist-info}/top_level.txt +0 -0
@@ -8,7 +8,7 @@ from hestia_earth.schema import EmissionMethodTier
8
8
  from hestia_earth.utils.lookup import download_lookup, get_table_value, column_name
9
9
  from hestia_earth.utils.tools import flatten, list_sum
10
10
 
11
- from hestia_earth.models.log import debugValues, logRequirements, logShouldRun
11
+ from hestia_earth.models.log import debugValues, logRequirements, logShouldRun, log_as_table
12
12
  from hestia_earth.models.utils.emission import _new_emission
13
13
  from hestia_earth.models.utils.input import load_impacts
14
14
  from hestia_earth.models.utils.blank_node import group_by_keys
@@ -52,71 +52,99 @@ MODEL_AGGREGATED = 'hestiaAggregatedData'
52
52
  TIER = EmissionMethodTier.BACKGROUND.value
53
53
 
54
54
 
55
- def _emission(cycle: dict, term_id: str, value: float, input: dict, model: str):
56
- # log run on each emission so we know it did run
57
- input_term_id = input.get('term', {}).get('@id')
58
- operation_term_id = input.get('operation', {}).get('@id')
59
- animal_term_id = input.get('animal', {}).get('@id')
60
-
61
- logShouldRun(cycle, model, term_id, True, methodTier=TIER,
62
- input=input_term_id,
63
- operation=operation_term_id,
64
- animal=animal_term_id)
65
-
55
+ def _emission(model: str, term_id: str, value: float, input: dict, operation={}, animal={}):
66
56
  emission = _new_emission(term_id, model)
67
57
  emission['value'] = [value]
68
58
  emission['methodTier'] = TIER
69
- emission['inputs'] = [input.get('term')]
70
- if input.get('operation'):
71
- emission['operation'] = input.get('operation')
72
- if input.get('animal'):
73
- emission['animals'] = [input.get('animal')]
59
+ emission['inputs'] = [input]
60
+ if operation:
61
+ emission['operation'] = operation
62
+ if animal:
63
+ emission['animals'] = [animal]
74
64
  return emission
75
65
 
76
66
 
67
+ def _run_emission(cycle: dict, term_id: str, data: dict):
68
+ def run_input(values: dict):
69
+ value = values.get('value', 0)
70
+ term = values.get('term', {})
71
+ operation = values.get('operation', {})
72
+ animal = values.get('animal', {})
73
+ is_aggregated = any(values.get('aggregated', []))
74
+ model = MODEL_AGGREGATED if is_aggregated else MODEL
75
+
76
+ details = values.get('details', {})
77
+ logRequirements(cycle, model=model, term=term_id,
78
+ values=log_as_table([{'impact-assessment-id': key} | value for key, value in details.items()]))
79
+
80
+ logShouldRun(cycle, model, term_id, True, methodTier=TIER,
81
+ input=term.get('@id'),
82
+ operation=operation.get('@id'),
83
+ animal=animal.get('@id'))
84
+
85
+ return _emission(model, term_id, value, input=term, operation=operation, animal=animal)
86
+
87
+ return list(map(run_input, data.values()))
88
+
89
+
77
90
  def _emission_group(term_id: str):
78
91
  lookup = download_lookup('emission.csv', True)
79
92
  return get_table_value(lookup, 'termid', term_id, column_name('inputProductionGroupId'))
80
93
 
81
94
 
82
95
  def _group_emissions(impact: dict):
83
- def _group_by(prev: dict, emission: dict):
96
+ def _group_by(group: dict, emission: dict):
84
97
  term_id = emission.get('term', {}).get('@id')
85
98
  grouping = _emission_group(term_id)
86
99
  value = emission.get('value') or 0
87
100
  if grouping:
88
- prev[grouping] = prev.get(grouping, 0) + value
89
- return prev
101
+ group[grouping] = group.get(grouping, 0) + value
102
+ return group
90
103
 
91
104
  emissions = impact.get('emissionsResourceUse', [])
92
105
  return reduce(_group_by, emissions, {})
93
106
 
94
107
 
95
- def _run_input(cycle: dict):
96
- def run(inputs: list):
97
- input = inputs[0]
98
- input_value = list_sum(flatten(input.get('value', []) for input in inputs))
99
- term_id = input.get('term', {}).get('@id')
100
- impact = input.get('impactAssessment')
101
- model = MODEL_AGGREGATED if impact.get('aggregated', False) else MODEL
102
- emissions = _group_emissions(impact)
103
-
104
- logRequirements(cycle, model=model, term=term_id,
105
- impact_assessment_id=input.get('impactAssessment', {}).get('@id'),
106
- input_value=input_value)
107
- logShouldRun(cycle, model, term_id, True, methodTier=TIER)
108
-
109
- return [
110
- _emission(cycle, id, input_value * value, input, model) for id, value in emissions.items()
111
- ]
112
- return run
113
-
114
-
115
108
  def _animal_inputs(animal: dict):
116
109
  inputs = load_impacts(animal.get('inputs', []))
117
110
  return [(input | {'animal': animal.get('term', {})}) for input in inputs]
118
111
 
119
112
 
113
+ def _group_input_emissions(input: dict):
114
+ impact = input.get('impactAssessment')
115
+ emissions = _group_emissions(impact)
116
+ return input | {'emissions': emissions}
117
+
118
+
119
+ def _group_inputs(group: dict, values: tuple):
120
+ # input_group_key = 'group-id'
121
+ # inputs = [{'term': {}, 'value':[], 'impactAssessment': {}, 'emissions': {'co2ToAirInputsProduction': 10}}]
122
+ input_group_key, inputs = values
123
+ for input in inputs:
124
+ input_value = list_sum(input.get('value'))
125
+ emissions = input.get('emissions', {})
126
+ for emission_term_id, emission_value in emissions.items():
127
+ group[emission_term_id] = group.get(emission_term_id, {})
128
+
129
+ grouped_inputs = group[emission_term_id].get(input_group_key, {
130
+ 'term': input.get('term', {}),
131
+ 'operation': input.get('operation', {}),
132
+ 'animal': input.get('animal', {}),
133
+ 'value': 0,
134
+ 'aggregated': [],
135
+ 'details': {}
136
+ })
137
+ grouped_inputs['aggregated'].append(input.get('impactAssessment', {}).get('agregated', False))
138
+ grouped_inputs['value'] = grouped_inputs['value'] + (emission_value * input_value)
139
+ # for logging
140
+ grouped_inputs['details'][input.get('impactAssessment', {}).get('@id')] = {
141
+ 'emission-value': emission_value,
142
+ 'input-value': input_value
143
+ }
144
+ group[emission_term_id][input_group_key] = grouped_inputs
145
+ return group
146
+
147
+
120
148
  def run(_, cycle: dict):
121
149
  inputs = flatten(
122
150
  load_impacts(cycle.get('inputs', [])) +
@@ -124,9 +152,16 @@ def run(_, cycle: dict):
124
152
  )
125
153
  inputs = [i for i in inputs if list_sum(i.get('value', [])) > 0]
126
154
 
155
+ # group inputs with same term/operation/animal to avoid adding emissions twice
156
+ # inputs = {'group-id': [{'term': {},'value':[10],'impactAssessment': {}}]}
157
+ inputs = reduce(group_by_keys(['term', 'operation', 'animal']), inputs, {})
158
+ inputs = {key: list(map(_group_input_emissions, value)) for key, value in inputs.items()}
159
+
127
160
  debugValues(cycle, model=MODEL,
128
161
  nb_inputs=len(inputs))
129
162
 
130
- # group inputs with same id/operation to avoid adding emissions twice
131
- inputs = reduce(group_by_keys(['term', 'operation', 'animal']), inputs, {})
132
- return flatten(map(_run_input(cycle), inputs.values()))
163
+ # finally group everything by emission so we can log inputs together
164
+ # emissions = {'co2ToAirInputsProduct': {'group-id':{'term':{},'value':10,'details':{}}}}
165
+ emissions = reduce(_group_inputs, inputs.items(), {})
166
+
167
+ return flatten([_run_emission(cycle, term_id, data) for term_id, data in emissions.items()])