From 5ccd85fd243d69cb4ec622f7bfbd9a3849879b18 Mon Sep 17 00:00:00 2001 From: Konstantin Stadler Date: Thu, 25 Jan 2024 12:40:12 +0100 Subject: [PATCH] continue with docs for adjust --- doc/source/notebooks/adjusting.ipynb | 118 +++++++++++++++++++-------- doc/source/notebooks/adjusting.py | 103 +++++++++++++++++++++++ doc/source/notebooks/explore.py | 10 +-- 3 files changed, 194 insertions(+), 37 deletions(-) create mode 100644 doc/source/notebooks/adjusting.py diff --git a/doc/source/notebooks/adjusting.ipynb b/doc/source/notebooks/adjusting.ipynb index 6b1517e6..7084cd9e 100644 --- a/doc/source/notebooks/adjusting.ipynb +++ b/doc/source/notebooks/adjusting.ipynb @@ -10,7 +10,9 @@ { "cell_type": "code", "execution_count": 2, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "import pymrio" @@ -19,7 +21,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "mrio = pymrio.load_test()" @@ -29,13 +33,15 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We can use several functions to get a quick overview over the MRIO system:" + "We can use several functions to get a quick overview over the MRIO system [(see the Exploring MRIO tutorial for more examples)](explore.ipynb)." ] }, { "cell_type": "code", "execution_count": 4, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { @@ -57,7 +63,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { @@ -77,7 +85,9 @@ { "cell_type": "code", "execution_count": 6, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { @@ -109,12 +119,14 @@ { "cell_type": "code", "execution_count": 7, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, "execution_count": 7, @@ -136,7 +148,9 @@ { "cell_type": "code", "execution_count": 8, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { @@ -164,12 +178,14 @@ { "cell_type": "code", "execution_count": 9, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, "execution_count": 9, @@ -184,7 +200,9 @@ { "cell_type": "code", "execution_count": 10, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { @@ -211,12 +229,14 @@ { "cell_type": "code", "execution_count": 11, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, "execution_count": 11, @@ -232,7 +252,33 @@ { "cell_type": "code", "execution_count": 12, - "metadata": {}, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Index(['food', 'dwarf business', 'manufactoring', 'electricity',\n", + " 'construction', 'trade', 'transport', 'other'],\n", + " dtype='object', name='sector')" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mrio.get_sectors()" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { @@ -245,13 +291,12 @@ " dtype='object', name='category')" ] }, - "execution_count": 12, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "mrio.get_sectors()\n", "mrio.get_Y_categories()" ] }, @@ -265,8 +310,10 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": {}, + "execution_count": 14, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "mrio_class = pymrio.get_classification(mrio_name=\"test\")" @@ -288,8 +335,10 @@ }, { "cell_type": "code", - "execution_count": 14, - "metadata": {}, + "execution_count": 15, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { @@ -391,7 +440,7 @@ "7 8 other sec8 misc" ] }, - "execution_count": 14, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -409,8 +458,10 @@ }, { "cell_type": "code", - "execution_count": 19, - "metadata": {}, + "execution_count": 16, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { @@ -425,7 +476,7 @@ " 'other': 'sec8'}" ] }, - "execution_count": 19, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -446,8 +497,10 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": {}, + "execution_count": 17, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "mrio = pymrio.load_test()" @@ -455,9 +508,10 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 18, "metadata": { - "lines_to_next_cell": 2 + "lines_to_next_cell": 2, + "tags": [] }, "outputs": [ { @@ -466,7 +520,7 @@ "Index(['sec1', 'sec2', 'sec3', 'sec4', 'sec5', 'sec6', 'sec7', 'sec8'], dtype='object', name='sector')" ] }, - "execution_count": 21, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -496,7 +550,7 @@ "formats": "ipynb,py:light" }, "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -510,7 +564,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.5" + "version": "3.10.12" } }, "nbformat": 4, diff --git a/doc/source/notebooks/adjusting.py b/doc/source/notebooks/adjusting.py new file mode 100644 index 00000000..b95f5c78 --- /dev/null +++ b/doc/source/notebooks/adjusting.py @@ -0,0 +1,103 @@ +# --- +# jupyter: +# jupytext: +# formats: ipynb,py:light +# text_representation: +# extension: .py +# format_name: light +# format_version: '1.5' +# jupytext_version: 1.15.0 +# kernelspec: +# display_name: Python 3 (ipykernel) +# language: python +# name: python3 +# --- + +# # Adjusting, Renaming and Restructuring + +# In this tutorial we will cover how to modify the structure of an MRIO system. +# This includes renaming sectors, regions and Y categories, +# as well as restructuring (extracting and merging) satellite accounts. + + +# We use the small test MRIO system that is included in the pymrio package for all examples. +# The method work the same for any MRIO system. + +import pymrio + +mrio = pymrio.load_test() + +# We can use several functions to get a quick overview over the MRIO system [(see the Exploring MRIO tutorial for more examples)](explore.ipynb). + +mrio.get_sectors() + +mrio.get_regions() + +mrio.get_Y_categories() + +# A list of available satellite accounts can be obtained by + +mrio.get_extensions() + +# this returns a generator over all extensions, to just get the names which can be used to loop over the extensions. To just get the names: + +list(mrio.get_extensions()) + +# ## Renaming + +# ### Manual renaming + +# All the names returned are literally just names and can easily be renamed. +# To rename sectors or regions just pass a dictionary with the new names: + +mrio.rename_regions({"reg1": "REGION A", "reg2": "REGION B"}) + +mrio.get_regions() + +# Renaming sectors or Y categories works the same way + +mrio.rename_sectors({"mining": "dwarf business"}) +mrio.rename_Y_categories({"Final consumption expenditure by households": "fin_house"}) + +mrio.get_sectors() + +mrio.get_Y_categories() + +# ### Renaming based on included classifications synonyms + +# Some MRIOs come with a selections of names which can be used for renaming. +# These can be obtained via 'get_classification', passing the name of the IO (pass None to get a list of available classifications). + +mrio_class = pymrio.get_classification(mrio_name="test") + +# The classification data contains different names and aggregation levels for the sectors and final demand categories. The easiest way to explore the classification data is by using the autocomplete functionality. Depending on the editor your are using this might work with typing mrio_class.sectors. pressing Tab or Ctrl-Space. The same works for mrio_class.finaldemand. + +# Alternatively it is possible to just print the underlying dataframes with (similar for finaldemand) + +mrio_class.sectors + +# This can be used to generate dictionaries for renaming the sectors of the MRIO, eg with: + +conv_dict = mrio_class.get_sector_dict( + mrio_class.sectors.TestMrioName, mrio_class.sectors.TestMrioCode +) +conv_dict + +# This can then be used for renaming the sectors: + +mrio = pymrio.load_test() + +mrio.rename_sectors(conv_dict) +mrio.get_sectors() + + +# In the mrio_class.sectors you will also find an entry 'Type' which represents a many to one mapping. This can be used for aggregating the MRIO and is explained [in the aggregation tutorial](aggregation_examples.ipynb#Aggregation-by-renaming). + +## Extracting satellite accounts + +# Each extension/satellite account of the MRIO contains a method 'extract'. + + +# Satellite accounts can be extracted from the MRIO system by using the extension_extract method. This method takes the name of the satellite account as first argument and a list of regions as second argument. The regions can be given as a list of region names or as a list of region indices. The method returns a new MRIO system with the extracted satellite account. + +mrio.extension_extract("emissions", ["reg1", "reg2"]) diff --git a/doc/source/notebooks/explore.py b/doc/source/notebooks/explore.py index 1e8a5c02..f3cf1192 100644 --- a/doc/source/notebooks/explore.py +++ b/doc/source/notebooks/explore.py @@ -113,9 +113,9 @@ # The MRIO class also contains a set of specific regular expresion search methods, mirroring the 'contains', 'match' and 'fullmatch' # methods of the pandas DataFrame str column type. See the pandas documentation for details, in short: # -# -. 'contains' looks for a match anywhere in the string -# -. 'match' looks for a match at the beginning of the string -# -. 'fullmatch' looks for a match of the whole string +# - 'contains' looks for a match anywhere in the string +# - 'match' looks for a match at the beginning of the string +# - 'fullmatch' looks for a match of the whole string # # These methods are available for all index columns of the MRIO and have a similar signature: # @@ -127,7 +127,7 @@ # %% mrio.contains(find_all = 'ad') -mrio.contains('ad') +mrio.contains('ad') # find_all is the default argument # %% mrio.match('ad') @@ -170,7 +170,7 @@ # %% [markdown] # This allows to search for terms that are only in some index levels. -# Locially, this is an 'or' search. +# Logically, this is an 'or' search. # %% mrio.factor_inputs.contains(compartment='air', inputtype="Value")