hestia-earth-models 0.64.8__py3-none-any.whl → 0.64.9__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/cycle/siteArea.py +2 -1
- hestia_earth/models/environmentalFootprintV3/soilQualityIndexLandOccupation.py +73 -82
- hestia_earth/models/environmentalFootprintV3/soilQualityIndexLandTransformation.py +102 -116
- hestia_earth/models/environmentalFootprintV3/soilQualityIndexTotalLandUseEffects.py +27 -16
- hestia_earth/models/faostat2018/landTransformationFromCropland100YearAverage.py +3 -2
- hestia_earth/models/faostat2018/landTransformationFromCropland20YearAverage.py +3 -2
- hestia_earth/models/frischknechtEtAl2000/ionisingRadiationKbqU235Eq.py +69 -37
- hestia_earth/models/ipcc2019/animal/fatContent.py +38 -0
- hestia_earth/models/ipcc2019/animal/liveweightGain.py +3 -54
- hestia_earth/models/ipcc2019/animal/liveweightPerHead.py +3 -54
- hestia_earth/models/ipcc2019/animal/pregnancyRateTotal.py +38 -0
- hestia_earth/models/ipcc2019/animal/trueProteinContent.py +38 -0
- hestia_earth/models/ipcc2019/animal/utils.py +87 -3
- hestia_earth/models/ipcc2019/animal/weightAtMaturity.py +4 -10
- hestia_earth/models/mocking/search-results.json +265 -259
- hestia_earth/models/poschEtAl2008/terrestrialAcidificationPotentialAccumulatedExceedance.py +4 -1
- hestia_earth/models/poschEtAl2008/terrestrialEutrophicationPotentialAccumulatedExceedance.py +4 -1
- hestia_earth/models/utils/__init__.py +5 -4
- hestia_earth/models/utils/impact_assessment.py +13 -4
- hestia_earth/models/version.py +1 -1
- {hestia_earth_models-0.64.8.dist-info → hestia_earth_models-0.64.9.dist-info}/METADATA +1 -1
- {hestia_earth_models-0.64.8.dist-info → hestia_earth_models-0.64.9.dist-info}/RECORD +37 -31
- tests/models/environmentalFootprintV3/test_soilQualityIndexLandOccupation.py +97 -66
- tests/models/environmentalFootprintV3/test_soilQualityIndexLandTransformation.py +137 -74
- tests/models/environmentalFootprintV3/test_soilQualityIndexTotalLandUseEffects.py +15 -10
- tests/models/frischknechtEtAl2000/test_ionisingRadiationKbqU235Eq.py +67 -44
- tests/models/ipcc2019/animal/test_fatContent.py +22 -0
- tests/models/ipcc2019/animal/test_liveweightGain.py +4 -2
- tests/models/ipcc2019/animal/test_liveweightPerHead.py +4 -2
- tests/models/ipcc2019/animal/test_pregnancyRateTotal.py +22 -0
- tests/models/ipcc2019/animal/test_trueProteinContent.py +22 -0
- tests/models/ipcc2019/animal/test_weightAtMaturity.py +2 -1
- tests/models/poschEtAl2008/test_terrestrialAcidificationPotentialAccumulatedExceedance.py +30 -17
- tests/models/poschEtAl2008/test_terrestrialEutrophicationPotentialAccumulatedExceedance.py +28 -14
- {hestia_earth_models-0.64.8.dist-info → hestia_earth_models-0.64.9.dist-info}/LICENSE +0 -0
- {hestia_earth_models-0.64.8.dist-info → hestia_earth_models-0.64.9.dist-info}/WHEEL +0 -0
- {hestia_earth_models-0.64.8.dist-info → hestia_earth_models-0.64.9.dist-info}/top_level.txt +0 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
from functools import reduce
|
|
2
1
|
from hestia_earth.schema import TermTermType
|
|
3
|
-
from hestia_earth.utils.
|
|
2
|
+
from hestia_earth.utils.lookup import get_table_value, download_lookup, column_name
|
|
3
|
+
from hestia_earth.utils.model import filter_list_term_type
|
|
4
|
+
from hestia_earth.utils.tools import flatten
|
|
4
5
|
|
|
5
|
-
from hestia_earth.models.log import logRequirements, logShouldRun
|
|
6
|
-
from hestia_earth.models.utils.emission import filter_emission_inputs
|
|
6
|
+
from hestia_earth.models.log import logRequirements, logShouldRun, log_as_table
|
|
7
7
|
from hestia_earth.models.utils.indicator import _new_indicator
|
|
8
|
-
from hestia_earth.models.utils.lookup import
|
|
8
|
+
from hestia_earth.models.utils.lookup import _node_value
|
|
9
9
|
from . import MODEL
|
|
10
10
|
|
|
11
11
|
REQUIREMENTS = {
|
|
@@ -18,7 +18,7 @@ REQUIREMENTS = {
|
|
|
18
18
|
"ionisingCompoundsToWaterInputsProduction",
|
|
19
19
|
"ionisingCompoundsToSaltwaterInputsProduction"
|
|
20
20
|
],
|
|
21
|
-
"inputs":
|
|
21
|
+
"inputs": {"@type": "Term", "term.termType": "waste", "term.units": "kg"}
|
|
22
22
|
}]
|
|
23
23
|
}
|
|
24
24
|
}
|
|
@@ -30,15 +30,23 @@ LOOKUPS = {
|
|
|
30
30
|
]
|
|
31
31
|
}
|
|
32
32
|
RETURNS = {
|
|
33
|
-
"Indicator": {
|
|
33
|
+
"Indicator": [{
|
|
34
34
|
"value": "",
|
|
35
35
|
"inputs": ""
|
|
36
|
-
}
|
|
36
|
+
}]
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
TERM_ID = 'ionisingRadiationKbqU235Eq'
|
|
40
40
|
|
|
41
41
|
|
|
42
|
+
def _valid_waste(input: dict) -> bool:
|
|
43
|
+
return input.get('units', '').startswith("kg") and input.get('termType', '') == TermTermType.WASTE.value
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _valid_emission(emission: dict) -> bool:
|
|
47
|
+
return len(emission.get('inputs', [])) == 1 and isinstance(_node_value(emission), (int, float))
|
|
48
|
+
|
|
49
|
+
|
|
42
50
|
def _indicator(value: float, input: dict):
|
|
43
51
|
indicator = _new_indicator(TERM_ID, MODEL)
|
|
44
52
|
indicator['value'] = value
|
|
@@ -46,45 +54,69 @@ def _indicator(value: float, input: dict):
|
|
|
46
54
|
return indicator
|
|
47
55
|
|
|
48
56
|
|
|
49
|
-
def _run(
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
model=MODEL,
|
|
54
|
-
term_id=TERM_ID,
|
|
55
|
-
lookup_name=f"{TermTermType.WASTE.value}.csv",
|
|
56
|
-
lookup_col=i.get('emission').get('term', {}).get('@id')
|
|
57
|
-
)(data=i.get('emission') | {'term': i.get('input')})
|
|
58
|
-
for i in grouped_emissions_inputs
|
|
57
|
+
def _run(emissions: list):
|
|
58
|
+
indicators = [
|
|
59
|
+
_indicator(value=emission['value'] * emission['coefficient'], input=emission['input'])
|
|
60
|
+
for emission in emissions
|
|
59
61
|
]
|
|
60
|
-
|
|
61
|
-
return _indicator(value, input) if value else None
|
|
62
|
+
return indicators
|
|
62
63
|
|
|
63
64
|
|
|
64
65
|
def _should_run(impact_assessment: dict):
|
|
65
|
-
emissions =
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
emissions = [emission for emission in filter_list_term_type(impact_assessment.get('emissionsResourceUse', []),
|
|
67
|
+
TermTermType.EMISSION)
|
|
68
|
+
if emission.get('term', {}).get('@id', '') in LOOKUPS[TermTermType.WASTE.value]]
|
|
69
|
+
|
|
70
|
+
has_emissions = bool(emissions)
|
|
71
|
+
|
|
72
|
+
emissions_unpacked = flatten(
|
|
73
|
+
[
|
|
74
|
+
[
|
|
75
|
+
{
|
|
76
|
+
"input-term-id": input.get('@id'),
|
|
77
|
+
"input-term-type": input.get('termType'),
|
|
78
|
+
"indicator-term-id": emission['term']['@id'],
|
|
79
|
+
"indicator-is-valid": _valid_emission(emission),
|
|
80
|
+
"input": input,
|
|
81
|
+
"indicator-input-is-valid": _valid_waste(input),
|
|
82
|
+
"value": _node_value(emission),
|
|
83
|
+
"coefficient": get_table_value(array=download_lookup(filename="waste.csv"),
|
|
84
|
+
col_match='termid',
|
|
85
|
+
col_match_with=input.get('@id'),
|
|
86
|
+
col_val=column_name(emission['term']['@id'])) if input else None
|
|
87
|
+
} for input in emission['inputs'] or [{}]]
|
|
88
|
+
for emission in emissions
|
|
89
|
+
]
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
valid_emission_with_cf = [em for em in emissions_unpacked if em['coefficient'] is not None
|
|
93
|
+
and em['indicator-is-valid'] is True
|
|
94
|
+
and em['indicator-input-is-valid'] is True]
|
|
95
|
+
|
|
96
|
+
valid_input_requirements = all([
|
|
97
|
+
all([
|
|
98
|
+
em['indicator-is-valid'],
|
|
99
|
+
em['indicator-input-is-valid']
|
|
69
100
|
])
|
|
70
|
-
for
|
|
71
|
-
if emission.get('term', {}).get('@id') in LOOKUPS[TermTermType.WASTE.value]
|
|
101
|
+
for em in emissions_unpacked
|
|
72
102
|
])
|
|
73
|
-
emissions_per_input = reduce(
|
|
74
|
-
lambda p, c: p | {c.get('input').get('@id'): p.get(c.get('input').get('@id'), []) + [c]},
|
|
75
|
-
emissions,
|
|
76
|
-
{}
|
|
77
|
-
)
|
|
78
103
|
|
|
79
|
-
|
|
80
|
-
|
|
104
|
+
all_emissions_have_known_cf = all([em['coefficient'] is not None for em in emissions_unpacked]) and bool(
|
|
105
|
+
emissions_unpacked)
|
|
106
|
+
|
|
107
|
+
logRequirements(impact_assessment, model=MODEL, term=TERM_ID,
|
|
108
|
+
has_emissions=has_emissions,
|
|
109
|
+
valid_input_requirements=valid_input_requirements,
|
|
110
|
+
all_emissions_have_known_CF=all_emissions_have_known_cf,
|
|
111
|
+
emissions=log_as_table(emissions_unpacked)
|
|
112
|
+
)
|
|
81
113
|
|
|
82
|
-
should_run =
|
|
114
|
+
should_run = valid_input_requirements
|
|
83
115
|
|
|
84
116
|
logShouldRun(impact_assessment, MODEL, TERM_ID, should_run)
|
|
85
|
-
return should_run,
|
|
117
|
+
return should_run, valid_emission_with_cf
|
|
86
118
|
|
|
87
119
|
|
|
88
120
|
def run(impact_assessment: dict):
|
|
89
|
-
should_run,
|
|
90
|
-
return
|
|
121
|
+
should_run, emissions = _should_run(impact_assessment)
|
|
122
|
+
return _run(emissions) if should_run else []
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from .utils import should_run_by_productivity_lookup, run_animal_by_productivity
|
|
2
|
+
|
|
3
|
+
REQUIREMENTS = {
|
|
4
|
+
"Cycle": {
|
|
5
|
+
"site": {
|
|
6
|
+
"@type": "Site",
|
|
7
|
+
"country": {"@type": "Term", "termType": "region"}
|
|
8
|
+
},
|
|
9
|
+
"animals": [{
|
|
10
|
+
"@type": "Animal",
|
|
11
|
+
"term.termType": "liveAnimal",
|
|
12
|
+
"practices": {
|
|
13
|
+
"@type": "Practice",
|
|
14
|
+
"term.termType": "animalManagement"
|
|
15
|
+
}
|
|
16
|
+
}]
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
LOOKUPS = {
|
|
20
|
+
"region-liveAnimal-milkFatContent": "fat content",
|
|
21
|
+
"liveAnimal": "milkYieldPracticeTermIds"
|
|
22
|
+
}
|
|
23
|
+
RETURNS = {
|
|
24
|
+
"Animal": [{
|
|
25
|
+
"practices": [{
|
|
26
|
+
"@type": "Practice",
|
|
27
|
+
"value": ""
|
|
28
|
+
}]
|
|
29
|
+
}]
|
|
30
|
+
}
|
|
31
|
+
TERM_ID = 'fatContent'
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def run(cycle: dict):
|
|
35
|
+
animals = should_run_by_productivity_lookup(
|
|
36
|
+
TERM_ID, cycle, list(LOOKUPS.keys())[0], practice_id=LOOKUPS['liveAnimal']
|
|
37
|
+
)
|
|
38
|
+
return list(map(run_animal_by_productivity(TERM_ID, include_practice=True), animals))
|
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
from
|
|
2
|
-
from hestia_earth.utils.model import filter_list_term_type
|
|
3
|
-
|
|
4
|
-
from hestia_earth.models.log import logRequirements, logShouldRun
|
|
5
|
-
from hestia_earth.models.utils.blank_node import merge_blank_nodes
|
|
6
|
-
from hestia_earth.models.utils.property import _new_property, node_has_no_property
|
|
7
|
-
from .utils import productivity_lookup_value
|
|
8
|
-
from .. import MODEL
|
|
1
|
+
from .utils import should_run_by_productivity_lookup, run_animal_by_productivity
|
|
9
2
|
|
|
10
3
|
REQUIREMENTS = {
|
|
11
4
|
"Cycle": {
|
|
@@ -40,50 +33,6 @@ RETURNS = {
|
|
|
40
33
|
TERM_ID = 'liveweightGain'
|
|
41
34
|
|
|
42
35
|
|
|
43
|
-
def _property(value: float):
|
|
44
|
-
prop = _new_property(TERM_ID, MODEL)
|
|
45
|
-
prop['value'] = value
|
|
46
|
-
return prop
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
def _run_animal(data: dict):
|
|
50
|
-
animal = data.get('animal')
|
|
51
|
-
value = data.get('value')
|
|
52
|
-
return animal | {
|
|
53
|
-
'properties': merge_blank_nodes(animal.get('properties', []), [_property(value)])
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
def _should_run(cycle: dict):
|
|
58
|
-
country = cycle.get('site', {}).get('country', {})
|
|
59
|
-
country_id = country.get('@id')
|
|
60
|
-
live_animals = filter_list_term_type(cycle.get('animals', []), TermTermType.LIVEANIMAL)
|
|
61
|
-
live_animals = list(filter(node_has_no_property(TERM_ID), live_animals))
|
|
62
|
-
live_animals_with_value = [{
|
|
63
|
-
'animal': animal,
|
|
64
|
-
'value': productivity_lookup_value(TERM_ID, list(LOOKUPS.keys())[0], country, animal)
|
|
65
|
-
} for animal in live_animals]
|
|
66
|
-
|
|
67
|
-
def _should_run_animal(value: dict):
|
|
68
|
-
animal = value.get('animal')
|
|
69
|
-
lookup_value = value.get('value')
|
|
70
|
-
term_id = animal.get('term').get('@id')
|
|
71
|
-
|
|
72
|
-
logRequirements(cycle, model=MODEL, term=term_id, property=TERM_ID,
|
|
73
|
-
country_id=country_id,
|
|
74
|
-
liveweightGain=lookup_value)
|
|
75
|
-
|
|
76
|
-
should_run = all([
|
|
77
|
-
country_id,
|
|
78
|
-
lookup_value is not None
|
|
79
|
-
])
|
|
80
|
-
logShouldRun(cycle, MODEL, term_id, should_run, property=TERM_ID)
|
|
81
|
-
|
|
82
|
-
return should_run
|
|
83
|
-
|
|
84
|
-
return list(filter(_should_run_animal, live_animals_with_value))
|
|
85
|
-
|
|
86
|
-
|
|
87
36
|
def run(cycle: dict):
|
|
88
|
-
animals =
|
|
89
|
-
return list(map(
|
|
37
|
+
animals = should_run_by_productivity_lookup(TERM_ID, cycle, list(LOOKUPS.keys())[0])
|
|
38
|
+
return list(map(run_animal_by_productivity(TERM_ID), animals))
|
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
from
|
|
2
|
-
from hestia_earth.utils.model import filter_list_term_type
|
|
3
|
-
|
|
4
|
-
from hestia_earth.models.log import logRequirements, logShouldRun
|
|
5
|
-
from hestia_earth.models.utils.blank_node import merge_blank_nodes
|
|
6
|
-
from hestia_earth.models.utils.property import _new_property, node_has_no_property
|
|
7
|
-
from .utils import productivity_lookup_value
|
|
8
|
-
from .. import MODEL
|
|
1
|
+
from .utils import should_run_by_productivity_lookup, run_animal_by_productivity
|
|
9
2
|
|
|
10
3
|
REQUIREMENTS = {
|
|
11
4
|
"Cycle": {
|
|
@@ -40,50 +33,6 @@ RETURNS = {
|
|
|
40
33
|
TERM_ID = 'liveweightPerHead'
|
|
41
34
|
|
|
42
35
|
|
|
43
|
-
def _property(value: float):
|
|
44
|
-
prop = _new_property(TERM_ID, MODEL)
|
|
45
|
-
prop['value'] = value
|
|
46
|
-
return prop
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
def _run_animal(data: dict):
|
|
50
|
-
animal = data.get('animal')
|
|
51
|
-
value = data.get('value')
|
|
52
|
-
return animal | {
|
|
53
|
-
'properties': merge_blank_nodes(animal.get('properties', []), [_property(value)])
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
def _should_run(cycle: dict):
|
|
58
|
-
country = cycle.get('site', {}).get('country', {})
|
|
59
|
-
country_id = country.get('@id')
|
|
60
|
-
live_animals = filter_list_term_type(cycle.get('animals', []), TermTermType.LIVEANIMAL)
|
|
61
|
-
live_animals = list(filter(node_has_no_property(TERM_ID), live_animals))
|
|
62
|
-
live_animals_with_value = [{
|
|
63
|
-
'animal': animal,
|
|
64
|
-
'value': productivity_lookup_value(TERM_ID, list(LOOKUPS.keys())[0], country, animal)
|
|
65
|
-
} for animal in live_animals]
|
|
66
|
-
|
|
67
|
-
def _should_run_animal(value: dict):
|
|
68
|
-
animal = value.get('animal')
|
|
69
|
-
lookup_value = value.get('value')
|
|
70
|
-
term_id = animal.get('term').get('@id')
|
|
71
|
-
|
|
72
|
-
logRequirements(cycle, model=MODEL, term=term_id, property=TERM_ID,
|
|
73
|
-
country_id=country_id,
|
|
74
|
-
liveweightPerHead=lookup_value)
|
|
75
|
-
|
|
76
|
-
should_run = all([
|
|
77
|
-
country_id,
|
|
78
|
-
lookup_value is not None
|
|
79
|
-
])
|
|
80
|
-
logShouldRun(cycle, MODEL, term_id, should_run, property=TERM_ID)
|
|
81
|
-
|
|
82
|
-
return should_run
|
|
83
|
-
|
|
84
|
-
return list(filter(_should_run_animal, live_animals_with_value))
|
|
85
|
-
|
|
86
|
-
|
|
87
36
|
def run(cycle: dict):
|
|
88
|
-
animals =
|
|
89
|
-
return list(map(
|
|
37
|
+
animals = should_run_by_productivity_lookup(TERM_ID, cycle, list(LOOKUPS.keys())[0])
|
|
38
|
+
return list(map(run_animal_by_productivity(TERM_ID), animals))
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from .utils import should_run_by_productivity_lookup, run_animal_by_productivity
|
|
2
|
+
|
|
3
|
+
REQUIREMENTS = {
|
|
4
|
+
"Cycle": {
|
|
5
|
+
"site": {
|
|
6
|
+
"@type": "Site",
|
|
7
|
+
"country": {"@type": "Term", "termType": "region"}
|
|
8
|
+
},
|
|
9
|
+
"animals": [{
|
|
10
|
+
"@type": "Animal",
|
|
11
|
+
"term.termType": "liveAnimal",
|
|
12
|
+
"none": {
|
|
13
|
+
"properties": [{
|
|
14
|
+
"@type": "Property",
|
|
15
|
+
"value": "",
|
|
16
|
+
"term.@id": "pregnancyRateTotal"
|
|
17
|
+
}]
|
|
18
|
+
}
|
|
19
|
+
}]
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
LOOKUPS = {
|
|
23
|
+
"region-liveAnimal-pregnancyRateTotal": "pregnancy rate"
|
|
24
|
+
}
|
|
25
|
+
RETURNS = {
|
|
26
|
+
"Animal": [{
|
|
27
|
+
"properties": [{
|
|
28
|
+
"@type": "Property",
|
|
29
|
+
"value": ""
|
|
30
|
+
}]
|
|
31
|
+
}]
|
|
32
|
+
}
|
|
33
|
+
TERM_ID = 'pregnancyRateTotal'
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def run(cycle: dict):
|
|
37
|
+
animals = should_run_by_productivity_lookup(TERM_ID, cycle, list(LOOKUPS.keys())[0])
|
|
38
|
+
return list(map(run_animal_by_productivity(TERM_ID), animals))
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from .utils import should_run_by_productivity_lookup, run_animal_by_productivity
|
|
2
|
+
|
|
3
|
+
REQUIREMENTS = {
|
|
4
|
+
"Cycle": {
|
|
5
|
+
"site": {
|
|
6
|
+
"@type": "Site",
|
|
7
|
+
"country": {"@type": "Term", "termType": "region"}
|
|
8
|
+
},
|
|
9
|
+
"animals": [{
|
|
10
|
+
"@type": "Animal",
|
|
11
|
+
"term.termType": "liveAnimal",
|
|
12
|
+
"practices": {
|
|
13
|
+
"@type": "Practice",
|
|
14
|
+
"term.termType": "animalManagement"
|
|
15
|
+
}
|
|
16
|
+
}]
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
LOOKUPS = {
|
|
20
|
+
"region-liveAnimal-milkTrueProteinContent": "protein content",
|
|
21
|
+
"liveAnimal": "milkYieldPracticeTermIds"
|
|
22
|
+
}
|
|
23
|
+
RETURNS = {
|
|
24
|
+
"Animal": [{
|
|
25
|
+
"practices": [{
|
|
26
|
+
"@type": "Practice",
|
|
27
|
+
"value": ""
|
|
28
|
+
}]
|
|
29
|
+
}]
|
|
30
|
+
}
|
|
31
|
+
TERM_ID = 'trueProteinContent'
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def run(cycle: dict):
|
|
35
|
+
animals = should_run_by_productivity_lookup(
|
|
36
|
+
TERM_ID, cycle, list(LOOKUPS.keys())[0], practice_id=LOOKUPS['liveAnimal']
|
|
37
|
+
)
|
|
38
|
+
return list(map(run_animal_by_productivity(TERM_ID, include_practice=True), animals))
|
|
@@ -1,11 +1,26 @@
|
|
|
1
|
+
from hestia_earth.schema import TermTermType
|
|
1
2
|
from hestia_earth.utils.lookup import download_lookup, get_table_value, column_name, extract_grouped_data
|
|
2
|
-
from hestia_earth.utils.
|
|
3
|
+
from hestia_earth.utils.model import filter_list_term_type
|
|
4
|
+
from hestia_earth.utils.tools import safe_parse_float, non_empty_list
|
|
3
5
|
|
|
4
|
-
from hestia_earth.models.log import debugMissingLookup
|
|
6
|
+
from hestia_earth.models.log import logRequirements, logShouldRun, debugMissingLookup
|
|
7
|
+
from hestia_earth.models.utils.blank_node import merge_blank_nodes
|
|
8
|
+
from hestia_earth.models.utils.property import _new_property, node_has_no_property
|
|
5
9
|
from hestia_earth.models.utils.productivity import PRODUCTIVITY, get_productivity
|
|
10
|
+
from hestia_earth.models.utils.term import get_lookup_value
|
|
6
11
|
from .. import MODEL
|
|
7
12
|
|
|
8
13
|
|
|
14
|
+
def _get_practice(term_id: str, animal: dict, practice_id: str):
|
|
15
|
+
term = animal.get('term', {})
|
|
16
|
+
value = get_lookup_value(term, practice_id, model=MODEL, term=term_id)
|
|
17
|
+
practice_ids = non_empty_list((value or '').split(';'))
|
|
18
|
+
return next(
|
|
19
|
+
(p for p in animal.get('practices', []) if p.get('term', {}).get('@id') in practice_ids),
|
|
20
|
+
{}
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
9
24
|
def productivity_lookup_value(term_id: str, lookup: str, country: dict, animal: dict):
|
|
10
25
|
country_id = country.get('@id')
|
|
11
26
|
productivity_key = get_productivity(country)
|
|
@@ -16,5 +31,74 @@ def productivity_lookup_value(term_id: str, lookup: str, country: dict, animal:
|
|
|
16
31
|
debugMissingLookup(lookup_name, 'termid', country_id, column, value, model=MODEL, term=term_id)
|
|
17
32
|
return safe_parse_float(
|
|
18
33
|
extract_grouped_data(value, productivity_key.value) or
|
|
19
|
-
extract_grouped_data(value, PRODUCTIVITY.HIGH.value) # defaults to high if low is not found
|
|
34
|
+
extract_grouped_data(value, PRODUCTIVITY.HIGH.value), # defaults to high if low is not found
|
|
35
|
+
None
|
|
20
36
|
)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def map_live_animals_by_productivity_lookup(term_id: str, cycle: dict, lookup_col: str, practice_id: str = None):
|
|
40
|
+
country = cycle.get('site', {}).get('country', {})
|
|
41
|
+
live_animals = filter_list_term_type(cycle.get('animals', []), TermTermType.LIVEANIMAL)
|
|
42
|
+
live_animals = list(filter(node_has_no_property(term_id), live_animals))
|
|
43
|
+
return [{
|
|
44
|
+
'animal': animal,
|
|
45
|
+
'value': productivity_lookup_value(term_id, lookup_col, country, animal)
|
|
46
|
+
} | ({
|
|
47
|
+
'practice': _get_practice(term_id, animal, practice_id)
|
|
48
|
+
} if practice_id else {}) for animal in live_animals]
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def should_run_by_productivity_lookup(term_id: str, cycle: dict, lookup_col: str, practice_id: str = None):
|
|
52
|
+
country = cycle.get('site', {}).get('country', {})
|
|
53
|
+
country_id = country.get('@id')
|
|
54
|
+
live_animals_with_value = map_live_animals_by_productivity_lookup(term_id, cycle, lookup_col, practice_id)
|
|
55
|
+
|
|
56
|
+
def _should_run_animal(value: dict):
|
|
57
|
+
animal = value.get('animal')
|
|
58
|
+
lookup_value = value.get('value')
|
|
59
|
+
practice = value.get('practice')
|
|
60
|
+
term_id = animal.get('term').get('@id')
|
|
61
|
+
|
|
62
|
+
logRequirements(cycle, model=MODEL, term=term_id, property=term_id,
|
|
63
|
+
country_id=country_id,
|
|
64
|
+
**({
|
|
65
|
+
lookup_col.replace('-', '_'): lookup_value
|
|
66
|
+
} | ({
|
|
67
|
+
'practice': practice.get('term', {}).get('@id')
|
|
68
|
+
} if practice_id else {})))
|
|
69
|
+
|
|
70
|
+
should_run = all([
|
|
71
|
+
country_id,
|
|
72
|
+
not practice_id or bool(practice),
|
|
73
|
+
lookup_value is not None
|
|
74
|
+
])
|
|
75
|
+
logShouldRun(cycle, MODEL, term_id, should_run, property=term_id)
|
|
76
|
+
|
|
77
|
+
return should_run
|
|
78
|
+
|
|
79
|
+
return list(filter(_should_run_animal, live_animals_with_value))
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _property(term_id: str, value: float):
|
|
83
|
+
prop = _new_property(term_id, MODEL)
|
|
84
|
+
prop['value'] = value
|
|
85
|
+
return prop
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def run_animal_by_productivity(term_id: str, include_practice: bool = False):
|
|
89
|
+
def run(data: dict):
|
|
90
|
+
animal = data.get('animal')
|
|
91
|
+
value = data.get('value')
|
|
92
|
+
practice = data.get('practice')
|
|
93
|
+
return animal | ({
|
|
94
|
+
'practices': [
|
|
95
|
+
(
|
|
96
|
+
p | ({
|
|
97
|
+
'properties': merge_blank_nodes(p.get('properties', []), [_property(term_id, value)])
|
|
98
|
+
} if p.get('term', {}).get('@id') == practice.get('term', {}).get('@id') else {})
|
|
99
|
+
) for p in animal.get('practices', [])
|
|
100
|
+
]
|
|
101
|
+
} if include_practice else {
|
|
102
|
+
'properties': merge_blank_nodes(animal.get('properties', []), [_property(term_id, value)])
|
|
103
|
+
})
|
|
104
|
+
return run
|
|
@@ -2,13 +2,12 @@
|
|
|
2
2
|
Note: when the `liveweightPerHead` property is provided, this model will only work if the returned value is
|
|
3
3
|
greater than or equal to `liveweightPerHead` value.
|
|
4
4
|
"""
|
|
5
|
-
from hestia_earth.
|
|
6
|
-
from hestia_earth.utils.model import filter_list_term_type, find_term_match
|
|
5
|
+
from hestia_earth.utils.model import find_term_match
|
|
7
6
|
|
|
8
7
|
from hestia_earth.models.log import logRequirements, logShouldRun
|
|
9
8
|
from hestia_earth.models.utils.blank_node import merge_blank_nodes
|
|
10
|
-
from hestia_earth.models.utils.property import _new_property
|
|
11
|
-
from .utils import
|
|
9
|
+
from hestia_earth.models.utils.property import _new_property
|
|
10
|
+
from .utils import map_live_animals_by_productivity_lookup
|
|
12
11
|
from .. import MODEL
|
|
13
12
|
|
|
14
13
|
REQUIREMENTS = {
|
|
@@ -68,12 +67,7 @@ def _run_animal(data: dict):
|
|
|
68
67
|
def _should_run(cycle: dict):
|
|
69
68
|
country = cycle.get('site', {}).get('country', {})
|
|
70
69
|
country_id = country.get('@id')
|
|
71
|
-
|
|
72
|
-
live_animals = list(filter(node_has_no_property(TERM_ID), live_animals))
|
|
73
|
-
live_animals_with_value = [{
|
|
74
|
-
'animal': animal,
|
|
75
|
-
'value': productivity_lookup_value(TERM_ID, list(LOOKUPS.keys())[0], country, animal)
|
|
76
|
-
} for animal in live_animals]
|
|
70
|
+
live_animals_with_value = map_live_animals_by_productivity_lookup(TERM_ID, cycle, list(LOOKUPS.keys())[0])
|
|
77
71
|
|
|
78
72
|
def _should_run_animal(value: dict):
|
|
79
73
|
animal = value.get('animal')
|