hestia-earth-models 0.64.11__py3-none-any.whl → 0.64.12__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.

Files changed (25) hide show
  1. hestia_earth/models/cycle/concentrateFeed.py +31 -19
  2. hestia_earth/models/faostat2018/utils.py +72 -12
  3. hestia_earth/models/hestia/__init__.py +13 -0
  4. hestia_earth/models/hestia/landCover.py +725 -0
  5. hestia_earth/models/ipcc2019/animal/fatContent.py +1 -1
  6. hestia_earth/models/ipcc2019/animal/milkYieldPerAnimal.py +91 -0
  7. hestia_earth/models/ipcc2019/animal/trueProteinContent.py +1 -1
  8. hestia_earth/models/ipcc2019/animal/utils.py +17 -12
  9. hestia_earth/models/ipcc2019/co2ToAirAboveGroundBiomassStockChange.py +8 -4
  10. hestia_earth/models/ipcc2019/co2ToAirBelowGroundBiomassStockChange.py +7 -3
  11. hestia_earth/models/ipcc2019/co2ToAirCarbonStockChange_utils.py +45 -3
  12. hestia_earth/models/ipcc2019/co2ToAirSoilOrganicCarbonStockChange.py +7 -3
  13. hestia_earth/models/mocking/search-results.json +914 -914
  14. hestia_earth/models/utils/lookup.py +2 -1
  15. hestia_earth/models/version.py +1 -1
  16. {hestia_earth_models-0.64.11.dist-info → hestia_earth_models-0.64.12.dist-info}/METADATA +1 -1
  17. {hestia_earth_models-0.64.11.dist-info → hestia_earth_models-0.64.12.dist-info}/RECORD +25 -18
  18. tests/models/faostat2018/test_faostat_utils.py +84 -0
  19. tests/models/hestia/__init__.py +0 -0
  20. tests/models/hestia/test_landCover.py +209 -0
  21. tests/models/ipcc2019/animal/test_milkYieldPerAnimal.py +21 -0
  22. tests/models/ipcc2019/test_co2ToAirSoilOrganicCarbonStockChange.py +48 -1
  23. {hestia_earth_models-0.64.11.dist-info → hestia_earth_models-0.64.12.dist-info}/LICENSE +0 -0
  24. {hestia_earth_models-0.64.11.dist-info → hestia_earth_models-0.64.12.dist-info}/WHEEL +0 -0
  25. {hestia_earth_models-0.64.11.dist-info → hestia_earth_models-0.64.12.dist-info}/top_level.txt +0 -0
@@ -8,7 +8,7 @@ from hestia_earth.schema import TermTermType
8
8
  from hestia_earth.utils.model import find_primary_product
9
9
  from hestia_earth.utils.tools import list_sum, non_empty_list, flatten
10
10
 
11
- from hestia_earth.models.log import logRequirements, logShouldRun
11
+ from hestia_earth.models.log import logRequirements, logShouldRun, log_as_table
12
12
  from hestia_earth.models.utils.blank_node import merge_blank_nodes
13
13
  from hestia_earth.models.utils.property import _new_property, get_node_property_value, get_property_lookup_value
14
14
  from hestia_earth.models.utils.term import get_digestible_energy_terms, get_energy_digestibility_terms
@@ -42,7 +42,6 @@ LOOKUPS = {
42
42
  "property": "commonToSupplementInAnimalFeed"
43
43
  }
44
44
  TERM_ID = 'concentrateFeedBlend,concentrateFeedUnspecified,feedMix'
45
- FIRST_TERM_ID = TERM_ID.split(',')[0]
46
45
  INPUT_TERM_TYPES = [
47
46
  TermTermType.CROP.value,
48
47
  TermTermType.FORAGE.value,
@@ -70,40 +69,53 @@ def _weighted_value(values: list):
70
69
 
71
70
 
72
71
  def _calculate_value(cycle: dict, product: dict, inputs: list, property_id: str, values: list):
73
- values = [(prop_value, input_value) for id, prop_value, input_value in values if input_value and prop_value]
74
- ratio_inputs_with_props = len(values) / len(inputs) if len(inputs) and len(values) else 0
72
+ valid_values = [
73
+ (value.get('property-value'), value.get('input-value'))
74
+ for value in values
75
+ if all([value.get('property-value'), value.get('input-value')])
76
+ ]
77
+ ratio_inputs_with_props = len(valid_values) / len(inputs) if len(inputs) and len(valid_values) else 0
75
78
  min_ratio = _min_ratio(property_id)
76
79
 
77
80
  term_id = product.get('term', {}).get('@id')
78
81
  logRequirements(cycle, model=MODEL, term=term_id, property=property_id,
79
82
  nb_inputs=len(inputs),
80
- nb_inputs_with_prop=len(values),
83
+ nb_inputs_with_prop=len(valid_values),
81
84
  ratio_inputs_with_props=ratio_inputs_with_props,
82
- min_ratio=min_ratio)
85
+ min_ratio=min_ratio,
86
+ details=log_as_table(values))
83
87
 
84
88
  should_run = all([ratio_inputs_with_props >= min_ratio])
85
89
  logShouldRun(cycle, MODEL, term_id, should_run, property=property_id)
86
90
 
87
- return [(property_id, _weighted_value(values))] if should_run else []
91
+ return [(property_id, _weighted_value(valid_values))] if should_run else []
88
92
 
89
93
 
90
94
  def _calculate_default_value(cycle: dict, product: dict, inputs: list, property_id: str):
91
- values = [(
92
- i.get('term', {}).get('@id'),
93
- get_node_property_value(MODEL, i, property_id, handle_percents=False, term=FIRST_TERM_ID),
94
- list_sum(i.get('value', []))
95
- ) for i in inputs]
95
+ term_id = product.get('term', {}).get('@id')
96
+ values = [{
97
+ 'input-id': i.get('term', {}).get('@id'),
98
+ 'input-value': list_sum(i.get('value', [])),
99
+ 'property-id': property_id,
100
+ 'property-value': get_node_property_value(
101
+ MODEL, i, property_id, handle_percents=False, term=term_id, property=property_id
102
+ ),
103
+ } for i in inputs]
96
104
  return _calculate_value(cycle, product, inputs, property_id, values)
97
105
 
98
106
 
99
107
  def _calculate_N_value(cycle: dict, product: dict, inputs: list, property_id: str):
100
- values = [(
101
- i.get('term', {}).get('@id'),
102
- get_node_property_value(MODEL, i, property_id, handle_percents=False, term=FIRST_TERM_ID) or
103
- get_node_property_value(
104
- MODEL, i, 'crudeProteinContent', default=0, handle_percents=False, term=FIRST_TERM_ID) * 0.16,
105
- list_sum(i.get('value', []))
106
- ) for i in inputs]
108
+ term_id = product.get('term', {}).get('@id')
109
+ values = [{
110
+ 'input-id': i.get('term', {}).get('@id'),
111
+ 'input-value': list_sum(i.get('value', [])),
112
+ 'property-id': property_id,
113
+ 'property-value': get_node_property_value(
114
+ MODEL, i, property_id, handle_percents=False, term=term_id, property=property_id
115
+ ) or get_node_property_value(
116
+ MODEL, i, 'crudeProteinContent', default=0, handle_percents=False, term=term_id, property=property_id
117
+ ) * 0.16
118
+ } for i in inputs]
107
119
  return _calculate_value(cycle, product, inputs, property_id, values)
108
120
 
109
121
 
@@ -2,6 +2,7 @@ from hestia_earth.schema import TermTermType
2
2
  from hestia_earth.utils.api import download_hestia
3
3
  from hestia_earth.utils.lookup import download_lookup, get_table_value, column_name, extract_grouped_data_closest_date
4
4
  from hestia_earth.utils.tools import safe_parse_float
5
+ from numpy import recarray
5
6
 
6
7
  from hestia_earth.models.log import logger, debugMissingLookup
7
8
  from hestia_earth.models.utils.animalProduct import (
@@ -53,7 +54,7 @@ def product_equivalent_value(product: dict, year: int, country: str):
53
54
  return conv_value
54
55
 
55
56
 
56
- def _cropland_split_delta(table_value: str, start_year: int, end_year: int):
57
+ def _split_delta(table_value: str, start_year: int, end_year: int):
57
58
  start_value = extract_grouped_data_closest_date(table_value, start_year)
58
59
  end_value = extract_grouped_data_closest_date(table_value, end_year)
59
60
  return safe_parse_float(end_value) - safe_parse_float(start_value) if all([
@@ -61,22 +62,81 @@ def _cropland_split_delta(table_value: str, start_year: int, end_year: int):
61
62
  ]) else None
62
63
 
63
64
 
64
- def get_cropland_ratio(country: str, start_year: int, end_year: int):
65
+ def get_sum_of_columns(lookup: recarray, country: str, year: int, columns_list: list) -> float:
66
+ return sum(
67
+ [safe_parse_float(
68
+ extract_grouped_data_closest_date(
69
+ data=get_table_value(lookup, 'termid', country, column_name(col)),
70
+ year=year
71
+ )
72
+ ) for col in columns_list]
73
+ )
74
+
75
+
76
+ def get_single_delta(country: str, start_year: int, end_year: int, column: str):
65
77
  lookup = download_lookup('region-faostatArea.csv')
66
- total_delta = _cropland_split_delta(
67
- get_table_value(lookup, 'termid', country, column_name('Cropland')), start_year, end_year
78
+ return _split_delta(
79
+ get_table_value(lookup, 'termid', country, column_name(column)), start_year, end_year
68
80
  )
69
81
 
70
- # get both values and only return result if we have both
71
- permanent_delta = _cropland_split_delta(
72
- get_table_value(lookup, 'termid', country, column_name('Permanent crops')), start_year, end_year
82
+
83
+ def get_land_ratio(
84
+ country: str, start_year: int, end_year: int, first_column: str, second_column: str, total_column: str = None
85
+ ):
86
+ """
87
+ total_column is optional. Assumes that, if missing, total is the sum of values from first and second.
88
+ """
89
+ lookup = download_lookup('region-faostatArea.csv')
90
+ first_delta = _split_delta(
91
+ get_table_value(lookup, 'termid', country, column_name(first_column)), start_year, end_year
92
+ )
93
+ second_delta = _split_delta(
94
+ get_table_value(lookup, 'termid', country, column_name(second_column)), start_year, end_year
73
95
  )
74
- temporary_delta = _cropland_split_delta(
75
- get_table_value(lookup, 'termid', country, column_name('Arable land')), start_year, end_year
96
+ total_delta = (
97
+ get_sum_of_columns(
98
+ lookup=lookup,
99
+ country=country,
100
+ year=end_year,
101
+ columns_list=[first_column, second_column]
102
+ ) - get_sum_of_columns(
103
+ lookup=lookup,
104
+ country=country,
105
+ year=start_year,
106
+ columns_list=[first_column, second_column]
107
+ )
108
+ ) if total_column is None else _split_delta(
109
+ get_table_value(lookup, 'termid', country, column_name(total_column)), start_year, end_year
76
110
  )
77
111
 
78
112
  return (None, None, None) if any([
79
113
  total_delta is None,
80
- permanent_delta is None,
81
- temporary_delta is None
82
- ]) else (total_delta, permanent_delta, temporary_delta)
114
+ first_delta is None,
115
+ second_delta is None
116
+ ]) else (total_delta, first_delta, second_delta)
117
+
118
+
119
+ def get_cropland_ratio(country: str, start_year: int, end_year: int):
120
+ return get_land_ratio(
121
+ country=country,
122
+ start_year=start_year,
123
+ end_year=end_year,
124
+ first_column='Permanent crops',
125
+ second_column='Arable land',
126
+ total_column='Cropland'
127
+ )
128
+
129
+
130
+ def get_change_in_harvested_area_for_crop(country_id: str, crop_name: str, start_year: int, end_year: int = 0):
131
+ lookup = download_lookup('region-crop-cropGroupingFaostatProduction-areaHarvested.csv')
132
+ if end_year == 0 or end_year == start_year:
133
+ return safe_parse_float(
134
+ extract_grouped_data_closest_date(
135
+ data=get_table_value(lookup, 'termid', country_id, column_name(crop_name)),
136
+ year=start_year
137
+ )
138
+ )
139
+ else:
140
+ return _split_delta(
141
+ get_table_value(lookup, 'termid', country_id, column_name(crop_name)), start_year, end_year
142
+ )
@@ -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 = 'hestia'
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))