mrio-toolbox 1.0.0__py3-none-any.whl → 1.1.2__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.

Files changed (67) hide show
  1. __init__.py +21 -0
  2. {mrio_toolbox/_parts → _parts}/_Axe.py +95 -37
  3. {mrio_toolbox/_parts → _parts}/_Part.py +346 -111
  4. _parts/__init__.py +7 -0
  5. {mrio_toolbox/_parts → _parts}/part_operations.py +24 -17
  6. extractors/__init__.py +20 -0
  7. extractors/downloaders.py +36 -0
  8. extractors/emerging/__init__.py +3 -0
  9. extractors/emerging/emerging_extractor.py +117 -0
  10. extractors/eora/__init__.py +3 -0
  11. extractors/eora/eora_extractor.py +132 -0
  12. extractors/exiobase/__init__.py +3 -0
  13. extractors/exiobase/exiobase_extractor.py +270 -0
  14. extractors/extractors.py +81 -0
  15. extractors/figaro/__init__.py +3 -0
  16. extractors/figaro/figaro_downloader.py +280 -0
  17. extractors/figaro/figaro_extractor.py +187 -0
  18. extractors/gloria/__init__.py +3 -0
  19. extractors/gloria/gloria_extractor.py +202 -0
  20. extractors/gtap11/__init__.py +7 -0
  21. extractors/gtap11/extraction/__init__.py +3 -0
  22. extractors/gtap11/extraction/extractor.py +129 -0
  23. extractors/gtap11/extraction/harpy_files/__init__.py +6 -0
  24. extractors/gtap11/extraction/harpy_files/_header_sets.py +279 -0
  25. extractors/gtap11/extraction/harpy_files/har_file.py +262 -0
  26. extractors/gtap11/extraction/harpy_files/har_file_io.py +974 -0
  27. extractors/gtap11/extraction/harpy_files/header_array.py +300 -0
  28. extractors/gtap11/extraction/harpy_files/sl4.py +229 -0
  29. extractors/gtap11/gtap_mrio/__init__.py +6 -0
  30. extractors/gtap11/gtap_mrio/mrio_builder.py +158 -0
  31. extractors/icio/__init__.py +3 -0
  32. extractors/icio/icio_extractor.py +121 -0
  33. extractors/wiod/__init__.py +3 -0
  34. extractors/wiod/wiod_extractor.py +143 -0
  35. mrio_toolbox/mrio.py → mrio.py +254 -94
  36. {mrio_toolbox-1.0.0.dist-info → mrio_toolbox-1.1.2.dist-info}/METADATA +11 -7
  37. mrio_toolbox-1.1.2.dist-info/RECORD +59 -0
  38. {mrio_toolbox-1.0.0.dist-info → mrio_toolbox-1.1.2.dist-info}/WHEEL +1 -1
  39. mrio_toolbox-1.1.2.dist-info/top_level.txt +6 -0
  40. msm/__init__.py +6 -0
  41. msm/multi_scale_mapping.py +863 -0
  42. utils/__init__.py +3 -0
  43. utils/converters/__init__.py +5 -0
  44. {mrio_toolbox/utils → utils}/converters/pandas.py +5 -6
  45. {mrio_toolbox/utils → utils}/converters/xarray.py +6 -15
  46. utils/formatting/formatter.py +527 -0
  47. utils/loaders/__init__.py +7 -0
  48. {mrio_toolbox/utils → utils}/loaders/_loader.py +60 -4
  49. {mrio_toolbox/utils → utils}/loaders/_loader_factory.py +22 -1
  50. {mrio_toolbox/utils → utils}/loaders/_nc_loader.py +37 -1
  51. {mrio_toolbox/utils → utils}/loaders/_pandas_loader.py +29 -3
  52. {mrio_toolbox/utils → utils}/loaders/_parameter_loader.py +61 -16
  53. {mrio_toolbox/utils → utils}/savers/__init__.py +3 -0
  54. utils/savers/_path_checker.py +37 -0
  55. {mrio_toolbox/utils → utils}/savers/_to_folder.py +6 -1
  56. utils/savers/_to_nc.py +60 -0
  57. mrio_toolbox/__init__.py +0 -5
  58. mrio_toolbox/_parts/__init__.py +0 -3
  59. mrio_toolbox/utils/converters/__init__.py +0 -2
  60. mrio_toolbox/utils/loaders/__init__.py +0 -3
  61. mrio_toolbox/utils/savers/_path_checker.py +0 -19
  62. mrio_toolbox/utils/savers/_to_nc.py +0 -52
  63. mrio_toolbox-1.0.0.dist-info/RECORD +0 -26
  64. mrio_toolbox-1.0.0.dist-info/top_level.txt +0 -1
  65. {mrio_toolbox-1.0.0.dist-info → mrio_toolbox-1.1.2.dist-info/licenses}/LICENSE +0 -0
  66. {mrio_toolbox/utils → utils/formatting}/__init__.py +0 -0
  67. {mrio_toolbox/utils → utils}/loaders/_np_loader.py +0 -0
@@ -0,0 +1,262 @@
1
+ """
2
+ .. autoclass: HarFileObj
3
+ :members:
4
+
5
+ Created on Mar 12 09:53:27 2018
6
+ """
7
+
8
+ from .har_file_io import HarFileIO, HarFileInfoObj
9
+ from .header_array import HeaderArrayObj
10
+ from collections import OrderedDict
11
+ from typing import TypeVar, List, Union
12
+ from os import path
13
+ import warnings
14
+ TypeHarFileObj = TypeVar('TypeHarFileObj', bound='HarFileObj')
15
+
16
+ class HarFileObj(object):
17
+ """
18
+ HAR file object - essentially a memory representation of a HAR file.
19
+
20
+ ``HarFileObj`` stores a `list` of ``harpy.HeaderArrayObj`` in ``self``.
21
+ Each ``harpy.HeaderArrayObj`` corresponds to a header-array.
22
+ If ``HarFileObj`` is provided with ``filename``, then the header-arrays in that file will be loaded - i.e., each ``harpy.HeaderArrayObj`` in ``self`` will correspond to a header-array in that file.
23
+
24
+ Access to the ``HarFileObj`` is provided in a dict-like style: `__getitem__`, `__setitem__`, `__delitem__`, and `__contains__` are implemented. Each can take list arguments and returns results as a list.
25
+ Note that all methods are case insensitive with respect to header names.
26
+
27
+ Example: given file ex.har with Headers HD1 and HD2::
28
+
29
+ >>> from harpy import HarFileObj
30
+ >>> thisHar = HarFileObj("ex.har")
31
+ >>> headersOnFile = thisHar.getHeaderArrayNames() # ["HD1","HD2"]
32
+ >>> hd1Head = thisHar["HD1"] # obtain the HeaderArrayObj for HD1
33
+ >>> del thisHar["HD1"] # remove HD1 from HarFile
34
+ >>> print("HD1" in thisHar)
35
+ False
36
+ >>> thisHar.writeToDisk() # overwrites ex.har, now only contains HD2
37
+
38
+ Attributes
39
+ ----------
40
+ head_arrs : OrderedDict
41
+ Returned/provided as a `list` of ``HeaderArrayObj`` defining all ``HeaderArrayObj`` associated with a file.
42
+
43
+ _hfi : HarFileInfoObj
44
+ Basic info of the HAR file content. This is used in conjunction with `head_arrs` to permit on-the-fly reading of ``HeaderArrayObj`` and thus reduce the memory footprint.
45
+ """
46
+
47
+
48
+ def __init__(self, filename: str=None):
49
+ self._head_arrs = OrderedDict()
50
+ self.filename=filename
51
+ if isinstance(filename, str):
52
+ if path.isfile(filename):
53
+ self._hfi = HarFileIO.readHarFileInfo(filename)
54
+ else:
55
+ self._hfi = HarFileInfoObj(file=filename)
56
+
57
+ def __getitem__(self, item : 'Union[str, List[str]]' ):
58
+ if isinstance(item,str):
59
+ return self._getHeaderArrayObj(item)
60
+ elif isinstance(item,list):
61
+ if not all([isinstance(myitem,str) for myitem in item]):
62
+ raise TypeError("All items in item must be of type 'str'")
63
+ return self._getHeaderArrayObjs(item)
64
+ else:
65
+ raise TypeError("item must be string or list of strings")
66
+
67
+
68
+ def __setitem__(self, key: 'Union[str, List[str]]', value: 'Union[HeaderArrayObj, List[HeaderArrayObj]]'):
69
+ if isinstance(key, str) and isinstance(value,HeaderArrayObj):
70
+ self._addHeaderArrayObj(key, value)
71
+ elif isinstance(key, list) and isinstance(value,list):
72
+ if not all([isinstance(mykey,str) for mykey in key]):
73
+ raise TypeError("All items in key must be of type 'str'")
74
+ if not all([isinstance(myval,HeaderArrayObj) for myval in value]):
75
+ raise TypeError("All items in value must be of type 'HeaderArrayObj'")
76
+ self._addHeaderArrayObjs(key, value)
77
+ else:
78
+ raise TypeError("Only combination str-HeaderArrayObj or list(str)-list(HeaderArrayObj) permitted in __getitem__'")
79
+
80
+ return None
81
+
82
+ def __delitem__(self, key):
83
+ if isinstance(key,str):
84
+ if key.strip().upper() in self._head_arrs:
85
+ del self._head_arrs[key.strip().upper()]
86
+ elif isinstance(key,list):
87
+ for mykey in key:
88
+ if mykey in self._head_arrs:
89
+ del self[mykey]
90
+ else:
91
+ raise TypeError("key must be string or list of strings")
92
+
93
+ return None
94
+
95
+ def __contains__(self, key):
96
+ if isinstance(key,str):
97
+ return key.strip().upper() in self._head_arrs
98
+ return False
99
+
100
+
101
+
102
+
103
+ def getHeaderArrayNames(self):
104
+ """
105
+ :return: Returns the name of all ``harpy.HeaderArrayObj()`` stored with ``self``.
106
+ """
107
+
108
+ if not self._hfi.is_valid(fatal=False):
109
+ warnings.warn("Har file "+self._hfi.filename+" has changed since last access, rereading information")
110
+ self._hfi=HarFileObj(self._hfi.file)._hfi
111
+ self._head_arrs=OrderedDict()
112
+
113
+ return self._hfi.getHeaderArrayNames()
114
+
115
+ def getRealHeaderArrayNames(self):
116
+ """
117
+ :return: Returns only the names of arrays of type 2D or 7D - i.e. multi-dimensional header arrays of floating point numbers.
118
+ """
119
+
120
+ if not self._hfi.is_valid():
121
+ warnings.warn("Har file "+self._hfi.filename+" has changed since last access, rereading information")
122
+ self._hfi = HarFileObj(self._hfi.file)._hfi
123
+ self._head_arrs = OrderedDict()
124
+ return [key for key,val in self._hfi.items() if val.data_type in ["RE","RL","2R"]]
125
+
126
+
127
+ def _getHeaderArrayObj(self, ha_name: str):
128
+ """
129
+ Retrieve a single ``harpy.HeaderArrayObj``.
130
+
131
+ :param ha_name: The ``"name"`` of the ``harpy.HeaderArrayObj``.
132
+ :return: A ``harpy.HeaderArrayObj``.
133
+ """
134
+
135
+ if not self._hfi.is_valid(fatal=False):
136
+ warnings.warn("Har file "+self._hfi.filename+" has changed since last access, rereading information")
137
+ self._hfi = HarFileObj(self._hfi.file)._hfi
138
+ self._head_arrs = OrderedDict()
139
+
140
+ if not isinstance(ha_name, str):
141
+ raise TypeError("'ha_name' must be a string.")
142
+
143
+ upname=ha_name.strip().upper()
144
+ if not upname in self._hfi:
145
+ raise KeyError("HeaderArrayObj '%s' does not exist in HarFileObj." % ha_name)
146
+ if not upname in self._head_arrs:
147
+ hnames, haos= HarFileIO.readHeaderArraysFromFile(self._hfi, ha_names=upname)
148
+ self._head_arrs[upname]=haos[0]
149
+
150
+ return self._head_arrs[upname]
151
+
152
+ def _getHeaderArrayObjs(self, ha_names=None):
153
+ """
154
+ Retrieve a `list` of `harpy.HeaderArrayObj`.
155
+
156
+ :param 'Union[None,str,List[str]]' ha_names: The name or `list` of names of ``harpy.HeaderArrayObj``. If `None` is provided (the default) then all ``harpy.HeaderArrayObj`` are returned.
157
+ :return: `list` of ``harpy.HeaderArrayObj``.
158
+ """
159
+
160
+ if isinstance(ha_names, str):
161
+ ha_names = [ha_names]
162
+ elif ha_names is None:
163
+ ha_names = self.getHeaderArrayNames()
164
+
165
+ ha_objs = []
166
+ for ha_name in ha_names:
167
+ ha_objs.append(self._getHeaderArrayObj(ha_name))
168
+ return ha_objs
169
+
170
+ def _readHeaderArrayObjs(self, ha_names = None):
171
+ """
172
+ Reads the header array objects with names ``ha_names`` from ``filename``. If `None` (the default), read all header array objects. `harpy.HeaderArrayObj` are stored in ``self`` and can be retrieved with the ``self.getHeaderArrayObjs()`` method.
173
+
174
+ :param 'Union[None,str,List[str]]' ha_names:
175
+ """
176
+ hnames, haos = HarFileIO.readHeaderArraysFromFile(self._hfi, ha_names=ha_names)
177
+ self._head_arrs=OrderedDict(zip(hnames, haos))
178
+
179
+
180
+ def writeToDisk(self, filename: str=None, ha_names=None):
181
+ """
182
+ :param str filename: Writes `harpy.HeaderArrayObj` with ``ha_names`` to ``filename``. If ``ha_names`` is None, write all the `harpy.HeaderArrayObj` stored in ``self``.
183
+ :param 'Union[None,str,List[str]]' ha_names: The names of the header arrays to write to ``filename``.
184
+ """
185
+ if filename is None and self.filename is None:
186
+ raise ValueError("No filename specified in write or upon creation, use writeToDisk(filename=YOURFILENAME)")
187
+ if filename is None:
188
+ filename=self.filename
189
+ if ha_names is None:
190
+ ha_names = self.getHeaderArrayNames()
191
+ elif isinstance(ha_names, str):
192
+ ha_names = [ha_names]
193
+
194
+ ha_to_write = self._getHeaderArrayObjs(ha_names)
195
+
196
+ HarFileIO.writeHeaders(filename, ha_names, ha_to_write)
197
+ self._hfi.updateMtime()
198
+
199
+ def _removeHeaderArrayObjs(self, ha_names):
200
+ """
201
+ TODO: its more of a pop, not a remove, maybe rename
202
+ :param 'Union[str,List[str]]' ha_names: Remove one or more `harpy.HeaderArrayObj` from ``self``.
203
+ """
204
+
205
+ if isinstance(ha_names, str):
206
+ ha_names = [ha_names]
207
+
208
+ outlist=self._getHeaderArrayObjs(ha_names)
209
+
210
+ for ha_name in ha_names:
211
+ if ha_name.strip().upper() in self._hfi:
212
+ del self._hfi._ha_infos[ha_name.strip().upper()]
213
+ if ha_name.strip().upper() in self._head_arrs:
214
+ del self._head_arrs[ha_name.strip().upper()]
215
+ return outlist
216
+
217
+ def _addHeaderArrayObjs(self, hnames, ha_objs) -> None:
218
+ """
219
+ :param 'Union[HeaderArrayObj,List[HeaderArrayObj]]' ha_objs: Add one or more `harpy.HeaderArrayObj` to ``self``.
220
+ """
221
+
222
+ if isinstance(ha_objs, HeaderArrayObj):
223
+ ha_objs = [ha_objs]
224
+ if isinstance(hnames, str):
225
+ hnames = [hnames]
226
+
227
+ for hname, ha_obj in zip(hnames,ha_objs):
228
+ if ha_obj.is_valid():
229
+ self._addHeaderArrayObj(hname, ha_obj)
230
+
231
+ return None
232
+
233
+ def _addHeaderArrayObj(self, hname : str, ha_obj: HeaderArrayObj):
234
+ """
235
+ :param ha_obj: A `harpy.HeaderArrayObj` object.
236
+ """
237
+
238
+ if len(hname.strip()) > 4:
239
+ raise HarFileObj.InvalidHeaderArrayName("Name of Header too long")
240
+
241
+ self._hfi.addHAInfo(hname.strip().upper(),0,0)
242
+ self._head_arrs[hname.strip().upper()]= ha_obj
243
+
244
+
245
+ @staticmethod
246
+ def _loadFromDisk(filename: str, ha_names: list = None) -> TypeHarFileObj:
247
+ """Loads a HAR file into memory, returning a HarFileObj.
248
+
249
+ :param filename: The name of the file to load.
250
+ :param ha_names: If provided, only reads headers with the names matching the strings contained in this list. By default, all header arrays are read.
251
+ :return "HarFileObj": Returns ``HarFileObj`` with
252
+ """
253
+
254
+ hfo = HarFileObj(filename=filename)
255
+ hfo._readHeaderArrayObjs(ha_names=ha_names)
256
+
257
+ return hfo
258
+
259
+
260
+ class InvalidHeaderArrayName(ValueError):
261
+ """Raised if header array name is not exactly four (alphanumeric) characters long."""
262
+ pass