14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program. If not, see <https://www.gnu.org/licenses/>.
16
16
17
+ import copy
17
18
import json
18
19
import tempfile
19
20
import textwrap
20
21
from collections .abc import Iterable
21
22
22
- import numpy
23
+ import numpy as np
23
24
import pymrio
24
25
25
26
26
- class TempMemmap (numpy .memmap ):
27
+ def _fast_sum (array : np .ndarray , axis : int ) -> np .ndarray :
28
+ """
29
+ Perform a fast summation over the specified axis of the input array using einsum.
30
+
31
+ Parameters:
32
+ array (np.ndarray): Input array.
33
+ axis (int): Axis along which to perform the summation.
34
+
35
+ Returns:
36
+ np.ndarray: Summed array along the specified axis.
37
+
38
+ Raises:
39
+ ValueError: If the specified axis is out of bounds for the input array.
40
+
41
+ Example:
42
+ array = np.array([[1, 2, 3], [4, 5, 6]])
43
+ result = _fast_sum(array, 0) # Sum along the first axis
44
+ print(result) # Output: [5 7 9]
45
+ """
46
+ if axis == 0 :
47
+ if array .ndim == 1 :
48
+ return np .einsum ("i->" , array )
49
+ if array .ndim == 2 :
50
+ return np .einsum ("ij->j" , array )
51
+ if array .ndim == 3 :
52
+ return np .einsum ("ijk->jk" , array )
53
+ else :
54
+ raise NotImplementedError ("Too many dimensions." )
55
+ elif axis == 1 :
56
+ if array .ndim == 2 :
57
+ return np .einsum ("ij->i" , array )
58
+ if array .ndim == 3 :
59
+ return np .einsum ("ijk->ik" , array )
60
+ else :
61
+ raise NotImplementedError ("Too many dimensions." )
62
+ elif axis == 2 :
63
+ return np .einsum ("ijk->ij" , array )
64
+ else :
65
+ raise ValueError ("Axis out of bounds for input array." )
66
+
67
+
68
+ def _divide_arrays_ignore (a , b ):
69
+ with np .errstate (divide = "ignore" , invalid = "ignore" ):
70
+ ret = np .divide (a , b )
71
+ np .nan_to_num (ret )
72
+ return ret
73
+
74
+
75
+ class TempMemmap (np .memmap ):
27
76
def __new__ (
28
77
subtype ,
29
78
filename ,
30
- dtype = numpy .float64 ,
79
+ dtype = np .float64 ,
31
80
mode = "r+" ,
32
81
offset = 0 ,
33
82
shape = None ,
34
83
order = "C" ,
35
84
save = False ,
36
85
):
37
86
if save :
38
- self = numpy .memmap .__new__ (
87
+ self = np .memmap .__new__ (
39
88
subtype ,
40
89
filename = filename ,
41
90
dtype = dtype ,
@@ -47,7 +96,7 @@ def __new__(
47
96
return self
48
97
else :
49
98
ntf = tempfile .NamedTemporaryFile ()
50
- self = numpy .memmap .__new__ (
99
+ self = np .memmap .__new__ (
51
100
subtype ,
52
101
ntf ,
53
102
dtype = dtype ,
@@ -67,11 +116,11 @@ def __del__(self):
67
116
68
117
class CustomNumpyEncoder (json .JSONEncoder ):
69
118
def default (self , obj ):
70
- if isinstance (obj , numpy .integer ):
119
+ if isinstance (obj , np .integer ):
71
120
return int (obj )
72
- elif isinstance (obj , numpy .floating ):
121
+ elif isinstance (obj , np .floating ):
73
122
return float (obj )
74
- elif isinstance (obj , numpy .ndarray ):
123
+ elif isinstance (obj , np .ndarray ):
75
124
return obj .tolist ()
76
125
else :
77
126
return super (CustomNumpyEncoder , self ).default (obj )
@@ -85,7 +134,7 @@ def flatten(lst):
85
134
yield el
86
135
87
136
88
- def lexico_reindex (mrio : pymrio .IOSystem ) -> pymrio .IOSystem :
137
+ def lexico_reindex (mriot : pymrio .IOSystem ) -> pymrio .IOSystem :
89
138
"""Reindex IOSystem lexicographicaly
90
139
91
140
Sort indexes and columns of the dataframe of a ``pymrio.IOSystem`` by
@@ -103,23 +152,26 @@ def lexico_reindex(mrio: pymrio.IOSystem) -> pymrio.IOSystem:
103
152
104
153
105
154
"""
106
- if mrio .Z is None :
107
- raise ValueError ("Given mrio has no Z attribute set" )
108
- mrio .Z = mrio .Z .reindex (sorted (mrio .Z .index ), axis = 0 )
109
- mrio .Z = mrio .Z .reindex (sorted (mrio .Z .columns ), axis = 1 )
110
- if mrio .Y is None :
111
- raise ValueError ("Given mrio has no Y attribute set" )
112
- mrio .Y = mrio .Y .reindex (sorted (mrio .Y .index ), axis = 0 )
113
- mrio .Y = mrio .Y .reindex (sorted (mrio .Y .columns ), axis = 1 )
114
- if mrio .x is None :
115
- raise ValueError ("Given mrio has no x attribute set" )
116
- mrio .x = mrio .x .reindex (sorted (mrio .x .index ), axis = 0 )
117
- if mrio .A is None :
118
- raise ValueError ("Given mrio has no A attribute set" )
119
- mrio .A = mrio .A .reindex (sorted (mrio .A .index ), axis = 0 )
120
- mrio .A = mrio .A .reindex (sorted (mrio .A .columns ), axis = 1 )
121
-
122
- return mrio
155
+ mriot = copy .deepcopy (mriot )
156
+ if getattr (mriot ,"Z" ,None ) is None :
157
+ raise ValueError ("Given mriot has no Z attribute set" )
158
+ mriot .Z = mriot .Z .reindex (sorted (mriot .Z .index ), axis = 0 )
159
+ mriot .Z = mriot .Z .reindex (sorted (mriot .Z .columns ), axis = 1 )
160
+ if getattr (mriot ,"Y" ,None ) is None :
161
+ raise ValueError ("Given mriot has no Y attribute set" )
162
+ mriot .Y = mriot .Y .reindex (sorted (mriot .Y .index ), axis = 0 )
163
+ mriot .Y = mriot .Y .reindex (sorted (mriot .Y .columns ), axis = 1 )
164
+ if getattr (mriot ,"x" ,None ) is None :
165
+ raise ValueError ("Given mriot has no x attribute set" )
166
+ mriot .x = mriot .x .reindex (
167
+ sorted (mriot .x .index ), axis = 0
168
+ ) # ignore type (wrong type hinting in pymrio)
169
+ if getattr (mriot ,"A" ,None ) is None :
170
+ raise ValueError ("Given mriot has no A attribute set" )
171
+ mriot .A = mriot .A .reindex (sorted (mriot .A .index ), axis = 0 )
172
+ mriot .A = mriot .A .reindex (sorted (mriot .A .columns ), axis = 1 )
173
+
174
+ return mriot
123
175
124
176
125
177
def sizeof_fmt (num , suffix = "B" ):
0 commit comments