hestia-earth-models 0.61.8__py3-none-any.whl → 0.62.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.
Potentially problematic release.
This version of hestia-earth-models might be problematic. Click here for more details.
- hestia_earth/models/blonkConsultants2016/utils.py +3 -2
- hestia_earth/models/cycle/completeness/electricityFuel.py +5 -1
- hestia_earth/models/cycle/post_checks/__init__.py +3 -2
- hestia_earth/models/cycle/post_checks/otherSites.py +40 -0
- hestia_earth/models/cycle/pre_checks/__init__.py +2 -1
- hestia_earth/models/cycle/pre_checks/otherSites.py +42 -0
- hestia_earth/models/cycle/pre_checks/site.py +1 -1
- hestia_earth/models/cycle/product/economicValueShare.py +47 -31
- hestia_earth/models/ecoinventV3AndEmberClimate/utils.py +1 -1
- hestia_earth/models/emepEea2019/utils.py +4 -3
- hestia_earth/models/geospatialDatabase/heavyWinterPrecipitation.py +1 -1
- hestia_earth/models/ipcc2019/animal/pastureGrass.py +36 -30
- hestia_earth/models/ipcc2019/co2ToAirSoilOrganicCarbonStockChangeManagementChange.py +30 -4
- hestia_earth/models/ipcc2019/n2OToAirExcretaDirect.py +6 -2
- hestia_earth/models/ipcc2019/n2OToAirExcretaIndirect.py +1 -1
- hestia_earth/models/ipcc2019/n2OToAirInorganicFertiliserDirect.py +1 -1
- hestia_earth/models/ipcc2019/n2OToAirInorganicFertiliserIndirect.py +1 -1
- hestia_earth/models/ipcc2019/n2OToAirOrganicFertiliserIndirect.py +1 -1
- hestia_earth/models/ipcc2019/organicCarbonPerHa.py +4 -6
- hestia_earth/models/ipcc2019/organicCarbonPerHa_tier_1_utils.py +4 -2
- hestia_earth/models/ipcc2019/organicCarbonPerHa_tier_2_utils.py +12 -11
- hestia_earth/models/ipcc2019/pastureGrass.py +40 -22
- hestia_earth/models/ipcc2019/pastureGrass_utils.py +43 -60
- hestia_earth/models/mocking/search-results.json +261 -257
- hestia_earth/models/schererPfister2015/utils.py +2 -2
- hestia_earth/models/site/brackishWater.py +1 -1
- hestia_earth/models/site/flowingWater.py +1 -1
- hestia_earth/models/site/freshWater.py +1 -1
- hestia_earth/models/site/management.py +29 -11
- hestia_earth/models/site/pre_checks/cache_sources.py +9 -13
- hestia_earth/models/site/salineWater.py +1 -1
- hestia_earth/models/stehfestBouwman2006/n2OToAirCropResidueDecompositionDirect.py +12 -2
- hestia_earth/models/stehfestBouwman2006/n2OToAirExcretaDirect.py +12 -2
- hestia_earth/models/stehfestBouwman2006/n2OToAirInorganicFertiliserDirect.py +11 -1
- hestia_earth/models/stehfestBouwman2006/n2OToAirOrganicFertiliserDirect.py +11 -1
- hestia_earth/models/stehfestBouwman2006/noxToAirCropResidueDecomposition.py +12 -2
- hestia_earth/models/stehfestBouwman2006/noxToAirExcreta.py +12 -2
- hestia_earth/models/stehfestBouwman2006/noxToAirInorganicFertiliser.py +11 -1
- hestia_earth/models/stehfestBouwman2006/noxToAirOrganicFertiliser.py +11 -1
- hestia_earth/models/stehfestBouwman2006GisImplementation/noxToAirCropResidueDecomposition.py +12 -2
- hestia_earth/models/stehfestBouwman2006GisImplementation/noxToAirExcreta.py +12 -2
- hestia_earth/models/stehfestBouwman2006GisImplementation/noxToAirInorganicFertiliser.py +11 -1
- hestia_earth/models/stehfestBouwman2006GisImplementation/noxToAirOrganicFertiliser.py +11 -1
- hestia_earth/models/utils/blank_node.py +159 -146
- hestia_earth/models/utils/constant.py +2 -0
- hestia_earth/models/utils/lookup.py +19 -6
- hestia_earth/models/utils/source.py +1 -1
- hestia_earth/models/version.py +1 -1
- {hestia_earth_models-0.61.8.dist-info → hestia_earth_models-0.62.1.dist-info}/METADATA +3 -3
- {hestia_earth_models-0.61.8.dist-info → hestia_earth_models-0.62.1.dist-info}/RECORD +64 -59
- tests/models/cycle/post_checks/test_otherSites.py +15 -0
- tests/models/cycle/pre_checks/test_otherSites.py +21 -0
- tests/models/cycle/product/test_economicValueShare.py +8 -0
- tests/models/ipcc2019/animal/test_pastureGrass.py +2 -2
- tests/models/ipcc2019/test_co2ToAirSoilOrganicCarbonStockChangeManagementChange.py +5 -2
- tests/models/ipcc2019/test_organicCarbonPerHa.py +2 -1
- tests/models/ipcc2019/test_pastureGrass.py +0 -16
- tests/models/site/pre_checks/test_cache_sources.py +6 -10
- tests/models/site/test_management.py +162 -2
- tests/models/utils/test_blank_node.py +154 -296
- tests/models/utils/test_lookup.py +10 -0
- {hestia_earth_models-0.61.8.dist-info → hestia_earth_models-0.62.1.dist-info}/LICENSE +0 -0
- {hestia_earth_models-0.61.8.dist-info → hestia_earth_models-0.62.1.dist-info}/WHEEL +0 -0
- {hestia_earth_models-0.61.8.dist-info → hestia_earth_models-0.62.1.dist-info}/top_level.txt +0 -0
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
import calendar
|
|
1
2
|
from calendar import monthrange
|
|
2
3
|
from collections import defaultdict
|
|
3
4
|
from collections.abc import Iterable
|
|
4
|
-
from datetime import datetime
|
|
5
|
+
from datetime import datetime, timedelta
|
|
6
|
+
from uuid import uuid4
|
|
7
|
+
|
|
5
8
|
from dateutil.relativedelta import relativedelta
|
|
6
9
|
from enum import Enum
|
|
7
10
|
from functools import reduce
|
|
8
|
-
from statistics import mode, mean
|
|
9
11
|
from typing import (
|
|
10
12
|
Any,
|
|
11
13
|
List,
|
|
@@ -24,9 +26,10 @@ from hestia_earth.utils.tools import (
|
|
|
24
26
|
safe_parse_float,
|
|
25
27
|
non_empty_list
|
|
26
28
|
)
|
|
29
|
+
from hestia_earth.utils.blank_node import ArrayTreatment, get_node_value
|
|
27
30
|
|
|
28
31
|
from ..log import debugValues, log_as_table
|
|
29
|
-
from . import is_from_model, _filter_list_term_unit, is_iterable
|
|
32
|
+
from . import is_from_model, _filter_list_term_unit, is_iterable, _omit
|
|
30
33
|
from .constant import Units
|
|
31
34
|
from .property import get_node_property, get_node_property_value
|
|
32
35
|
from .lookup import (
|
|
@@ -46,7 +49,9 @@ def group_by_keys(group_keys: list = ['term']):
|
|
|
46
49
|
return run
|
|
47
50
|
|
|
48
51
|
|
|
49
|
-
def _module_term_id(term_id: str, module):
|
|
52
|
+
def _module_term_id(term_id: str, module):
|
|
53
|
+
term_id_str = term_id.split('.')[-1] if '.' in term_id else term_id
|
|
54
|
+
return getattr(module, 'TERM_ID', term_id_str).split(',')[0]
|
|
50
55
|
|
|
51
56
|
|
|
52
57
|
def _run_model_required(model: str, term: dict, data: dict):
|
|
@@ -342,111 +347,6 @@ def convert_to_carbon(node: dict, model: str, term_id: str, blank_nodes: list, *
|
|
|
342
347
|
]) if len(missing_carbon_property) == 0 else None
|
|
343
348
|
|
|
344
349
|
|
|
345
|
-
class ArrayTreatment(Enum):
|
|
346
|
-
"""
|
|
347
|
-
Enum representing different treatments for arrays of values.
|
|
348
|
-
"""
|
|
349
|
-
MEAN = 'mean'
|
|
350
|
-
MODE = 'mode'
|
|
351
|
-
SUM = 'sum'
|
|
352
|
-
FIRST = 'first'
|
|
353
|
-
LAST = 'last'
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
def _should_run_array_treatment(value):
|
|
357
|
-
return isinstance(value, Iterable) and len(value) > 0
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
DEFAULT_ARRAY_TREATMENT = ArrayTreatment.MEAN
|
|
361
|
-
ARRAY_TREATMENT_TO_REDUCER = {
|
|
362
|
-
ArrayTreatment.MEAN: lambda value: mean(value) if _should_run_array_treatment(value) else 0,
|
|
363
|
-
ArrayTreatment.MODE: lambda value: mode(value) if _should_run_array_treatment(value) else 0,
|
|
364
|
-
ArrayTreatment.SUM: lambda value: sum(value) if _should_run_array_treatment(value) else 0,
|
|
365
|
-
ArrayTreatment.FIRST: lambda value: value[0] if _should_run_array_treatment(value) else 0,
|
|
366
|
-
ArrayTreatment.LAST: lambda value: value[-1] if _should_run_array_treatment(value) else 0
|
|
367
|
-
}
|
|
368
|
-
"""
|
|
369
|
-
A dictionary mapping ArrayTreatment enums to corresponding reducer functions.
|
|
370
|
-
"""
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
def _retrieve_array_treatment(
|
|
374
|
-
node: dict,
|
|
375
|
-
is_larger_unit: bool = False,
|
|
376
|
-
default: ArrayTreatment = ArrayTreatment.FIRST
|
|
377
|
-
) -> ArrayTreatment:
|
|
378
|
-
"""
|
|
379
|
-
Retrieves the array treatment for a given node.
|
|
380
|
-
|
|
381
|
-
Array treatments are used to reduce an array's list of values into
|
|
382
|
-
a single value. The array treatment is retrieved from a lookup on
|
|
383
|
-
the node's term.
|
|
384
|
-
|
|
385
|
-
Parameters
|
|
386
|
-
----------
|
|
387
|
-
node : dict
|
|
388
|
-
The dictionary representing the node.
|
|
389
|
-
is_larger_unit : bool, optional
|
|
390
|
-
Flag indicating whether to use the larger unit lookup, by default `False`.
|
|
391
|
-
default : ArrayTreatment, optional
|
|
392
|
-
Default value to return if the lookup fails, by default `ArrayTreatment.FIRST`.
|
|
393
|
-
|
|
394
|
-
Returns
|
|
395
|
-
-------
|
|
396
|
-
ArrayTreatment
|
|
397
|
-
The retrieved array treatment.
|
|
398
|
-
|
|
399
|
-
"""
|
|
400
|
-
ARRAY_TREATMENT_LOOKUPS = [
|
|
401
|
-
'arrayTreatmentLargerUnitOfTime',
|
|
402
|
-
'arrayTreatment'
|
|
403
|
-
]
|
|
404
|
-
lookup = ARRAY_TREATMENT_LOOKUPS[0] if is_larger_unit else ARRAY_TREATMENT_LOOKUPS[1]
|
|
405
|
-
|
|
406
|
-
term = node.get('term', {})
|
|
407
|
-
lookup_value = get_lookup_value(term, lookup, skip_debug=True)
|
|
408
|
-
|
|
409
|
-
return next(
|
|
410
|
-
(treatment for treatment in ArrayTreatment if treatment.value == lookup_value),
|
|
411
|
-
default
|
|
412
|
-
)
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
def get_node_value(
|
|
416
|
-
node: dict,
|
|
417
|
-
is_larger_unit: bool = False,
|
|
418
|
-
array_treatment: Optional[ArrayTreatment] = None,
|
|
419
|
-
default: Any = 0
|
|
420
|
-
) -> Union[float, bool]:
|
|
421
|
-
"""
|
|
422
|
-
Get the value from the dictionary representing the node,
|
|
423
|
-
applying optional array treatment if the value is a list.
|
|
424
|
-
|
|
425
|
-
Parameters
|
|
426
|
-
----------
|
|
427
|
-
node : dict
|
|
428
|
-
The dictionary representing the node.
|
|
429
|
-
is_larger_unit : bool, optional
|
|
430
|
-
A flag indicating whether the unit of time is larger, by default `False`.
|
|
431
|
-
array_treatment : ArrayTreatment, optional
|
|
432
|
-
An optional override for the treatment to be applied to an array value, if `None` the array treatment in the
|
|
433
|
-
node's term's lookup is used (which defaults to `FIRST` if no array treatment is specified), by default `None`.
|
|
434
|
-
|
|
435
|
-
Returns
|
|
436
|
-
-------
|
|
437
|
-
float | bool
|
|
438
|
-
The extracted value from the node.
|
|
439
|
-
|
|
440
|
-
"""
|
|
441
|
-
value = node.get("value", 0)
|
|
442
|
-
|
|
443
|
-
reducer = ARRAY_TREATMENT_TO_REDUCER[(
|
|
444
|
-
array_treatment or _retrieve_array_treatment(node, is_larger_unit=is_larger_unit)
|
|
445
|
-
)] if isinstance(value, list) and len(value) > 0 else None
|
|
446
|
-
|
|
447
|
-
return reducer(value) if reducer else value if isinstance(value, bool) else value or default
|
|
448
|
-
|
|
449
|
-
|
|
450
350
|
def _convert_to_set(
|
|
451
351
|
variable: Union[Iterable[Any], Any]
|
|
452
352
|
) -> set:
|
|
@@ -560,7 +460,7 @@ def cumulative_nodes_match(
|
|
|
560
460
|
"""
|
|
561
461
|
values = [
|
|
562
462
|
get_node_value(
|
|
563
|
-
node, is_larger_unit, array_treatment
|
|
463
|
+
node, 'value', is_larger_unit, array_treatment
|
|
564
464
|
) or default_node_value for node in nodes if function(node)
|
|
565
465
|
]
|
|
566
466
|
|
|
@@ -995,33 +895,6 @@ def _validate_time_fraction_dict(
|
|
|
995
895
|
])
|
|
996
896
|
|
|
997
897
|
|
|
998
|
-
def _build_update_dict(node: dict, years: list, target_year: int) -> dict:
|
|
999
|
-
"""
|
|
1000
|
-
Build an update dictionary containing values and dates from a node that fall within a given year.
|
|
1001
|
-
|
|
1002
|
-
This is only required if when `group_nodes_by_year` `mode = GroupNodesByYearMode.DATES`
|
|
1003
|
-
|
|
1004
|
-
Parameters
|
|
1005
|
-
----------
|
|
1006
|
-
node : dict
|
|
1007
|
-
The node containing values and dates.
|
|
1008
|
-
year : int
|
|
1009
|
-
The year to be matched.
|
|
1010
|
-
|
|
1011
|
-
Returns
|
|
1012
|
-
-------
|
|
1013
|
-
dict
|
|
1014
|
-
An update dictionary containing "value" and "dates" keys.
|
|
1015
|
-
"""
|
|
1016
|
-
valid_indices = {
|
|
1017
|
-
i for i, y in enumerate(years) if y == target_year
|
|
1018
|
-
}
|
|
1019
|
-
return {
|
|
1020
|
-
"value": [node.get("value")[i] for i in valid_indices],
|
|
1021
|
-
"dates": [node.get("dates")[i] for i in valid_indices]
|
|
1022
|
-
}
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
898
|
def group_nodes_by_year(
|
|
1026
899
|
nodes: list[dict],
|
|
1027
900
|
default_node_duration: int = 1,
|
|
@@ -1057,7 +930,7 @@ def group_nodes_by_year(
|
|
|
1057
930
|
should_run_node = GROUP_NODES_BY_YEAR_MODE_TO_SHOULD_RUN_NODE_FUNCTION[mode]
|
|
1058
931
|
get_node_datetime_range = GROUP_NODES_BY_YEAR_MODE_TO_GET_DATETIME_RANGE_FUNCTION[mode]
|
|
1059
932
|
|
|
1060
|
-
valid_nodes =
|
|
933
|
+
valid_nodes = non_empty_list(flatten(split_node_by_dates(node) for node in nodes if should_run_node(node)))
|
|
1061
934
|
|
|
1062
935
|
def group_node(groups: dict, index: int):
|
|
1063
936
|
node = valid_nodes[index]
|
|
@@ -1066,12 +939,6 @@ def group_nodes_by_year(
|
|
|
1066
939
|
node, default_node_duration=default_node_duration
|
|
1067
940
|
)
|
|
1068
941
|
|
|
1069
|
-
# pre-parse the "dates" field so it doesn't get re-calculated in each iteration of the for-loop
|
|
1070
|
-
years = (
|
|
1071
|
-
[safe_parse_date(datestr).year for datestr in node.get("dates", [])]
|
|
1072
|
-
if mode == GroupNodesByYearMode.DATES else []
|
|
1073
|
-
)
|
|
1074
|
-
|
|
1075
942
|
range_start = node_datetime_range.start.year if node_datetime_range else 0
|
|
1076
943
|
range_end = node_datetime_range.end.year + 1 if node_datetime_range else 0
|
|
1077
944
|
|
|
@@ -1085,7 +952,6 @@ def group_nodes_by_year(
|
|
|
1085
952
|
is_final_year = _datetime_within_range(node_datetime_range.end, group_datetime_range)
|
|
1086
953
|
|
|
1087
954
|
time_fraction_dict = _build_time_fraction_dict(group_datetime_range, node_datetime_range)
|
|
1088
|
-
update_dict = _build_update_dict(node, years, year) if mode == GroupNodesByYearMode.DATES else {}
|
|
1089
955
|
|
|
1090
956
|
should_run = (
|
|
1091
957
|
mode == GroupNodesByYearMode.DATES
|
|
@@ -1096,7 +962,7 @@ def group_nodes_by_year(
|
|
|
1096
962
|
)
|
|
1097
963
|
|
|
1098
964
|
should_run and groups[year].append(
|
|
1099
|
-
node | time_fraction_dict
|
|
965
|
+
node | time_fraction_dict
|
|
1100
966
|
)
|
|
1101
967
|
|
|
1102
968
|
return groups
|
|
@@ -1111,6 +977,57 @@ def group_nodes_by_year(
|
|
|
1111
977
|
return dict(sorted(iterated.items())) if sort_result else iterated
|
|
1112
978
|
|
|
1113
979
|
|
|
980
|
+
def split_node_by_dates(node: dict) -> list[dict]:
|
|
981
|
+
"""
|
|
982
|
+
Split a node with an array-like `value` and `dates` with multiple elements into a list of nodes with a single
|
|
983
|
+
`value` and `dates`. All other array-like node fields (`sd`, `min`, `max`, and `observations`) will be also be
|
|
984
|
+
split. Any other fields will be copied with no modifications.
|
|
985
|
+
|
|
986
|
+
All split fields will still be array-like, but will only contain one element. Any array-like fields with a
|
|
987
|
+
different number of elements to `value` will not be split.
|
|
988
|
+
|
|
989
|
+
This function should only run on nodes with array-like `value` and `dates` (e.g., nodes with `@type` == `Emission`,
|
|
990
|
+
`Input`,`Measurement`, `Practice` or `Product`).
|
|
991
|
+
|
|
992
|
+
Parameters
|
|
993
|
+
----------
|
|
994
|
+
node : dict
|
|
995
|
+
A HESTIA blank node with array-like `value` and `dates` (and optional array-like fields `sd`, `min`, `max`, and
|
|
996
|
+
`observations`).
|
|
997
|
+
|
|
998
|
+
Returns
|
|
999
|
+
-------
|
|
1000
|
+
list[dict]
|
|
1001
|
+
A list of nodes with single `value` and `dates`.
|
|
1002
|
+
"""
|
|
1003
|
+
REQUIRED_KEYS = ["value", "dates"]
|
|
1004
|
+
OPTIONAL_KEYS = ["sd", "min", "max", "observations"]
|
|
1005
|
+
|
|
1006
|
+
value = node.get("value", [])
|
|
1007
|
+
target_len = len(value) if isinstance(value, list) else -1
|
|
1008
|
+
|
|
1009
|
+
def should_run_key(key: str) -> bool:
|
|
1010
|
+
item = node.get(key, [])
|
|
1011
|
+
return isinstance(item, list) and len(item) == target_len
|
|
1012
|
+
|
|
1013
|
+
should_run = all([
|
|
1014
|
+
target_len > 0,
|
|
1015
|
+
all(should_run_key(key) for key in REQUIRED_KEYS)
|
|
1016
|
+
])
|
|
1017
|
+
|
|
1018
|
+
valid_keys = REQUIRED_KEYS + [key for key in OPTIONAL_KEYS if should_run_key(key)]
|
|
1019
|
+
|
|
1020
|
+
def split(result: list[dict], index: int) -> list[dict]:
|
|
1021
|
+
update = {key: [node[key][index]] for key in valid_keys}
|
|
1022
|
+
result.append(node | update)
|
|
1023
|
+
return result
|
|
1024
|
+
|
|
1025
|
+
return (
|
|
1026
|
+
sorted(reduce(split, range(len(value)), list()), key=lambda node: node.get("dates", []))
|
|
1027
|
+
if should_run else [node]
|
|
1028
|
+
)
|
|
1029
|
+
|
|
1030
|
+
|
|
1114
1031
|
def group_nodes_by_year_and_month(
|
|
1115
1032
|
nodes: list[dict],
|
|
1116
1033
|
default_node_duration: int = 1,
|
|
@@ -1257,3 +1174,99 @@ def get_inputs_from_properties(input: dict, term_types: Union[TermTermType, List
|
|
|
1257
1174
|
} for p in (properties or []) if all([p.get('key'), p.get('value')])
|
|
1258
1175
|
]) if input_value > 0 else []
|
|
1259
1176
|
return filter_list_term_type(inputs, term_types)
|
|
1177
|
+
|
|
1178
|
+
|
|
1179
|
+
def _get_condensed_nodes(nodes: list) -> tuple[list, bool]:
|
|
1180
|
+
"""Only considers nodes which already match on non-date criteria."""
|
|
1181
|
+
CONDENSABLE_UNITS = [Units.BOOLEAN.value, Units.PERCENTAGE_AREA.value]
|
|
1182
|
+
condensed_nodes = []
|
|
1183
|
+
matched_uuids = set()
|
|
1184
|
+
nodes_by_start_date = {_full_date_str(date_str=n["startDate"], is_end=True): n for n in nodes if "startDate" in n}
|
|
1185
|
+
if len(nodes_by_start_date) != len(nodes):
|
|
1186
|
+
return nodes, False
|
|
1187
|
+
|
|
1188
|
+
for node in nodes:
|
|
1189
|
+
search_date = _offset_date(date_str=node.get("endDate", ""))
|
|
1190
|
+
if node["uuid"] in matched_uuids:
|
|
1191
|
+
continue
|
|
1192
|
+
if (search_date in nodes_by_start_date and nodes_by_start_date[search_date]["uuid"] not in matched_uuids
|
|
1193
|
+
and node.get("term", {}).get("units") in CONDENSABLE_UNITS):
|
|
1194
|
+
new_node = node.copy()
|
|
1195
|
+
new_node["endDate"] = nodes_by_start_date[search_date]["endDate"]
|
|
1196
|
+
condensed_nodes.append(new_node)
|
|
1197
|
+
matched_uuids.add(nodes_by_start_date[search_date]["uuid"])
|
|
1198
|
+
elif node["uuid"] not in matched_uuids:
|
|
1199
|
+
condensed_nodes.append(node)
|
|
1200
|
+
|
|
1201
|
+
return condensed_nodes, len(matched_uuids) > 0
|
|
1202
|
+
|
|
1203
|
+
|
|
1204
|
+
def condense_nodes(nodes: list) -> list:
|
|
1205
|
+
grouped_nodes = _group_nodes_by_term_and_value(nodes)
|
|
1206
|
+
condensed_nodes = dict()
|
|
1207
|
+
any_changes_made = False
|
|
1208
|
+
|
|
1209
|
+
for key, node_group in grouped_nodes.items():
|
|
1210
|
+
condensed_nodes[key] = node_group
|
|
1211
|
+
while len(condensed_nodes[key]) > 1:
|
|
1212
|
+
condensed_nodes[key], changes_made = _get_condensed_nodes(condensed_nodes[key])
|
|
1213
|
+
if not changes_made:
|
|
1214
|
+
break
|
|
1215
|
+
any_changes_made = True
|
|
1216
|
+
|
|
1217
|
+
if not any_changes_made:
|
|
1218
|
+
return [_omit(values=n, keys=["uuid"]) for n in nodes]
|
|
1219
|
+
|
|
1220
|
+
return sorted(
|
|
1221
|
+
flatten([_omit(values=n, keys=["uuid"]) for nodes in condensed_nodes.values() for n in nodes]),
|
|
1222
|
+
key=lambda x: x["startDate"]
|
|
1223
|
+
)
|
|
1224
|
+
|
|
1225
|
+
|
|
1226
|
+
DATE_FORMAT = "%Y-%m-%d"
|
|
1227
|
+
|
|
1228
|
+
|
|
1229
|
+
def _variable_length_str_to_date(date_str: str, is_end: bool) -> datetime:
|
|
1230
|
+
"""Converts to date, adding start or end of year to YYYY strings as indicated by is_end."""
|
|
1231
|
+
return datetime.strptime(_full_date_str(date_str, is_end=is_end), DATE_FORMAT)
|
|
1232
|
+
|
|
1233
|
+
|
|
1234
|
+
def _full_date_str(date_str: str, is_end: bool) -> str:
|
|
1235
|
+
suffix = ""
|
|
1236
|
+
if len(date_str) == 4:
|
|
1237
|
+
# Format YYYY
|
|
1238
|
+
suffix = "-12-31" if is_end else "-01-01"
|
|
1239
|
+
elif len(date_str) == 7:
|
|
1240
|
+
# Format YYYY-MM
|
|
1241
|
+
suffix = f"-{calendar.monthrange(int(date_str[:4]), int(date_str[5:7]))[1]}" if is_end else "-01"
|
|
1242
|
+
|
|
1243
|
+
return date_str + suffix
|
|
1244
|
+
|
|
1245
|
+
|
|
1246
|
+
def _with_full_dates(node: dict) -> dict:
|
|
1247
|
+
output_node = node.copy()
|
|
1248
|
+
if "startDate" in output_node:
|
|
1249
|
+
output_node["startDate"] = _full_date_str(output_node["startDate"], is_end=False)
|
|
1250
|
+
if "endDate" in output_node:
|
|
1251
|
+
output_node["endDate"] = _full_date_str(output_node["endDate"], is_end=True)
|
|
1252
|
+
|
|
1253
|
+
return output_node
|
|
1254
|
+
|
|
1255
|
+
|
|
1256
|
+
def _offset_date(date_str: str, days: int = 1, is_end: bool = True) -> str:
|
|
1257
|
+
return (
|
|
1258
|
+
_variable_length_str_to_date(date_str=date_str, is_end=is_end) + timedelta(days=days)
|
|
1259
|
+
).strftime(DATE_FORMAT)
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
def _group_nodes_by_term_and_value(nodes: list) -> dict:
|
|
1263
|
+
grouped_nodes = defaultdict(list)
|
|
1264
|
+
|
|
1265
|
+
for node in nodes:
|
|
1266
|
+
term_id = node.get("term", {}).get("@id", "")
|
|
1267
|
+
value = "-".join([str(v) for v in node.get("value")]) if isinstance(node.get("value"), list) \
|
|
1268
|
+
else node.get("value")
|
|
1269
|
+
node["uuid"] = uuid4()
|
|
1270
|
+
grouped_nodes[(term_id, value)].append(_with_full_dates(node))
|
|
1271
|
+
|
|
1272
|
+
return grouped_nodes
|
|
@@ -3,6 +3,7 @@ from hestia_earth.utils.tools import list_sum
|
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
class Units(Enum):
|
|
6
|
+
BOOLEAN = 'boolean'
|
|
6
7
|
HEAD = 'head'
|
|
7
8
|
NUMBER = 'number'
|
|
8
9
|
KG = 'kg'
|
|
@@ -31,6 +32,7 @@ class Units(Enum):
|
|
|
31
32
|
KG_COLD_CARCASS_WEIGHT = 'kg cold carcass weight'
|
|
32
33
|
KG_COLD_DRESSED_CARCASS_WEIGHT = 'kg cold dressed carcass weight'
|
|
33
34
|
KG_READY_TO_COOK_WEIGHT = 'kg ready-to-cook weight'
|
|
35
|
+
PERCENTAGE_AREA = '% area'
|
|
34
36
|
TO_C = '-C'
|
|
35
37
|
TO_N = '-N'
|
|
36
38
|
|
|
@@ -76,6 +76,17 @@ _ALLOW_ALL = 'all'
|
|
|
76
76
|
def _is_site(site: dict): return site.get('@type', site.get('type')) == SchemaType.SITE.value
|
|
77
77
|
|
|
78
78
|
|
|
79
|
+
def _get_sites(node: dict):
|
|
80
|
+
site = node.get('site', node.get('cycle', {}).get('site'))
|
|
81
|
+
other_sites = node.get('otherSites', node.get('cycle', {}).get('otherSites', []))
|
|
82
|
+
return non_empty_list([site] + other_sites)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _get_site_types(node: dict):
|
|
86
|
+
sites = [node] if _is_site(node) else _get_sites(node)
|
|
87
|
+
return non_empty_list([site.get('siteType') for site in sites])
|
|
88
|
+
|
|
89
|
+
|
|
79
90
|
def _model_lookup_values(model: str, term: dict, restriction: str):
|
|
80
91
|
lookup = download_lookup(f"{term.get('termType')}-model-{restriction}.csv")
|
|
81
92
|
values = get_table_value(lookup, 'termid', term.get('@id'), column_name(model))
|
|
@@ -83,10 +94,11 @@ def _model_lookup_values(model: str, term: dict, restriction: str):
|
|
|
83
94
|
|
|
84
95
|
|
|
85
96
|
def is_model_siteType_allowed(model: str, term: dict, data: dict):
|
|
86
|
-
|
|
87
|
-
site_type = site.get('siteType')
|
|
97
|
+
site_types = _get_site_types(data)
|
|
88
98
|
allowed_values = _model_lookup_values(model, term, 'siteTypesAllowed')
|
|
89
|
-
return True if _ALLOW_ALL in allowed_values or not
|
|
99
|
+
return True if _ALLOW_ALL in allowed_values or not site_types else any([
|
|
100
|
+
(site_type in allowed_values) for site_type in site_types
|
|
101
|
+
])
|
|
90
102
|
|
|
91
103
|
|
|
92
104
|
def _lookup_values(term: dict, column: str):
|
|
@@ -96,10 +108,11 @@ def _lookup_values(term: dict, column: str):
|
|
|
96
108
|
|
|
97
109
|
|
|
98
110
|
def is_siteType_allowed(data: dict, term: dict):
|
|
99
|
-
|
|
100
|
-
site_type = site.get('siteType')
|
|
111
|
+
site_types = _get_site_types(data)
|
|
101
112
|
allowed_values = _lookup_values(term, 'siteTypesAllowed')
|
|
102
|
-
return True if _ALLOW_ALL in allowed_values or not
|
|
113
|
+
return True if _ALLOW_ALL in allowed_values or not site_types else any([
|
|
114
|
+
(site_type in allowed_values) for site_type in site_types
|
|
115
|
+
])
|
|
103
116
|
|
|
104
117
|
|
|
105
118
|
def is_product_termType_allowed(data: dict, term: dict):
|
|
@@ -18,7 +18,7 @@ def _find_source(biblio_title: str = None):
|
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
def get_source(node: dict, biblio_title: str = None):
|
|
21
|
-
source = cached_value(node, CACHE_SOURCES_KEY, {}).get(biblio_title
|
|
21
|
+
source = cached_value(node, CACHE_SOURCES_KEY, {}).get(biblio_title) or _find_source(biblio_title)
|
|
22
22
|
return {'source': source} if source else {}
|
|
23
23
|
|
|
24
24
|
|
hestia_earth/models/version.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
VERSION = '0.
|
|
1
|
+
VERSION = '0.62.1'
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: hestia-earth-models
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.62.1
|
|
4
4
|
Summary: Hestia's set of modules for filling gaps in the activity data using external datasets (e.g. populating soil properties with a geospatial dataset using provided coordinates) and internal lookups (e.g. populating machinery use from fuel use). Includes rules for when gaps should be filled versus not (e.g. never gap fill yield, gap fill crop residue if yield provided etc.).
|
|
5
5
|
Home-page: https://gitlab.com/hestia-earth/hestia-engine-models
|
|
6
6
|
Author: Hestia Team
|
|
@@ -11,8 +11,8 @@ Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
|
|
|
11
11
|
Classifier: Programming Language :: Python :: 3.6
|
|
12
12
|
Description-Content-Type: text/markdown
|
|
13
13
|
License-File: LICENSE
|
|
14
|
-
Requires-Dist: hestia-earth.schema ==
|
|
15
|
-
Requires-Dist: hestia-earth.utils >=0.13.
|
|
14
|
+
Requires-Dist: hestia-earth.schema ==29.*
|
|
15
|
+
Requires-Dist: hestia-earth.utils >=0.13.2
|
|
16
16
|
Requires-Dist: python-dateutil >=2.8.1
|
|
17
17
|
Requires-Dist: CurrencyConverter ==0.16.8
|
|
18
18
|
Requires-Dist: haversine >=2.7.0
|