hestia-earth-models 0.70.0__py3-none-any.whl → 0.70.1__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.
Files changed (168) hide show
  1. hestia_earth/models/aware/scarcityWeightedWaterUse.py +8 -16
  2. hestia_earth/models/config/Cycle.json +52 -64
  3. hestia_earth/models/config/ImpactAssessment.json +12 -4
  4. hestia_earth/models/config/Site.json +33 -22
  5. hestia_earth/models/cycle/transformation.py +1 -1
  6. hestia_earth/models/cycle/utils.py +0 -6
  7. hestia_earth/models/data/ecoinventV3/__init__.py +15 -13
  8. hestia_earth/models/ecoalimV9/__init__.py +13 -0
  9. hestia_earth/models/ecoalimV9/cycle.py +128 -0
  10. hestia_earth/models/ecoalimV9/impact_assessment.py +125 -0
  11. hestia_earth/models/ecoalimV9/utils.py +31 -0
  12. hestia_earth/models/ecoinventV3/__init__.py +6 -14
  13. hestia_earth/models/ecoinventV3/utils.py +1 -29
  14. hestia_earth/models/ecoinventV3AndEmberClimate/__init__.py +8 -2
  15. hestia_earth/models/emissionNotRelevant/__init__.py +33 -8
  16. hestia_earth/models/{cycle → hestia}/aboveGroundCropResidue.py +4 -3
  17. hestia_earth/models/{cycle → hestia}/aboveGroundCropResidueTotal.py +1 -1
  18. hestia_earth/models/{site → hestia}/brackishWater.py +1 -1
  19. hestia_earth/models/{site → hestia}/cationExchangeCapacityPerKgSoil.py +1 -1
  20. hestia_earth/models/{cycle → hestia}/coldCarcassWeightPerHead.py +1 -1
  21. hestia_earth/models/{cycle → hestia}/coldDressedCarcassWeightPerHead.py +1 -1
  22. hestia_earth/models/{cycle → hestia}/concentrateFeed.py +1 -1
  23. hestia_earth/models/{cycle → hestia}/cropResidueManagement.py +1 -1
  24. hestia_earth/models/{cycle → hestia}/croppingIntensity.py +1 -1
  25. hestia_earth/models/{cycle → hestia}/energyContentLowerHeatingValue.py +1 -1
  26. hestia_earth/models/{cycle → hestia}/excretaKgMass.py +7 -2
  27. hestia_earth/models/{cycle → hestia}/excretaKgN.py +1 -1
  28. hestia_earth/models/{cycle → hestia}/excretaKgVs.py +1 -1
  29. hestia_earth/models/{cycle → hestia}/feedConversionRatio/__init__.py +1 -1
  30. hestia_earth/models/{site → hestia}/flowingWater.py +1 -1
  31. hestia_earth/models/{site → hestia}/freshWater.py +1 -1
  32. hestia_earth/models/{cycle → hestia}/inorganicFertiliser.py +1 -1
  33. hestia_earth/models/{cycle → hestia}/irrigatedTypeUnspecified.py +14 -19
  34. hestia_earth/models/hestia/landCover.py +30 -22
  35. hestia_earth/models/{cycle → hestia}/liveAnimal.py +1 -1
  36. hestia_earth/models/{cycle → hestia}/longFallowRatio.py +1 -1
  37. hestia_earth/models/{cycle → hestia}/materialAndSubstrate.py +1 -1
  38. hestia_earth/models/{cycle → hestia}/milkYield.py +1 -1
  39. hestia_earth/models/{site → hestia}/netPrimaryProduction.py +1 -1
  40. hestia_earth/models/{site → hestia}/organicCarbonPerHa.py +1 -1
  41. hestia_earth/models/{cycle → hestia}/pastureGrass.py +1 -1
  42. hestia_earth/models/{cycle → hestia}/pastureSystem.py +1 -1
  43. hestia_earth/models/{site → hestia}/potentialEvapotranspirationAnnual.py +3 -3
  44. hestia_earth/models/{site → hestia}/potentialEvapotranspirationMonthly.py +3 -3
  45. hestia_earth/models/{site → hestia}/precipitationAnnual.py +3 -3
  46. hestia_earth/models/{site → hestia}/precipitationMonthly.py +3 -3
  47. hestia_earth/models/{site → hestia}/rainfallAnnual.py +3 -3
  48. hestia_earth/models/{site → hestia}/rainfallMonthly.py +3 -3
  49. hestia_earth/models/{cycle → hestia}/readyToCookWeightPerHead.py +1 -1
  50. hestia_earth/models/{cycle → hestia}/residueBurnt.py +1 -1
  51. hestia_earth/models/{cycle → hestia}/residueIncorporated.py +1 -1
  52. hestia_earth/models/{cycle → hestia}/residueLeftOnField.py +1 -1
  53. hestia_earth/models/hestia/residueRemoved.py +65 -13
  54. hestia_earth/models/{site → hestia}/salineWater.py +1 -1
  55. hestia_earth/models/{site → hestia}/soilMeasurement.py +1 -1
  56. hestia_earth/models/{cycle → hestia}/stockingDensityAnimalHousingAverage.py +1 -1
  57. hestia_earth/models/{site → hestia}/temperatureAnnual.py +3 -3
  58. hestia_earth/models/{site → hestia}/temperatureMonthly.py +3 -3
  59. hestia_earth/models/{site → hestia}/totalNitrogenPerKgSoil.py +1 -1
  60. hestia_earth/models/{cycle → hestia}/unknownPreSeasonWaterRegime.py +1 -1
  61. hestia_earth/models/hestia/utils.py +93 -0
  62. hestia_earth/models/{site → hestia}/waterDepth.py +1 -1
  63. hestia_earth/models/hestia/waterSalinity.py +78 -0
  64. hestia_earth/models/ipcc2019/aboveGroundBiomass.py +1 -1
  65. hestia_earth/models/ipcc2019/belowGroundBiomass.py +1 -1
  66. hestia_earth/models/ipcc2019/biomass_utils.py +2 -4
  67. hestia_earth/models/ipcc2019/ch4ToAirFloodedRice.py +163 -78
  68. hestia_earth/models/ipcc2019/co2ToAirCarbonStockChange_utils.py +31 -20
  69. hestia_earth/models/ipcc2019/co2ToAirUreaHydrolysis.py +16 -9
  70. hestia_earth/models/ipcc2019/nonCo2EmissionsToAirNaturalVegetationBurning.py +35 -47
  71. hestia_earth/models/ipcc2019/organicCarbonPerHa_tier_1.py +86 -1
  72. hestia_earth/models/ipcc2019/organicCarbonPerHa_tier_2.py +127 -1
  73. hestia_earth/models/ipcc2019/organicCarbonPerHa_utils.py +7 -5
  74. hestia_earth/models/mocking/search-results.json +764 -772
  75. hestia_earth/models/pooreNemecek2018/utils.py +8 -2
  76. hestia_earth/models/schmidt2007/ch4ToAirWasteTreatment.py +1 -4
  77. hestia_earth/models/schmidt2007/h2SToAirWasteTreatment.py +1 -4
  78. hestia_earth/models/schmidt2007/n2OToAirWasteTreatmentDirect.py +1 -4
  79. hestia_earth/models/schmidt2007/nh3ToAirWasteTreatment.py +1 -4
  80. hestia_earth/models/utils/background_emissions.py +52 -0
  81. hestia_earth/models/utils/blank_node.py +9 -5
  82. hestia_earth/models/utils/impact_assessment.py +26 -17
  83. hestia_earth/models/utils/lookup.py +48 -39
  84. hestia_earth/models/utils/measurement.py +3 -3
  85. hestia_earth/models/version.py +1 -1
  86. {hestia_earth_models-0.70.0.dist-info → hestia_earth_models-0.70.1.dist-info}/METADATA +2 -2
  87. {hestia_earth_models-0.70.0.dist-info → hestia_earth_models-0.70.1.dist-info}/RECORD +163 -158
  88. tests/models/aware/test_scarcityWeightedWaterUse.py +1 -12
  89. tests/models/ecoalimV9/__init__.py +0 -0
  90. tests/models/ecoalimV9/test_cycle.py +21 -0
  91. tests/models/ecoalimV9/test_impact_assessment.py +24 -0
  92. tests/models/environmentalFootprintV3_1/test_scarcityWeightedWaterUse.py +4 -2
  93. tests/models/{cycle → hestia}/test_aboveGroundCropResidue.py +1 -1
  94. tests/models/{cycle → hestia}/test_aboveGroundCropResidueTotal.py +1 -1
  95. tests/models/{site → hestia}/test_brackishWater.py +1 -1
  96. tests/models/{site → hestia}/test_cationExchangeCapacityPerKgSoil.py +1 -1
  97. tests/models/{cycle → hestia}/test_coldCarcassWeightPerHead.py +1 -1
  98. tests/models/{cycle → hestia}/test_coldDressedCarcassWeightPerHead.py +1 -1
  99. tests/models/{cycle → hestia}/test_concentrateFeed.py +1 -1
  100. tests/models/{cycle → hestia}/test_cropResidueManagement.py +1 -1
  101. tests/models/{cycle → hestia}/test_croppingIntensity.py +1 -1
  102. tests/models/{cycle → hestia}/test_energyContentLowerHeatingValue.py +5 -3
  103. tests/models/{cycle → hestia}/test_excretaKgMass.py +1 -1
  104. tests/models/{cycle → hestia}/test_excretaKgN.py +1 -1
  105. tests/models/{cycle → hestia}/test_excretaKgVs.py +1 -1
  106. tests/models/{cycle → hestia}/test_feedConversionRatio.py +1 -1
  107. tests/models/{site → hestia}/test_flowingWater.py +1 -1
  108. tests/models/{site → hestia}/test_freshWater.py +1 -1
  109. tests/models/{cycle → hestia}/test_inorganicFertiliser.py +1 -1
  110. tests/models/{cycle → hestia}/test_irrigatedTypeUnspecified.py +2 -5
  111. tests/models/hestia/test_landCover.py +4 -34
  112. tests/models/{cycle → hestia}/test_liveAnimal.py +1 -1
  113. tests/models/{cycle → hestia}/test_longFallowRatio.py +1 -1
  114. tests/models/{site → hestia}/test_management.py +1 -1
  115. tests/models/{cycle → hestia}/test_materialsAndSubstrate.py +1 -1
  116. tests/models/{cycle → hestia}/test_milkYield.py +1 -1
  117. tests/models/{site → hestia}/test_netPrimaryProduction.py +1 -1
  118. tests/models/{site → hestia}/test_organicCarbonPerHa.py +1 -1
  119. tests/models/{site → hestia}/test_organicCarbonPerKgSoil.py +1 -1
  120. tests/models/{site → hestia}/test_organicCarbonPerM3Soil.py +1 -1
  121. tests/models/{site → hestia}/test_organicMatterPerKgSoil.py +1 -1
  122. tests/models/{site → hestia}/test_organicMatterPerM3Soil.py +1 -1
  123. tests/models/{cycle → hestia}/test_pastureGrass.py +1 -1
  124. tests/models/{cycle → hestia}/test_pastureSystem.py +1 -1
  125. tests/models/{site → hestia}/test_potentialEvapotranspirationAnnual.py +1 -1
  126. tests/models/{site → hestia}/test_potentialEvapotranspirationMonthly.py +1 -1
  127. tests/models/{site → hestia}/test_precipitationAnnual.py +1 -1
  128. tests/models/{site → hestia}/test_precipitationMonthly.py +1 -1
  129. tests/models/{site → hestia}/test_rainfallAnnual.py +1 -1
  130. tests/models/{site → hestia}/test_rainfallMonthly.py +1 -1
  131. tests/models/{cycle → hestia}/test_readyToCookWeightPerHead.py +1 -1
  132. tests/models/{cycle → hestia}/test_residueBurnt.py +1 -1
  133. tests/models/{cycle → hestia}/test_residueIncorporated.py +1 -1
  134. tests/models/{cycle → hestia}/test_residueLeftOnField.py +1 -1
  135. tests/models/hestia/test_residueRemoved.py +15 -3
  136. tests/models/{site → hestia}/test_salineWater.py +1 -1
  137. tests/models/{site → hestia}/test_soilMeasurement.py +2 -2
  138. tests/models/{cycle → hestia}/test_stockingDensityAnimalHousingAverage.py +1 -1
  139. tests/models/{site → hestia}/test_temperatureAnnual.py +1 -1
  140. tests/models/{site → hestia}/test_temperatureMonthly.py +1 -1
  141. tests/models/{site → hestia}/test_totalNitrogenPerKgSoil.py +1 -1
  142. tests/models/{cycle → hestia}/test_unknownPreSeasonWaterRegime.py +1 -1
  143. tests/models/{site → hestia}/test_waterDepth.py +1 -1
  144. tests/models/hestia/test_waterSalinity.py +26 -0
  145. tests/models/ipcc2019/test_ch4ToAirFloodedRice.py +10 -42
  146. tests/models/ipcc2019/test_co2ToAirSoilOrganicCarbonStockChange.py +2 -1
  147. tests/models/ipcc2019/test_nonCo2EmissionsToAirNaturalVegetationBurning.py +3 -2
  148. tests/models/test_ecoinventV3AndEmberClimate.py +2 -2
  149. tests/models/test_emissionNotRelevant.py +0 -8
  150. tests/models/utils/test_measurement.py +1 -1
  151. hestia_earth/models/cycle/residueRemoved.py +0 -54
  152. hestia_earth/models/hestia/nh3ToSurfaceWaterAquacultureSystems.py +0 -64
  153. hestia_earth/models/site/utils.py +0 -93
  154. tests/models/cycle/test_residueRemoved.py +0 -37
  155. tests/models/hestia/test_nh3ToSurfaceWaterAquacultureSystems.py +0 -51
  156. /hestia_earth/models/{cycle → hestia}/feedConversionRatio/feedConversionRatioCarbon.py +0 -0
  157. /hestia_earth/models/{cycle → hestia}/feedConversionRatio/feedConversionRatioDryMatter.py +0 -0
  158. /hestia_earth/models/{cycle → hestia}/feedConversionRatio/feedConversionRatioEnergy.py +0 -0
  159. /hestia_earth/models/{cycle → hestia}/feedConversionRatio/feedConversionRatioFedWeight.py +0 -0
  160. /hestia_earth/models/{cycle → hestia}/feedConversionRatio/feedConversionRatioNitrogen.py +0 -0
  161. /hestia_earth/models/{site → hestia}/management.py +0 -0
  162. /hestia_earth/models/{site → hestia}/organicCarbonPerKgSoil.py +0 -0
  163. /hestia_earth/models/{site → hestia}/organicCarbonPerM3Soil.py +0 -0
  164. /hestia_earth/models/{site → hestia}/organicMatterPerKgSoil.py +0 -0
  165. /hestia_earth/models/{site → hestia}/organicMatterPerM3Soil.py +0 -0
  166. {hestia_earth_models-0.70.0.dist-info → hestia_earth_models-0.70.1.dist-info}/LICENSE +0 -0
  167. {hestia_earth_models-0.70.0.dist-info → hestia_earth_models-0.70.1.dist-info}/WHEEL +0 -0
  168. {hestia_earth_models-0.70.0.dist-info → hestia_earth_models-0.70.1.dist-info}/top_level.txt +0 -0
@@ -1,32 +1,34 @@
1
1
  import os
2
- from hestia_earth.utils.lookup import column_name, get_table_value, load_lookup
2
+ from functools import lru_cache
3
+ from hestia_earth.utils.lookup import column_name, get_table_value, load_lookup, lookup_columns
3
4
  from hestia_earth.utils.tools import non_empty_list
4
5
 
5
6
  from hestia_earth.models.log import logger
6
7
 
7
8
  CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
8
- ECOINVENT_FILEPATH = os.getenv('ECOINVENT_V3_FILEPATH', f"{os.path.join(CURRENT_DIR, 'ecoinventV3_excerpt')}.csv")
9
+ _FILEPATH = os.getenv('ECOINVENT_V3_FILEPATH', f"{os.path.join(CURRENT_DIR, 'ecoinventV3_excerpt')}.csv")
9
10
 
10
11
 
12
+ @lru_cache()
11
13
  def _get_file():
12
- if not os.path.exists(ECOINVENT_FILEPATH):
13
- logger.warn('Ecoinvent file not found. Please make sure to set env variable "ECOINVENT_V3_FILEPATH".')
14
+ if not os.path.exists(_FILEPATH):
15
+ logger.warning('Ecoinvent file not found. Please make sure to set env variable "ECOINVENT_V3_FILEPATH".')
14
16
  return None
15
17
 
16
- return load_lookup(filepath=ECOINVENT_FILEPATH, keep_in_memory=True)
18
+ return load_lookup(filepath=_FILEPATH, keep_in_memory=True)
17
19
 
18
20
 
19
21
  def ecoinventV3_emissions(ecoinventName: str):
20
22
  lookup = _get_file()
21
23
  col_name = column_name('ecoinventName')
22
24
 
23
- def emission(index: int):
24
- id = get_table_value(
25
- lookup, col_name, ecoinventName, column_name(f"emissionsResourceUse.{index}.term.id")
26
- )
27
- value = get_table_value(
28
- lookup, col_name, ecoinventName, column_name(f"emissionsResourceUse.{index}.value")
29
- )
25
+ def emission(column: str):
26
+ id = get_table_value(lookup, col_name, ecoinventName, column_name(column))
27
+ value = get_table_value(lookup, col_name, ecoinventName, column_name(column.replace('termid', 'value')))
30
28
  return (id, value) if id else None
31
29
 
32
- return non_empty_list(map(emission, range(0, 12)))
30
+ columns = [
31
+ col for col in lookup_columns(lookup)
32
+ if col.endswith(column_name('termid'))
33
+ ]
34
+ return non_empty_list(map(emission, columns))
@@ -0,0 +1,13 @@
1
+ from os.path import dirname, abspath
2
+ import sys
3
+ from importlib import import_module
4
+
5
+ from hestia_earth.models.utils.blank_node import run_if_required
6
+
7
+ CURRENT_DIR = dirname(abspath(__file__)) + '/'
8
+ sys.path.append(CURRENT_DIR)
9
+ MODEL = 'ecoalimV9'
10
+ PKG = '.'.join(['hestia_earth', 'models', MODEL])
11
+
12
+
13
+ def run(model: str, data): return run_if_required(MODEL, model, data, import_module(f".{model}", package=PKG))
@@ -0,0 +1,128 @@
1
+ from functools import reduce
2
+ from statistics import mean
3
+ from hestia_earth.schema import EmissionMethodTier
4
+ from hestia_earth.utils.tools import flatten, list_sum
5
+
6
+ from hestia_earth.models.log import debugValues, logShouldRun, logRequirements
7
+ from hestia_earth.models.utils.emission import _new_emission
8
+ from hestia_earth.models.utils.background_emissions import get_background_inputs, no_gap_filled_background_emissions
9
+ from hestia_earth.models.utils.blank_node import group_by_keys
10
+ from .utils import get_input_mappings, ecoalim_values
11
+ from . import MODEL
12
+
13
+ REQUIREMENTS = {
14
+ "Cycle": {
15
+ "inputs": [{
16
+ "@type": "Input",
17
+ "value": "> 0",
18
+ "none": {
19
+ "fromCycle": "True",
20
+ "producedInCycle": "True"
21
+ }
22
+ }],
23
+ "optional": {
24
+ "animals": [{
25
+ "@type": "Animal",
26
+ "inputs": [{
27
+ "@type": "Input",
28
+ "value": "> 0",
29
+ "none": {
30
+ "fromCycle": "True",
31
+ "producedInCycle": "True"
32
+ }
33
+ }]
34
+ }]
35
+ }
36
+ }
37
+ }
38
+ RETURNS = {
39
+ "Emission": [{
40
+ "term": "",
41
+ "value": "",
42
+ "methodTier": "background",
43
+ "inputs": "",
44
+ "operation": "",
45
+ "animals": ""
46
+ }]
47
+ }
48
+ LOOKUPS = {
49
+ "ecoalim-emissionsResourceUse": "emission-",
50
+ "crop": "ecoalimMapping",
51
+ "processedFood": "ecoalimMapping",
52
+ "animalProduct": "ecoalimMapping",
53
+ "forage": "ecoalimMapping",
54
+ "feedFoodAdditive": "ecoalimMapping"
55
+ }
56
+ MODEL_KEY = 'cycle'
57
+ TIER = EmissionMethodTier.BACKGROUND.value
58
+
59
+
60
+ def _emission(term_id: str, value: float, input: dict):
61
+ emission = _new_emission(term_id, MODEL)
62
+ emission['value'] = [value]
63
+ emission['methodTier'] = TIER
64
+ emission['inputs'] = [input.get('term')]
65
+ if input.get('operation'):
66
+ emission['operation'] = input.get('operation')
67
+ if input.get('animal'):
68
+ emission['animals'] = [input.get('animal')]
69
+ return emission
70
+
71
+
72
+ def _add_emission(cycle: dict, input: dict):
73
+ input_term_id = input.get('term', {}).get('@id')
74
+ operation_term_id = input.get('operation', {}).get('@id')
75
+ animal_term_id = input.get('animal', {}).get('@id')
76
+
77
+ def add(prev: dict, mapping: tuple):
78
+ gadm_id, ecoalim_key = mapping
79
+ # all countries have the same coefficient
80
+ coefficient = 1
81
+ emissions = ecoalim_values(ecoalim_key, 'emission')
82
+ for emission_term_id, value in emissions:
83
+ # log run on each emission so we know it did run
84
+ debugValues(cycle, model=MODEL, term=emission_term_id, model_key=MODEL_KEY,
85
+ value=value,
86
+ coefficient=coefficient,
87
+ input=input_term_id,
88
+ operation=operation_term_id,
89
+ animal=animal_term_id)
90
+ prev[emission_term_id] = prev.get(emission_term_id, []) + [value * coefficient]
91
+ return prev
92
+ return add
93
+
94
+
95
+ def _run_input(cycle: dict):
96
+ no_gap_filled_background_emissions_func = no_gap_filled_background_emissions(cycle)
97
+
98
+ def run(inputs: list):
99
+ input = inputs[0]
100
+ input_term_id = input.get('term', {}).get('@id')
101
+ input_value = list_sum(flatten(input.get('value', []) for input in inputs))
102
+ mappings = get_input_mappings(MODEL, input)
103
+ has_mappings = len(mappings) > 0
104
+
105
+ # skip input that has background emissions we have already gap-filled (model run before)
106
+ has_no_gap_filled_background_emissions = no_gap_filled_background_emissions_func(input)
107
+
108
+ logRequirements(cycle, model=MODEL, term=input_term_id, model_key=MODEL_KEY,
109
+ has_ecoalim_mappings=has_mappings,
110
+ ecoalim_mappings=';'.join([v[1] for v in mappings]),
111
+ has_no_gap_filled_background_emissions=has_no_gap_filled_background_emissions,
112
+ input_value=input_value)
113
+
114
+ should_run = all([has_mappings, has_no_gap_filled_background_emissions, input_value])
115
+ logShouldRun(cycle, MODEL, input_term_id, should_run, methodTier=TIER, model_key=MODEL_KEY)
116
+
117
+ grouped_emissions = reduce(_add_emission(cycle, input), mappings, {}) if should_run else {}
118
+ return [
119
+ _emission(term_id, mean(value) * input_value, input)
120
+ for term_id, value in grouped_emissions.items()
121
+ ]
122
+ return run
123
+
124
+
125
+ def run(cycle: dict):
126
+ inputs = get_background_inputs(cycle)
127
+ grouped_inputs = reduce(group_by_keys(['term', 'operation', 'animal']), inputs, {})
128
+ return flatten(map(_run_input(cycle), grouped_inputs.values()))
@@ -0,0 +1,125 @@
1
+ from functools import reduce
2
+ from statistics import mean
3
+ from hestia_earth.schema import IndicatorMethodTier
4
+ from hestia_earth.utils.tools import flatten, list_sum
5
+
6
+ from hestia_earth.models.log import debugValues, logShouldRun, logRequirements
7
+ from hestia_earth.models.utils.indicator import _new_indicator
8
+ from hestia_earth.models.utils.background_emissions import get_background_inputs
9
+ from hestia_earth.models.utils.blank_node import group_by_keys
10
+ from .utils import get_input_mappings, ecoalim_values
11
+ from . import MODEL
12
+
13
+ REQUIREMENTS = {
14
+ "ImpactAssessment": {
15
+ "cycle": {
16
+ "@type": "Cycle",
17
+ "inputs": [{
18
+ "@type": "Input",
19
+ "value": "> 0",
20
+ "none": {
21
+ "fromCycle": "True",
22
+ "producedInCycle": "True"
23
+ }
24
+ }],
25
+ "optional": {
26
+ "animals": [{
27
+ "@type": "Animal",
28
+ "inputs": [{
29
+ "@type": "Input",
30
+ "value": "> 0",
31
+ "none": {
32
+ "fromCycle": "True",
33
+ "producedInCycle": "True"
34
+ }
35
+ }]
36
+ }]
37
+ }
38
+ }
39
+ }
40
+ }
41
+ RETURNS = {
42
+ "Indicator": [{
43
+ "term": "",
44
+ "value": "",
45
+ "methodTier": "background",
46
+ "inputs": "",
47
+ "operation": ""
48
+ }]
49
+ }
50
+ LOOKUPS = {
51
+ "ecoalim-emissionsResourceUse": "resourceUse-",
52
+ "crop": "ecoalimMapping",
53
+ "processedFood": "ecoalimMapping",
54
+ "animalProduct": "ecoalimMapping",
55
+ "forage": "ecoalimMapping",
56
+ "feedFoodAdditive": "ecoalimMapping"
57
+ }
58
+ MODEL_KEY = 'impact_assessment'
59
+ TIER = IndicatorMethodTier.BACKGROUND.value
60
+
61
+
62
+ def _indicator(term_id: str, value: float, input: dict):
63
+ indicator = _new_indicator(term_id, MODEL)
64
+ indicator['value'] = value
65
+ indicator['methodTier'] = TIER
66
+ indicator['inputs'] = [input.get('term')]
67
+ if input.get('operation'):
68
+ indicator['operation'] = input.get('operation')
69
+ return indicator
70
+
71
+
72
+ def _add_indicator(cycle: dict, input: dict):
73
+ input_term_id = input.get('term', {}).get('@id')
74
+ operation_term_id = input.get('operation', {}).get('@id')
75
+ animal_term_id = input.get('animal', {}).get('@id')
76
+
77
+ def add(prev: dict, mapping: tuple):
78
+ gadm_id, ecoalim_key = mapping
79
+ # all countries have the same coefficient
80
+ coefficient = 1
81
+ indicators = ecoalim_values(ecoalim_key, 'resourceUse')
82
+ for indicator_term_id, value in indicators:
83
+ # log run on each indicator so we know it did run
84
+ debugValues(cycle, model=MODEL, term=indicator_term_id, model_key=MODEL_KEY,
85
+ value=value,
86
+ coefficient=coefficient,
87
+ input=input_term_id,
88
+ operation=operation_term_id,
89
+ animal=animal_term_id)
90
+ if value is not None:
91
+ prev[indicator_term_id] = prev.get(indicator_term_id, []) + [value * coefficient]
92
+ return prev
93
+ return add
94
+
95
+
96
+ def _run_input(impact_assessment: dict):
97
+ def run(inputs: list):
98
+ input = inputs[0]
99
+ input_term_id = input.get('term', {}).get('@id')
100
+ input_value = list_sum(flatten(input.get('value', []) for input in inputs))
101
+ mappings = get_input_mappings(MODEL, input)
102
+ has_mappings = len(mappings) > 0
103
+
104
+ logRequirements(impact_assessment, model=MODEL, term=input_term_id, model_key=MODEL_KEY,
105
+ has_ecoalim_mappings=has_mappings,
106
+ ecoalim_mappings=';'.join([v[1] for v in mappings]),
107
+ input_value=input_value)
108
+
109
+ should_run = all([has_mappings, input_value])
110
+ logShouldRun(
111
+ impact_assessment, MODEL, input_term_id, should_run, methodTier=TIER, model_key=MODEL_KEY
112
+ )
113
+
114
+ grouped_indicators = reduce(_add_indicator(impact_assessment, input), mappings, {}) if should_run else {}
115
+ return [
116
+ _indicator(term_id, mean(value) * input_value, input)
117
+ for term_id, value in grouped_indicators.items()
118
+ ]
119
+ return run
120
+
121
+
122
+ def run(impact_assessment: dict):
123
+ inputs = get_background_inputs(impact_assessment.get('cycle', {}))
124
+ grouped_inputs = reduce(group_by_keys(['term', 'operation']), inputs, {})
125
+ return flatten(map(_run_input(impact_assessment), grouped_inputs.values()))
@@ -0,0 +1,31 @@
1
+ from hestia_earth.utils.lookup import download_lookup, get_table_value, column_name, lookup_columns
2
+ from hestia_earth.utils.tools import non_empty_list
3
+
4
+ from hestia_earth.models.utils.term import get_lookup_value
5
+
6
+
7
+ _LOOKUP = "ecoalim-emissionsResourceUse.csv"
8
+
9
+
10
+ def get_input_mappings(model: str, input: dict):
11
+ term = input.get('term', {})
12
+ term_id = term.get('@id')
13
+ value = get_lookup_value(term, 'ecoalimMapping', model=model, term=term_id)
14
+ mappings = non_empty_list(value.split(';')) if value else []
15
+ return [(m.split(':')[0], m.split(':')[1]) for m in mappings]
16
+
17
+
18
+ def ecoalim_values(mapping: str, column_prefix: str):
19
+ lookup = download_lookup(_LOOKUP)
20
+ col_name = column_name('ecoalimMappingName')
21
+
22
+ def emission(column: str):
23
+ id = get_table_value(lookup, col_name, mapping, column)
24
+ value = get_table_value(lookup, col_name, mapping, column.replace('term', 'value'))
25
+ return (id, value) if id else None
26
+
27
+ columns = [
28
+ col for col in lookup_columns(lookup)
29
+ if col.startswith(column_name(column_prefix)) and col.endswith(column_name('term'))
30
+ ]
31
+ return non_empty_list(map(emission, columns))
@@ -4,12 +4,12 @@ from hestia_earth.utils.tools import flatten, list_sum
4
4
 
5
5
  from hestia_earth.models.log import debugValues, logShouldRun, logRequirements
6
6
  from hestia_earth.models.data.ecoinventV3 import ecoinventV3_emissions
7
- from hestia_earth.models.utils import is_from_model
8
7
  from hestia_earth.models.utils.emission import _new_emission
8
+ from hestia_earth.models.utils.background_emissions import get_background_inputs, no_gap_filled_background_emissions
9
9
  from hestia_earth.models.utils.blank_node import group_by_keys
10
10
  from hestia_earth.models.utils.pesticideAI import get_pesticides_from_inputs
11
11
  from hestia_earth.models.utils.fertiliser import get_fertilisers_from_inputs
12
- from .utils import get_background_inputs, get_input_mappings
12
+ from .utils import get_input_mappings
13
13
 
14
14
  REQUIREMENTS = {
15
15
  "Cycle": {
@@ -96,26 +96,17 @@ def _add_emission(cycle: dict, input: dict):
96
96
 
97
97
 
98
98
  def _run_input(cycle: dict):
99
- emissions = cycle.get('emissions', [])
99
+ no_gap_filled_background_emissions_func = no_gap_filled_background_emissions(cycle)
100
100
 
101
101
  def run(inputs: list):
102
102
  input = inputs[0]
103
103
  input_term_id = input.get('term', {}).get('@id')
104
- operation_term_id = input.get('operation', {}).get('@id')
105
- animal_term_id = input.get('animal', {}).get('@id')
106
104
  input_value = list_sum(flatten(input.get('value', []) for input in inputs))
107
105
  mappings = get_input_mappings(MODEL, cycle, input)
108
106
  has_mappings = len(mappings) > 0
107
+
109
108
  # skip input that has background emissions we have already gap-filled (model run before)
110
- has_no_gap_filled_background_emissions = not any([
111
- is_from_model(e)
112
- for e in emissions
113
- if all([
114
- any([i.get('@id') == input_term_id for i in e.get('inputs', [])]),
115
- e.get('operation', {}).get('@id') == operation_term_id,
116
- e.get('animal', {}).get('@id') == animal_term_id
117
- ])
118
- ])
109
+ has_no_gap_filled_background_emissions = no_gap_filled_background_emissions_func(input)
119
110
 
120
111
  logRequirements(cycle, model=MODEL, term=input_term_id,
121
112
  has_ecoinvent_mappings=has_mappings,
@@ -125,6 +116,7 @@ def _run_input(cycle: dict):
125
116
 
126
117
  should_run = all([has_mappings, has_no_gap_filled_background_emissions, input_value])
127
118
  logShouldRun(cycle, MODEL, input_term_id, should_run, methodTier=TIER)
119
+
128
120
  grouped_emissions = reduce(_add_emission(cycle, input), mappings, {}) if should_run else {}
129
121
  return [
130
122
  _emission(term_id, value * input_value, input)
@@ -1,36 +1,8 @@
1
- from hestia_earth.utils.model import find_term_match
2
- from hestia_earth.utils.tools import flatten, non_empty_list
1
+ from hestia_earth.utils.tools import non_empty_list
3
2
 
4
3
  from hestia_earth.models.utils.term import get_lookup_value
5
4
 
6
5
 
7
- def _animal_inputs(animal: dict):
8
- inputs = animal.get('inputs', [])
9
- return [(input | {'animal': animal.get('term', {})}) for input in inputs]
10
-
11
-
12
- def _should_run_input(products: list):
13
- def should_run(input: dict):
14
- return all([
15
- # make sure Input is not a Product as well or we might double-count emissions
16
- find_term_match(products, input.get('term', {}).get('@id'), None) is None,
17
- # ignore inputs which are flagged as Product of the Cycle
18
- not input.get('fromCycle', False),
19
- not input.get('producedInCycle', False)
20
- ])
21
- return should_run
22
-
23
-
24
- def get_background_inputs(cycle: dict, extra_inputs: list = []):
25
- # add all the properties of some Term that inlcude others with the mapping
26
- inputs = flatten(
27
- cycle.get('inputs', []) +
28
- list(map(_animal_inputs, cycle.get('animals', []))) +
29
- extra_inputs
30
- )
31
- return list(filter(_should_run_input(cycle.get('products', [])), inputs))
32
-
33
-
34
6
  def get_input_mappings(model: str, cycle: dict, input: dict):
35
7
  term = input.get('term', {})
36
8
  term_id = term.get('@id')
@@ -4,12 +4,13 @@ from hestia_earth.schema import EmissionMethodTier
4
4
 
5
5
  from hestia_earth.models.log import logShouldRun, logRequirements, debugValues
6
6
  from hestia_earth.models.utils.emission import _new_emission
7
+ from hestia_earth.models.utils.background_emissions import get_background_inputs, no_gap_filled_background_emissions
7
8
  from hestia_earth.models.utils.blank_node import group_by_keys
8
9
  from hestia_earth.models.utils.completeness import _is_term_type_complete
9
10
  from hestia_earth.models.utils.term import get_electricity_grid_mix_terms
10
11
  from hestia_earth.models.data.ecoinventV3 import ecoinventV3_emissions
11
12
  from .utils import get_input_coefficient
12
- from ..ecoinventV3.utils import get_background_inputs, get_input_mappings
13
+ from ..ecoinventV3.utils import get_input_mappings
13
14
 
14
15
  REQUIREMENTS = {
15
16
  "Cycle": {
@@ -101,6 +102,7 @@ def _add_emission(cycle: dict, input: dict):
101
102
 
102
103
  def _run_input(cycle: dict):
103
104
  electricity_complete = _is_term_type_complete(cycle, 'electricityFuel')
105
+ no_gap_filled_background_emissions_func = no_gap_filled_background_emissions(cycle)
104
106
 
105
107
  def run(inputs: list):
106
108
  input = inputs[0]
@@ -109,12 +111,16 @@ def _run_input(cycle: dict):
109
111
  mappings = get_input_mappings(MODEL, cycle, input)
110
112
  has_mappings = len(mappings) > 0
111
113
 
114
+ # skip input that has background emissions we have already gap-filled (model run before)
115
+ has_no_gap_filled_background_emissions = no_gap_filled_background_emissions_func(input)
116
+
112
117
  logRequirements(cycle, model=MODEL, term=input_term_id,
113
118
  has_ecoinvent_mappings=has_mappings,
114
119
  ecoinvent_mappings=';'.join([v[0] for v in mappings]),
120
+ has_no_gap_filled_background_emissions=has_no_gap_filled_background_emissions,
115
121
  input_value=input_value)
116
122
 
117
- should_run = all([electricity_complete, has_mappings, input_value])
123
+ should_run = all([electricity_complete, has_mappings, has_no_gap_filled_background_emissions, input_value])
118
124
  logShouldRun(cycle, MODEL, input_term_id, should_run, methodTier=TIER)
119
125
 
120
126
  grouped_emissions = reduce(_add_emission(cycle, input), mappings, {}) if should_run else {}
@@ -1,10 +1,11 @@
1
- from hestia_earth.schema import NodeType, EmissionMethodTier
1
+ from hestia_earth.schema import NodeType, EmissionMethodTier, TermTermType
2
+ from hestia_earth.utils.lookup import download_lookup, lookup_term_ids, lookup_columns, column_name
2
3
  from hestia_earth.utils.lookup_utils import is_in_system_boundary
4
+ from hestia_earth.utils.tools import flatten
3
5
 
4
6
  from hestia_earth.models.log import logRequirements, logShouldRun
5
7
  from hestia_earth.models.utils.emission import _new_emission
6
- from hestia_earth.models.utils.blank_node import _run_required
7
- from hestia_earth.models.utils.term import get_all_emission_terms
8
+ from hestia_earth.models.utils.blank_node import _run_required, _run_model_required
8
9
 
9
10
  REQUIREMENTS = {
10
11
  "Cycle": {
@@ -18,7 +19,17 @@ RETURNS = {
18
19
  }]
19
20
  }
20
21
  LOOKUPS = {
21
- "emission": "inHestiaDefaultSystemBoundary"
22
+ "emission": [
23
+ "term.id",
24
+ "inHestiaDefaultSystemBoundary",
25
+ "inputTermTypesAllowed",
26
+ "productTermIdsAllowed",
27
+ "productTermTypesAllowed",
28
+ "siteTypesAllowed",
29
+ "typesAllowed"
30
+ ],
31
+ "emission-model-productTermIdsAllowed": "",
32
+ "emission-model-siteTypesAllowed": ""
22
33
  }
23
34
  MODEL = 'emissionNotRelevant'
24
35
  TIER = EmissionMethodTier.NOT_RELEVANT.value
@@ -31,9 +42,22 @@ def _emission(term_id: str):
31
42
  return emission
32
43
 
33
44
 
34
- def _should_run_emission(cycle: dict):
45
+ def _emission_ids():
46
+ return lookup_term_ids(download_lookup(f"{TermTermType.EMISSION.value}.csv"))
47
+
48
+
49
+ def _model_ids(lookup_suffix: str):
50
+ return [
51
+ col for col in lookup_columns(download_lookup(f"emission-model-{lookup_suffix}.csv"))
52
+ if col != column_name('term.id')
53
+ ]
54
+
55
+
56
+ def _should_run_emission(cycle: dict, model_ids: list):
35
57
  def run(term_id: str):
36
- is_not_relevant = not _run_required(MODEL, term_id, cycle)
58
+ is_not_relevant = not _run_required(MODEL, term_id, cycle) or any([
59
+ not _run_model_required(model_id, term_id, cycle, skip_logs=True) for model_id in model_ids
60
+ ])
37
61
  in_system_boundary = is_in_system_boundary(term_id)
38
62
 
39
63
  should_run = all([is_not_relevant, in_system_boundary])
@@ -49,8 +73,9 @@ def _should_run_emission(cycle: dict):
49
73
 
50
74
 
51
75
  def _run(cycle: dict):
52
- emissions = get_all_emission_terms()
53
- term_ids = list(filter(_should_run_emission(cycle), emissions))
76
+ emissions = _emission_ids()
77
+ model_ids = list(set(flatten(map(_model_ids, ['productTermIdsAllowed', 'siteTypesAllowed']))))
78
+ term_ids = list(filter(_should_run_emission(cycle, model_ids), emissions))
54
79
  return list(map(_emission, term_ids))
55
80
 
56
81
 
@@ -33,6 +33,10 @@ TOTAL_TERM_ID = 'aboveGroundCropResidueTotal'
33
33
  REMAINING_MODEL = PRODUCT_ID_TO_PRACTICES_ID[-1]['product']
34
34
 
35
35
 
36
+ def _product(term_id: str, value: float):
37
+ return _new_product(term_id, round(value, 7), MODEL)
38
+
39
+
36
40
  def _get_practices(term_id: str):
37
41
  return flatten([
38
42
  model.get('practices', []) for model in PRODUCT_ID_TO_PRACTICES_ID if all([
@@ -50,9 +54,6 @@ def _get_practice_value(term_ids: list, cycle: dict) -> float:
50
54
  return list_sum(values) / 100 if len(values) > 0 else None
51
55
 
52
56
 
53
- def _product(term_id: str, value: float): return _new_product(term_id, round(value, 7))
54
-
55
-
56
57
  def _should_run_model(model, cycle: dict, total_value: float):
57
58
  term_id = model.get('product')
58
59
  practice_value = _get_practice_value(model.get('practices'), cycle)
@@ -22,7 +22,7 @@ TERM_ID = 'aboveGroundCropResidueTotal'
22
22
 
23
23
 
24
24
  def _product(value: float):
25
- product = _new_product(TERM_ID, value)
25
+ product = _new_product(TERM_ID, value, MODEL)
26
26
  return product
27
27
 
28
28
 
@@ -22,7 +22,7 @@ TERM_ID = 'brackishWater'
22
22
 
23
23
 
24
24
  def _measurement():
25
- data = _new_measurement(TERM_ID)
25
+ data = _new_measurement(TERM_ID, MODEL)
26
26
  data['value'] = [True]
27
27
  data['methodClassification'] = MeasurementMethodClassification.MODELLED_USING_OTHER_MEASUREMENTS.value
28
28
  return data
@@ -30,7 +30,7 @@ BIBLIO_TITLE = 'Contribution of Organic Matter and Clay to Soil Cation-Exchange
30
30
 
31
31
 
32
32
  def _measurement(site: dict, value: float, depthUpper: int = None, depthLower: int = None):
33
- data = _new_measurement(TERM_ID)
33
+ data = _new_measurement(TERM_ID, MODEL)
34
34
  data['value'] = [value]
35
35
  if depthUpper is not None:
36
36
  data['depthUpper'] = depthUpper
@@ -36,7 +36,7 @@ TERM_ID = 'coldCarcassWeightPerHead'
36
36
 
37
37
 
38
38
  def _property(value: float):
39
- prop = _new_property(TERM_ID)
39
+ prop = _new_property(TERM_ID, MODEL)
40
40
  prop['value'] = value
41
41
  return prop
42
42
 
@@ -40,7 +40,7 @@ TERM_ID = 'coldDressedCarcassWeightPerHead'
40
40
 
41
41
 
42
42
  def _property(value: float):
43
- prop = _new_property(TERM_ID)
43
+ prop = _new_property(TERM_ID, MODEL)
44
44
  prop['value'] = value
45
45
  return prop
46
46
 
@@ -45,7 +45,7 @@ INPUT_TERM_TYPES = [
45
45
 
46
46
 
47
47
  def _property(term_id: str, value: float):
48
- prop = _new_property(term_id)
48
+ prop = _new_property(term_id, MODEL)
49
49
  prop['value'] = value
50
50
  return prop
51
51
 
@@ -31,7 +31,7 @@ PRACTICE_IDS = [
31
31
 
32
32
 
33
33
  def _practice(term_id: str):
34
- practice = _new_practice(term_id)
34
+ practice = _new_practice(term_id, MODEL)
35
35
  practice['value'] = [0]
36
36
  return practice
37
37
 
@@ -24,7 +24,7 @@ TERM_ID = 'croppingIntensity'
24
24
 
25
25
 
26
26
  def _practice(value: float):
27
- practice = _new_practice(TERM_ID)
27
+ practice = _new_practice(TERM_ID, MODEL)
28
28
  practice['value'] = [round(value, 7)]
29
29
  return practice
30
30
 
@@ -28,7 +28,7 @@ DRY_VALUE = 19.2 # Bone dry wood has an energy content of 19.2 MJ/kg
28
28
 
29
29
 
30
30
  def _property(value: float):
31
- prop = _new_property(TERM_ID)
31
+ prop = _new_property(TERM_ID, MODEL)
32
32
  prop['value'] = value
33
33
  return prop
34
34