Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwigPtrView: look up unhandled attributes in _swigptr #2405

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
if not ax:
fig, ax = plt.subplots()
if not observable_indices:
observable_indices = range(rdata["y"].shape[1])
observable_indices = range(rdata.ny)

Check warning on line 112 in python/sdist/amici/plotting.py

View check run for this annotation

Codecov / codecov/patch

python/sdist/amici/plotting.py#L112

Added line #L112 was not covered by tests

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
Loading