mrio-toolbox 1.0.0__py3-none-any.whl → 1.1.1__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.
- mrio_toolbox/__init__.py +18 -2
- mrio_toolbox/_parts/_Axe.py +95 -37
- mrio_toolbox/_parts/_Part.py +264 -70
- mrio_toolbox/_parts/__init__.py +4 -0
- mrio_toolbox/_parts/part_operations.py +24 -17
- mrio_toolbox/extractors/__init__.py +20 -0
- mrio_toolbox/extractors/downloaders.py +36 -0
- mrio_toolbox/extractors/emerging/__init__.py +3 -0
- mrio_toolbox/extractors/emerging/emerging_extractor.py +117 -0
- mrio_toolbox/extractors/eora/__init__.py +3 -0
- mrio_toolbox/extractors/eora/eora_extractor.py +132 -0
- mrio_toolbox/extractors/exiobase/__init__.py +3 -0
- mrio_toolbox/extractors/exiobase/exiobase_extractor.py +270 -0
- mrio_toolbox/extractors/extractors.py +79 -0
- mrio_toolbox/extractors/figaro/__init__.py +3 -0
- mrio_toolbox/extractors/figaro/figaro_downloader.py +280 -0
- mrio_toolbox/extractors/figaro/figaro_extractor.py +187 -0
- mrio_toolbox/extractors/gloria/__init__.py +3 -0
- mrio_toolbox/extractors/gloria/gloria_extractor.py +202 -0
- mrio_toolbox/extractors/gtap11/__init__.py +7 -0
- mrio_toolbox/extractors/gtap11/extraction/__init__.py +3 -0
- mrio_toolbox/extractors/gtap11/extraction/extractor.py +129 -0
- mrio_toolbox/extractors/gtap11/extraction/harpy_files/__init__.py +6 -0
- mrio_toolbox/extractors/gtap11/extraction/harpy_files/_header_sets.py +279 -0
- mrio_toolbox/extractors/gtap11/extraction/harpy_files/har_file.py +262 -0
- mrio_toolbox/extractors/gtap11/extraction/harpy_files/har_file_io.py +974 -0
- mrio_toolbox/extractors/gtap11/extraction/harpy_files/header_array.py +300 -0
- mrio_toolbox/extractors/gtap11/extraction/harpy_files/sl4.py +229 -0
- mrio_toolbox/extractors/gtap11/gtap_mrio/__init__.py +6 -0
- mrio_toolbox/extractors/gtap11/gtap_mrio/mrio_builder.py +158 -0
- mrio_toolbox/extractors/icio/__init__.py +3 -0
- mrio_toolbox/extractors/icio/icio_extractor.py +121 -0
- mrio_toolbox/extractors/wiod/__init__.py +3 -0
- mrio_toolbox/extractors/wiod/wiod_extractor.py +143 -0
- mrio_toolbox/mrio.py +254 -94
- mrio_toolbox/msm/__init__.py +6 -0
- mrio_toolbox/msm/multi_scale_mapping.py +863 -0
- mrio_toolbox/utils/__init__.py +3 -0
- mrio_toolbox/utils/converters/__init__.py +3 -0
- mrio_toolbox/utils/converters/pandas.py +8 -6
- mrio_toolbox/utils/converters/xarray.py +2 -13
- mrio_toolbox/utils/formatting/__init__.py +0 -0
- mrio_toolbox/utils/formatting/formatter.py +528 -0
- mrio_toolbox/utils/loaders/__init__.py +4 -0
- mrio_toolbox/utils/loaders/_loader.py +60 -4
- mrio_toolbox/utils/loaders/_loader_factory.py +22 -1
- mrio_toolbox/utils/loaders/_nc_loader.py +37 -1
- mrio_toolbox/utils/loaders/_pandas_loader.py +29 -3
- mrio_toolbox/utils/loaders/_parameter_loader.py +61 -16
- mrio_toolbox/utils/savers/__init__.py +3 -0
- mrio_toolbox/utils/savers/_path_checker.py +25 -7
- mrio_toolbox/utils/savers/_to_folder.py +6 -1
- mrio_toolbox/utils/savers/_to_nc.py +26 -18
- {mrio_toolbox-1.0.0.dist-info → mrio_toolbox-1.1.1.dist-info}/METADATA +10 -6
- mrio_toolbox-1.1.1.dist-info/RECORD +59 -0
- {mrio_toolbox-1.0.0.dist-info → mrio_toolbox-1.1.1.dist-info}/WHEEL +1 -1
- mrio_toolbox-1.0.0.dist-info/RECORD +0 -26
- {mrio_toolbox-1.0.0.dist-info → mrio_toolbox-1.1.1.dist-info/licenses}/LICENSE +0 -0
- {mrio_toolbox-1.0.0.dist-info → mrio_toolbox-1.1.1.dist-info}/top_level.txt +0 -0
mrio_toolbox/mrio.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
# -*- coding: utf-8 -*-
|
|
2
2
|
"""
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Representation of economic MRIO tables
|
|
3
|
+
This module provides the MRIO class.
|
|
6
4
|
|
|
5
|
+
Created on Thu Mar 30 10:42:23 2023
|
|
7
6
|
@author: beaufils
|
|
8
7
|
"""
|
|
9
8
|
|
|
@@ -16,6 +15,7 @@ from mrio_toolbox._parts import Part
|
|
|
16
15
|
from mrio_toolbox.utils import converters
|
|
17
16
|
from mrio_toolbox.utils.loaders import make_loader
|
|
18
17
|
from mrio_toolbox.utils.savers import save_mrio_to_folder,save_to_nc
|
|
18
|
+
from mrio_toolbox.utils.formatting import formatter
|
|
19
19
|
|
|
20
20
|
log = logging.getLogger(__name__)
|
|
21
21
|
|
|
@@ -23,72 +23,105 @@ class MRIO:
|
|
|
23
23
|
"""
|
|
24
24
|
Representation of an MRIO table
|
|
25
25
|
|
|
26
|
-
An MRIO table holds a collection of Parts, each representing a different
|
|
27
|
-
|
|
28
|
-
The MRIO instance allows
|
|
26
|
+
An MRIO table holds a collection of Parts, each representing a different aspect
|
|
27
|
+
of the table (e.g., inter-industry matrix, final demand, satellite accounts, etc).
|
|
28
|
+
The MRIO instance allows performing basic operations on the table, such as
|
|
29
|
+
loading parts, setting groupings, filtering, aggregating, and saving data.
|
|
29
30
|
|
|
30
31
|
Instance variables
|
|
31
|
-
|
|
32
|
+
------------------
|
|
32
33
|
metadata : dict
|
|
33
|
-
|
|
34
|
+
Dictionary storing the metadata of the MRIO table.
|
|
34
35
|
labels : dict
|
|
35
|
-
Labels of the table parts:
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
Labels of the table parts, including:
|
|
37
|
+
- List of countries
|
|
38
|
+
- List of sectors
|
|
38
39
|
groupings : dict
|
|
39
|
-
Groupings of the MRIO table.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
but can be used for visualization or aggregation purposes.
|
|
40
|
+
Groupings of the MRIO table. Groupings are used to group labels into
|
|
41
|
+
larger categories (e.g., countries into zones, sectors into aggregate
|
|
42
|
+
sectors). These groupings are primarily used for visualization or
|
|
43
|
+
aggregation purposes.
|
|
44
44
|
c : int
|
|
45
|
-
Number of countries in the table
|
|
45
|
+
Number of countries in the table.
|
|
46
46
|
s : int
|
|
47
|
-
Number of sectors in the table
|
|
47
|
+
Number of sectors in the table.
|
|
48
48
|
parts : dict of Part objects
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
Dictionary containing the different parts of the MRIO table.
|
|
50
|
+
|
|
51
51
|
Methods
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
52
|
+
-------
|
|
53
|
+
__init__(**kwargs):
|
|
54
|
+
Initialize an MRIO instance from a file or explicit parameters.
|
|
55
|
+
load_part(update_part=True, standalone=False, **kwargs):
|
|
56
|
+
Load a Part object into the MRIO table.
|
|
57
|
+
set_groupings(groupings=None):
|
|
58
|
+
Set the groupings of the MRIO table.
|
|
59
|
+
filter(threshold, fill_value=0):
|
|
60
|
+
Filter the MRIO table by removing values below a specified threshold.
|
|
61
|
+
add_part(part, name=None, update_part=True):
|
|
62
|
+
Add a Part object to the MRIO table.
|
|
63
|
+
new_part(data=None, name="part", dimensions=None, fill_value=0.0, **kwargs):
|
|
64
|
+
Create a new Part object from data or dimensions.
|
|
65
|
+
add_dimensions(dimensions):
|
|
66
|
+
Add new dimensions to the MRIO table.
|
|
67
|
+
add_labels(new_indices, dimension, fill_value=0.0):
|
|
68
|
+
Add items to a label of the MRIO instance.
|
|
69
|
+
replace_labels(name, new_labels):
|
|
70
|
+
Replace labels for one or more dimensions in all MRIO parts.
|
|
71
|
+
rename_dimensions(old_names, new_names):
|
|
72
|
+
Rename the names of the dimensions in the MRIO table.
|
|
73
|
+
aggregate(on="sectors"):
|
|
74
|
+
Aggregate the MRIO table on a given dimension.
|
|
75
|
+
has_neg(parts=None):
|
|
76
|
+
Check whether some parts of the MRIO table have negative values.
|
|
77
|
+
copy():
|
|
78
|
+
Create a copy of the MRIO object.
|
|
79
|
+
save(file, name=None, extension="npy", overwrite=False, **kwargs):
|
|
80
|
+
Save the current MRIO instance to a file or folder.
|
|
81
|
+
to_xarray():
|
|
82
|
+
Convert the MRIO instance to an xarray Dataset.
|
|
83
|
+
|
|
84
|
+
Notes
|
|
85
|
+
-----
|
|
86
|
+
This class provides a comprehensive interface for working with MRIO tables,
|
|
87
|
+
including loading, modifying, and saving data, as well as performing
|
|
88
|
+
operations like filtering and aggregation.
|
|
68
89
|
"""
|
|
90
|
+
|
|
69
91
|
def __init__(self,**kwargs):
|
|
70
92
|
"""
|
|
71
|
-
Initialize an MRIO instance
|
|
93
|
+
Initialize an MRIO instance.
|
|
72
94
|
|
|
73
|
-
There are
|
|
74
|
-
-
|
|
75
|
-
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
Loading an MRIO from explicit parameters requires at least a path to the data and the labels.
|
|
79
|
-
|
|
80
|
-
You can also provide the path to a .yaml file containing the loading instructions,
|
|
81
|
-
using the "file" parameter.
|
|
95
|
+
There are multiple ways to initialize an MRIO instance:
|
|
96
|
+
- From a `.nc` file: Provide the "file" parameter with the path to the file.
|
|
97
|
+
- From explicit parameters: Provide the data, labels, and metadata explicitly.
|
|
98
|
+
- From a `.yaml` file: Provide the "file" parameter with the path to a `.yaml` file
|
|
99
|
+
containing loading instructions.
|
|
82
100
|
|
|
83
|
-
|
|
84
|
-
It is intended for internal use only.
|
|
101
|
+
If no arguments are provided, an empty MRIO instance is created.
|
|
85
102
|
|
|
86
103
|
Parameters
|
|
87
104
|
----------
|
|
88
105
|
file : str, optional
|
|
89
|
-
Path to the file to load the MRIO table from.
|
|
90
|
-
|
|
106
|
+
Path to the file to load the MRIO table from. If a `.yaml` file is provided,
|
|
107
|
+
it is interpreted as loading instructions.
|
|
108
|
+
data : dict, xarray.DataArray, xarray.Dataset, or pandas.DataFrame, optional
|
|
109
|
+
Data to initialize the MRIO instance. If provided, the instance is created
|
|
110
|
+
from this data.
|
|
111
|
+
kwargs : dict
|
|
112
|
+
Additional parameters for initializing the MRIO instance.
|
|
113
|
+
|
|
114
|
+
Raises
|
|
115
|
+
------
|
|
116
|
+
ValueError
|
|
117
|
+
If the provided data type is not supported for initializing the MRIO instance.
|
|
118
|
+
|
|
119
|
+
Notes
|
|
120
|
+
-----
|
|
121
|
+
The "data" parameter is reserved for setting an MRIO instance from a dictionary.
|
|
122
|
+
It is intended for internal use only.
|
|
91
123
|
"""
|
|
124
|
+
|
|
92
125
|
if not kwargs:
|
|
93
126
|
#Create an empty MRIO instance
|
|
94
127
|
kwargs = {
|
|
@@ -135,12 +168,9 @@ class MRIO:
|
|
|
135
168
|
|
|
136
169
|
#Initialize the parts
|
|
137
170
|
self.parts = dict()
|
|
138
|
-
available_parts =
|
|
139
|
-
"parts",
|
|
140
|
-
self.loader.available_parts(
|
|
171
|
+
available_parts = self.loader.available_parts(
|
|
141
172
|
extension = kwargs.get("extension",None)
|
|
142
173
|
)
|
|
143
|
-
)
|
|
144
174
|
|
|
145
175
|
to_load = {part:part for part in available_parts}
|
|
146
176
|
if "part_settings" in self.loader.__dict__ and bool(self.loader.part_settings):
|
|
@@ -230,14 +260,16 @@ class MRIO:
|
|
|
230
260
|
part.set_labels(part_labels)
|
|
231
261
|
|
|
232
262
|
def _get_labels(self,l):
|
|
233
|
-
"""
|
|
263
|
+
"""
|
|
264
|
+
Find the labels fitting an axis with a given shape
|
|
234
265
|
|
|
235
266
|
Available labels:
|
|
236
|
-
|
|
237
|
-
countries
|
|
238
|
-
|
|
239
|
-
zones
|
|
240
|
-
|
|
267
|
+
|
|
268
|
+
- countries and sectors
|
|
269
|
+
- countries
|
|
270
|
+
- zones and sectors
|
|
271
|
+
- zones
|
|
272
|
+
- sectors
|
|
241
273
|
|
|
242
274
|
If no fitting label is found, data are labelled numerically
|
|
243
275
|
|
|
@@ -289,8 +321,10 @@ class MRIO:
|
|
|
289
321
|
Groupings of the MRIO table.
|
|
290
322
|
The default is None.
|
|
291
323
|
If None, the groupings are set to the identity.
|
|
292
|
-
Groupings should be provided as a dict of dict
|
|
324
|
+
Groupings should be provided as a dict of dict::
|
|
325
|
+
|
|
293
326
|
{dimension : {group : [items]}}
|
|
327
|
+
|
|
294
328
|
where dimension is the name of the label to group,
|
|
295
329
|
group is the name of the group,
|
|
296
330
|
and items is a list of items to group.
|
|
@@ -301,16 +335,16 @@ class MRIO:
|
|
|
301
335
|
for key in groupings.keys():
|
|
302
336
|
labels = self.labels[key]
|
|
303
337
|
covered = []
|
|
304
|
-
for group in groupings[key]:
|
|
305
|
-
for item in groupings[key][group]:
|
|
306
|
-
if item not in
|
|
338
|
+
for group in list(groupings[key]):
|
|
339
|
+
for item in list(groupings[key][group]):
|
|
340
|
+
if item not in labels:
|
|
307
341
|
log.warning(
|
|
308
342
|
f"Item {item} not found in {key} labels"
|
|
309
343
|
)
|
|
310
344
|
groupings[key][group].remove(item)
|
|
311
345
|
else:
|
|
312
346
|
covered.append(item)
|
|
313
|
-
if len(group) == 0:
|
|
347
|
+
if len(groupings[key][group]) == 0:
|
|
314
348
|
log.warning(f"Group {group} is empty")
|
|
315
349
|
groupings[key].pop(group)
|
|
316
350
|
for item in labels:
|
|
@@ -395,9 +429,11 @@ class MRIO:
|
|
|
395
429
|
dimensions or labels : list of str, list of ints, str, list of dicts, optional
|
|
396
430
|
Labels of the Part.
|
|
397
431
|
Either of these formats are accepted:
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
List of
|
|
432
|
+
|
|
433
|
+
- Dictionary of explicit labels for each axis
|
|
434
|
+
- List of explicit labels for each axis
|
|
435
|
+
- List of existing dimension names
|
|
436
|
+
|
|
401
437
|
If None, the labels are inferred from the data shape.
|
|
402
438
|
multiplier : str, optional
|
|
403
439
|
multiplier of the data. The default is None.
|
|
@@ -420,6 +456,8 @@ class MRIO:
|
|
|
420
456
|
return {dimensions:self.labels[dimensions]}
|
|
421
457
|
if dimensions in self.groupings.keys():
|
|
422
458
|
return {dimensions:list(self.groupings[dimensions])}
|
|
459
|
+
log.warning(f"Dimension {dimensions} not found in labels or groupings\n"+\
|
|
460
|
+
" Available dimensions are "+str(self.labels.keys()))
|
|
423
461
|
raise ValueError(f"Invalid dimension {dimensions}")
|
|
424
462
|
if isinstance(dimensions,int):
|
|
425
463
|
#Try to infer the dimension from the length of the data
|
|
@@ -494,8 +532,34 @@ class MRIO:
|
|
|
494
532
|
"""
|
|
495
533
|
for dimension in dimensions.keys():
|
|
496
534
|
log.info("Add dimension "+dimension+" to MRIO table")
|
|
535
|
+
# check whether the dimension is already in the labels
|
|
536
|
+
if dimension in self.labels.keys():
|
|
537
|
+
log.info(f"Dimension {dimension} already exists in MRIO labels")
|
|
538
|
+
if dimensions[dimension] != self.labels[dimension]:
|
|
539
|
+
log.warning(f"There already exist different or differently ordered labels for dimension '{dimension}' in the MRIO labels. "+
|
|
540
|
+
"You are now overwriting them which leads to inconsistent labels in the parts of your MRIO table!")
|
|
541
|
+
|
|
542
|
+
if isinstance(dimensions[dimension],str):
|
|
543
|
+
#For single item dimensions, convert to list
|
|
544
|
+
dimensions[dimension] = [dimensions[dimension]]
|
|
497
545
|
self.labels[dimension] = dimensions[dimension]
|
|
498
546
|
|
|
547
|
+
def check_label_consistency(self):
|
|
548
|
+
"""
|
|
549
|
+
Check whether the labels of of all parts of the MRIO table are consistent.
|
|
550
|
+
"""
|
|
551
|
+
|
|
552
|
+
for part in self.parts.keys():
|
|
553
|
+
for key, labels in self.parts[part].labels.items():
|
|
554
|
+
if key in self.labels.keys():
|
|
555
|
+
if labels != self.labels[key]:
|
|
556
|
+
log.warning(f"Labels for dimension'{key}' in part '{part}' are inconsistent with MRIO labels")
|
|
557
|
+
return False
|
|
558
|
+
else:
|
|
559
|
+
log.warning(f"Label {key} not found in MRIO labels")
|
|
560
|
+
return False
|
|
561
|
+
return True
|
|
562
|
+
|
|
499
563
|
def add_labels(self,new_indices,dimension,
|
|
500
564
|
fill_value=0.0):
|
|
501
565
|
"""
|
|
@@ -530,28 +594,59 @@ class MRIO:
|
|
|
530
594
|
|
|
531
595
|
def replace_labels(self,name,new_labels):
|
|
532
596
|
"""
|
|
533
|
-
Replace labels in all MRIO parts
|
|
597
|
+
Replace labels for one or more dimensions in all MRIO parts.
|
|
598
|
+
|
|
599
|
+
Parameters
|
|
600
|
+
----------
|
|
601
|
+
name : str or list of str
|
|
602
|
+
Name of the dimension for which the labels should be replaced
|
|
603
|
+
new_labels : list of str or dict of list of str
|
|
604
|
+
New labels for given dimension in the labels dictionary.
|
|
605
|
+
"""
|
|
606
|
+
log.info(f"Replace labels for {name}")
|
|
607
|
+
if isinstance(name,str):
|
|
608
|
+
self._replace_individual_labels(name = name, new_labels = new_labels)
|
|
609
|
+
elif isinstance(name,list):
|
|
610
|
+
for dimension in name:
|
|
611
|
+
if isinstance(new_labels,list):
|
|
612
|
+
raise ValueError("You provided multiple dimensions but only one set of new labels.")
|
|
613
|
+
elif isinstance(new_labels,dict):
|
|
614
|
+
new_labels = new_labels[dimension]
|
|
615
|
+
else:
|
|
616
|
+
raise TypeError(f"Invalid type for new labels: {type(new_labels)}")
|
|
617
|
+
self._replace_individual_labels(dimension,new_labels)
|
|
618
|
+
|
|
619
|
+
def _replace_individual_labels(self,name, new_labels): # check for usages and update them
|
|
620
|
+
"""
|
|
621
|
+
Replace labels for one dimension in all MRIO parts.
|
|
534
622
|
|
|
535
623
|
Parameters
|
|
536
624
|
----------
|
|
537
|
-
|
|
538
|
-
Name of the labels
|
|
539
|
-
new_labels :
|
|
540
|
-
|
|
625
|
+
name : str
|
|
626
|
+
Name of the labels to replace. Usually this is 'countries', 'sectors', 'va_labs' or 'y_labs'
|
|
627
|
+
new_labels : list of str
|
|
628
|
+
New labels for given name in the label dictionary.
|
|
541
629
|
"""
|
|
542
630
|
log.info(f"Replace labels for {name}")
|
|
631
|
+
if name not in self.labels.keys():
|
|
632
|
+
log.warning(f"Label {name} is not in MRIO labels and cannot be replaced.")
|
|
633
|
+
return
|
|
543
634
|
for part in self.parts.keys():
|
|
544
|
-
self.parts[part]
|
|
545
|
-
if name != new_labels.keys():
|
|
546
|
-
self.labels.update(new_labels)
|
|
547
|
-
self.labels.pop(name)
|
|
548
|
-
if name in self.groupings.keys():
|
|
549
|
-
self.groupings.pop(name)
|
|
635
|
+
self.parts[part].replace_labels(name, new_labels)
|
|
550
636
|
self.loader.set_labels(self.labels)
|
|
637
|
+
self.labels[name] = new_labels
|
|
638
|
+
|
|
639
|
+
if name in self.groupings.keys():
|
|
640
|
+
log.warning("Groupings for all dimensions are reset to identity, because the labels have changed.\n \
|
|
641
|
+
Please update the groupings manually if needed.")
|
|
642
|
+
self.set_groupings(groupings=None)
|
|
551
643
|
|
|
552
|
-
def
|
|
644
|
+
def rename_dimensions(self,old_names,new_names):
|
|
553
645
|
"""
|
|
554
|
-
Rename
|
|
646
|
+
Rename the names of the dimensions.
|
|
647
|
+
|
|
648
|
+
The keys of the label dicts for the MRIO instance are renamed.
|
|
649
|
+
The content of the labels and the groupings is conserved.
|
|
555
650
|
|
|
556
651
|
Parameters
|
|
557
652
|
----------
|
|
@@ -566,16 +661,49 @@ class MRIO:
|
|
|
566
661
|
new_names = [new_names]
|
|
567
662
|
for old,new in zip(old_names,new_names):
|
|
568
663
|
log.info(f"Rename label {old} to {new}")
|
|
569
|
-
|
|
664
|
+
|
|
665
|
+
if old in self.labels.keys():
|
|
666
|
+
self.labels = {new if key == old else key : value for key, value in self.labels.items()}
|
|
667
|
+
else:
|
|
570
668
|
log.warning(f"Label {old} is not in MRIO labels and cannot be renamed.")
|
|
571
669
|
continue
|
|
572
|
-
self.labels[new] = self.labels.pop(old)
|
|
573
670
|
if old in self.groupings.keys():
|
|
574
|
-
self.groupings
|
|
671
|
+
self.groupings = {new if key == old else key : value for key, value in self.groupings.items()}
|
|
575
672
|
for part in self.parts.keys():
|
|
576
673
|
self.parts[part].rename_labels(old,new)
|
|
577
|
-
|
|
578
|
-
|
|
674
|
+
|
|
675
|
+
def reorder_data(self, new_labels):
|
|
676
|
+
"""
|
|
677
|
+
Reorder the data of the MRIO instance based on new labels.
|
|
678
|
+
|
|
679
|
+
This method is used to reorder the data of the MRIO instance based on the new labels.
|
|
680
|
+
The new labels should be a dictionary with the same keys and items as the old mrio labels
|
|
681
|
+
but the keys and items can be in a different order. The data of the part is then reordered
|
|
682
|
+
to fit the new labels.
|
|
683
|
+
|
|
684
|
+
Parameters
|
|
685
|
+
----------
|
|
686
|
+
new_labels : dict
|
|
687
|
+
New labels to use for reordering.
|
|
688
|
+
"""
|
|
689
|
+
log.info("Reorder MRIO data with new labels")
|
|
690
|
+
old_labels = self.labels
|
|
691
|
+
if not isinstance(new_labels,dict):
|
|
692
|
+
raise TypeError(f"Invalid type for new labels: {type(new_labels)}. Expected a dict.")
|
|
693
|
+
if not set(new_labels.keys()).issubset(set(old_labels.keys())):
|
|
694
|
+
raise ValueError("You are trying to reorder data for dimensions that are not present in your MRIO instance")
|
|
695
|
+
|
|
696
|
+
for part in self.parts.keys():
|
|
697
|
+
new_part_labels = {}
|
|
698
|
+
for key, labels in new_labels.items():
|
|
699
|
+
if key in self.parts[part].labels.keys():
|
|
700
|
+
new_part_labels[key] = labels
|
|
701
|
+
self.parts[part].reorder_data(new_part_labels)
|
|
702
|
+
|
|
703
|
+
for key in new_labels.keys():
|
|
704
|
+
self.replace_labels(key, new_labels[key])
|
|
705
|
+
|
|
706
|
+
|
|
579
707
|
|
|
580
708
|
def aggregate(self,on="sectors"):
|
|
581
709
|
"""Aggregate the MRIO table on a given dimension
|
|
@@ -666,10 +794,10 @@ class MRIO:
|
|
|
666
794
|
-------
|
|
667
795
|
bool
|
|
668
796
|
"""
|
|
669
|
-
if parts
|
|
670
|
-
parts = "all"
|
|
671
|
-
elif isinstance(parts,str):
|
|
797
|
+
if isinstance(parts,str):
|
|
672
798
|
parts = [parts]
|
|
799
|
+
elif parts is None:
|
|
800
|
+
parts = self.parts.keys()
|
|
673
801
|
for part in parts:
|
|
674
802
|
if self.parts[part].hasneg():
|
|
675
803
|
return True
|
|
@@ -680,9 +808,9 @@ class MRIO:
|
|
|
680
808
|
return MRIO(data=self.__dict__)
|
|
681
809
|
|
|
682
810
|
def save(self,
|
|
683
|
-
file,
|
|
811
|
+
file=None,
|
|
684
812
|
name=None,
|
|
685
|
-
extension = "
|
|
813
|
+
extension = ".nc",
|
|
686
814
|
overwrite=False,
|
|
687
815
|
**kwargs):
|
|
688
816
|
"""
|
|
@@ -693,6 +821,7 @@ class MRIO:
|
|
|
693
821
|
- .csv
|
|
694
822
|
- .txt
|
|
695
823
|
- .xlsx
|
|
824
|
+
|
|
696
825
|
Labels are saved as .txt files and metadata as a .yaml file.
|
|
697
826
|
|
|
698
827
|
Otherwise the MRIO instance is saved as a .nc file.
|
|
@@ -712,9 +841,13 @@ class MRIO:
|
|
|
712
841
|
kwargs : dict
|
|
713
842
|
Additional arguments to pass to the saver.
|
|
714
843
|
"""
|
|
844
|
+
if file is None:
|
|
845
|
+
if file is None:
|
|
846
|
+
name = self.metadata.get("name","mrio")
|
|
847
|
+
file = name
|
|
715
848
|
file_extension = os.path.splitext(file)[1]
|
|
716
849
|
if file_extension == "" and extension == ".nc":
|
|
717
|
-
file = file+".nc"
|
|
850
|
+
file = os.path.join(file+".nc")
|
|
718
851
|
file_extension = ".nc"
|
|
719
852
|
if file_extension == "":
|
|
720
853
|
#If the file is a folder, save in folder
|
|
@@ -728,7 +861,7 @@ class MRIO:
|
|
|
728
861
|
)
|
|
729
862
|
elif file_extension == ".nc":
|
|
730
863
|
#If the file is a .nc, save the tables
|
|
731
|
-
save_to_nc(self,file,
|
|
864
|
+
save_to_nc(self,file,overwrite,**kwargs)
|
|
732
865
|
else:
|
|
733
866
|
raise NotImplementedError(f"Cannot save MRIO in {file_extension} format")
|
|
734
867
|
|
|
@@ -736,4 +869,31 @@ class MRIO:
|
|
|
736
869
|
"""
|
|
737
870
|
Convert the MRIO instance to an xarray Dataset
|
|
738
871
|
"""
|
|
739
|
-
return converters.xarray.to_DataSet(self)
|
|
872
|
+
return converters.xarray.to_DataSet(self)
|
|
873
|
+
|
|
874
|
+
def reallocate_negatives(self,**kwargs):
|
|
875
|
+
formatter.reallocate_negatives(self,**kwargs)
|
|
876
|
+
|
|
877
|
+
def adjust_intermediates(self,**kwargs):
|
|
878
|
+
formatter.adjust_intermediates(self,**kwargs)
|
|
879
|
+
|
|
880
|
+
def fill_empty_rows(self,**kwargs):
|
|
881
|
+
formatter.fill_empty_rows(self,**kwargs)
|
|
882
|
+
|
|
883
|
+
def balance_va(self,**kwargs):
|
|
884
|
+
formatter.balance_va(self,**kwargs)
|
|
885
|
+
|
|
886
|
+
def compute_technical_coefficients(self,**kwargs):
|
|
887
|
+
formatter.compute_technical_coefficients(self,**kwargs)
|
|
888
|
+
|
|
889
|
+
def compute_leontief(self,**kwargs):
|
|
890
|
+
formatter.compute_leontief(self,**kwargs)
|
|
891
|
+
|
|
892
|
+
def preprocess(self,**kwargs):
|
|
893
|
+
formatter.preprocess(self,**kwargs)
|
|
894
|
+
|
|
895
|
+
def rename_part(self,old_name,new_name):
|
|
896
|
+
formatter.rename_part(self,old_name,new_name)
|
|
897
|
+
|
|
898
|
+
def rename_parts(self,renaming_dict):
|
|
899
|
+
formatter.rename_parts(self,renaming_dict)
|