Skip to content

Commit

Permalink
SwigPtrView: look up unhandled attributes in _swigptr
Browse files Browse the repository at this point in the history
So far, only the explicitly listed attributes are accessible directly via SwigPtrView.
Things like ReturnData.ny are only available through ReturnDataView.ptr.ny, which is inconvenient.
Let's just look all non-private attributes that are not already explicitly handled by SwigPtrView
on _swigptr and return them as is.
  • Loading branch information
dweindl committed Apr 19, 2024
1 parent 51c8cad commit 5600235
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
17 changes: 10 additions & 7 deletions python/sdist/amici/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@ def __getitem__(self, item: str) -> Union[np.ndarray, float]:
if item in self._cache:
return self._cache[item]

if item == "id":
return getattr(self._swigptr, item)
if item in self._field_names:
value = _field_as_numpy(
self._field_dimensions, item, self._swigptr
)
self._cache[item] = value

if item not in self._field_names:
self.__missing__(item)
return value

if not item.startswith("_") and hasattr(self._swigptr, item):
return getattr(self._swigptr, item)

value = _field_as_numpy(self._field_dimensions, item, self._swigptr)
self._cache[item] = value
return value
self.__missing__(item)

def __missing__(self, key: str) -> None:
"""
Expand Down
2 changes: 1 addition & 1 deletion python/sdist/amici/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def plot_observable_trajectories(
if not ax:
fig, ax = plt.subplots()
if not observable_indices:
observable_indices = range(rdata["y"].shape[1])
observable_indices = range(rdata.ny)

if marker is None:
# Show marker if only one time point is available,
Expand Down
3 changes: 3 additions & 0 deletions python/tests/test_swig_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,9 @@ def test_rdataview(sbml_example_presimulation_module):
rdata = amici.runAmiciSimulation(model, model.getSolver())
assert isinstance(rdata, amici.ReturnDataView)

# check that non-array attributes are looked up in the wrapped object
assert rdata.ptr.ny == rdata.ny

# fields are accessible via dot notation and [] operator,
# __contains__ and __getattr__ are implemented correctly
with pytest.raises(AttributeError):
Expand Down

0 comments on commit 5600235

Please sign in to comment.