mrio-toolbox 1.0.0__py3-none-any.whl → 1.1.2__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 mrio-toolbox might be problematic. Click here for more details.

Files changed (67) hide show
  1. __init__.py +21 -0
  2. {mrio_toolbox/_parts → _parts}/_Axe.py +95 -37
  3. {mrio_toolbox/_parts → _parts}/_Part.py +346 -111
  4. _parts/__init__.py +7 -0
  5. {mrio_toolbox/_parts → _parts}/part_operations.py +24 -17
  6. extractors/__init__.py +20 -0
  7. extractors/downloaders.py +36 -0
  8. extractors/emerging/__init__.py +3 -0
  9. extractors/emerging/emerging_extractor.py +117 -0
  10. extractors/eora/__init__.py +3 -0
  11. extractors/eora/eora_extractor.py +132 -0
  12. extractors/exiobase/__init__.py +3 -0
  13. extractors/exiobase/exiobase_extractor.py +270 -0
  14. extractors/extractors.py +81 -0
  15. extractors/figaro/__init__.py +3 -0
  16. extractors/figaro/figaro_downloader.py +280 -0
  17. extractors/figaro/figaro_extractor.py +187 -0
  18. extractors/gloria/__init__.py +3 -0
  19. extractors/gloria/gloria_extractor.py +202 -0
  20. extractors/gtap11/__init__.py +7 -0
  21. extractors/gtap11/extraction/__init__.py +3 -0
  22. extractors/gtap11/extraction/extractor.py +129 -0
  23. extractors/gtap11/extraction/harpy_files/__init__.py +6 -0
  24. extractors/gtap11/extraction/harpy_files/_header_sets.py +279 -0
  25. extractors/gtap11/extraction/harpy_files/har_file.py +262 -0
  26. extractors/gtap11/extraction/harpy_files/har_file_io.py +974 -0
  27. extractors/gtap11/extraction/harpy_files/header_array.py +300 -0
  28. extractors/gtap11/extraction/harpy_files/sl4.py +229 -0
  29. extractors/gtap11/gtap_mrio/__init__.py +6 -0
  30. extractors/gtap11/gtap_mrio/mrio_builder.py +158 -0
  31. extractors/icio/__init__.py +3 -0
  32. extractors/icio/icio_extractor.py +121 -0
  33. extractors/wiod/__init__.py +3 -0
  34. extractors/wiod/wiod_extractor.py +143 -0
  35. mrio_toolbox/mrio.py → mrio.py +254 -94
  36. {mrio_toolbox-1.0.0.dist-info → mrio_toolbox-1.1.2.dist-info}/METADATA +11 -7
  37. mrio_toolbox-1.1.2.dist-info/RECORD +59 -0
  38. {mrio_toolbox-1.0.0.dist-info → mrio_toolbox-1.1.2.dist-info}/WHEEL +1 -1
  39. mrio_toolbox-1.1.2.dist-info/top_level.txt +6 -0
  40. msm/__init__.py +6 -0
  41. msm/multi_scale_mapping.py +863 -0
  42. utils/__init__.py +3 -0
  43. utils/converters/__init__.py +5 -0
  44. {mrio_toolbox/utils → utils}/converters/pandas.py +5 -6
  45. {mrio_toolbox/utils → utils}/converters/xarray.py +6 -15
  46. utils/formatting/formatter.py +527 -0
  47. utils/loaders/__init__.py +7 -0
  48. {mrio_toolbox/utils → utils}/loaders/_loader.py +60 -4
  49. {mrio_toolbox/utils → utils}/loaders/_loader_factory.py +22 -1
  50. {mrio_toolbox/utils → utils}/loaders/_nc_loader.py +37 -1
  51. {mrio_toolbox/utils → utils}/loaders/_pandas_loader.py +29 -3
  52. {mrio_toolbox/utils → utils}/loaders/_parameter_loader.py +61 -16
  53. {mrio_toolbox/utils → utils}/savers/__init__.py +3 -0
  54. utils/savers/_path_checker.py +37 -0
  55. {mrio_toolbox/utils → utils}/savers/_to_folder.py +6 -1
  56. utils/savers/_to_nc.py +60 -0
  57. mrio_toolbox/__init__.py +0 -5
  58. mrio_toolbox/_parts/__init__.py +0 -3
  59. mrio_toolbox/utils/converters/__init__.py +0 -2
  60. mrio_toolbox/utils/loaders/__init__.py +0 -3
  61. mrio_toolbox/utils/savers/_path_checker.py +0 -19
  62. mrio_toolbox/utils/savers/_to_nc.py +0 -52
  63. mrio_toolbox-1.0.0.dist-info/RECORD +0 -26
  64. mrio_toolbox-1.0.0.dist-info/top_level.txt +0 -1
  65. {mrio_toolbox-1.0.0.dist-info → mrio_toolbox-1.1.2.dist-info/licenses}/LICENSE +0 -0
  66. {mrio_toolbox/utils → utils/formatting}/__init__.py +0 -0
  67. {mrio_toolbox/utils → utils}/loaders/_np_loader.py +0 -0
@@ -0,0 +1,3 @@
1
+ """
2
+ This module contains the ICIO extractor.
3
+ """
@@ -0,0 +1,121 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Wed May 11 16:15:09 2022
4
+ Major modifications on 04.02.2025 by wirth
5
+
6
+ Load and convert ICIO MRIO files.
7
+ Please put the Readme excel file in the same folder as the data and adjust the filename in the code.
8
+
9
+ Supports ICIO 2021 in csv format
10
+ https://www.oecd.org/sti/ind/inter-country-input-output-tables.htm
11
+
12
+ @author: beaufils and wirth
13
+ """
14
+
15
+ import os
16
+ import logging
17
+ import pandas as pd
18
+
19
+ from mrio_toolbox import MRIO, Part
20
+ from mrio_toolbox.utils.savers._to_nc import save_to_nc
21
+
22
+ log = logging.getLogger(__name__)
23
+
24
+ def extract_icio(year, source, destination, extended=False):
25
+ """
26
+ Extract and convert ICIO MRIO data to NetCDF format for use with mrio_toolbox.
27
+
28
+ This function loads ICIO tables and associated labels from raw OECD files,
29
+ processes them, and stores the result as a NetCDF file. It supports both
30
+ standard and extended ICIO formats (where China and Mexico are split).
31
+
32
+ Parameters
33
+ ----------
34
+ year : str
35
+ Year of the ICIO data to load (e.g., "2021").
36
+ source : path-like
37
+ Path to the folder containing the raw ICIO data and ReadMe Excel file.
38
+ extended : bool, optional
39
+ If True, loads the extended version of the ICIO tables (China and Mexico split).
40
+ If False (default), loads the standard version.
41
+
42
+ Notes
43
+ -----
44
+ - The ReadMe Excel file must be present in the same source folder as the xlsx data file.
45
+ - Output filenames are automatically generated based on the year and format.
46
+ """
47
+
48
+ #Check source path
49
+ if not os.path.exists(source):
50
+ log.error(f"{os.path.abspath(source)} does not exist.")
51
+ raise NotADirectoryError(f"{os.path.abspath(source)} does not exist.")
52
+
53
+ #Check destination path
54
+ if not os.path.exists(destination):
55
+ log.info(f"{os.path.abspath(
56
+ destination)} does not exist. Creating directory.")
57
+ os.makedirs(destination)
58
+
59
+ # Adapt the filenames based on the extended parameter
60
+ if extended:
61
+ readme_filename = os.path.join(source, "ReadMe_ICIO_extended.xlsx")
62
+ data_filename = os.path.join(source, f"{year}.csv")
63
+ else:
64
+ readme_filename = os.path.join(source,"ReadMe_ICIO_small.xlsx")
65
+ data_filename = os.path.join(source, f"{year}_SML.csv")
66
+
67
+ # Load the labels
68
+ countries = pd.read_excel(readme_filename, sheet_name='Area_Activities', header=2)['countries'].dropna().to_list()
69
+ # Filter out everything in parenthesis, such as the additional references (1) and (2) for Israel and Cyprus
70
+ # as well as (People's Republic of China) for China and (People's Democratic Republic) for Lao
71
+ countries = [country.split('(')[0].strip() for country in countries]
72
+ sectors = pd.read_excel(readme_filename, sheet_name='Area_Activities', header=2)['Industry'].dropna().to_list()
73
+ df = pd.read_excel(readme_filename, sheet_name='ColItems',header=3)
74
+ index = df[df["Sector code"] == "Final demand items"].index[0]
75
+ y_labs = df.iloc[index:,4].to_list()
76
+ va_labs = ["Taxes less subsidies on intermediate and final products", "Value added at basic prices"]
77
+ labels = {
78
+ "countries": countries,
79
+ "sectors": sectors,
80
+ "y_labs": y_labs,
81
+ "va_labs": va_labs
82
+ }
83
+ if extended:
84
+ countries_y = countries[:-4] # remove MX1, MX2, CN1, CN2
85
+ labels["countries_y"] = countries_y
86
+
87
+ # Extract the raw data
88
+ s,c = len(sectors),len(countries)
89
+ raw = pd.read_csv(data_filename, header=0,index_col=0).to_numpy()
90
+ tables = {}
91
+ tables["t"] = raw[:c*s,:c*s]
92
+ tables["y"] = raw[:c*s,c*s:-1] # last column is cumulative output
93
+ tables["va"] = raw[c*s:-1,:c*s]
94
+ tables["vay"] = raw[c*s:-1,c*s:-1]
95
+
96
+ # Build MRIO object
97
+ m = MRIO()
98
+ m.add_dimensions(labels)
99
+ m.parts["t"] = m.new_part(name="t", data= tables["t"],
100
+ dimensions = [["countries","sectors"],["countries", "sectors"]])
101
+ m.parts["va"] = m.new_part(name="va", data= tables["va"],
102
+ dimensions = ["va_labs",["countries", "sectors"]])
103
+ if extended:
104
+ m.parts["y"] = m.new_part(name="y", data= tables["y"],
105
+ dimensions = [["countries","sectors"],["countries_y", "y_labs"]])
106
+ m.parts["vay"] = m.new_part(name="vay", data= tables["vay"],
107
+ dimensions = ["va_labs",["countries_y", "y_labs"]])
108
+ else:
109
+ m.parts["y"] = m.new_part(name="y",data= tables["y"],
110
+ dimensions = [["countries","sectors"],["countries", "y_labs"]])
111
+ m.parts["vay"] = m.new_part(name="vay", data= tables["vay"],
112
+ dimensions = ["va_labs",["countries", "y_labs"]])
113
+
114
+ # Save the mrio object to a NetCDF file
115
+ if extended:
116
+ destination = os.path.join(destination, f"icio_year{year}_extended.nc")
117
+ else:
118
+ destination = os.path.join(destination, f"icio_year{year}.nc")
119
+ save_to_nc(m, destination, overwrite=False)
120
+
121
+
@@ -0,0 +1,3 @@
1
+ """
2
+ This module contains the WIOD extractor.
3
+ """
@@ -0,0 +1,143 @@
1
+ """
2
+ Extractor for WIOD 2016 files.
3
+
4
+ This script extracts data from WIOD xlsb files and converts them to NetCDF files
5
+ for further use with the MRIO toolbox.
6
+
7
+ Supports WIOD 2016 in Excel format
8
+ https://www.rug.nl/ggdc/valuechain/wiod/wiod-2016-release
9
+
10
+
11
+ Created on 08.01, 2024
12
+ @author: wirth, based on code of beaufils
13
+ """
14
+
15
+ import os
16
+ import logging
17
+ import pandas as pd
18
+ from mrio_toolbox import MRIO
19
+ from mrio_toolbox.utils.savers._to_nc import save_to_nc
20
+
21
+ log = logging.getLogger(__name__)
22
+
23
+ def extract_wiod(
24
+ year,
25
+ source):
26
+ """
27
+ Extract WIOD data.
28
+
29
+ Loads WIOD tables and labels and store them as NetCDF for further use with
30
+ the mrio_toolbox library.
31
+
32
+ Parameters
33
+ ----------
34
+ year : str
35
+ Data year to load.
36
+ parts : str
37
+ Data blocks to load:
38
+ basic : T, Y
39
+ all : T, Y, VA, QT, QY
40
+ source : path-like
41
+ Path to folder where raw data is stored
42
+ """
43
+
44
+
45
+ #Check source path
46
+ if not os.path.exists(source):
47
+ log.error(f"{os.path.abspath(source)} does not exist.")
48
+ raise NotADirectoryError(f"{os.path.abspath(source)} does not exist.")
49
+
50
+ # WIOD 2016 comes with:
51
+ # - 43 countries + ROW
52
+ # - 56 sectors
53
+ # - 5 final demand categories
54
+ # - 6 value added category, including 5 tax categories
55
+ c,s,y,va = 44,56,5,6
56
+
57
+ log.info("Start loading")
58
+
59
+ tables = {}
60
+ raw = load_raw_WIOD(source, year)
61
+ countries, sectors, y_labs, va_labs, = [],[],[],[]
62
+ labels = raw.columns
63
+ for i in range(c):
64
+ countries.append(labels[i*s][2].strip())
65
+ for i in range(s):
66
+ sectors.append(labels[i][1].strip())
67
+ for i in range(y):
68
+ y_labs.append(labels[s*c + i][1].strip())
69
+ for i in range(va):
70
+ va_labs.append(raw.index[s*c + 1 + i][1].strip())
71
+
72
+
73
+ raw = raw.to_numpy()
74
+ parts = ["t","y","va","vay"]
75
+
76
+ for part in parts:
77
+ if part == "t":
78
+ tables[part] = raw[:c*s,:c*s]
79
+ elif part == "y":
80
+ tables[part] = raw[:c*s,c*s:-1]
81
+ elif part == "va":
82
+ tables[part] = raw[c*s+1:-1,:c*s]
83
+ elif part == "vay":
84
+ tables[part] = raw[c*s+1:-1,c*s:-1]
85
+
86
+ # build an MRIO object from labels and tables
87
+ m = MRIO()
88
+ labels = {
89
+ "countries" : countries,
90
+ "sectors" : sectors,
91
+ "y_labs" : y_labs,
92
+ "va_labs" : va_labs, # including 5 tax categories
93
+ }
94
+ m.add_dimensions(labels)
95
+
96
+ m.parts["t"] = m.new_part(name="t",
97
+ data= tables["t"],
98
+ dimensions = [["countries","sectors"],["countries", "sectors"]])
99
+ m.parts["y"] = m.new_part(name="y",
100
+ data= tables["y"],
101
+ dimensions = [["countries","sectors"],["countries", "y_labs"]])
102
+ m.parts["va"] = m.new_part(name="va",
103
+ data= tables["va"],
104
+ dimensions = ["va_labs",["countries", "sectors"]])
105
+ m.parts["vay"] = m.new_part(name="vay",
106
+ data= tables["vay"],
107
+ dimensions = ["va_labs",["countries", "y_labs"]])
108
+
109
+ m.name = f"wiod16_{year}"
110
+ return m
111
+
112
+
113
+ def load_raw_WIOD(path, year,release=16):
114
+ """
115
+ Load the raw WIOD matrix
116
+
117
+ Parameters
118
+ ----------
119
+ year : int-like
120
+ release : int-like, optional
121
+ Version of the WIOD database. The default is 2016.
122
+
123
+ Returns
124
+ -------
125
+ Pandas DataFrame
126
+ Full WIOD table as pandas DataFrame.
127
+
128
+ """
129
+ #Check source path
130
+
131
+ path = os.path.join(path, f'WIOT{year}_Nov{release}_ROW.xlsb')
132
+ if not os.path.exists(path):
133
+ log.error(f"{os.path.abspath(path)} does not exist.")
134
+ raise NotADirectoryError(f"{os.path.abspath(path)} does not exist.")
135
+
136
+
137
+ return pd.read_excel(path, header=[2,3,4,5],index_col=[0,1,2,3])
138
+
139
+ if __name__ == "__main__":
140
+ extract_wiod(year=2014,
141
+ release=16,
142
+ source='/home/florian/job_merkator_institut/MRIO Projects/MRIOs/WIOD 2016 release',
143
+ destination="/home/florian/job_merkator_institut/MRIO Projects/MRIOs/netCDF objects")