Skip to content

Commit

Permalink
Fix ReturnDataView AttributeError: messages (#2341)
Browse files Browse the repository at this point in the history
* Make log messages from ReturnData available through ReturnDataView.
* Add `LogItem.__repr__`

Closes #2331.
  • Loading branch information
dweindl authored Mar 4, 2024
1 parent 0f961d6 commit 1ab1d55
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
9 changes: 7 additions & 2 deletions python/sdist/amici/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import itertools
from typing import Literal, Union
from collections.abc import Iterator

from numbers import Number
import amici
import numpy as np
import sympy as sp
Expand Down Expand Up @@ -238,6 +238,7 @@ class ReturnDataView(SwigPtrView):
"numnonlinsolvconvfailsB",
"cpu_timeB",
"cpu_time_total",
"messages",
]

def __init__(self, rdata: Union[ReturnDataPtr, ReturnData]):
Expand Down Expand Up @@ -440,7 +441,11 @@ def _field_as_numpy(
attr = getattr(data, field)
if field_dim := field_dimensions.get(field, None):
return None if len(attr) == 0 else np.array(attr).reshape(field_dim)
return float(attr)

if isinstance(attr, Number):
return float(attr)

return attr


def _entity_type_from_id(
Expand Down
2 changes: 1 addition & 1 deletion python/tests/test_sbml_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def test_solver_reuse(model_steadystate_module):
assert rdata1.status == amici.AMICI_SUCCESS

for attr in rdata1:
if "time" in attr:
if "time" in attr or attr == "messages":
continue

val1 = getattr(rdata1, attr)
Expand Down
8 changes: 8 additions & 0 deletions swig/amici.i
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ def __repr__(self):
%}
};

%extend amici::LogItem {
%pythoncode %{
def __repr__(self):
return (f"{self.__class__.__name__}(severity={self.severity}, "
f"identifier={self.identifier!r}, message={self.message!r})")
%}
};


// Convert integer values to enum class
// defeats the purpose of enum class, but didn't find a better way to allow for
Expand Down

0 comments on commit 1ab1d55

Please sign in to comment.