This notebook contains the interactive version of the quick start given in the Pymrio article (Stadler et al 2018 sub).
Pymrio requires a Python version >= 3.7. If you don't have Python installed, I recommend to use the Anaconda Scientific Python package.
Pymrio is available on
and on
Thus, two possibilities exist to install Pymrio and all required packages.
For using the version on PyPI use:
pip install pymrio --upgrade
To install from the Anaconda Cloud do:
conda install -c conda-forge pymrio
Further down in that notebood we will also use the country_converter package as well as seaborn and matplotlib for some plotting. You can install these packages with pip or conda analog to pymrio. Alternatively, you can also run this notebook in the cloud via binder following this link:
You can than import the Pymrio package with
import pymrio
In this example here, we will use the WIOD MRIO database.
First, the Pymrio MRIO download function is used to get the WIOD MRIO database with:
raw_wiod_path = '/tmp/wiod/raw'
pymrio.download_wiod2013(storage_folder=raw_wiod_path,
years=[2008])
This downloads the 2008 MRIO table from WIOD. Omitting the year parameter would result getting all years. The function returns a Pymrio meta data object, which gives information about the WIOD version, system (in this case industry by industry) and records about from where the data was received.
To parse the database into a Pymrio object use:
wiod = pymrio.parse_wiod(raw_wiod_path, year=2008)
The available data can be explored by for example:
wiod.get_sectors()
or
wiod.get_regions()
wiod.Z
WIOD includes several satellite accounts, which are stored as child objects in Pymrio. For example, in order to see the AIR emissions provided by WIOD:
wiod.AIR.F
WIOD, however, does neither provide any normalized data (A-matrix, satellite account coefficient data) nor any consumption based accounts (footprints).
In order to calculate them, one could go through all the missing data and compute each account. Pymrio provides the required function, for example to calculate the A-matrix:
x = pymrio.calc_x(Z=wiod.Z, Y=wiod.Y)
A = pymrio.calc_A(Z=wiod.Z, x=x)
A.head()
Alternatively, Pymrio provides a function which iterates through all missing accounts and calculates them:
wiod.calc_all()
At this point, a basic EE MRIO analysis is accomplished. For example, the regional consumption based accounts of the AIR emissions are now given by:
wiod.AIR.D_cba_reg
wiod.AIR.unit
Pymrio can be linked with the country converter coco to ease the aggregation of MRIO and results into different classifications. Using the country converter, WIOD can be aggregated into EU and non-EU countries with singling out Germany by:
import country_converter as coco
wiod.aggregate(region_agg = coco.agg_conc(original_countries='WIOD',
aggregates=[{'DEU': 'DEU', 'GBR':'GBR'}, 'EU'],
missing_countries='Other',
merge_multiple_string=None))
We rename the EU account to reflect that is does not include Germany:
wiod.rename_regions({'EU':'Rest of EU'})
The regional footprint account are now:
wiod.AIR.D_cba_reg
To visualize for example the CH4 accounts:
import matplotlib.pyplot as plt
with plt.style.context('ggplot'):
wiod.AIR.plot_account('CH4', figsize=(8,5))
plt.savefig('/tmp/wiod/airch4.png', dpi=300)
plt.show()
To calculate the source (in terms of regions and sectors) of a certain stressor or impact driven by consumption, one needs to diagonalize this stressor/impact. This can be done with Pymrio by:
diag_CH4 = wiod.AIR.diag_stressor('CH4')
and be reassigned to the aggregated WIOD system:
wiod.CH4_source = diag_CH4
In the next step the automatic calculation routine of Pymrio is called again to compute the missing accounts in this new extension: and be reassigned to the aggregated WIOD system:
wiod.calc_all()
The diagonalized CH4 data now shows the source and destination of the specified stressor (CH4):
wiod.CH4_source.D_cba.head()
In this square footprint matrix, every column represents the amount of stressor occurring in each region - sector driven by the consumption stated in the column header. Conversly, each row states where the stressor impacts occurring in the row are distributed due (from where they are driven).
CH4_source_reg = wiod.CH4_source.D_cba.groupby(
level='region', axis=0).sum().groupby(
level='region', axis=1).sum()
CH4_source_reg
import seaborn as sns
CH4_source_reg.columns.name = 'Receiving region'
CH4_source_reg.index.name = 'Souce region'
sns.heatmap(CH4_source_reg, vmax=5E6,
annot=True, cmap='YlOrRd', linewidths=0.1,
cbar_kws={'label': 'CH4 emissions ({})'.format(wiod.CH4_source.unit.unit[0])})
plt.savefig('/tmp/wiod/airch4_source_reg.png', dpi=300)
plt.show()
Storing the MRIO database can be done with
storage_path = '/tmp/wiod/aly'
wiod.save_all(storage_path)
From where it can be received subsequently by:
wiod = pymrio.load_all(storage_path)
The meta attribute of Pymrio mentioned at the beginning kept track of all modifications of the system. This can be shown with:
wiod.meta
Custom notes can be added to the meta with:
wiod.meta.note("Custom note")
The history of the meta data can be filtered for specific entries like:
wiod.meta.file_io_history
This tutorial gave a short overview about the basic functionality of Pymrio. For more information about the capabilities of pymrio check the online documentation.