pymrio 0.6.0__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.
Files changed (140) hide show
  1. doc/source/conf.py +275 -0
  2. doc/source/notebooks/adjusting.py +177 -0
  3. doc/source/notebooks/advanced_group_stressors.py +139 -0
  4. doc/source/notebooks/autodownload.py +281 -0
  5. doc/source/notebooks/convert.py +474 -0
  6. doc/source/notebooks/explore.py +220 -0
  7. doc/source/notebooks/extract_data.py +175 -0
  8. doc/source/notebooks/full_tutorial.py +484 -0
  9. doc/source/notebooks/load_save_export.py +213 -0
  10. doc/source/notebooks/metadata.py +91 -0
  11. doc/source/notebooks/stressor_characterization.py +344 -0
  12. doc/source/notebooks/working_with_exiobase.py +202 -0
  13. doc/source/notebooks/working_with_wiod.py +107 -0
  14. pymrio/__init__.py +73 -0
  15. pymrio/core/__init__.py +0 -0
  16. pymrio/core/constants.py +437 -0
  17. pymrio/core/fileio.py +909 -0
  18. pymrio/core/mriosystem.py +4003 -0
  19. pymrio/mrio_models/__init__.py +0 -0
  20. pymrio/mrio_models/exio2_ixi/__init__.py +0 -0
  21. pymrio/mrio_models/exio2_ixi/concordance/ALL.txt +48 -0
  22. pymrio/mrio_models/exio2_ixi/concordance/Africa.txt +48 -0
  23. pymrio/mrio_models/exio2_ixi/concordance/America.txt +48 -0
  24. pymrio/mrio_models/exio2_ixi/concordance/AsiaPacific.txt +48 -0
  25. pymrio/mrio_models/exio2_ixi/concordance/BRIC.txt +48 -0
  26. pymrio/mrio_models/exio2_ixi/concordance/EU.txt +48 -0
  27. pymrio/mrio_models/exio2_ixi/concordance/Europe.txt +48 -0
  28. pymrio/mrio_models/exio2_ixi/concordance/ISIC_A.txt +163 -0
  29. pymrio/mrio_models/exio2_ixi/concordance/ISIC_B.txt +163 -0
  30. pymrio/mrio_models/exio2_ixi/concordance/ISIC_C.txt +163 -0
  31. pymrio/mrio_models/exio2_ixi/concordance/ISIC_D.txt +163 -0
  32. pymrio/mrio_models/exio2_ixi/concordance/ISIC_E.txt +163 -0
  33. pymrio/mrio_models/exio2_ixi/concordance/ISIC_F.txt +163 -0
  34. pymrio/mrio_models/exio2_ixi/concordance/ISIC_G_H.txt +163 -0
  35. pymrio/mrio_models/exio2_ixi/concordance/ISIC_I.txt +163 -0
  36. pymrio/mrio_models/exio2_ixi/concordance/ISIC_J_K.txt +163 -0
  37. pymrio/mrio_models/exio2_ixi/concordance/ISIC_L.txt +163 -0
  38. pymrio/mrio_models/exio2_ixi/concordance/ISIC_M_N_O.txt +163 -0
  39. pymrio/mrio_models/exio2_ixi/concordance/ISIC_P.txt +163 -0
  40. pymrio/mrio_models/exio2_ixi/concordance/MiddleEast.txt +48 -0
  41. pymrio/mrio_models/exio2_ixi/concordance/OECD.txt +48 -0
  42. pymrio/mrio_models/exio2_ixi/concordance/RoW.txt +48 -0
  43. pymrio/mrio_models/exio2_ixi/concordance/__init__.py +0 -0
  44. pymrio/mrio_models/exio2_ixi/concordance/orig_regions.txt +48 -0
  45. pymrio/mrio_models/exio2_ixi/concordance/pro_ISIC_A.txt +200 -0
  46. pymrio/mrio_models/exio2_ixi/concordance/pro_ISIC_B.txt +200 -0
  47. pymrio/mrio_models/exio2_ixi/concordance/pro_ISIC_C.txt +200 -0
  48. pymrio/mrio_models/exio2_ixi/concordance/pro_ISIC_D.txt +200 -0
  49. pymrio/mrio_models/exio2_ixi/concordance/pro_ISIC_E.txt +200 -0
  50. pymrio/mrio_models/exio2_ixi/concordance/pro_ISIC_F.txt +200 -0
  51. pymrio/mrio_models/exio2_ixi/concordance/pro_ISIC_G_H.txt +200 -0
  52. pymrio/mrio_models/exio2_ixi/concordance/pro_ISIC_I.txt +200 -0
  53. pymrio/mrio_models/exio2_ixi/concordance/pro_ISIC_J_K.txt +200 -0
  54. pymrio/mrio_models/exio2_ixi/concordance/pro_ISIC_L.txt +200 -0
  55. pymrio/mrio_models/exio2_ixi/concordance/pro_ISIC_M_N_O.txt +200 -0
  56. pymrio/mrio_models/exio2_ixi/concordance/pro_ISIC_P.txt +200 -0
  57. pymrio/mrio_models/exio2_ixi/misc/__init__.py +0 -0
  58. pymrio/mrio_models/exio2_ixi/misc/population.txt +2 -0
  59. pymrio/mrio_models/exio2_pxp/__init__.py +0 -0
  60. pymrio/mrio_models/exio2_pxp/concordance/ALL.txt +48 -0
  61. pymrio/mrio_models/exio2_pxp/concordance/Africa.txt +48 -0
  62. pymrio/mrio_models/exio2_pxp/concordance/America.txt +48 -0
  63. pymrio/mrio_models/exio2_pxp/concordance/AsiaPacific.txt +48 -0
  64. pymrio/mrio_models/exio2_pxp/concordance/BRIC.txt +48 -0
  65. pymrio/mrio_models/exio2_pxp/concordance/EU.txt +48 -0
  66. pymrio/mrio_models/exio2_pxp/concordance/Europe.txt +48 -0
  67. pymrio/mrio_models/exio2_pxp/concordance/ISIC_A.txt +163 -0
  68. pymrio/mrio_models/exio2_pxp/concordance/ISIC_B.txt +163 -0
  69. pymrio/mrio_models/exio2_pxp/concordance/ISIC_C.txt +163 -0
  70. pymrio/mrio_models/exio2_pxp/concordance/ISIC_D.txt +163 -0
  71. pymrio/mrio_models/exio2_pxp/concordance/ISIC_E.txt +163 -0
  72. pymrio/mrio_models/exio2_pxp/concordance/ISIC_F.txt +163 -0
  73. pymrio/mrio_models/exio2_pxp/concordance/ISIC_G_H.txt +163 -0
  74. pymrio/mrio_models/exio2_pxp/concordance/ISIC_I.txt +163 -0
  75. pymrio/mrio_models/exio2_pxp/concordance/ISIC_J_K.txt +163 -0
  76. pymrio/mrio_models/exio2_pxp/concordance/ISIC_L.txt +163 -0
  77. pymrio/mrio_models/exio2_pxp/concordance/ISIC_M_N_O.txt +163 -0
  78. pymrio/mrio_models/exio2_pxp/concordance/ISIC_P.txt +163 -0
  79. pymrio/mrio_models/exio2_pxp/concordance/MiddleEast.txt +48 -0
  80. pymrio/mrio_models/exio2_pxp/concordance/OECD.txt +48 -0
  81. pymrio/mrio_models/exio2_pxp/concordance/RoW.txt +48 -0
  82. pymrio/mrio_models/exio2_pxp/concordance/__init__.py +0 -0
  83. pymrio/mrio_models/exio2_pxp/concordance/orig_regions.txt +48 -0
  84. pymrio/mrio_models/exio2_pxp/concordance/pro_ISIC_A.txt +200 -0
  85. pymrio/mrio_models/exio2_pxp/concordance/pro_ISIC_B.txt +200 -0
  86. pymrio/mrio_models/exio2_pxp/concordance/pro_ISIC_C.txt +200 -0
  87. pymrio/mrio_models/exio2_pxp/concordance/pro_ISIC_D.txt +200 -0
  88. pymrio/mrio_models/exio2_pxp/concordance/pro_ISIC_E.txt +200 -0
  89. pymrio/mrio_models/exio2_pxp/concordance/pro_ISIC_F.txt +200 -0
  90. pymrio/mrio_models/exio2_pxp/concordance/pro_ISIC_G_H.txt +200 -0
  91. pymrio/mrio_models/exio2_pxp/concordance/pro_ISIC_I.txt +200 -0
  92. pymrio/mrio_models/exio2_pxp/concordance/pro_ISIC_J_K.txt +200 -0
  93. pymrio/mrio_models/exio2_pxp/concordance/pro_ISIC_L.txt +200 -0
  94. pymrio/mrio_models/exio2_pxp/concordance/pro_ISIC_M_N_O.txt +200 -0
  95. pymrio/mrio_models/exio2_pxp/concordance/pro_ISIC_P.txt +200 -0
  96. pymrio/mrio_models/exio2_pxp/misc/__init__.py +0 -0
  97. pymrio/mrio_models/exio2_pxp/misc/population.txt +2 -0
  98. pymrio/mrio_models/exio3_ixi/__init__.py +0 -0
  99. pymrio/mrio_models/exio3_pxp/__init__.py +0 -0
  100. pymrio/mrio_models/test_mrio/__init__.py +0 -0
  101. pymrio/mrio_models/test_mrio/concordance/EU.txt +6 -0
  102. pymrio/mrio_models/test_mrio/concordance/OECD.txt +6 -0
  103. pymrio/mrio_models/test_mrio/concordance/__init__.py +0 -0
  104. pymrio/mrio_models/test_mrio/concordance/orig_regions.txt +6 -0
  105. pymrio/mrio_models/test_mrio/concordance/orig_sectors.txt +8 -0
  106. pymrio/mrio_models/test_mrio/concordance/sector1.txt +8 -0
  107. pymrio/mrio_models/test_mrio/concordance/sector2.txt +8 -0
  108. pymrio/mrio_models/test_mrio/concordance/sector3.txt +8 -0
  109. pymrio/mrio_models/test_mrio/concordance/supreg1.txt +6 -0
  110. pymrio/mrio_models/test_mrio/concordance/supreg2.txt +6 -0
  111. pymrio/mrio_models/test_mrio/mrio_data/FDemissions.txt +4 -0
  112. pymrio/mrio_models/test_mrio/mrio_data/__init__.py +0 -0
  113. pymrio/mrio_models/test_mrio/mrio_data/emissions.txt +4 -0
  114. pymrio/mrio_models/test_mrio/mrio_data/factor_input.txt +3 -0
  115. pymrio/mrio_models/test_mrio/mrio_data/finald_demand_Y.txt +50 -0
  116. pymrio/mrio_models/test_mrio/mrio_data/metadata.json +9 -0
  117. pymrio/mrio_models/test_mrio/mrio_data/population.txt +2 -0
  118. pymrio/mrio_models/test_mrio/mrio_data/readme.txt +5 -0
  119. pymrio/mrio_models/test_mrio/mrio_data/trade_flows_Z.txt +50 -0
  120. pymrio/tools/__init__.py +1 -0
  121. pymrio/tools/ioclass.py +107 -0
  122. pymrio/tools/iodownloader.py +793 -0
  123. pymrio/tools/iomath.py +623 -0
  124. pymrio/tools/iometadata.py +477 -0
  125. pymrio/tools/ioparser.py +2481 -0
  126. pymrio/tools/ioutil.py +1542 -0
  127. pymrio/version.py +6 -0
  128. pymrio-0.6.0.dist-info/METADATA +232 -0
  129. pymrio-0.6.0.dist-info/RECORD +140 -0
  130. pymrio-0.6.0.dist-info/WHEEL +5 -0
  131. pymrio-0.6.0.dist-info/licenses/LICENSE +57 -0
  132. pymrio-0.6.0.dist-info/top_level.txt +3 -0
  133. tests/__init__.py +0 -0
  134. tests/test_aggregation.py +235 -0
  135. tests/test_core.py +1436 -0
  136. tests/test_integration.py +371 -0
  137. tests/test_math.py +754 -0
  138. tests/test_outputs.py +105 -0
  139. tests/test_parsers.py +296 -0
  140. tests/test_util.py +1104 -0
doc/source/conf.py ADDED
@@ -0,0 +1,275 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ #
4
+ # pymrio documentation build configuration file, created by
5
+ # sphinx-quickstart on Fri May 30 10:59:10 2014.
6
+ #
7
+ # Note that not all possible configuration values are present in this
8
+ # autogenerated file.
9
+ #
10
+ # All configuration values have a default; values that are commented out
11
+ # serve to show the default.
12
+
13
+ import os
14
+ import sys
15
+
16
+ # add the pymrio path for the autodoc function
17
+
18
+ # If extensions (or modules to document with autodoc) are in another directory,
19
+ # add these directories to sys.path here. If the directory is relative to the
20
+ # documentation root, use os.path.abspath to make it absolute, like shown here.
21
+ # Added all these to make readthedocs work
22
+ sys.path.insert(0, os.path.abspath("."))
23
+ sys.path.insert(0, os.path.abspath(".."))
24
+ sys.path.insert(0, os.path.abspath("../.."))
25
+ sys.path.insert(0, os.path.abspath("../../.."))
26
+
27
+ # -- General configuration ----
28
+
29
+
30
+ # If your documentation needs a minimal Sphinx version, state it here.
31
+ # needs_sphinx = '1.0'
32
+
33
+ # Add any Sphinx extension module names here, as strings. They can be
34
+ # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
35
+ extensions = [
36
+ "sphinx.ext.napoleon",
37
+ "sphinx.ext.autodoc",
38
+ "nbsphinx",
39
+ "IPython.sphinxext.ipython_console_highlighting",
40
+ "sphinx.ext.mathjax",
41
+ "sphinx.ext.coverage",
42
+ "sphinx.ext.autosummary",
43
+ ]
44
+
45
+ nbsphinx_execute = "never"
46
+
47
+ napoleon_numpy_docstring = True
48
+ napoleon_google_docstring = False
49
+
50
+ napoleon_include_init_with_doc = False
51
+ napoleon_include_private_with_doc = True
52
+ napoleon_use_param = True
53
+ napoleon_use_ivar = True
54
+
55
+ # automatically generate api references
56
+ autosummary_generate = ["api_references.rst"]
57
+
58
+ # Add any paths that contain templates here, relative to this directory.
59
+ templates_path = ["_templates"]
60
+
61
+ # The suffix of source filenames.
62
+ source_suffix = ".rst"
63
+
64
+ # The encoding of source files.
65
+ # source_encoding = 'utf-8-sig'
66
+
67
+ # The master toctree document.
68
+ master_doc = "index"
69
+
70
+ # General information about the project.
71
+ project = "pymrio"
72
+ copyright = "2014, Konstantin Stadler"
73
+
74
+ # The version info for the project you're documenting, acts as replacement for
75
+ # |version| and |release|, also used in various other places throughout the
76
+ # built documents.
77
+ #
78
+ # The short X.Y version.
79
+
80
+ import pymrio # noqa
81
+
82
+ version = pymrio.__version__
83
+ # The full version, including alpha/beta/rc tags.
84
+ release = pymrio.__version__
85
+
86
+ # The language for content autogenerated by Sphinx. Refer to documentation
87
+ # for a list of supported languages.
88
+ # language = None
89
+
90
+ # There are two options for replacing |today|: either, you set today to some
91
+ # non-false value, then it is used:
92
+ # today = ''
93
+ # Else, today_fmt is used as the format for a strftime call.
94
+ # today_fmt = '%B %d, %Y'
95
+
96
+ # List of patterns, relative to source directory, that match files and
97
+ # directories to ignore when looking for source files.
98
+ exclude_patterns = ["_build", "**.ipynb_checkpoints"]
99
+
100
+ # The reST default role (used for this markup: `text`) to use for all
101
+ # documents.
102
+ # default_role = None
103
+
104
+ # If true, '()' will be appended to :func: etc. cross-reference text.
105
+ # add_function_parentheses = True
106
+
107
+ # If true, the current module name will be prepended to all description
108
+ # unit titles (such as .. function::).
109
+ # add_module_names = True
110
+
111
+ # If true, sectionauthor and moduleauthor directives will be shown in the
112
+ # output. They are ignored by default.
113
+ # show_authors = False
114
+
115
+ # The name of the Pygments (syntax highlighting) style to use.
116
+ pygments_style = "sphinx"
117
+
118
+ # A list of ignored prefixes for module index sorting.
119
+ # modindex_common_prefix = []
120
+
121
+
122
+ # -- Options for HTML output ------------------------
123
+
124
+ # The theme to use for HTML and HTML Help pages. See the documentation for a
125
+ # list of builtin themes.
126
+ # html_theme = 'pyramid'
127
+ html_theme = "sphinx_rtd_theme"
128
+
129
+ # Theme options are theme-specific and customize the look and feel of a theme
130
+ # further. For a list of options available for each theme, see the
131
+ # documentation.
132
+ html_theme_options = {}
133
+
134
+ # Add any paths that contain custom themes here, relative to this directory.
135
+ # html_theme_path = []
136
+
137
+ # The name for this set of Sphinx documents. If None, it defaults to
138
+ # "<project> v<release> documentation".
139
+ # html_title = None
140
+
141
+ # A shorter title for the navigation bar. Default is the same as html_title.
142
+ # html_short_title = None
143
+
144
+ # The name of an image file (relative to this directory) to place at the top
145
+ # of the sidebar.
146
+ # html_logo = None
147
+
148
+ # The name of an image file (within the static path) to use as favicon of the
149
+ # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
150
+ # pixels large.
151
+ # html_favicon = None
152
+
153
+ # Add any paths that contain custom static files (such as style sheets) here,
154
+ # relative to this directory. They are copied after the builtin static files,
155
+ # so a file named "default.css" will overwrite the builtin "default.css".
156
+ # html_static_path = ["_static"]
157
+
158
+ # If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
159
+ # using the given strftime format.
160
+ # html_last_updated_fmt = '%b %d, %Y'
161
+
162
+ # If true, SmartyPants will be used to convert quotes and dashes to
163
+ # typographically correct entities.
164
+ # html_use_smartypants = True
165
+
166
+ # Custom sidebar templates, maps document names to template names.
167
+ # html_sidebars = {}
168
+
169
+ # Additional templates that should be rendered to pages, maps page names to
170
+ # template names.
171
+ # html_additional_pages = {}
172
+
173
+ # If false, no module index is generated.
174
+ # html_domain_indices = True
175
+
176
+ # If false, no index is generated.
177
+ # html_use_index = True
178
+
179
+ # If true, the index is split into individual pages for each letter.
180
+ # html_split_index = False
181
+
182
+ # If true, links to the reST sources are added to the pages.
183
+ # html_show_sourcelink = True
184
+
185
+ # If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
186
+ # html_show_sphinx = True
187
+
188
+ # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
189
+ # html_show_copyright = True
190
+
191
+ # If true, an OpenSearch description file will be output, and all pages will
192
+ # contain a <link> tag referring to it. The value of this option must be the
193
+ # base URL from which the finished HTML is served.
194
+ # html_use_opensearch = ''
195
+
196
+ # This is the file name suffix for HTML files (e.g. ".xhtml").
197
+ # html_file_suffix = None
198
+
199
+ # Output file base name for HTML help builder.
200
+ htmlhelp_basename = "pymriodoc"
201
+
202
+
203
+ # -- Options for LaTeX output ------------------------------
204
+
205
+ latex_elements = {
206
+ # The paper size ('letterpaper' or 'a4paper').
207
+ "papersize": "a4paper",
208
+ # The font size ('10pt', '11pt' or '12pt').
209
+ "pointsize": "11pt",
210
+ # Additional stuff for the LaTeX preamble.
211
+ # 'preamble': '',
212
+ }
213
+
214
+ # Grouping the document tree into LaTeX files. List of tuples (source start
215
+ # file, target name, title, author, documentclass [howto/manual]).
216
+ latex_documents = [
217
+ ("index", "pymrio.tex", "pymrio Documentation", "Konstantin Stadler", "manual"),
218
+ ]
219
+
220
+ # The name of an image file (relative to this directory) to place at the top of
221
+ # the title page.
222
+ # latex_logo = None
223
+
224
+ # For "manual" documents, if this is true, then toplevel headings are parts,
225
+ # not chapters.
226
+ # latex_use_parts = False
227
+
228
+ # If true, show page references after internal links.
229
+ # latex_show_pagerefs = False
230
+
231
+ # If true, show URL addresses after external links.
232
+ # latex_show_urls = False
233
+
234
+ # Documents to append as an appendix to all manuals.
235
+ # latex_appendices = []
236
+
237
+ # If false, no module index is generated.
238
+ # latex_domain_indices = True
239
+
240
+
241
+ # -- Options for manual page output --------------------------------
242
+
243
+ # One entry per manual page. List of tuples
244
+ # (source start file, name, description, authors, manual section).
245
+ man_pages = [("index", "pymrio", "pymrio Documentation", ["Konstantin Stadler"], 1)]
246
+
247
+ # If true, show URL addresses after external links.
248
+ # man_show_urls = False
249
+
250
+
251
+ # -- Options for Texinfo output --------------------------------------
252
+
253
+ # Grouping the document tree into Texinfo files. List of tuples
254
+ # (source start file, target name, title, author,
255
+ # dir menu entry, description, category)
256
+ texinfo_documents = [
257
+ (
258
+ "index",
259
+ "pymrio",
260
+ "pymrio Documentation",
261
+ "Konstantin Stadler",
262
+ "pymrio",
263
+ "One line description of project.",
264
+ "Miscellaneous",
265
+ ),
266
+ ]
267
+
268
+ # Documents to append as an appendix to all manuals.
269
+ # texinfo_appendices = []
270
+
271
+ # If false, no module index is generated.
272
+ # texinfo_domain_indices = True
273
+
274
+ # How to display URL addresses: 'footnote', 'no', or 'inline'.
275
+ # texinfo_show_urls = 'footnote'
@@ -0,0 +1,177 @@
1
+ # ---
2
+ # jupyter:
3
+ # jupytext:
4
+ # formats: ipynb,py:light
5
+ # text_representation:
6
+ # extension: .py
7
+ # format_name: light
8
+ # format_version: '1.5'
9
+ # jupytext_version: 1.16.4
10
+ # kernelspec:
11
+ # display_name: Python 3 (ipykernel)
12
+ # language: python
13
+ # name: python3
14
+ # ---
15
+
16
+ # # Adjusting, Renaming and Restructuring
17
+
18
+ # In this tutorial we will cover how to modify the structure of an MRIO system.
19
+ # This includes renaming sectors, regions and Y categories,
20
+ # as well as restructuring (extracting and merging) satellite accounts.
21
+
22
+
23
+ # We use the small test MRIO system that is included in the pymrio package for all examples.
24
+ # The method work the same for any MRIO system.
25
+
26
+ import pymrio
27
+
28
+ mrio = pymrio.load_test()
29
+
30
+ # We can use several functions to get a quick overview over the MRIO system [(see the Exploring MRIO tutorial for more examples)](explore.ipynb).
31
+
32
+ mrio.get_sectors()
33
+
34
+ mrio.get_regions()
35
+
36
+ mrio.get_Y_categories()
37
+
38
+ # A list of available satellite accounts can be obtained by
39
+
40
+ mrio.get_extensions()
41
+
42
+ # 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:
43
+
44
+ list(mrio.get_extensions())
45
+
46
+ # ## Renaming
47
+
48
+ # ### Manual renaming
49
+
50
+ # All the names returned are literally just names and can easily be renamed.
51
+ # To rename sectors or regions just pass a dictionary with the new names:
52
+
53
+ mrio.rename_regions({"reg1": "REGION A", "reg2": "REGION B"})
54
+
55
+ mrio.get_regions()
56
+
57
+ # Renaming sectors or Y categories works the same way
58
+
59
+ mrio.rename_sectors({"mining": "dwarf business"})
60
+ mrio.rename_Y_categories({"Final consumption expenditure by households": "fin_house"})
61
+
62
+ mrio.get_sectors()
63
+
64
+ mrio.get_Y_categories()
65
+
66
+ # ### Renaming based on included classifications synonyms
67
+
68
+ # Some MRIOs come with a selections of names which can be used for renaming.
69
+ # These can be obtained via 'get_classification', passing the name of the IO (pass None to get a list of available classifications).
70
+
71
+ mrio_class = pymrio.get_classification(mrio_name="test")
72
+
73
+ # 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.
74
+
75
+ # Alternatively it is possible to just print the underlying dataframes with (similar for finaldemand)
76
+
77
+ mrio_class.sectors
78
+
79
+ # This can be used to generate dictionaries for renaming the sectors of the MRIO, eg with:
80
+
81
+ conv_dict = mrio_class.get_sector_dict(
82
+ mrio_class.sectors.TestMrioName, mrio_class.sectors.TestMrioCode
83
+ )
84
+ conv_dict
85
+
86
+ # This can then be used for renaming the sectors:
87
+
88
+ mrio = pymrio.load_test()
89
+
90
+ mrio.rename_sectors(conv_dict)
91
+ mrio.get_sectors()
92
+
93
+
94
+ # In the mrio_class.sectors you will also find an entry 'Type' which
95
+ # represents a many to one mapping.
96
+ # This can be used for aggregating the
97
+ # MRIO and is explained [in the aggregation tutorial](aggregation_examples.ipynb#Aggregation-by-renaming).
98
+
99
+ # # Extracting satellite accounts
100
+
101
+ # Each extension/satellite account of the MRIO contains a method 'extract'.
102
+ # This takes an index and extracts the corresponding satellite account entries.
103
+ # The index can either be obtained manually, or one can use the contains/match/fullmatch methods of the extension to get the index.
104
+
105
+ # Below we extract all extensions rows from the emission satallite account which contain the word 'air'.
106
+
107
+ air_emission_index = mrio.emissions.contains("air")
108
+ air_accounts_dict = mrio.emissions.extract(index=air_emission_index)
109
+
110
+ # The result is a dictionary with the extracted satellite accounts. The keys are the names of the satellite accounts and the values are the extracted satellite accounts.
111
+
112
+ air_accounts_dict.keys()
113
+
114
+ # Alternatively, one can automatically convert the extracted satellite accounts to a new MRIO extension.
115
+
116
+ air_accounts_extension = mrio.emissions.extract(
117
+ index=air_emission_index, return_type="extension"
118
+ )
119
+ print(air_accounts_extension)
120
+
121
+ # The extract method can also be applied over all extensions.
122
+ # This work well in combinaton with the contains/match/fullmatch methods of the extension.
123
+
124
+ # Satellite accounts can be extracted from the MRIO system
125
+ # by using the extension_extract method.
126
+ # This method takes the name of the satellite account as first argument
127
+ # and a list of regions as second argument.
128
+ # The regions can be given as a list of region names or as a list of region indices.
129
+ # The method returns a new MRIO system with the extracted satellite account.
130
+
131
+ # In the somehow derived example below we extract entries accross
132
+ # all accounts that contain the word 'Added' or the letter 2.
133
+
134
+ cross_accounts_index = mrio.extension_contains("Added|2")
135
+
136
+ # We can also extract all data from the satellite accounts with these indexes.
137
+
138
+ cross_accounts_dict = mrio.extension_extract(cross_accounts_index)
139
+
140
+ # This returns a dictionary of dictionaries with the extracted satellite accounts.
141
+
142
+ cross_accounts_dict.keys()
143
+
144
+ cross_accounts_dict["Emissions"].keys()
145
+
146
+ # Again, we can also build a new MRIO extension from the extracted satellite accounts.
147
+
148
+ # This can be either one per extension (by passing return_type="extension")
149
+
150
+ cross_accounts_extension = mrio.extension_extract(
151
+ cross_accounts_index, return_type="extension"
152
+ )
153
+
154
+ # Or by passing any other string to return_type, which will be used as the new name of the extension.
155
+
156
+ cross_accounts_all_new = mrio.extension_extract(
157
+ cross_accounts_index, return_type="new_name"
158
+ )
159
+
160
+ print(cross_accounts_all_new)
161
+
162
+ # Index values levels and names of the new extension are based on the combination
163
+ # of the original extensions index levels.
164
+ # These are filled for nan for entries which did not have them before.
165
+
166
+ cross_accounts_all_new.get_rows()
167
+
168
+ # Both extract methods, extension_extract and extract, can also be used to extract only
169
+ # a subset of the available dataframe accounts. This can be done by passing a list of
170
+ # dataframe names to the dataframes parameter.
171
+
172
+ only_fys = mrio.extension_extract(
173
+ cross_accounts_index, dataframes=["F_Y"], include_empty=True
174
+ )
175
+
176
+ only_fys.keys()
177
+ only_fys["Emissions"].keys()
@@ -0,0 +1,139 @@
1
+ # ---
2
+ # jupyter:
3
+ # jupytext:
4
+ # text_representation:
5
+ # extension: .py
6
+ # format_name: percent
7
+ # format_version: '1.3'
8
+ # jupytext_version: 1.16.4
9
+ # kernelspec:
10
+ # display_name: Python 3 (ipykernel)
11
+ # language: python
12
+ # name: python3
13
+ # ---
14
+
15
+ # %% [markdown]
16
+ # # Advanced functionality - pandas groupby with pymrio satellite accounts
17
+
18
+ # %% [markdown]
19
+ # This notebook examplifies how to directly apply [Pandas](https://pandas.pydata.org/) core functions (in this case [groupby and aggregation](https://pandas.pydata.org/pandas-docs/stable/groupby.html)) to the pymrio system.
20
+
21
+ # %% [markdown]
22
+ # ## WIOD material extension aggregation - stressor w/o compartment info
23
+
24
+ # %% [markdown]
25
+ # Here we use the WIOD MRIO system (see the notebook ["Automatic downloading of MRIO databases"](autodownload.ipynb#WIOD-download) for how to automatically retrieve this database) and will aggregate the WIOD material stressor for used and unused materials. We assume, that the WIOD system is available at
26
+
27
+ # %%
28
+ wiod_folder = "/tmp/mrios/WIOD2013"
29
+
30
+ # %% [markdown]
31
+ # To get started we import pymrio
32
+
33
+ # %%
34
+ import pymrio
35
+
36
+ # %% [markdown]
37
+ # For the example here, we use the data from 2009:
38
+
39
+ # %%
40
+ wiod09 = pymrio.parse_wiod(path=wiod_folder, year=2009)
41
+
42
+ # %% [markdown]
43
+ # WIOD includes multiple material accounts, specified for the "Used" and "Unused" category, as well as information on the total. We will use the latter to confirm our calculations:
44
+
45
+ # %%
46
+ wiod09.mat.F
47
+
48
+ # %% [markdown]
49
+ # To aggregate these with the Pandas groupby function, we need to specify the groups which should be grouped by Pandas.
50
+ # Pymrio contains a helper function which builds such a matching dictionary.
51
+ # The matching can also include regular expressions to simplify the build:
52
+
53
+ # %%
54
+ groups = wiod09.mat.get_index(
55
+ as_dict=True,
56
+ grouping_pattern={".*_Used": "Material Used", ".*_Unused": "Material Unused"},
57
+ )
58
+ groups
59
+
60
+ # %% [markdown]
61
+ # Note, that the grouping contains the rows which do not match any of the specified groups.
62
+ # This allows to easily aggregates only parts of a specific stressor set. To actually omit these groups
63
+ # include them in the matching pattern and provide None as value.
64
+ #
65
+ # To have the aggregated data alongside the original data, we first copy the detailed satellite account:
66
+
67
+ # %%
68
+ wiod09.mat_agg = wiod09.mat.copy(new_name="Aggregated matrial accounts")
69
+
70
+ # %% [markdown]
71
+ # Then, we use the pymrio get_DataFrame iterator together with the pandas groupby and sum functions to aggregate the stressors.
72
+ # For the dataframe containing the unit information, we pass a custom function which concatenate non-unique unit strings.
73
+
74
+ # %%
75
+ for df_name, df in zip(
76
+ wiod09.mat_agg.get_DataFrame(data=False, with_unit=True, with_population=False),
77
+ wiod09.mat_agg.get_DataFrame(data=True, with_unit=True, with_population=False),
78
+ ):
79
+ if df_name == "unit":
80
+ wiod09.mat_agg.__dict__[df_name] = df.groupby(groups).apply(
81
+ lambda x: " & ".join(x.unit.unique())
82
+ )
83
+ else:
84
+ wiod09.mat_agg.__dict__[df_name] = df.groupby(groups).sum()
85
+
86
+ # %%
87
+ wiod09.mat_agg.F
88
+
89
+ # %%
90
+ wiod09.mat_agg.unit
91
+
92
+ # %% [markdown]
93
+ # ## Use with stressors including compartment information:
94
+
95
+ # %% [markdown]
96
+ # The same regular expression grouping can be used to aggregate stressor data which is given per compartment.
97
+ # To do so, the matching dict needs to consist of tuples corresponding to a valid index value in the DataFrames.
98
+ # Each position in the tuple is interprested as a regular expression.
99
+ # Using the get_index method gives a good indication how a valid grouping dict should look like:
100
+
101
+ # %%
102
+ tt = pymrio.load_test()
103
+ tt.emissions.get_index(as_dict=True)
104
+
105
+ # %% [markdown]
106
+ # With that information, we can now build our own grouping dict, e.g.:
107
+
108
+ # %%
109
+ agg_groups = {("emis.*", ".*"): "all emissions"}
110
+
111
+ # %%
112
+ group_dict = tt.emissions.get_index(as_dict=True, grouping_pattern=agg_groups)
113
+ group_dict
114
+
115
+ # %% [markdown]
116
+ # Which can then be used to aggregate the satellite account:
117
+
118
+ # %%
119
+ for df_name, df in zip(
120
+ tt.emissions.get_DataFrame(data=False, with_unit=True, with_population=False),
121
+ tt.emissions.get_DataFrame(data=True, with_unit=True, with_population=False),
122
+ ):
123
+ if df_name == "unit":
124
+ tt.emissions.__dict__[df_name] = df.groupby(group_dict).apply(
125
+ lambda x: " & ".join(x.unit.unique())
126
+ )
127
+ else:
128
+ tt.emissions.__dict__[df_name] = df.groupby(group_dict).sum()
129
+
130
+ # %% [markdown]
131
+ # In this case we loose the information on the compartment. To reset the index do:
132
+
133
+ # %%
134
+ import pandas as pd
135
+
136
+ tt.emissions.set_index(pd.Index(tt.emissions.get_index(), name="stressor"))
137
+
138
+ # %%
139
+ tt.emissions.F