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

Files changed (61) hide show
  1. {mrio_toolbox-1.1.1.dist-info → mrio_toolbox-1.1.3.dist-info}/METADATA +2 -2
  2. mrio_toolbox-1.1.3.dist-info/RECORD +5 -0
  3. mrio_toolbox-1.1.3.dist-info/top_level.txt +1 -0
  4. mrio_toolbox/__init__.py +0 -21
  5. mrio_toolbox/_parts/_Axe.py +0 -539
  6. mrio_toolbox/_parts/_Part.py +0 -1698
  7. mrio_toolbox/_parts/__init__.py +0 -7
  8. mrio_toolbox/_parts/part_operations.py +0 -57
  9. mrio_toolbox/extractors/__init__.py +0 -20
  10. mrio_toolbox/extractors/downloaders.py +0 -36
  11. mrio_toolbox/extractors/emerging/__init__.py +0 -3
  12. mrio_toolbox/extractors/emerging/emerging_extractor.py +0 -117
  13. mrio_toolbox/extractors/eora/__init__.py +0 -3
  14. mrio_toolbox/extractors/eora/eora_extractor.py +0 -132
  15. mrio_toolbox/extractors/exiobase/__init__.py +0 -3
  16. mrio_toolbox/extractors/exiobase/exiobase_extractor.py +0 -270
  17. mrio_toolbox/extractors/extractors.py +0 -79
  18. mrio_toolbox/extractors/figaro/__init__.py +0 -3
  19. mrio_toolbox/extractors/figaro/figaro_downloader.py +0 -280
  20. mrio_toolbox/extractors/figaro/figaro_extractor.py +0 -187
  21. mrio_toolbox/extractors/gloria/__init__.py +0 -3
  22. mrio_toolbox/extractors/gloria/gloria_extractor.py +0 -202
  23. mrio_toolbox/extractors/gtap11/__init__.py +0 -7
  24. mrio_toolbox/extractors/gtap11/extraction/__init__.py +0 -3
  25. mrio_toolbox/extractors/gtap11/extraction/extractor.py +0 -129
  26. mrio_toolbox/extractors/gtap11/extraction/harpy_files/__init__.py +0 -6
  27. mrio_toolbox/extractors/gtap11/extraction/harpy_files/_header_sets.py +0 -279
  28. mrio_toolbox/extractors/gtap11/extraction/harpy_files/har_file.py +0 -262
  29. mrio_toolbox/extractors/gtap11/extraction/harpy_files/har_file_io.py +0 -974
  30. mrio_toolbox/extractors/gtap11/extraction/harpy_files/header_array.py +0 -300
  31. mrio_toolbox/extractors/gtap11/extraction/harpy_files/sl4.py +0 -229
  32. mrio_toolbox/extractors/gtap11/gtap_mrio/__init__.py +0 -6
  33. mrio_toolbox/extractors/gtap11/gtap_mrio/mrio_builder.py +0 -158
  34. mrio_toolbox/extractors/icio/__init__.py +0 -3
  35. mrio_toolbox/extractors/icio/icio_extractor.py +0 -121
  36. mrio_toolbox/extractors/wiod/__init__.py +0 -3
  37. mrio_toolbox/extractors/wiod/wiod_extractor.py +0 -143
  38. mrio_toolbox/mrio.py +0 -899
  39. mrio_toolbox/msm/__init__.py +0 -6
  40. mrio_toolbox/msm/multi_scale_mapping.py +0 -863
  41. mrio_toolbox/utils/__init__.py +0 -3
  42. mrio_toolbox/utils/converters/__init__.py +0 -5
  43. mrio_toolbox/utils/converters/pandas.py +0 -247
  44. mrio_toolbox/utils/converters/xarray.py +0 -130
  45. mrio_toolbox/utils/formatting/__init__.py +0 -0
  46. mrio_toolbox/utils/formatting/formatter.py +0 -528
  47. mrio_toolbox/utils/loaders/__init__.py +0 -7
  48. mrio_toolbox/utils/loaders/_loader.py +0 -312
  49. mrio_toolbox/utils/loaders/_loader_factory.py +0 -96
  50. mrio_toolbox/utils/loaders/_nc_loader.py +0 -184
  51. mrio_toolbox/utils/loaders/_np_loader.py +0 -112
  52. mrio_toolbox/utils/loaders/_pandas_loader.py +0 -128
  53. mrio_toolbox/utils/loaders/_parameter_loader.py +0 -386
  54. mrio_toolbox/utils/savers/__init__.py +0 -11
  55. mrio_toolbox/utils/savers/_path_checker.py +0 -37
  56. mrio_toolbox/utils/savers/_to_folder.py +0 -165
  57. mrio_toolbox/utils/savers/_to_nc.py +0 -60
  58. mrio_toolbox-1.1.1.dist-info/RECORD +0 -59
  59. mrio_toolbox-1.1.1.dist-info/top_level.txt +0 -1
  60. {mrio_toolbox-1.1.1.dist-info → mrio_toolbox-1.1.3.dist-info}/WHEEL +0 -0
  61. {mrio_toolbox-1.1.1.dist-info → mrio_toolbox-1.1.3.dist-info}/licenses/LICENSE +0 -0
@@ -1,128 +0,0 @@
1
- """Routines for loading from Excel"""
2
-
3
- from mrio_toolbox.utils.loaders._np_loader import load_file
4
- from mrio_toolbox.utils.loaders._parameter_loader import Parameter_Loader
5
- from mrio_toolbox.utils import converters
6
- import os
7
- import logging
8
-
9
- log = logging.getLogger(__name__)
10
-
11
- class Pandas_Loader(Parameter_Loader):
12
- """
13
- Class for loading MRIO data through Pandas.
14
-
15
- The `Pandas_Loader` class extends the `Parameter_Loader` class to provide
16
- functionality for loading MRIO data from `.xlsx` and `.csv` files. It uses
17
- the Pandas library to read the data and extract metadata, labels, and parts.
18
-
19
- Instance variables
20
- ------------------
21
- groupings : dict
22
- Groupings for the labels, defining higher-level aggregations.
23
- labels : dict
24
- Explicit dictionary of labels for the MRIO data.
25
- dimensions : list of int
26
- List of label names.
27
- path : str
28
- Path to the data file.
29
- labels_path : str
30
- Path to the labels file.
31
- parts : dict
32
- Parts to load, with specific settings.
33
- loader_kwargs : dict
34
- Parameters passed to the underlying Pandas loader (e.g., `read_excel`, `read_csv`).
35
-
36
- Methods
37
- -------
38
- load_part(**kwargs):
39
- Load a specific Part from explicit parameters.
40
-
41
- """
42
-
43
- def __init__(
44
- self,
45
- **kwargs
46
- ):
47
- """
48
- Initialize a Pandas_Loader object.
49
-
50
- Parameters
51
- ----------
52
- loader_kwargs : dict, optional
53
- Parameters passed to the underlying loader.
54
- - .xlsx: pandas.read_excel
55
- - .csv: pandas.read_csv
56
- groupings : dict, optional
57
- Aggregation on labels
58
- labels : dict, optional
59
- Explicit dictionary of labels.
60
- dimensions : list of int, optional
61
- List of label names.
62
- path : str, optional
63
- Path to the data
64
- The following paths are recognized:
65
- - path
66
- - mrio_path
67
- - file
68
- - data_path
69
- - table/year/version
70
- labels_path : str, optional
71
- Path to the labels files
72
- parts : dict, optional
73
- Parts to load, with specific settings
74
- **kwargs : dict
75
- Metadata for the MRIO data.
76
- MRIO metadata are passed to associated parts.
77
-
78
- """
79
- super().__init__(**kwargs)
80
-
81
- def load_part(
82
- self,
83
- **kwargs
84
- ):
85
- """
86
- Load a Part from explicit parameters.
87
-
88
- Parameters provided as arguments overload the corresponding instance attributes.
89
-
90
- Returns
91
- -------
92
- dict
93
- Data for creating the Part object
94
-
95
- Raises
96
- ------
97
- FileNotFoundError
98
- If no file nor name argument is provided
99
- """
100
- #Update loader parameters
101
- self.update_attributes(**kwargs)
102
-
103
- file = self.get_file(**kwargs)
104
-
105
- loader_kwargs = kwargs.pop("loader_kwargs",self.loader_kwargs)
106
-
107
- name = kwargs.pop("name",os.path.splitext(os.path.basename(file))[0])
108
-
109
- log.info(f"Load part {name} from {file}")
110
-
111
- if name in self.part_settings:
112
- #Load preset settings
113
- part_settings = self.part_settings[name]
114
- kwargs.update(part_settings)
115
-
116
- autodetect_labels = True
117
- if any(key in loader_kwargs for key in ["index_col", "header"]):
118
- #If labels are explicitly provided, do not autodetect
119
- autodetect_labels = False
120
-
121
-
122
- return converters.pandas.make_part(load_file(file,
123
- **loader_kwargs,
124
- extension=self.extension,
125
- pandas=True),
126
- name=name,
127
- label_detection=autodetect_labels,
128
- **kwargs)
@@ -1,386 +0,0 @@
1
- """
2
- Routine for loading MRIO tables from explicit parameters
3
- """
4
-
5
- import os
6
- from mrio_toolbox.utils.loaders._loader import Loader
7
- from mrio_toolbox.utils.loaders._np_loader import load_file
8
- import pathlib
9
- import logging
10
- from pathlib import Path
11
-
12
- log = logging.getLogger(__name__)
13
-
14
- class Parameter_Loader(Loader):
15
- """
16
- Class for loading MRIO data from explicit parameters.
17
-
18
- The `Parameter_Loader` class extends the `Loader` class to provide
19
- functionality for loading MRIO data from explicit parameters, such as
20
- `.npy`, `.csv`, or `.txt` files. It supports metadata extraction, label
21
- formatting, and grouping management.
22
-
23
- Instance variables
24
- ------------------
25
- metadata : dict
26
- Metadata associated with the MRIO data.
27
- labels : dict
28
- Explicit dictionary of labels for the MRIO data.
29
- groupings : dict
30
- Groupings for the labels, defining higher-level aggregations.
31
- path : str
32
- Path to the data file or directory.
33
- labels_path : str
34
- Path to the labels file or directory.
35
- part_settings : dict
36
- Settings for loading specific parts of the MRIO data.
37
- extension : str or None
38
- File extension for the data files (e.g., `.npy`, `.csv`).
39
-
40
- Methods
41
- -------
42
- available_parts(extension=None):
43
- List the available parts in the current path.
44
- extract_path(update=False, **kwargs):
45
- Extract the path from the provided parameters.
46
- format_labels(labels):
47
- Process and format the label information.
48
- load_mrio(**kwargs):
49
- Load MRIO data from explicit parameters.
50
- get_file(**kwargs):
51
- Get the file to load based on the provided parameters.
52
- load_part(**kwargs):
53
- Load a specific Part from explicit parameters.
54
- _get_labels(l):
55
- Find the labels fitting an axis with a given shape.
56
-
57
- Notes
58
- -----
59
- This class is designed for loading MRIO data in non-netCDF formats.
60
- It provides flexible handling of paths, labels, and groupings, making it
61
- suitable for a variety of file formats and data structures.
62
- """
63
- def __init__(
64
- self,
65
- **kwargs
66
- ):
67
- """
68
- Initialize a Parameter_Loader object.
69
-
70
- Parameters
71
- ----------
72
- loader_kwargs : dict, optional
73
- Parameters passed to the underlying loader.
74
- - `.npy`: numpy.load
75
- - `.csv`, `.txt`: numpy.loadtxt
76
- groupings : dict, optional
77
- Groupings for the labels, defining higher-level aggregations.
78
- labels : dict, optional
79
- Explicit dictionary of labels for the MRIO data.
80
- dimensions : list of int, optional
81
- List of label names.
82
- path : str, optional
83
- Path to the data file or directory. Recognized paths include:
84
- - `path`
85
- - `mrio_path`
86
- - `file`
87
- - `data_path`
88
- - `table/year/version`
89
- labels_path : str, optional
90
- Path to the labels file or directory.
91
- parts : dict, optional
92
- Settings for loading specific parts of the MRIO data.
93
- extension : str, optional
94
- File extension for the data files (e.g., `.npy`, `.csv`).
95
- **kwargs : dict
96
- Additional metadata for the MRIO data.
97
-
98
- """
99
- self.extract_basic_info(**kwargs)
100
- self.extract_path(update=True,**kwargs)
101
- self.labels = dict()
102
-
103
- try:
104
- log.debug("Try bulk labels loading.")
105
- self.labels = load_file(
106
- os.path.join(
107
- self.metadata["path"],"labels.yaml"
108
- )
109
- )
110
- except FileNotFoundError:
111
- log.debug("No labels found in the path.")
112
- labels = kwargs.pop("labels",None)
113
- if labels is None:
114
- self.metadata["dimensions"] = kwargs.get("dimensions",None)
115
- labels = self.metadata["dimensions"]
116
- self.labels = dict()
117
- self.format_labels(labels)
118
-
119
- try:
120
- self.groupings = load_file(
121
- os.path.join(
122
- self.metadata["path"],"groupings.yaml"
123
- )
124
- )
125
- except FileNotFoundError:
126
- self.groupings = kwargs.pop("groupings",dict())
127
-
128
- self.extension = kwargs.get("extension",None)
129
-
130
- self.part_settings = kwargs.get("parts",dict())
131
- super().__init__()
132
-
133
- def available_parts(self,extension=None):
134
- """
135
- List the available parts in the current path.
136
-
137
- Parameters
138
- ----------
139
- extension : str, optional
140
- Extension of the files to look for.
141
- If not provided, all files are listed.
142
-
143
- Returns
144
- -------
145
- list
146
- List of available parts
147
- """
148
- if extension is None:
149
- extension = self.extension
150
-
151
- if extension is None:
152
- return os.listdir(self.path)
153
- files = os.listdir(self.path)
154
- parts = [
155
- Path(file).stem for file in files if file.endswith(extension)
156
- ]
157
- return parts
158
-
159
- def extract_path(self,update=False,**kwargs):
160
- """
161
- Extract the path from the kwargs.
162
-
163
- Valid formats are:
164
- - path
165
- - mrio_path
166
- - file
167
- - data_path
168
- - table/year/version
169
- In absence of explicit path, the current directory is used.
170
-
171
- Parameters
172
- ----------
173
- update : bool, optional
174
- Whether to update the path attribute.
175
- If a path is already set, it is not overridden.
176
- """
177
- if "path" in kwargs:
178
- self.path = kwargs.pop("path")
179
- elif "mrio_path" in kwargs:
180
- self.path = kwargs.pop("mrio_path")
181
- elif "data_path" in kwargs and update:
182
- self.path = kwargs.pop("data_path")
183
- elif "file" in kwargs and update:
184
- self.path = pathlib.Path(kwargs.pop("file")).parent
185
- elif "path" in self.__dict__.keys() and not update:
186
- log.debug("No path provided.")
187
- self.path = "."
188
-
189
- if "table" in kwargs and "year" in kwargs and "version" in kwargs:
190
- self.path = os.path.join(
191
- self.path,
192
- kwargs.pop("table"),
193
- str(kwargs.pop("year")),
194
- kwargs.pop("version"))
195
-
196
- self.labels_path = kwargs.get("labels_path",
197
- self.__dict__.get("labels_path",self.path)
198
- )
199
-
200
- #Store paths in metadata
201
- self.metadata["path"] = self.path
202
- self.metadata["labels_path"] = self.labels_path
203
-
204
- def format_labels(self,labels):
205
- """
206
- Treat the label information
207
-
208
- If labels are provided as dict, they are kept as is.
209
- If labels are provided as string, they are loaded from the labels_path folder.
210
- The labels are stored as a dict of lists.
211
- """
212
- if labels is None:
213
- log.debug("No labels provided.")
214
- return
215
- if isinstance(labels,dict):
216
- self.labels = labels
217
- if isinstance(labels,str):
218
- labels = [labels]
219
-
220
- for label in labels:
221
- if isinstance(label,list):
222
- for sublabel in label:
223
- self.format_labels(sublabel)
224
- elif label not in self.labels.keys():
225
- log.debug("Load labels: "+label)
226
- self.labels[label] = load_file(
227
- os.path.join(self.labels_path,label),dtype=str
228
- )
229
-
230
- def load_mrio(
231
- self,
232
- **kwargs
233
- ):
234
- """
235
- Load MRIO data from explicit parameters.
236
-
237
- If parameters are provided, they overload the corresponding instance attributes.
238
- """
239
- self.update_attributes(**kwargs)
240
-
241
- def get_file(self,**kwargs):
242
- """
243
- Get the file to load.
244
-
245
- Parameters
246
- ----------
247
- file : path-like, optional
248
- User-defined path to the file, by default None
249
-
250
- Returns
251
- -------
252
- path-like
253
- Path to the file to load from
254
-
255
- Raises
256
- ------
257
- ValueError
258
- If no file is provided nor currently loaded
259
- """
260
- self.check_instructions(**kwargs)
261
- #Check if new instructions are provided
262
-
263
- instructions = self.metadata.get("instructions",None)
264
-
265
- #Find file
266
- if "file" in kwargs and kwargs.get("file")!=instructions:
267
- #Ignore the file argument if it is the same as the one in the instructions
268
- return kwargs.pop("file")
269
- if "file_name" in kwargs:
270
- return os.path.join(self.path,kwargs.pop("file_name"))
271
- if "name" in kwargs:
272
- return os.path.join(self.path,kwargs.pop("name"))
273
- if self.file is None:
274
- log.error("No file provided: please provide a full file or a file name.")
275
- raise ValueError("No file provided: please provide a full file or a file name.")
276
- return self.file
277
-
278
-
279
- def load_part(
280
- self,
281
- **kwargs
282
- ):
283
- """
284
- Load a Part from explicit parameters.
285
-
286
- Parameters provided as arguments overload the corresponding instance attributes.
287
-
288
- Returns
289
- -------
290
- dict
291
- Data for creating the Part object
292
-
293
- Raises
294
- ------
295
- FileNotFoundError
296
- If no file nor name argument is provided
297
- """
298
- #Initialize Part specific parameters
299
- part_data = {
300
- "metadata" : dict()
301
- }
302
-
303
- #Update loader parameters
304
- self.update_attributes(**kwargs)
305
-
306
- file = self.get_file(**kwargs)
307
-
308
-
309
- loader_kwargs = kwargs.pop("loader_kwargs",self.loader_kwargs)
310
-
311
- name = kwargs.pop("name",os.path.splitext(os.path.basename(file))[0])
312
-
313
- log.info(f"Load part {name} from {file}")
314
-
315
- if name in self.part_settings:
316
- #Load preset settings
317
- part_settings = self.part_settings[name]
318
- kwargs.update(part_settings)
319
-
320
- part_data["data"] = load_file(file,
321
- extension=self.extension,**loader_kwargs)
322
-
323
- labels = []
324
- dimensions = kwargs.get("dimensions",
325
- self.metadata.get("dimensions",
326
- part_data["data"].shape)
327
- )
328
- if dimensions is None:
329
- dimensions = part_data["data"].shape
330
- for dim in dimensions:
331
- labels.append(self._get_labels(dim))
332
-
333
- part_data["metadata"] = self.metadata
334
- part_data["name"] = name
335
- part_data["metadata"]["path"] = self.path
336
- part_data["metadata"]["loader_kwargs"] = loader_kwargs
337
- part_data["labels"] = labels
338
- part_data["groupings"] = kwargs.get("groupings",self.groupings)
339
- return part_data
340
-
341
- def _get_labels(self,l):
342
- """Find the labels fitting an axis with a given shape
343
-
344
- If no fitting label is found, data are labelled numerically
345
-
346
- Parameters
347
- ----------
348
- l : int, list or str
349
- Length of the data dimension or name of the dimensions.
350
-
351
- Returns
352
- -------
353
- dict of str:list of str
354
- Labels of the axis.
355
-
356
- """
357
- if isinstance(l,list):
358
- output = dict()
359
- try:
360
- for label in l:
361
- output.update(self._get_labels(label))
362
- return output
363
- except IndexError:
364
- return {"0":l}
365
- if isinstance(l,str):
366
- if l not in self.labels.keys():
367
- self.format_labels(l)
368
- return {l:self.labels[l]}
369
- if not isinstance(l,int):
370
- log.error(f"Invalid dimension type {type(l)}")
371
- raise TypeError(f"Invalid dimension type {type(l)}")
372
- if l==1:
373
- return {0:"all"}
374
- log.debug("Try to infer label from axis of length "+str(l))
375
- for label in self.labels:
376
- #Look whether a basic label fits the axis
377
- if l == len(self.labels[label]):
378
- log.debug(f"Label {label} fits axis of length {l}")
379
- return {label:self.labels[label]}
380
- for grouping in self.groupings:
381
- #Look whether a grouped label fits the axis
382
- if l == len(self.groupings[grouping]):
383
- log.debug(f"Label {label} fits axis of length {l}")
384
- return {grouping:list(self.groupings[grouping]).keys()}
385
- log.warning("No label found for axis of length "+str(l))
386
- return {"0":[i for i in range(l)]}
@@ -1,11 +0,0 @@
1
- """
2
- This module provides utility functions for saving MRIO objects.
3
- """
4
- from mrio_toolbox.utils.savers._to_folder import save_mrio_to_folder, save_part_to_folder
5
- from mrio_toolbox.utils.savers._to_nc import save_to_nc
6
-
7
- __all__ = [
8
- "save_mrio_to_folder",
9
- "save_part_to_folder",
10
- "save_to_nc"
11
- ]
@@ -1,37 +0,0 @@
1
- """
2
- Routines for checking and extending file paths to avoid overwriting existing files.
3
- """
4
- import os
5
- import re
6
-
7
- def check_path(path):
8
- """
9
- Extend the name path to avoid overwriting existing files.
10
-
11
- Parameters
12
- ----------
13
- path : str
14
- Path currently selected
15
- """
16
-
17
- i = 1
18
- while os.path.isfile(path):
19
- base_path, ext = os.path.splitext(path)
20
-
21
- # Remove existing _number suffix if present
22
- base_path = re.sub(r'_\d+$', repl, base_path)
23
-
24
- path = f"{base_path}_{i}{ext}"
25
- i += 1
26
-
27
- return path
28
-
29
- def repl(match):
30
- '''
31
- Condition for replacing the _number suffix
32
-
33
- If the suffix as 2 or 4 digits, we keep it and add version number after
34
- 2 digits can be used to identify MRIO tables, 4 digits for years
35
- '''
36
- s = match.group()
37
- return s if len(s) == 3 or len(s) == 5 else ""