hestia-earth-models 0.64.2__py3-none-any.whl → 0.64.3__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/agribalyse2016/machineryInfrastructureDepreciatedAmountPerCycle.py +2 -2
- hestia_earth/models/cycle/animal/input/hestiaAggregatedData.py +5 -2
- hestia_earth/models/cycle/animal/input/properties.py +2 -1
- hestia_earth/models/cycle/animal/milkYield.py +2 -1
- hestia_earth/models/cycle/concentrateFeed.py +8 -8
- hestia_earth/models/cycle/cycleDuration.py +4 -5
- hestia_earth/models/cycle/siteDuration.py +15 -5
- hestia_earth/models/cycle/startDateDefinition.py +3 -4
- hestia_earth/models/cycle/stockingDensityAnimalHousingAverage.py +52 -0
- hestia_earth/models/ipcc2019/aboveGroundBiomass.py +762 -0
- hestia_earth/models/ipcc2019/aboveGroundBiomass_utils.py +180 -0
- hestia_earth/models/ipcc2019/animal/liveweightGain.py +88 -0
- hestia_earth/models/ipcc2019/animal/liveweightPerHead.py +88 -0
- hestia_earth/models/ipcc2019/animal/pastureGrass.py +51 -42
- hestia_earth/models/ipcc2019/animal/utils.py +20 -0
- hestia_earth/models/ipcc2019/animal/weightAtMaturity.py +10 -15
- hestia_earth/models/ipcc2019/ch4ToAirExcreta.py +2 -2
- hestia_earth/models/ipcc2019/organicCarbonPerHa_tier_1_utils.py +37 -50
- hestia_earth/models/ipcc2019/organicCarbonPerHa_utils.py +0 -19
- hestia_earth/models/ipcc2019/pastureGrass.py +44 -31
- hestia_earth/models/ipcc2019/pastureGrass_utils.py +38 -22
- hestia_earth/models/mocking/search-results.json +223 -223
- hestia_earth/models/poschEtAl2008/terrestrialEutrophicationPotentialAccumulatedExceedance.py +40 -0
- hestia_earth/models/utils/blank_node.py +20 -1
- hestia_earth/models/utils/crop.py +4 -0
- hestia_earth/models/utils/ecoClimateZone.py +99 -0
- hestia_earth/models/utils/productivity.py +1 -1
- hestia_earth/models/utils/property.py +2 -2
- hestia_earth/models/version.py +1 -1
- {hestia_earth_models-0.64.2.dist-info → hestia_earth_models-0.64.3.dist-info}/METADATA +1 -1
- {hestia_earth_models-0.64.2.dist-info → hestia_earth_models-0.64.3.dist-info}/RECORD +45 -31
- tests/models/cycle/test_siteDuration.py +22 -0
- tests/models/cycle/test_stockingDensityAnimalHousingAverage.py +42 -0
- tests/models/ipcc2019/animal/test_liveweightGain.py +20 -0
- tests/models/ipcc2019/animal/test_liveweightPerHead.py +20 -0
- tests/models/ipcc2019/animal/test_pastureGrass.py +1 -1
- tests/models/ipcc2019/test_aboveGroundBiomass.py +182 -0
- tests/models/ipcc2019/test_aboveGroundBiomass_utils.py +92 -0
- tests/models/ipcc2019/test_organicCarbonPerHa_tier_1_utils.py +3 -2
- tests/models/ipcc2019/test_pastureGrass.py +2 -2
- tests/models/poschEtAl2008/test_terrestrialEutrophicationPotentialAccumulatedExceedance.py +44 -0
- tests/models/utils/test_ecoClimateZone.py +152 -0
- {hestia_earth_models-0.64.2.dist-info → hestia_earth_models-0.64.3.dist-info}/LICENSE +0 -0
- {hestia_earth_models-0.64.2.dist-info → hestia_earth_models-0.64.3.dist-info}/WHEEL +0 -0
- {hestia_earth_models-0.64.2.dist-info → hestia_earth_models-0.64.3.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from numpy.typing import NDArray
|
|
3
|
+
from os.path import isfile
|
|
4
|
+
from pytest import mark
|
|
5
|
+
from unittest.mock import MagicMock, patch
|
|
6
|
+
|
|
7
|
+
from hestia_earth.models.ipcc2019.aboveGroundBiomass import (
|
|
8
|
+
_is_lcc_event, _rescale_category_cover, _should_run, MODEL, run, TERM_ID
|
|
9
|
+
)
|
|
10
|
+
from hestia_earth.models.ipcc2019.aboveGroundBiomass_utils import BiomassCategory, sample_constant
|
|
11
|
+
|
|
12
|
+
from tests.utils import fake_new_measurement, fixtures_path
|
|
13
|
+
|
|
14
|
+
class_path = f"hestia_earth.models.{MODEL}.{TERM_ID}"
|
|
15
|
+
utils_path = f"hestia_earth.models.{MODEL}.{TERM_ID}_utils"
|
|
16
|
+
term_path = "hestia_earth.models.utils.term"
|
|
17
|
+
property_path = "hestia_earth.models.utils.property"
|
|
18
|
+
|
|
19
|
+
fixtures_folder = f"{fixtures_path}/{MODEL}/{TERM_ID}"
|
|
20
|
+
|
|
21
|
+
_ITERATIONS = 1000
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _load_fixture(path: str, default=None):
|
|
25
|
+
if isfile(path):
|
|
26
|
+
with open(path, encoding="utf-8") as f:
|
|
27
|
+
return json.load(f)
|
|
28
|
+
return default
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _fake_calc_descriptive_stats(arr: NDArray, *_args, **_kwargs):
|
|
32
|
+
return {"value": [round(row[0], 6) for row in arr]}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# subfolder, should_run
|
|
36
|
+
PARAMS_SHOULD_RUN = [
|
|
37
|
+
("forest-to-cropland", True),
|
|
38
|
+
("forest-to-cropland-greater-than-100", True),
|
|
39
|
+
("forest-to-cropland-less-than-100", True),
|
|
40
|
+
("forest-to-cropland-lcc-q2", True),
|
|
41
|
+
("forest-to-cropland-lcc-q3", True),
|
|
42
|
+
("forest-to-cropland-lcc-q4", True),
|
|
43
|
+
("forest-to-orchard", True),
|
|
44
|
+
("forest-to-orchard-with-backup-factors", True),
|
|
45
|
+
("forest-to-orchard-with-in-category-lcc", True),
|
|
46
|
+
("historical-land-cover-mix", True),
|
|
47
|
+
("historical-argentina-pasture", True),
|
|
48
|
+
("historical-brazil-maize", True),
|
|
49
|
+
("perennial-to-grassland-with-pasture-condition", True)
|
|
50
|
+
]
|
|
51
|
+
IDS_SHOULD_RUN = [p[0] for p in PARAMS_SHOULD_RUN]
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@mark.parametrize("subfolder, should_run", PARAMS_SHOULD_RUN, ids=IDS_SHOULD_RUN)
|
|
55
|
+
def test_should_run(subfolder: str, should_run: bool):
|
|
56
|
+
folder = f"{fixtures_folder}/{subfolder}"
|
|
57
|
+
|
|
58
|
+
site = _load_fixture(f"{folder}/site.jsonld", {})
|
|
59
|
+
|
|
60
|
+
result, *_ = _should_run(site)
|
|
61
|
+
assert result == should_run
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def test_should_run_no_data():
|
|
65
|
+
SITE = {}
|
|
66
|
+
EXPECTED = False
|
|
67
|
+
|
|
68
|
+
result, *_ = _should_run(SITE)
|
|
69
|
+
assert result == EXPECTED
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
PARAMS_RUN = [subfolder for subfolder, should_run in PARAMS_SHOULD_RUN if should_run]
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@mark.parametrize("subfolder", PARAMS_RUN)
|
|
76
|
+
@patch(f"{class_path}.calc_descriptive_stats", side_effect=_fake_calc_descriptive_stats)
|
|
77
|
+
@patch(f"{class_path}._new_measurement", side_effect=fake_new_measurement)
|
|
78
|
+
@patch(f"{utils_path}._get_sample_func", return_value=sample_constant)
|
|
79
|
+
def test_run(
|
|
80
|
+
_get_sample_func_mock: MagicMock,
|
|
81
|
+
_new_measurement_mock: MagicMock,
|
|
82
|
+
_calc_descriptive_stats_mock: MagicMock,
|
|
83
|
+
subfolder: str
|
|
84
|
+
):
|
|
85
|
+
folder = f"{fixtures_folder}/{subfolder}"
|
|
86
|
+
|
|
87
|
+
site = _load_fixture(f"{folder}/site.jsonld", {})
|
|
88
|
+
expected = _load_fixture(f"{folder}/result.jsonld", [])
|
|
89
|
+
|
|
90
|
+
with patch(f"{class_path}._ITERATIONS", _ITERATIONS):
|
|
91
|
+
result = run(site)
|
|
92
|
+
|
|
93
|
+
assert result == expected
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
# subfolder
|
|
97
|
+
PARAMS_RUN_WITH_STATS = [
|
|
98
|
+
"forest-to-cropland-with-stats",
|
|
99
|
+
"forest-to-orchard-with-in-category-lcc-with-stats",
|
|
100
|
+
"historical-land-cover-mix-with-stats"
|
|
101
|
+
]
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@mark.parametrize("subfolder", PARAMS_RUN_WITH_STATS)
|
|
105
|
+
@patch(f"{class_path}._new_measurement", side_effect=fake_new_measurement)
|
|
106
|
+
def test_run_with_stats(
|
|
107
|
+
_new_measurement_mock: MagicMock,
|
|
108
|
+
subfolder: str
|
|
109
|
+
):
|
|
110
|
+
folder = f"{fixtures_folder}/{subfolder}"
|
|
111
|
+
|
|
112
|
+
site = _load_fixture(f"{folder}/site.jsonld", {})
|
|
113
|
+
expected = _load_fixture(f"{folder}/result.jsonld", [])
|
|
114
|
+
|
|
115
|
+
with patch(f"{class_path}._ITERATIONS", _ITERATIONS):
|
|
116
|
+
result = run(site)
|
|
117
|
+
|
|
118
|
+
assert result == expected
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
# input, expected
|
|
122
|
+
PARAMS_RESCALE_CATEGORY_COVER = [
|
|
123
|
+
(
|
|
124
|
+
{BiomassCategory.ANNUAL_CROPS: 90},
|
|
125
|
+
{BiomassCategory.ANNUAL_CROPS: 90, BiomassCategory.OTHER: 10}
|
|
126
|
+
),
|
|
127
|
+
(
|
|
128
|
+
{BiomassCategory.OTHER: 90},
|
|
129
|
+
{BiomassCategory.OTHER: 100}
|
|
130
|
+
),
|
|
131
|
+
(
|
|
132
|
+
{BiomassCategory.ANNUAL_CROPS: 60, BiomassCategory.VINE: 60},
|
|
133
|
+
{BiomassCategory.ANNUAL_CROPS: 50, BiomassCategory.VINE: 50}
|
|
134
|
+
),
|
|
135
|
+
(
|
|
136
|
+
{BiomassCategory.NATURAL_FOREST: 100},
|
|
137
|
+
{BiomassCategory.NATURAL_FOREST: 100}
|
|
138
|
+
)
|
|
139
|
+
]
|
|
140
|
+
IDS_RESCALE_CATEGORY_COVER = ["fill", "fill w/ other", "squash", "do nothing"]
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
@mark.parametrize("input, expected", PARAMS_RESCALE_CATEGORY_COVER, ids=IDS_RESCALE_CATEGORY_COVER)
|
|
144
|
+
def test_rescale_category_cover(input: dict, expected: dict):
|
|
145
|
+
assert _rescale_category_cover(input) == expected
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
# a, b, expected
|
|
149
|
+
PARAMS_IS_LCC_EVENT = [
|
|
150
|
+
(
|
|
151
|
+
{
|
|
152
|
+
"appleTree": 33.333,
|
|
153
|
+
"pearTree": 33.333,
|
|
154
|
+
BiomassCategory.ANNUAL_CROPS: 33.334,
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
"appleTree": 33.33333,
|
|
158
|
+
"pearTree": 33.33333,
|
|
159
|
+
BiomassCategory.ANNUAL_CROPS: 33.33334,
|
|
160
|
+
},
|
|
161
|
+
True
|
|
162
|
+
),
|
|
163
|
+
(
|
|
164
|
+
{
|
|
165
|
+
"appleTree": 33.3333,
|
|
166
|
+
"pearTree": 33.3333,
|
|
167
|
+
BiomassCategory.ANNUAL_CROPS: 33.3334,
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
"appleTree": 33.33333,
|
|
171
|
+
"pearTree": 33.33333,
|
|
172
|
+
BiomassCategory.ANNUAL_CROPS: 33.33334,
|
|
173
|
+
},
|
|
174
|
+
False
|
|
175
|
+
)
|
|
176
|
+
]
|
|
177
|
+
IDS_IS_LCC_EVENT = ["True", "False"]
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
@mark.parametrize("a, b, expected", PARAMS_IS_LCC_EVENT, ids=IDS_IS_LCC_EVENT)
|
|
181
|
+
def test_is_lcc_event(a: dict, b: dict, expected: bool):
|
|
182
|
+
assert _is_lcc_event(a, b) is expected
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
from pytest import mark
|
|
2
|
+
|
|
3
|
+
from itertools import product
|
|
4
|
+
from hestia_earth.models.ipcc2019.aboveGroundBiomass_utils import (
|
|
5
|
+
_build_col_name, _get_sample_func, assign_biomass_category, BiomassCategory, EcoClimateZone,
|
|
6
|
+
sample_biomass_equilibrium, sample_constant, sample_plus_minus_error
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
_ITERATIONS = 1000
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# kwargs, sample_func, expected_shape
|
|
13
|
+
PARAMS_GET_SAMPLE_FUNC = [
|
|
14
|
+
({"value": 1}, sample_constant),
|
|
15
|
+
({"value": 1, "error": 10}, sample_plus_minus_error),
|
|
16
|
+
]
|
|
17
|
+
IDS_GET_SAMPLE_FUNC = ["constant", "+/- error"]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@mark.parametrize("kwargs, sample_func", PARAMS_GET_SAMPLE_FUNC, ids=IDS_GET_SAMPLE_FUNC)
|
|
21
|
+
def test_get_sample_func(kwargs, sample_func):
|
|
22
|
+
result = _get_sample_func(kwargs)
|
|
23
|
+
assert result == sample_func
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
PARAMS_BIOMASS = [p for p in product(BiomassCategory, EcoClimateZone)]
|
|
27
|
+
IDS_BIOMASS = [f"{p[0].name} + {p[1].name}" for p in PARAMS_BIOMASS]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@mark.parametrize("biomass_category, eco_climate_zone", PARAMS_BIOMASS, ids=IDS_BIOMASS)
|
|
31
|
+
def test_sample_biomass_equilibrium(biomass_category, eco_climate_zone):
|
|
32
|
+
EXPECTED_SHAPE = (1, _ITERATIONS)
|
|
33
|
+
result = sample_biomass_equilibrium(_ITERATIONS, biomass_category, eco_climate_zone)
|
|
34
|
+
assert result.shape == EXPECTED_SHAPE
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# input, expected
|
|
38
|
+
PARAMS_ASSIGN_BIOMASS_CATEGORY = [
|
|
39
|
+
("Annual crops", BiomassCategory.ANNUAL_CROPS),
|
|
40
|
+
("Coconut", BiomassCategory.COCONUT),
|
|
41
|
+
("Forest", BiomassCategory.FOREST),
|
|
42
|
+
("Grassland", BiomassCategory.GRASSLAND),
|
|
43
|
+
("Jatropha", BiomassCategory.JATROPHA),
|
|
44
|
+
("Jojoba", BiomassCategory.JOJOBA),
|
|
45
|
+
("Natural forest", BiomassCategory.NATURAL_FOREST),
|
|
46
|
+
("Oil palm", BiomassCategory.OIL_PALM),
|
|
47
|
+
("Olive", BiomassCategory.OLIVE),
|
|
48
|
+
("Orchard", BiomassCategory.ORCHARD),
|
|
49
|
+
("Other", BiomassCategory.OTHER),
|
|
50
|
+
("Plantation forest", BiomassCategory.PLANTATION_FOREST),
|
|
51
|
+
("Rubber", BiomassCategory.RUBBER),
|
|
52
|
+
("Short rotation coppice", BiomassCategory.SHORT_ROTATION_COPPICE),
|
|
53
|
+
("Tea", BiomassCategory.TEA),
|
|
54
|
+
("Vine", BiomassCategory.VINE),
|
|
55
|
+
("Woody perennial", BiomassCategory.WOODY_PERENNIAL),
|
|
56
|
+
("Miscellaneous value", None)
|
|
57
|
+
]
|
|
58
|
+
IDS_ASSIGN_BIOMASS_CATEGORY = [p[0] for p in PARAMS_ASSIGN_BIOMASS_CATEGORY]
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@mark.parametrize("input, expected", PARAMS_ASSIGN_BIOMASS_CATEGORY, ids=IDS_ASSIGN_BIOMASS_CATEGORY)
|
|
62
|
+
def test_assign_biomass_category(input: str, expected: BiomassCategory):
|
|
63
|
+
assert assign_biomass_category(input) == expected
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# input, expected
|
|
67
|
+
PARAMS_BUILD_COLUMN_NAME = [
|
|
68
|
+
(BiomassCategory.ANNUAL_CROPS, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_ANNUAL_CROPS"),
|
|
69
|
+
(BiomassCategory.COCONUT, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_COCONUT"),
|
|
70
|
+
(BiomassCategory.FOREST, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_FOREST"),
|
|
71
|
+
(BiomassCategory.GRASSLAND, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_GRASSLAND"),
|
|
72
|
+
(BiomassCategory.JATROPHA, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_JATROPHA"),
|
|
73
|
+
(BiomassCategory.JOJOBA, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_JOJOBA"),
|
|
74
|
+
(BiomassCategory.NATURAL_FOREST, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_NATURAL_FOREST"),
|
|
75
|
+
(BiomassCategory.OIL_PALM, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_OIL_PALM"),
|
|
76
|
+
(BiomassCategory.OLIVE, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_OLIVE"),
|
|
77
|
+
(BiomassCategory.ORCHARD, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_ORCHARD"),
|
|
78
|
+
(BiomassCategory.OTHER, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_OTHER"),
|
|
79
|
+
(BiomassCategory.PLANTATION_FOREST, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_PLANTATION_FOREST"),
|
|
80
|
+
(BiomassCategory.RUBBER, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_RUBBER"),
|
|
81
|
+
(BiomassCategory.SHORT_ROTATION_COPPICE, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_SHORT_ROTATION_COPPICE"),
|
|
82
|
+
(BiomassCategory.TEA, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_TEA"),
|
|
83
|
+
(BiomassCategory.VINE, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_VINE"),
|
|
84
|
+
(BiomassCategory.WOODY_PERENNIAL, "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_WOODY_PERENNIAL"),
|
|
85
|
+
("Miscellaneous value", "AG_BIOMASS_EQUILIBRIUM_KG_C_HECTARE_OTHER")
|
|
86
|
+
]
|
|
87
|
+
IDS_BUILD_COLUMN_NAME = [p[0] for p in PARAMS_BUILD_COLUMN_NAME]
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@mark.parametrize("input, expected", PARAMS_BUILD_COLUMN_NAME, ids=IDS_BUILD_COLUMN_NAME)
|
|
91
|
+
def test_build_col_name(input: BiomassCategory, expected: str):
|
|
92
|
+
assert _build_col_name(input) == expected
|
|
@@ -5,10 +5,11 @@ from numpy.testing import assert_array_almost_equal
|
|
|
5
5
|
from pytest import mark
|
|
6
6
|
from unittest.mock import MagicMock, patch
|
|
7
7
|
|
|
8
|
+
from hestia_earth.models.utils.ecoClimateZone import EcoClimateZone
|
|
8
9
|
from hestia_earth.models.ipcc2019.organicCarbonPerHa import MODEL, TERM_ID
|
|
9
10
|
from hestia_earth.models.ipcc2019.organicCarbonPerHa_utils import (
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
IpccCarbonInputCategory, IpccLandUseCategory, IpccManagementCategory, IpccSoilCategory, sample_constant,
|
|
12
|
+
sample_plus_minus_error, sample_plus_minus_uncertainty
|
|
12
13
|
)
|
|
13
14
|
from hestia_earth.models.ipcc2019.organicCarbonPerHa_tier_1_utils import (
|
|
14
15
|
_assign_ipcc_carbon_input_category, _assign_ipcc_land_use_category, _assign_ipcc_management_category,
|
|
@@ -27,11 +27,11 @@ TERMS_BY_ID = {
|
|
|
27
27
|
]
|
|
28
28
|
},
|
|
29
29
|
'energyContentHigherHeatingValue': {'units': 'MJ / kg'},
|
|
30
|
-
'energyDigestibilityRuminants': {'units': '%'}
|
|
30
|
+
'energyDigestibilityRuminants': {'units': '%'}
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
|
|
34
|
-
def fake_download_hestia(term_id: str, *args): return TERMS_BY_ID
|
|
34
|
+
def fake_download_hestia(term_id: str, *args): return TERMS_BY_ID.get(term_id, {})
|
|
35
35
|
|
|
36
36
|
|
|
37
37
|
@patch(f"{class_path_utils}.download_hestia", side_effect=fake_download_hestia)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from unittest.mock import patch
|
|
3
|
+
|
|
4
|
+
from hestia_earth.models.poschEtAl2008.terrestrialEutrophicationPotentialAccumulatedExceedance import MODEL, TERM_ID, \
|
|
5
|
+
run
|
|
6
|
+
from tests.utils import fixtures_path, fake_new_indicator
|
|
7
|
+
|
|
8
|
+
class_path = f"hestia_earth.models.{MODEL}.{TERM_ID}"
|
|
9
|
+
fixtures_folder = f"{fixtures_path}/{MODEL}/{TERM_ID}"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@patch(f"{class_path}._new_indicator", side_effect=fake_new_indicator)
|
|
13
|
+
def test_run(*args):
|
|
14
|
+
with open(f"{fixtures_folder}/impact-assessment.jsonld", encoding='utf-8') as f:
|
|
15
|
+
cycle = json.load(f)
|
|
16
|
+
|
|
17
|
+
with open(f"{fixtures_folder}/result.jsonld", encoding='utf-8') as f:
|
|
18
|
+
expected = json.load(f)
|
|
19
|
+
|
|
20
|
+
value = run(cycle)
|
|
21
|
+
assert value == expected
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@patch(f"{class_path}._new_indicator", side_effect=fake_new_indicator)
|
|
25
|
+
def test_lookup_to_bad_country(*args):
|
|
26
|
+
with open(f"{fixtures_folder}/impact-assessment.jsonld", encoding='utf-8') as f:
|
|
27
|
+
cycle = json.load(f)
|
|
28
|
+
cycle['country']['@id'] = "example-land-not-real"
|
|
29
|
+
|
|
30
|
+
value = run(cycle)
|
|
31
|
+
assert value['value'] is None
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@patch(f"{class_path}._new_indicator", side_effect=fake_new_indicator)
|
|
35
|
+
def test_lookup_no_term_type(*args):
|
|
36
|
+
"""
|
|
37
|
+
We currently do not filter out bad termTypes so this is expected behavior.
|
|
38
|
+
"""
|
|
39
|
+
with open(f"{fixtures_folder}/impact-assessment.jsonld", encoding='utf-8') as f:
|
|
40
|
+
cycle = json.load(f)
|
|
41
|
+
del cycle['emissionsResourceUse'][0]['term']['termType']
|
|
42
|
+
|
|
43
|
+
value = run(cycle)
|
|
44
|
+
assert value['value'] == 7.47
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
from pytest import mark
|
|
2
|
+
from typing import Any
|
|
3
|
+
from unittest.mock import MagicMock, patch
|
|
4
|
+
|
|
5
|
+
from hestia_earth.models.utils.ecoClimateZone import (
|
|
6
|
+
_eco_climate_zone_node_value_to_enum, EcoClimateZone, get_eco_climate_zone_value,
|
|
7
|
+
get_ecoClimateZone_lookup_grouped_value
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
class_path = "hestia_earth.models.utils.ecoClimateZone"
|
|
11
|
+
|
|
12
|
+
# value, expected
|
|
13
|
+
PARAMS_TO_ENUM = [
|
|
14
|
+
(0, None),
|
|
15
|
+
(1, EcoClimateZone.WARM_TEMPERATE_MOIST),
|
|
16
|
+
(2, EcoClimateZone.WARM_TEMPERATE_DRY),
|
|
17
|
+
(3, EcoClimateZone.COOL_TEMPERATE_MOIST),
|
|
18
|
+
(4, EcoClimateZone.COOL_TEMPERATE_DRY),
|
|
19
|
+
(5, EcoClimateZone.POLAR_MOIST),
|
|
20
|
+
(6, EcoClimateZone.POLAR_DRY),
|
|
21
|
+
(7, EcoClimateZone.BOREAL_MOIST),
|
|
22
|
+
(8, EcoClimateZone.BOREAL_DRY),
|
|
23
|
+
(9, EcoClimateZone.TROPICAL_MONTANE),
|
|
24
|
+
(10, EcoClimateZone.TROPICAL_WET),
|
|
25
|
+
(11, EcoClimateZone.TROPICAL_MOIST),
|
|
26
|
+
(12, EcoClimateZone.TROPICAL_DRY),
|
|
27
|
+
(13, None),
|
|
28
|
+
("string", None),
|
|
29
|
+
(None, None)
|
|
30
|
+
]
|
|
31
|
+
IDS_TO_ENUM = [str(input_) for input_, _ in PARAMS_TO_ENUM]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@mark.parametrize("value, expected", PARAMS_TO_ENUM, ids=IDS_TO_ENUM)
|
|
35
|
+
def test_eco_climate_zone_node_value_to_enum(value, expected):
|
|
36
|
+
assert _eco_climate_zone_node_value_to_enum(value) == expected
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# node, as_enum, expected
|
|
40
|
+
PARAMS_GET_ECZ_VALUE = [
|
|
41
|
+
(
|
|
42
|
+
{
|
|
43
|
+
"@type": "Site",
|
|
44
|
+
"measurements": [
|
|
45
|
+
{
|
|
46
|
+
"@type": "Measurement",
|
|
47
|
+
"term": {
|
|
48
|
+
"@type": "Term",
|
|
49
|
+
"@id": "ecoClimateZone"
|
|
50
|
+
},
|
|
51
|
+
"value": [2],
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
False,
|
|
56
|
+
2
|
|
57
|
+
),
|
|
58
|
+
(
|
|
59
|
+
{
|
|
60
|
+
"@type": "Site",
|
|
61
|
+
"measurements": [
|
|
62
|
+
{
|
|
63
|
+
"@type": "Measurement",
|
|
64
|
+
"term": {
|
|
65
|
+
"@type": "Term",
|
|
66
|
+
"@id": "ecoClimateZone"
|
|
67
|
+
},
|
|
68
|
+
"value": [2],
|
|
69
|
+
}
|
|
70
|
+
]
|
|
71
|
+
},
|
|
72
|
+
True,
|
|
73
|
+
EcoClimateZone.WARM_TEMPERATE_DRY
|
|
74
|
+
),
|
|
75
|
+
(
|
|
76
|
+
{
|
|
77
|
+
"@type": "Cycle",
|
|
78
|
+
"site": {
|
|
79
|
+
"@type": "Site",
|
|
80
|
+
"measurements": [
|
|
81
|
+
{
|
|
82
|
+
"@type": "Measurement",
|
|
83
|
+
"term": {
|
|
84
|
+
"@type": "Term",
|
|
85
|
+
"@id": "ecoClimateZone"
|
|
86
|
+
},
|
|
87
|
+
"value": [2],
|
|
88
|
+
}
|
|
89
|
+
]
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
False,
|
|
93
|
+
2
|
|
94
|
+
),
|
|
95
|
+
(
|
|
96
|
+
{
|
|
97
|
+
"@type": "Cycle",
|
|
98
|
+
"site": {
|
|
99
|
+
"@type": "Site",
|
|
100
|
+
"measurements": [
|
|
101
|
+
{
|
|
102
|
+
"@type": "Measurement",
|
|
103
|
+
"term": {
|
|
104
|
+
"@type": "Term",
|
|
105
|
+
"@id": "ecoClimateZone"
|
|
106
|
+
},
|
|
107
|
+
"value": [2],
|
|
108
|
+
}
|
|
109
|
+
]
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
True,
|
|
113
|
+
EcoClimateZone.WARM_TEMPERATE_DRY
|
|
114
|
+
),
|
|
115
|
+
({}, False, None),
|
|
116
|
+
({}, True, None),
|
|
117
|
+
]
|
|
118
|
+
IDS_GET_ECZ_VALUE = [
|
|
119
|
+
"site", "site as enum",
|
|
120
|
+
"cycle", "cycle as enum",
|
|
121
|
+
"other", "other as enum"
|
|
122
|
+
]
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
@mark.parametrize("node, as_enum, expected", PARAMS_GET_ECZ_VALUE, ids=IDS_GET_ECZ_VALUE)
|
|
126
|
+
def test_get_eco_climate_zone_value(node, as_enum, expected):
|
|
127
|
+
assert get_eco_climate_zone_value(node, as_enum=as_enum) == expected
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
# lookup_return_value, expected
|
|
131
|
+
PARAMS_LOOKUP_GROUPED = [
|
|
132
|
+
("1", None),
|
|
133
|
+
("-", None),
|
|
134
|
+
("", None),
|
|
135
|
+
("value:1", {"value": 1}),
|
|
136
|
+
("value:-1", {"value": -1}),
|
|
137
|
+
("value:1;sd:0.5", {"value": 1, "sd": 0.5})
|
|
138
|
+
]
|
|
139
|
+
IDS_LOOKUP_GROUPED = [p[0] for p in PARAMS_LOOKUP_GROUPED]
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
@mark.parametrize("lookup_return_value, expected", PARAMS_LOOKUP_GROUPED, ids=IDS_LOOKUP_GROUPED)
|
|
143
|
+
@patch(f"{class_path}.download_lookup", return_value=None)
|
|
144
|
+
@patch(f"{class_path}._get_single_table_value")
|
|
145
|
+
def test_get_ecoClimateZone_lookup_grouped_value(
|
|
146
|
+
get_single_table_value_mock: MagicMock,
|
|
147
|
+
_download_lookup_mock: MagicMock,
|
|
148
|
+
lookup_return_value: str,
|
|
149
|
+
expected: Any
|
|
150
|
+
):
|
|
151
|
+
get_single_table_value_mock.return_value = lookup_return_value
|
|
152
|
+
assert get_ecoClimateZone_lookup_grouped_value(1, "TEST") == expected
|
|
File without changes
|
|
File without changes
|
|
File without changes
|