mrio-toolbox 1.1.2__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.
- {mrio_toolbox-1.1.2.dist-info → mrio_toolbox-1.1.3.dist-info}/METADATA +1 -1
- mrio_toolbox-1.1.3.dist-info/RECORD +5 -0
- mrio_toolbox-1.1.3.dist-info/top_level.txt +1 -0
- __init__.py +0 -21
- _parts/_Axe.py +0 -539
- _parts/_Part.py +0 -1739
- _parts/__init__.py +0 -7
- _parts/part_operations.py +0 -57
- extractors/__init__.py +0 -20
- extractors/downloaders.py +0 -36
- extractors/emerging/__init__.py +0 -3
- extractors/emerging/emerging_extractor.py +0 -117
- extractors/eora/__init__.py +0 -3
- extractors/eora/eora_extractor.py +0 -132
- extractors/exiobase/__init__.py +0 -3
- extractors/exiobase/exiobase_extractor.py +0 -270
- extractors/extractors.py +0 -81
- extractors/figaro/__init__.py +0 -3
- extractors/figaro/figaro_downloader.py +0 -280
- extractors/figaro/figaro_extractor.py +0 -187
- extractors/gloria/__init__.py +0 -3
- extractors/gloria/gloria_extractor.py +0 -202
- extractors/gtap11/__init__.py +0 -7
- extractors/gtap11/extraction/__init__.py +0 -3
- extractors/gtap11/extraction/extractor.py +0 -129
- extractors/gtap11/extraction/harpy_files/__init__.py +0 -6
- extractors/gtap11/extraction/harpy_files/_header_sets.py +0 -279
- extractors/gtap11/extraction/harpy_files/har_file.py +0 -262
- extractors/gtap11/extraction/harpy_files/har_file_io.py +0 -974
- extractors/gtap11/extraction/harpy_files/header_array.py +0 -300
- extractors/gtap11/extraction/harpy_files/sl4.py +0 -229
- extractors/gtap11/gtap_mrio/__init__.py +0 -6
- extractors/gtap11/gtap_mrio/mrio_builder.py +0 -158
- extractors/icio/__init__.py +0 -3
- extractors/icio/icio_extractor.py +0 -121
- extractors/wiod/__init__.py +0 -3
- extractors/wiod/wiod_extractor.py +0 -143
- mrio.py +0 -899
- mrio_toolbox-1.1.2.dist-info/RECORD +0 -59
- mrio_toolbox-1.1.2.dist-info/top_level.txt +0 -6
- msm/__init__.py +0 -6
- msm/multi_scale_mapping.py +0 -863
- utils/__init__.py +0 -3
- utils/converters/__init__.py +0 -5
- utils/converters/pandas.py +0 -244
- utils/converters/xarray.py +0 -132
- utils/formatting/__init__.py +0 -0
- utils/formatting/formatter.py +0 -527
- utils/loaders/__init__.py +0 -7
- utils/loaders/_loader.py +0 -312
- utils/loaders/_loader_factory.py +0 -96
- utils/loaders/_nc_loader.py +0 -184
- utils/loaders/_np_loader.py +0 -112
- utils/loaders/_pandas_loader.py +0 -128
- utils/loaders/_parameter_loader.py +0 -386
- utils/savers/__init__.py +0 -11
- utils/savers/_path_checker.py +0 -37
- utils/savers/_to_folder.py +0 -165
- utils/savers/_to_nc.py +0 -60
- {mrio_toolbox-1.1.2.dist-info → mrio_toolbox-1.1.3.dist-info}/WHEEL +0 -0
- {mrio_toolbox-1.1.2.dist-info → mrio_toolbox-1.1.3.dist-info}/licenses/LICENSE +0 -0
utils/formatting/formatter.py
DELETED
|
@@ -1,527 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Format MRIO tables to meet specific requirements
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import os
|
|
7
|
-
import sys
|
|
8
|
-
import yaml
|
|
9
|
-
import numpy as np
|
|
10
|
-
import logging as log
|
|
11
|
-
|
|
12
|
-
#from mrio_toolbox import MRIO
|
|
13
|
-
|
|
14
|
-
log = log.getLogger(__name__)
|
|
15
|
-
|
|
16
|
-
def reallocate_negatives(mrio,
|
|
17
|
-
final_demand="y",
|
|
18
|
-
value_added="va",
|
|
19
|
-
category_fd=None,
|
|
20
|
-
category_va=None,
|
|
21
|
-
destination_fd = -1,
|
|
22
|
-
destination_va = -1,
|
|
23
|
-
from_fd=True,
|
|
24
|
-
from_va=True,
|
|
25
|
-
validate_intermediates=False):
|
|
26
|
-
"""Reallocate negative values between final demand and value added parts
|
|
27
|
-
|
|
28
|
-
Negative values might interfere with some MRIO operation.
|
|
29
|
-
This function reallocates negative values from final demand to value added parts and vice versa.
|
|
30
|
-
|
|
31
|
-
Parameters
|
|
32
|
-
----------
|
|
33
|
-
mrio : MRIO instance
|
|
34
|
-
Instance on which the operation applies
|
|
35
|
-
final_demand : str, optional
|
|
36
|
-
Name of the final demand part. The default is "y"
|
|
37
|
-
value_added : str, optional
|
|
38
|
-
Name of the value added part. The default is "va"
|
|
39
|
-
origin_fd : list of str, optional
|
|
40
|
-
List of final demand labels that are transferable. The default is None.
|
|
41
|
-
If no label is given, all negative values are transferred.
|
|
42
|
-
origin_va : list of str, optional
|
|
43
|
-
List of value added labels that are transferable. The default is None.
|
|
44
|
-
If no label is given, all negative values are transferred.
|
|
45
|
-
destination_fd : str, optional
|
|
46
|
-
Final demand categories to which negative values are transferred.
|
|
47
|
-
The default is -1, i.e. the last final demand category.
|
|
48
|
-
destination_va : str, optional
|
|
49
|
-
Value added category to which negative values are transferred.
|
|
50
|
-
The default is -1, i.e. the last value added category.
|
|
51
|
-
from_fd : bool, optional
|
|
52
|
-
Whether to reallocate from final demand to value added. The default is True.
|
|
53
|
-
from_va : bool, optional
|
|
54
|
-
Whether to reallocate from value added to final demand. The default is True.
|
|
55
|
-
validate_intermediates : bool, optional
|
|
56
|
-
Whether to double check the validity of intermediate use,
|
|
57
|
-
i.e., that intermediate inputs demand do not exceed the total output.
|
|
58
|
-
See the corresponding function's documentation
|
|
59
|
-
|
|
60
|
-
"""
|
|
61
|
-
if final_demand not in mrio.parts.keys():
|
|
62
|
-
log.error(f"Part {final_demand} not found in MRIO. Cannot reallocate final demand.")
|
|
63
|
-
raise ValueError(f"Part {final_demand} not found in MRIO. Cannot reallocate final demand.")
|
|
64
|
-
if value_added not in mrio.parts.keys():
|
|
65
|
-
log.error(f"Part {value_added} not found in MRIO. Cannot reallocate final demand.")
|
|
66
|
-
raise ValueError(f"Part {value_added} not found in MRIO. Cannot reallocate final demand.")
|
|
67
|
-
|
|
68
|
-
log.info(f"Reallocate final values between {final_demand} and {value_added}")
|
|
69
|
-
|
|
70
|
-
fd = mrio.parts[final_demand]
|
|
71
|
-
va = mrio.parts[value_added]
|
|
72
|
-
|
|
73
|
-
###Extract the negative value added and replace them by
|
|
74
|
-
if from_fd:
|
|
75
|
-
log.info("Remove negatives from the final demand")
|
|
76
|
-
selectable_fd = fd
|
|
77
|
-
#Select transferrable data
|
|
78
|
-
if category_fd is not None and fd.axes[1].ndim==2:
|
|
79
|
-
selectable_fd = fd["all",["all",category_fd]]
|
|
80
|
-
|
|
81
|
-
loc_negs_fd = np.where(selectable_fd.data<0)
|
|
82
|
-
neg_fd = selectable_fd.data[loc_negs_fd]
|
|
83
|
-
#Sum over importing countries and final demand categories
|
|
84
|
-
selectable_fd.data[loc_negs_fd] = 0
|
|
85
|
-
if category_fd is not None and fd.axes[1].ndim==2:
|
|
86
|
-
fd["all",["all",category_fd]] = selectable_fd
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if from_va:
|
|
90
|
-
log.info("Remove negatives from the value added")
|
|
91
|
-
selectable_va = va
|
|
92
|
-
#Select transferrable data
|
|
93
|
-
if category_va is not None:
|
|
94
|
-
if selectable_va.ndim==1:
|
|
95
|
-
log.warning("The value added part is aggregated. The argument 'category_va' will be ignored.")
|
|
96
|
-
else:
|
|
97
|
-
selectable_va = va[category_va]
|
|
98
|
-
|
|
99
|
-
loc_negs_va = np.where(selectable_va.data<0)
|
|
100
|
-
neg_va = selectable_va.data[loc_negs_va].copy()
|
|
101
|
-
selectable_va.data[loc_negs_va] = 0
|
|
102
|
-
if neg_va.ndim>1:
|
|
103
|
-
#Sum over va categories if needed
|
|
104
|
-
neg_va = neg_va.sum(axis=0)
|
|
105
|
-
|
|
106
|
-
if category_va is not None and selectable_va.ndim>1:
|
|
107
|
-
va[category_va] = selectable_va
|
|
108
|
-
|
|
109
|
-
###Add the negative values to the other part
|
|
110
|
-
|
|
111
|
-
if from_fd:
|
|
112
|
-
log.info("Add final demand negatives to the value added")
|
|
113
|
-
for value,position in zip(neg_fd,loc_negs_fd[0]):
|
|
114
|
-
if va.ndim==2:
|
|
115
|
-
va[[destination_va],[position]] += -value
|
|
116
|
-
else:
|
|
117
|
-
va[[position]] += -value
|
|
118
|
-
|
|
119
|
-
if from_va:
|
|
120
|
-
log.info("Add value added negatives to the final demand")
|
|
121
|
-
|
|
122
|
-
#Negative value added are only added to the domestic final demand
|
|
123
|
-
for value,position in zip(neg_va,loc_negs_va[-1]):
|
|
124
|
-
country = position // fd.axes[0].multipliers[fd.axes[0].dimensions[0]]
|
|
125
|
-
if fd.axes[1].ndim==1:
|
|
126
|
-
fd[[[position],[country]]] += -value
|
|
127
|
-
else:
|
|
128
|
-
fd[position,[[country],[destination_fd]]] += -value
|
|
129
|
-
|
|
130
|
-
if validate_intermediates:
|
|
131
|
-
mrio.validate_intermediates()
|
|
132
|
-
|
|
133
|
-
def adjust_intermediates(mrio,
|
|
134
|
-
final_demand = "y",
|
|
135
|
-
inter_industry = "t",
|
|
136
|
-
destination = -1,
|
|
137
|
-
save_gross_output=True):
|
|
138
|
-
"""
|
|
139
|
-
Ensures that intermediate use is always less than gross output
|
|
140
|
-
|
|
141
|
-
This is required to avoid negative value added as residual.
|
|
142
|
-
The domestic final demand is adjusted to make sure gross output is larger than intermediate use.
|
|
143
|
-
|
|
144
|
-
Parameters
|
|
145
|
-
----------
|
|
146
|
-
mrio : MRIO instance
|
|
147
|
-
Instance on which the operation applies
|
|
148
|
-
final_demand : str, optional
|
|
149
|
-
Name of the final demand part. The default is "y"
|
|
150
|
-
inter_industry : str, optional
|
|
151
|
-
Name of the inter-industry part. The default is "t"
|
|
152
|
-
destination : str, optional
|
|
153
|
-
Final demand categories to which negative values are transferred.
|
|
154
|
-
The default is -1, i.e. the last final demand category.
|
|
155
|
-
"""
|
|
156
|
-
if final_demand not in mrio.parts.keys():
|
|
157
|
-
log.error(f"Part {final_demand} not found in MRIO. Cannot readjust intermediates.")
|
|
158
|
-
raise ValueError(f"Part {final_demand} not found in MRIO. Cannot readjust intermediates.")
|
|
159
|
-
if inter_industry not in mrio.parts.keys():
|
|
160
|
-
log.error(f"Part {inter_industry} not found in MRIO. Cannot readjust intermediates.")
|
|
161
|
-
raise ValueError(f"Part {inter_industry} not found in MRIO. Cannot readjust intermediates.")
|
|
162
|
-
|
|
163
|
-
log.info(f"Adjust intermediates in parts {inter_industry} and {final_demand}")
|
|
164
|
-
fd = mrio.parts[final_demand]
|
|
165
|
-
t = mrio.parts[inter_industry]
|
|
166
|
-
original_x = t.sum(1) + fd.sum(1)
|
|
167
|
-
residual = original_x - t.sum(0)
|
|
168
|
-
|
|
169
|
-
loc_negs = np.where(residual.data<0)
|
|
170
|
-
negs = residual.data[loc_negs]
|
|
171
|
-
|
|
172
|
-
for value,position in zip(negs,loc_negs[-1]):
|
|
173
|
-
country = position // fd.axes[0].multipliers[fd.axes[0].dimensions[0]]
|
|
174
|
-
if fd.axes[1].ndim==1:
|
|
175
|
-
fd[[[position],[country]]] += -value
|
|
176
|
-
else:
|
|
177
|
-
fd[position,[[country],[destination]]] += -value
|
|
178
|
-
|
|
179
|
-
if save_gross_output:
|
|
180
|
-
if not isinstance(save_gross_output,str):
|
|
181
|
-
save_gross_output = "x"
|
|
182
|
-
mrio.parts[save_gross_output] = t.sum(1) + fd.sum(1)
|
|
183
|
-
|
|
184
|
-
def fill_empty_rows(
|
|
185
|
-
mrio,
|
|
186
|
-
final_demand = "y",
|
|
187
|
-
inter_industry = "t",
|
|
188
|
-
target = -1,
|
|
189
|
-
fill_value=0.5,
|
|
190
|
-
save_gross_output = True
|
|
191
|
-
):
|
|
192
|
-
"""
|
|
193
|
-
Insert fill values in empty rows
|
|
194
|
-
|
|
195
|
-
This avoids any issue with missing data (e.g. for the Leontief inversion)
|
|
196
|
-
The fill value is inserted on the diagonal of empty rows in the inter-industry matrix
|
|
197
|
-
and as domestic consumption for final demand,
|
|
198
|
-
such that it does not interfere with the rest of the table.
|
|
199
|
-
The fill_value 1 must be avoided, as it conflicts with the Leontief inversion.
|
|
200
|
-
Note that the fill value slightly changes the aggregate.
|
|
201
|
-
"""
|
|
202
|
-
if inter_industry not in mrio.parts.keys():
|
|
203
|
-
log.error(f"Part {inter_industry} not found in MRIO. Cannot fill the empty rows.")
|
|
204
|
-
raise ValueError(f"Part {inter_industry} not found in MRIO. Cannot fill the empty rows.")
|
|
205
|
-
if final_demand not in mrio.parts.keys():
|
|
206
|
-
log.error(f"Part {final_demand} not found in MRIO. Cannot fill the empty rows.")
|
|
207
|
-
raise ValueError(f"Part {final_demand} not found in MRIO. Cannot fill the empty rows.")
|
|
208
|
-
|
|
209
|
-
log.info(f"Fill empty rows in parts {inter_industry} and {final_demand}")
|
|
210
|
-
t = mrio.parts[inter_industry]
|
|
211
|
-
y = mrio.parts[final_demand]
|
|
212
|
-
|
|
213
|
-
x = t.sum(1) + y.sum(1)
|
|
214
|
-
zeros = np.where(x.data==0)
|
|
215
|
-
sectors = t.axes[0].labels[t.axes[0].dimensions[1]]
|
|
216
|
-
for zero in zeros[0]:
|
|
217
|
-
|
|
218
|
-
t[[zero],[zero]] = fill_value
|
|
219
|
-
country = zero // y.axes[0].multipliers[y.axes[0].dimensions[0]]
|
|
220
|
-
sector = sectors[zero % y.axes[0].multipliers[y.axes[0].dimensions[0]]]
|
|
221
|
-
log.info(f"Fill empty row {zero}: {mrio.countries[country]}, {sector}")
|
|
222
|
-
if y.axes[1].ndim==1:
|
|
223
|
-
y[[zero],[country]] = fill_value
|
|
224
|
-
else:
|
|
225
|
-
y[[zero],[[country],[target]]] = fill_value
|
|
226
|
-
|
|
227
|
-
if save_gross_output:
|
|
228
|
-
if not isinstance(save_gross_output,str):
|
|
229
|
-
save_gross_output = "x"
|
|
230
|
-
mrio.parts[save_gross_output] = t.sum(1) + y.sum(1)
|
|
231
|
-
|
|
232
|
-
def balance_va(
|
|
233
|
-
mrio,
|
|
234
|
-
final_demand="y",
|
|
235
|
-
value_added="va",
|
|
236
|
-
inter_industry="t",
|
|
237
|
-
save_as_category=False,
|
|
238
|
-
category="residual"
|
|
239
|
-
):
|
|
240
|
-
"""Fill residuals in the value added part.
|
|
241
|
-
|
|
242
|
-
This makes sure the balance is respected.
|
|
243
|
-
If save_as_category is False, the value_added part is overwritten
|
|
244
|
-
in order to balance the table.
|
|
245
|
-
Otherwise, the residuals are stored in the given category.
|
|
246
|
-
If a non-existing category is provided, it is added to the existing va labels.
|
|
247
|
-
|
|
248
|
-
Parameters
|
|
249
|
-
----------
|
|
250
|
-
mrio : MRIO instance
|
|
251
|
-
The MRIO instance to format.
|
|
252
|
-
final_demand : str
|
|
253
|
-
The name of the final demand part.
|
|
254
|
-
inter_industry : str
|
|
255
|
-
The name of the inter-industry part.
|
|
256
|
-
value_added : str
|
|
257
|
-
The name of the value added part.
|
|
258
|
-
save_as_category : bool
|
|
259
|
-
Whether to save the residuals in a category.
|
|
260
|
-
Otherwise, the value added is defined as the full residual
|
|
261
|
-
category : str
|
|
262
|
-
Name of the category under which the residual is stored.
|
|
263
|
-
If the category does not exist, it is created.
|
|
264
|
-
If the value added was flat, a va_labs dimension is added.
|
|
265
|
-
"""
|
|
266
|
-
if inter_industry not in mrio.parts.keys():
|
|
267
|
-
log.error(f"Part {inter_industry} not found in MRIO. Cannot fill the empty rows.")
|
|
268
|
-
raise ValueError(f"Part {inter_industry} not found in MRIO. Cannot fill the empty rows.")
|
|
269
|
-
if final_demand not in mrio.parts.keys():
|
|
270
|
-
log.error(f"Part {final_demand} not found in MRIO. Cannot fill the empty rows.")
|
|
271
|
-
raise ValueError(f"Part {final_demand} not found in MRIO. Cannot fill the empty rows.")
|
|
272
|
-
|
|
273
|
-
log.info(f"Balance value added in part {value_added}")
|
|
274
|
-
t,y = mrio.parts[inter_industry], mrio.parts[final_demand]
|
|
275
|
-
|
|
276
|
-
x = t.sum(1) + y.sum(1)
|
|
277
|
-
|
|
278
|
-
if not save_as_category:
|
|
279
|
-
log.info(f"Write balanced value added in part {value_added}")
|
|
280
|
-
mrio.parts[value_added] = x - t.sum(0)
|
|
281
|
-
return
|
|
282
|
-
|
|
283
|
-
log.info(f"Save residuals in category '{category}' of part {value_added}")
|
|
284
|
-
if value_added not in mrio.parts.keys():
|
|
285
|
-
log.error(f"Part {value_added} not found in MRIO. Cannot fill the empty rows.")
|
|
286
|
-
raise ValueError(f"Part {value_added} not found in MRIO. Cannot fill the empty rows.")
|
|
287
|
-
va = mrio.parts[value_added]
|
|
288
|
-
residual = x - t.sum(0) - va.sum(0)
|
|
289
|
-
|
|
290
|
-
#Adjust va dimensions
|
|
291
|
-
if va.ndim == 1:
|
|
292
|
-
mrio.add_dimensions({
|
|
293
|
-
"va_labs" : ["original_va", category]
|
|
294
|
-
})
|
|
295
|
-
new_va = mrio.new_part(
|
|
296
|
-
name="balanced_va",
|
|
297
|
-
data = np.zeros((2,va.shape[0])),
|
|
298
|
-
dims = ["va_labs", va.axes[0].dimensions]
|
|
299
|
-
)
|
|
300
|
-
new_va["original_va"] = va
|
|
301
|
-
|
|
302
|
-
if category not in va.axes[0].labels[va.axes[0].dimensions[0]]:
|
|
303
|
-
va_labs = va.axes[0].dimensions[0]
|
|
304
|
-
mrio.add_labels([category], va_labs)
|
|
305
|
-
va = mrio.parts[value_added] #Reload va with new labels
|
|
306
|
-
|
|
307
|
-
va[category] += residual
|
|
308
|
-
|
|
309
|
-
def compute_technical_coefficients(mrio,
|
|
310
|
-
name="a",
|
|
311
|
-
inter_industry="t",
|
|
312
|
-
final_demand = "y"
|
|
313
|
-
):
|
|
314
|
-
"""
|
|
315
|
-
Compute the technical coefficients matrix.
|
|
316
|
-
|
|
317
|
-
This is the inter-industry matrix normalized by the gross output.
|
|
318
|
-
|
|
319
|
-
Parameters
|
|
320
|
-
----------
|
|
321
|
-
mrio : MRIO
|
|
322
|
-
The MRIO instance to format.
|
|
323
|
-
name : str
|
|
324
|
-
The name under which the technical coefficients matrix will be stored.
|
|
325
|
-
The default is "a".
|
|
326
|
-
If no name is provided, the technical coefficients matrix is not stored and directly returned.
|
|
327
|
-
inter_industry : str
|
|
328
|
-
The name of the inter-industry matrix stored.
|
|
329
|
-
The default is "t".
|
|
330
|
-
final_demand : str
|
|
331
|
-
The name of the final demand matrix stored.
|
|
332
|
-
The default is "y".
|
|
333
|
-
"""
|
|
334
|
-
if inter_industry not in mrio.parts.keys():
|
|
335
|
-
log.error(f"Part {inter_industry} not found in MRIO. Cannot compute the technical coefficients matrix.")
|
|
336
|
-
raise ValueError(f"Part {inter_industry} not found in MRIO. Cannot compute the technical coefficients matrix.")
|
|
337
|
-
t = mrio.parts[inter_industry]
|
|
338
|
-
|
|
339
|
-
if final_demand not in mrio.parts.keys():
|
|
340
|
-
log.error(f"Part {final_demand} not found in MRIO. Cannot compute the technical coefficients matrix.")
|
|
341
|
-
raise ValueError(f"Part {final_demand} not found in MRIO. Cannot compute the technical coefficients matrix.")
|
|
342
|
-
y = mrio.parts[final_demand]
|
|
343
|
-
x = t.sum(1) + y.sum(1)
|
|
344
|
-
x.data[x.data==0] = 1 #Avoid division by zero
|
|
345
|
-
|
|
346
|
-
if not name:
|
|
347
|
-
return t.mul((1/x).diag())
|
|
348
|
-
|
|
349
|
-
mrio.parts[name] = t.mul((1/x).diag())
|
|
350
|
-
|
|
351
|
-
def compute_leontief(mrio,
|
|
352
|
-
name="l",
|
|
353
|
-
technical_coefficients_matrix="a",
|
|
354
|
-
**kwargs):
|
|
355
|
-
"""
|
|
356
|
-
Compute the Leontief inverse matrix.
|
|
357
|
-
|
|
358
|
-
If the technical coefficients matrix is not provided, it will be computed.
|
|
359
|
-
|
|
360
|
-
Parameters
|
|
361
|
-
----------
|
|
362
|
-
mrio : MRIO
|
|
363
|
-
The MRIO instance to format.
|
|
364
|
-
name : str
|
|
365
|
-
The name under which the Leontief inverse matrix will be stored.
|
|
366
|
-
technical_coefficients_matrix : str
|
|
367
|
-
The name of the technical coefficients matrix to use.
|
|
368
|
-
If the matrix does not exist, it is computed and saved under the corresponding name.
|
|
369
|
-
If False, the technical coefficients matrix is computed but not stored.
|
|
370
|
-
kwargs : dict
|
|
371
|
-
Additional keyword arguments for the technical coefficients matrix computation.
|
|
372
|
-
"""
|
|
373
|
-
if not technical_coefficients_matrix:
|
|
374
|
-
a = compute_technical_coefficients(mrio,
|
|
375
|
-
name=technical_coefficients_matrix,**kwargs)
|
|
376
|
-
else:
|
|
377
|
-
compute_technical_coefficients(mrio, name= technical_coefficients_matrix,
|
|
378
|
-
**kwargs)
|
|
379
|
-
a = mrio.parts[technical_coefficients_matrix]
|
|
380
|
-
mrio.parts[name] = a.leontief_inversion()
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
def preprocess(
|
|
384
|
-
mrio,
|
|
385
|
-
reallocate_negatives = False,
|
|
386
|
-
adjust_intermediates = False,
|
|
387
|
-
fill_empty_rows = False,
|
|
388
|
-
balance_va = False,
|
|
389
|
-
group_parts = False,
|
|
390
|
-
final_demand="y",
|
|
391
|
-
inter_industry="t",
|
|
392
|
-
value_added="va",
|
|
393
|
-
compute_technical_coefficients = False,
|
|
394
|
-
compute_leontief = False,
|
|
395
|
-
save = False
|
|
396
|
-
):
|
|
397
|
-
"""
|
|
398
|
-
Preprocess MRIO data.
|
|
399
|
-
|
|
400
|
-
The following operations are available:
|
|
401
|
-
- reallocate_negatives:
|
|
402
|
-
Allocate negative final demand and value added
|
|
403
|
-
to the opposite member
|
|
404
|
-
- adjust_intermediates:
|
|
405
|
-
Adjust intermediate consumption values to avoid negative value added.
|
|
406
|
-
- fill_empty_rows:
|
|
407
|
-
Add empty rows in the MRIO tables, to avoid computational errors.
|
|
408
|
-
- balance_va:
|
|
409
|
-
Balance the table by adding residuals to the value added.
|
|
410
|
-
- group_parts:
|
|
411
|
-
Group parts in the MRIO tables.
|
|
412
|
-
- compute_technical_coefficients:
|
|
413
|
-
Compute the technical coefficients matrix.
|
|
414
|
-
- compute_leontief:
|
|
415
|
-
Compute the Leontief inverse matrix.
|
|
416
|
-
|
|
417
|
-
Parameters
|
|
418
|
-
----------
|
|
419
|
-
mrio : MRIO instance
|
|
420
|
-
MRIO instance to preprocess.
|
|
421
|
-
reallocate_negatives : dict
|
|
422
|
-
Parameters for reallocating negative values.
|
|
423
|
-
adjust_intermediates : dict
|
|
424
|
-
Parameters for adjusting intermediate values.
|
|
425
|
-
fill_empty_rows : dict
|
|
426
|
-
Parameters for filling empty rows.
|
|
427
|
-
balance_va : dict
|
|
428
|
-
Parameters for balancing value added.
|
|
429
|
-
group_parts : dict
|
|
430
|
-
Parameters for grouping parts.
|
|
431
|
-
Must be set as a dictionary:
|
|
432
|
-
part_name : parameters for the sum operator (axes, dimension)
|
|
433
|
-
final_demand : str
|
|
434
|
-
Name of the final demand matrix.
|
|
435
|
-
inter_industry : str
|
|
436
|
-
Name of the inter-industry matrix.
|
|
437
|
-
value_added : str
|
|
438
|
-
Name of the value added matrix.
|
|
439
|
-
compute_technical_coefficients : bool or dict
|
|
440
|
-
Whether to compute the technical coefficients matrix.
|
|
441
|
-
If dict, optional parameters for the computation.
|
|
442
|
-
compute_leontief : bool or dict
|
|
443
|
-
Whether to compute the Leontief inverse matrix.
|
|
444
|
-
If dict, optional parameters for the computation.
|
|
445
|
-
save : dict
|
|
446
|
-
Parameters to save the processed MRIO instance.
|
|
447
|
-
Parameters for the saving process.
|
|
448
|
-
"""
|
|
449
|
-
log.info("Start formatting MRIO")
|
|
450
|
-
|
|
451
|
-
if group_parts:
|
|
452
|
-
for part, args in group_parts.items():
|
|
453
|
-
log.info(f"Group part '{part}' over {args}")
|
|
454
|
-
mrio.parts[part] = mrio.parts[part].sum(**args)
|
|
455
|
-
|
|
456
|
-
if reallocate_negatives:
|
|
457
|
-
if isinstance(reallocate_negatives, bool):
|
|
458
|
-
reallocate_negatives = dict()
|
|
459
|
-
mrio.reallocate_negatives(
|
|
460
|
-
final_demand=final_demand,
|
|
461
|
-
value_added=value_added,
|
|
462
|
-
**reallocate_negatives
|
|
463
|
-
)
|
|
464
|
-
|
|
465
|
-
if fill_empty_rows:
|
|
466
|
-
if isinstance(fill_empty_rows, bool):
|
|
467
|
-
fill_empty_rows = dict()
|
|
468
|
-
mrio.fill_empty_rows(
|
|
469
|
-
final_demand=final_demand,
|
|
470
|
-
inter_industry=inter_industry,
|
|
471
|
-
**fill_empty_rows
|
|
472
|
-
)
|
|
473
|
-
|
|
474
|
-
if adjust_intermediates:
|
|
475
|
-
if isinstance(adjust_intermediates, bool):
|
|
476
|
-
adjust_intermediates = dict()
|
|
477
|
-
mrio.adjust_intermediates(
|
|
478
|
-
final_demand=final_demand,
|
|
479
|
-
inter_industry=inter_industry,
|
|
480
|
-
**adjust_intermediates
|
|
481
|
-
)
|
|
482
|
-
|
|
483
|
-
if balance_va:
|
|
484
|
-
if isinstance(balance_va, bool):
|
|
485
|
-
balance_va = dict()
|
|
486
|
-
mrio.balance_va(
|
|
487
|
-
final_demand=final_demand,
|
|
488
|
-
inter_industry=inter_industry,
|
|
489
|
-
value_added=value_added,
|
|
490
|
-
**balance_va
|
|
491
|
-
)
|
|
492
|
-
|
|
493
|
-
if compute_technical_coefficients:
|
|
494
|
-
if isinstance(compute_technical_coefficients, bool):
|
|
495
|
-
compute_technical_coefficients = dict()
|
|
496
|
-
mrio.compute_technical_coefficients(
|
|
497
|
-
inter_industry=inter_industry,
|
|
498
|
-
final_demand=final_demand,
|
|
499
|
-
**compute_technical_coefficients
|
|
500
|
-
)
|
|
501
|
-
|
|
502
|
-
if compute_leontief:
|
|
503
|
-
if isinstance(compute_leontief, bool):
|
|
504
|
-
compute_leontief = dict()
|
|
505
|
-
mrio.compute_leontief(
|
|
506
|
-
inter_industry=inter_industry,
|
|
507
|
-
final_demand=final_demand,
|
|
508
|
-
**compute_leontief
|
|
509
|
-
)
|
|
510
|
-
|
|
511
|
-
if save:
|
|
512
|
-
if isinstance(save, bool):
|
|
513
|
-
save = dict()
|
|
514
|
-
mrio.save(**save)
|
|
515
|
-
|
|
516
|
-
def rename_part(mrio,old_name,new_name):
|
|
517
|
-
"""Change the name of an MRIO part"""
|
|
518
|
-
if old_name not in mrio.parts.keys():
|
|
519
|
-
raise KeyError(f"Part '{old_name}' not found in MRIO. Cannot rename it to '{new_name}'.")
|
|
520
|
-
log.info(f"Rename part '{old_name}' to '{new_name}'")
|
|
521
|
-
mrio.parts[new_name] = mrio.parts.pop(old_name)
|
|
522
|
-
mrio.parts[new_name].name = new_name
|
|
523
|
-
|
|
524
|
-
def rename_parts(mrio,renaming_dict):
|
|
525
|
-
"""Change the name of a collection of MRIO parts"""
|
|
526
|
-
for old_name,new_name in renaming_dict.items():
|
|
527
|
-
rename_part(mrio,old_name,new_name)
|