Skip to content

Commit 163a481

Browse files
authored
Merge branch 'feature/better-event-creation' into develop
2 parents 34e3250 + 2e90e58 commit 163a481

File tree

9 files changed

+3316
-2760
lines changed

9 files changed

+3316
-2760
lines changed

boario/event.py

Lines changed: 641 additions & 1020 deletions
Large diffs are not rendered by default.

boario/model_base.py

Lines changed: 590 additions & 995 deletions
Large diffs are not rendered by default.

boario/simulation.py

Lines changed: 940 additions & 207 deletions
Large diffs are not rendered by default.

boario/utils/misc.py

Lines changed: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,77 @@
1414
# You should have received a copy of the GNU General Public License
1515
# along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
import copy
1718
import json
1819
import tempfile
1920
import textwrap
2021
from collections.abc import Iterable
2122

22-
import numpy
23+
import numpy as np
2324
import pymrio
2425

2526

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):
2776
def __new__(
2877
subtype,
2978
filename,
30-
dtype=numpy.float64,
79+
dtype=np.float64,
3180
mode="r+",
3281
offset=0,
3382
shape=None,
3483
order="C",
3584
save=False,
3685
):
3786
if save:
38-
self = numpy.memmap.__new__(
87+
self = np.memmap.__new__(
3988
subtype,
4089
filename=filename,
4190
dtype=dtype,
@@ -47,7 +96,7 @@ def __new__(
4796
return self
4897
else:
4998
ntf = tempfile.NamedTemporaryFile()
50-
self = numpy.memmap.__new__(
99+
self = np.memmap.__new__(
51100
subtype,
52101
ntf,
53102
dtype=dtype,
@@ -67,11 +116,11 @@ def __del__(self):
67116

68117
class CustomNumpyEncoder(json.JSONEncoder):
69118
def default(self, obj):
70-
if isinstance(obj, numpy.integer):
119+
if isinstance(obj, np.integer):
71120
return int(obj)
72-
elif isinstance(obj, numpy.floating):
121+
elif isinstance(obj, np.floating):
73122
return float(obj)
74-
elif isinstance(obj, numpy.ndarray):
123+
elif isinstance(obj, np.ndarray):
75124
return obj.tolist()
76125
else:
77126
return super(CustomNumpyEncoder, self).default(obj)
@@ -85,7 +134,7 @@ def flatten(lst):
85134
yield el
86135

87136

88-
def lexico_reindex(mrio: pymrio.IOSystem) -> pymrio.IOSystem:
137+
def lexico_reindex(mriot: pymrio.IOSystem) -> pymrio.IOSystem:
89138
"""Reindex IOSystem lexicographicaly
90139
91140
Sort indexes and columns of the dataframe of a ``pymrio.IOSystem`` by
@@ -103,23 +152,26 @@ def lexico_reindex(mrio: pymrio.IOSystem) -> pymrio.IOSystem:
103152
104153
105154
"""
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
123175

124176

125177
def sizeof_fmt(num, suffix="B"):

poetry.lock

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pydoclint = "^0.4.1"
3939
autoapi = "^2.0.1"
4040
sphinx-autodoc-typehints = "^2.0.0"
4141
numpydoc = "^1.7.0"
42+
pandas-stubs = "^2.2.2.240514"
4243

4344
[build-system]
4445
requires = ["poetry-core"]
@@ -100,4 +101,7 @@ extend-ignore = ['E501','E203']
100101

101102

102103
defineConstant = { DEBUG = true }
103-
stubPath = "boario/stubs"
104+
stubPath = "boario/stubs"
105+
106+
[tool.pytest.ini_options]
107+
addopts = "--cov=boario --cov-report html"

0 commit comments

Comments
 (0)