hestia-earth-models 0.70.5__py3-none-any.whl → 0.70.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.
@@ -118,6 +118,7 @@ def _run_input(cycle: dict):
118
118
  has_ecoinvent_mappings=has_mappings,
119
119
  ecoinvent_mappings=';'.join([v[0] for v in mappings]),
120
120
  has_no_gap_filled_background_emissions=has_no_gap_filled_background_emissions,
121
+ termType_electricityFuel_complete=electricity_complete,
121
122
  input_value=input_value)
122
123
 
123
124
  should_run = all([electricity_complete, has_mappings, has_no_gap_filled_background_emissions, input_value])
@@ -1,8 +1,8 @@
1
1
  from hestia_earth.schema import EmissionMethodTier
2
- from hestia_earth.utils.tools import list_sum
3
2
 
4
- from hestia_earth.models.log import logRequirements, logShouldRun
5
- from .utils import get_fuel_values, _emission
3
+ from hestia_earth.models.log import logRequirements, logShouldRun, log_as_table
4
+ from hestia_earth.models.utils.completeness import _is_term_type_complete
5
+ from .utils import get_fuel_inputs, group_fuel_inputs, _emission, _run_inputs
6
6
  from . import MODEL
7
7
 
8
8
  REQUIREMENTS = {
@@ -20,6 +20,8 @@ REQUIREMENTS = {
20
20
  RETURNS = {
21
21
  "Emission": [{
22
22
  "value": "",
23
+ "inputs": "",
24
+ "operation": "",
23
25
  "methodTier": "tier 1"
24
26
  }]
25
27
  }
@@ -31,23 +33,26 @@ TERM_ID = 'co2ToAirFuelCombustion'
31
33
  TIER = EmissionMethodTier.TIER_1.value
32
34
 
33
35
 
34
- def _run(fuel_values: list):
35
- value = list_sum(fuel_values)
36
- return [_emission(value=value, tier=TIER, term_id=TERM_ID)]
37
-
38
-
39
36
  def _should_run(cycle: dict):
40
- fuel_values = get_fuel_values(TERM_ID, cycle, LOOKUPS['fuel'])
41
- has_fuel = len(fuel_values) > 0
37
+ electricity_complete = _is_term_type_complete(cycle, 'electricityFuel')
38
+ fuel_inputs, valid_inputs = get_fuel_inputs(TERM_ID, cycle, LOOKUPS['fuel'])
42
39
 
43
40
  logRequirements(cycle, model=MODEL, term=TERM_ID,
44
- has_fuel=has_fuel)
41
+ termType_electricityFuel_complete=electricity_complete,
42
+ fuel_inputs=log_as_table(fuel_inputs))
45
43
 
46
- should_run = any([has_fuel])
44
+ should_run = any([bool(valid_inputs), electricity_complete])
47
45
  logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
48
- return should_run, fuel_values
46
+ return should_run, group_fuel_inputs(valid_inputs)
49
47
 
50
48
 
51
49
  def run(cycle: dict):
52
- should_run, fuel_values = _should_run(cycle)
53
- return _run(fuel_values) if should_run else []
50
+ should_run, fuel_inputs = _should_run(cycle)
51
+ return (
52
+ [
53
+ _run_inputs(inputs, tier=TIER, term_id=TERM_ID)
54
+ for inputs in fuel_inputs.values()
55
+ ] if fuel_inputs else [
56
+ _emission(value=0, tier=TIER, term_id=TERM_ID)
57
+ ]
58
+ ) if should_run else []
@@ -1,8 +1,8 @@
1
1
  from hestia_earth.schema import EmissionMethodTier
2
- from hestia_earth.utils.tools import list_sum
3
2
 
4
- from hestia_earth.models.log import logRequirements, logShouldRun
5
- from .utils import get_fuel_values, _emission
3
+ from hestia_earth.models.log import logRequirements, logShouldRun, log_as_table
4
+ from hestia_earth.models.utils.completeness import _is_term_type_complete
5
+ from .utils import get_fuel_inputs, group_fuel_inputs, _emission, _run_inputs
6
6
  from . import MODEL
7
7
 
8
8
  REQUIREMENTS = {
@@ -10,7 +10,7 @@ REQUIREMENTS = {
10
10
  "or": {
11
11
  "inputs": [
12
12
  {"@type": "Input", "value": "", "term.termType": "fuel", "optional": {
13
- "operation": ""
13
+ "operation": ""
14
14
  }}
15
15
  ],
16
16
  "completeness.electricityFuel": "True"
@@ -20,6 +20,8 @@ REQUIREMENTS = {
20
20
  RETURNS = {
21
21
  "Emission": [{
22
22
  "value": "",
23
+ "inputs": "",
24
+ "operation": "",
23
25
  "methodTier": "tier 1"
24
26
  }]
25
27
  }
@@ -31,23 +33,26 @@ TERM_ID = 'n2OToAirFuelCombustionDirect'
31
33
  TIER = EmissionMethodTier.TIER_1.value
32
34
 
33
35
 
34
- def _run(fuel_values: list):
35
- value = list_sum(fuel_values)
36
- return [_emission(value=value, tier=TIER, term_id=TERM_ID)]
37
-
38
-
39
36
  def _should_run(cycle: dict):
40
- fuel_values = get_fuel_values(TERM_ID, cycle, LOOKUPS['fuel'])
41
- has_fuel = len(fuel_values) > 0
37
+ electricity_complete = _is_term_type_complete(cycle, 'electricityFuel')
38
+ fuel_inputs, valid_inputs = get_fuel_inputs(TERM_ID, cycle, LOOKUPS['fuel'])
42
39
 
43
40
  logRequirements(cycle, model=MODEL, term=TERM_ID,
44
- has_fuel=has_fuel)
41
+ termType_electricityFuel_complete=electricity_complete,
42
+ fuel_inputs=log_as_table(fuel_inputs))
45
43
 
46
- should_run = any([has_fuel])
44
+ should_run = any([bool(valid_inputs), electricity_complete])
47
45
  logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
48
- return should_run, fuel_values
46
+ return should_run, group_fuel_inputs(valid_inputs)
49
47
 
50
48
 
51
49
  def run(cycle: dict):
52
- should_run, fuel_values = _should_run(cycle)
53
- return _run(fuel_values) if should_run else []
50
+ should_run, fuel_inputs = _should_run(cycle)
51
+ return (
52
+ [
53
+ _run_inputs(inputs, tier=TIER, term_id=TERM_ID)
54
+ for inputs in fuel_inputs.values()
55
+ ] if fuel_inputs else [
56
+ _emission(value=0, tier=TIER, term_id=TERM_ID)
57
+ ]
58
+ ) if should_run else []
@@ -1,8 +1,8 @@
1
1
  from hestia_earth.schema import EmissionMethodTier
2
- from hestia_earth.utils.tools import list_sum
3
2
 
4
- from hestia_earth.models.log import logRequirements, logShouldRun
5
- from .utils import get_fuel_values, _emission
3
+ from hestia_earth.models.log import logRequirements, logShouldRun, log_as_table
4
+ from hestia_earth.models.utils.completeness import _is_term_type_complete
5
+ from .utils import get_fuel_inputs, group_fuel_inputs, _emission, _run_inputs
6
6
  from . import MODEL
7
7
 
8
8
  REQUIREMENTS = {
@@ -20,6 +20,8 @@ REQUIREMENTS = {
20
20
  RETURNS = {
21
21
  "Emission": [{
22
22
  "value": "",
23
+ "inputs": "",
24
+ "operation": "",
23
25
  "methodTier": "tier 1"
24
26
  }]
25
27
  }
@@ -31,23 +33,26 @@ TERM_ID = 'noxToAirFuelCombustion'
31
33
  TIER = EmissionMethodTier.TIER_1.value
32
34
 
33
35
 
34
- def _run(fuel_values: list):
35
- value = list_sum(fuel_values)
36
- return [_emission(value=value, tier=TIER, term_id=TERM_ID)]
37
-
38
-
39
36
  def _should_run(cycle: dict):
40
- fuel_values = get_fuel_values(TERM_ID, cycle, LOOKUPS['fuel'])
41
- has_fuel = len(fuel_values) > 0
37
+ electricity_complete = _is_term_type_complete(cycle, 'electricityFuel')
38
+ fuel_inputs, valid_inputs = get_fuel_inputs(TERM_ID, cycle, LOOKUPS['fuel'])
42
39
 
43
40
  logRequirements(cycle, model=MODEL, term=TERM_ID,
44
- has_fuel=has_fuel)
41
+ termType_electricityFuel_complete=electricity_complete,
42
+ fuel_inputs=log_as_table(fuel_inputs))
45
43
 
46
- should_run = any([has_fuel])
44
+ should_run = any([bool(valid_inputs), electricity_complete])
47
45
  logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
48
- return should_run, fuel_values
46
+ return should_run, group_fuel_inputs(valid_inputs)
49
47
 
50
48
 
51
49
  def run(cycle: dict):
52
- should_run, fuel_values = _should_run(cycle)
53
- return _run(fuel_values) if should_run else []
50
+ should_run, fuel_inputs = _should_run(cycle)
51
+ return (
52
+ [
53
+ _run_inputs(inputs, tier=TIER, term_id=TERM_ID)
54
+ for inputs in fuel_inputs.values()
55
+ ] if fuel_inputs else [
56
+ _emission(value=0, tier=TIER, term_id=TERM_ID)
57
+ ]
58
+ ) if should_run else []
@@ -1,8 +1,8 @@
1
1
  from hestia_earth.schema import EmissionMethodTier
2
- from hestia_earth.utils.tools import list_sum
3
2
 
4
- from hestia_earth.models.log import logRequirements, logShouldRun
5
- from .utils import get_fuel_values, _emission
3
+ from hestia_earth.models.log import logRequirements, logShouldRun, log_as_table
4
+ from hestia_earth.models.utils.completeness import _is_term_type_complete
5
+ from .utils import get_fuel_inputs, group_fuel_inputs, _emission, _run_inputs
6
6
  from . import MODEL
7
7
 
8
8
  REQUIREMENTS = {
@@ -20,6 +20,8 @@ REQUIREMENTS = {
20
20
  RETURNS = {
21
21
  "Emission": [{
22
22
  "value": "",
23
+ "inputs": "",
24
+ "operation": "",
23
25
  "methodTier": "tier 1"
24
26
  }]
25
27
  }
@@ -31,23 +33,26 @@ TERM_ID = 'so2ToAirFuelCombustion'
31
33
  TIER = EmissionMethodTier.TIER_1.value
32
34
 
33
35
 
34
- def _run(fuel_values: list):
35
- value = list_sum(fuel_values)
36
- return [_emission(value=value, tier=TIER, term_id=TERM_ID)]
37
-
38
-
39
36
  def _should_run(cycle: dict):
40
- fuel_values = get_fuel_values(TERM_ID, cycle, LOOKUPS['fuel'])
41
- has_fuel = len(fuel_values) > 0
37
+ electricity_complete = _is_term_type_complete(cycle, 'electricityFuel')
38
+ fuel_inputs, valid_inputs = get_fuel_inputs(TERM_ID, cycle, LOOKUPS['fuel'])
42
39
 
43
40
  logRequirements(cycle, model=MODEL, term=TERM_ID,
44
- has_fuel=has_fuel)
41
+ termType_electricityFuel_complete=electricity_complete,
42
+ fuel_inputs=log_as_table(fuel_inputs))
45
43
 
46
- should_run = any([has_fuel])
44
+ should_run = any([bool(valid_inputs), electricity_complete])
47
45
  logShouldRun(cycle, MODEL, TERM_ID, should_run, methodTier=TIER)
48
- return should_run, fuel_values
46
+ return should_run, group_fuel_inputs(valid_inputs)
49
47
 
50
48
 
51
49
  def run(cycle: dict):
52
- should_run, fuel_values = _should_run(cycle)
53
- return _run(fuel_values) if should_run else []
50
+ should_run, fuel_inputs = _should_run(cycle)
51
+ return (
52
+ [
53
+ _run_inputs(inputs, tier=TIER, term_id=TERM_ID)
54
+ for inputs in fuel_inputs.values()
55
+ ] if fuel_inputs else [
56
+ _emission(value=0, tier=TIER, term_id=TERM_ID)
57
+ ]
58
+ ) if should_run else []
@@ -1,48 +1,96 @@
1
+ from functools import reduce
1
2
  from hestia_earth.schema import TermTermType, SiteSiteType
2
3
  from hestia_earth.utils.model import filter_list_term_type
3
4
  from hestia_earth.utils.lookup import extract_grouped_data
4
- from hestia_earth.utils.tools import list_sum, safe_parse_float, non_empty_list
5
+ from hestia_earth.utils.tools import list_sum, safe_parse_float
5
6
 
6
7
  from hestia_earth.models.log import logRequirements, logShouldRun
7
8
  from hestia_earth.models.utils.completeness import _is_term_type_complete
9
+ from hestia_earth.models.utils.blank_node import group_by_keys
8
10
  from hestia_earth.models.utils.term import get_lookup_value
9
11
  from hestia_earth.models.utils.cycle import get_animals_by_period
10
12
  from hestia_earth.models.utils.emission import _new_emission
11
13
  from . import MODEL
12
14
 
13
15
 
14
- def _emission(value: float, tier: str, term_id: str):
16
+ def _emission(value: float, tier: str, term_id: str, input: dict = None, operation: dict = None):
15
17
  emission = _new_emission(term_id, MODEL)
16
18
  emission['value'] = [value]
17
19
  emission['methodTier'] = tier
20
+ if input:
21
+ emission['inputs'] = [input]
22
+ if operation:
23
+ emission['operation'] = operation
18
24
  return emission
19
25
 
20
26
 
21
- def _get_fuel_input_value(term_id: str, lookup_col: str):
22
- def get_value(input: dict):
23
- input_term = input.get('term', {})
24
- input_term_id = input_term.get('@id')
25
- operation_term = input.get('operation', {})
26
- input_value = list_sum(input.get('value', []))
27
-
28
- operation_factor = extract_grouped_data(
29
- get_lookup_value(operation_term, lookup_col, model=MODEL, term=term_id), input_term_id
30
- ) if operation_term else None
31
- input_factor = operation_factor or get_lookup_value(input_term, lookup_col, model=MODEL, term=term_id)
32
- factor = safe_parse_float(input_factor, None)
33
-
34
- return input_value * factor if factor is not None else None
35
- return get_value
36
-
27
+ def _run_inputs(inputs: list, tier: str, term_id: str):
28
+ total_value = list_sum([
29
+ (i.get('input-value') or 0) * (i.get('operation-factor') or i.get('input-default-factor') or 0)
30
+ for i in inputs
31
+ ])
32
+ input_term = {
33
+ '@type': 'Term',
34
+ '@id': inputs[0].get('input-id'),
35
+ 'termType': inputs[0].get('input-termType'),
36
+ 'units': inputs[0].get('input-units'),
37
+ }
38
+ operation_term = {
39
+ '@type': 'Term',
40
+ '@id': inputs[0].get('operation-id'),
41
+ 'termType': inputs[0].get('operation-termType'),
42
+ 'units': inputs[0].get('operation-units'),
43
+ } if inputs[0].get('operation-id') else None
44
+ return _emission(
45
+ value=total_value,
46
+ tier=tier,
47
+ term_id=term_id,
48
+ input=input_term,
49
+ operation=operation_term
50
+ )
37
51
 
38
- def get_fuel_values(term_id: str, cycle: dict, lookup_col: str):
39
- inputs = filter_list_term_type(cycle.get('inputs', []), TermTermType.FUEL)
40
- values = non_empty_list(map(_get_fuel_input_value(term_id, lookup_col), inputs))
41
52
 
42
- return [0] if all([
43
- len(values) == 0,
44
- _is_term_type_complete(cycle, 'electricityFuel')
45
- ]) else values
53
+ def _fuel_input_data(term_id: str, lookup_col: str, input: dict):
54
+ input_term = input.get('term', {})
55
+ input_term_id = input_term.get('@id')
56
+ operation_term = input.get('operation', {})
57
+ input_value = list_sum(input.get('value', []), None)
58
+
59
+ operation_factor = extract_grouped_data(
60
+ data=get_lookup_value(operation_term, lookup_col, model=MODEL, term=term_id),
61
+ key=input_term_id
62
+ ) if operation_term else None
63
+ input_factor = get_lookup_value(input_term, lookup_col, model=MODEL, term=term_id)
64
+
65
+ return {
66
+ 'input-id': input_term_id,
67
+ 'input-termType': input_term.get('termType'),
68
+ 'input-units': input_term.get('units'),
69
+ 'input-value': input_value,
70
+ 'input-default-factor': safe_parse_float(input_factor, default=None),
71
+ 'operation-id': operation_term.get('@id'),
72
+ 'operation-termType': operation_term.get('termType'),
73
+ 'operation-units': operation_term.get('units'),
74
+ 'operation-factor': safe_parse_float(operation_factor, default=None)
75
+ }
76
+
77
+
78
+ def get_fuel_inputs(term_id: str, cycle: dict, lookup_col: str):
79
+ inputs = [
80
+ _fuel_input_data(term_id, lookup_col, i)
81
+ for i in filter_list_term_type(cycle.get('inputs', []), TermTermType.FUEL)
82
+ ]
83
+ valid_inputs = [
84
+ i for i in inputs if all([
85
+ i.get('input-value') is not None,
86
+ (i.get('operation-factor') or i.get('input-default-factor')) is not None
87
+ ])
88
+ ]
89
+ return inputs, valid_inputs
90
+
91
+
92
+ def group_fuel_inputs(inputs: list):
93
+ return reduce(group_by_keys(['input-id', 'operation-id']), inputs, {}) if len(inputs) > 0 else None
46
94
 
47
95
 
48
96
  def _get_emissions_factor(animal: dict, lookup_col: str) -> float:
@@ -135,9 +135,9 @@ def management(data: dict):
135
135
  return node
136
136
 
137
137
 
138
- def _get_cycle_duration(cycle: dict, land_cover_id: str):
138
+ def _get_cycle_duration(cycle: dict, land_cover_id: str = None):
139
139
  cycle_duration = cycle.get('cycleDuration')
140
- lookup_value = None if cycle_duration else safe_parse_float(get_table_value(
140
+ lookup_value = None if cycle_duration or not land_cover_id else safe_parse_float(get_table_value(
141
141
  download_lookup("crop.csv"),
142
142
  column_name('landCoverTermId'),
143
143
  land_cover_id,
@@ -157,7 +157,7 @@ def _gap_filled_date_obj(date_str: str, mode: str = DatestrGapfillMode.END) -> d
157
157
  )
158
158
 
159
159
 
160
- def _gap_filled_start_date(land_cover_id: str, end_date: str, cycle: dict) -> dict:
160
+ def _gap_filled_start_date(cycle: dict, end_date: str, land_cover_id: str = None) -> dict:
161
161
  """If possible, gap-fill the startDate based on the endDate - cycleDuration"""
162
162
  cycle_duration = _get_cycle_duration(cycle, land_cover_id)
163
163
  return {
@@ -216,9 +216,9 @@ def _get_relevant_items(cycle: dict, item_name: str, term_types: List[TermTermTy
216
216
  _include_with_date_gap_fill(cycle, ["startDate", "endDate"]) |
217
217
  _include(
218
218
  _gap_filled_start_date(
219
- land_cover_id=get_landCover_term_id(item.get('term', {})),
219
+ cycle=cycle,
220
220
  end_date=item.get("endDate") if "endDate" in item else cycle.get("endDate", ""),
221
- cycle=cycle
221
+ land_cover_id=get_landCover_term_id(item.get('term', {})),
222
222
  ) if "startDate" not in item else {},
223
223
  "startDate"
224
224
  ) |
@@ -255,12 +255,17 @@ def _run_from_siteType(site: dict, cycle: dict):
255
255
  site_type = site.get('siteType')
256
256
  site_type_id = get_landCover_term_id_from_site_type(site_type) if site_type not in _SKIP_LAND_COVER_SITE_TYPES \
257
257
  else None
258
+ start_date = cycle.get('startDate') or _gap_filled_start_date(
259
+ cycle=cycle,
260
+ end_date=cycle.get('endDate'),
261
+ land_cover_id=site_type_id
262
+ ).get('startDate')
258
263
 
259
- should_run = all([site_type_id])
264
+ should_run = all([site_type_id, start_date])
260
265
  return [{
261
266
  'id': site_type_id,
262
267
  'value': 100,
263
- 'startDate': cycle.get('startDate'),
268
+ 'startDate': start_date,
264
269
  'endDate': cycle.get('endDate')
265
270
  }] if should_run else []
266
271