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

Enable deepcopy for ExpData(View) #2196

Merged
merged 4 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 17 additions & 1 deletion python/sdist/amici/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def __deepcopy__(self, memo):

:returns: SwigPtrView deep copy
"""
other = SwigPtrView(self._swigptr)
# We assume we have a copy-ctor for the swigptr object
other = self.__class__(copy.deepcopy(self._swigptr))
other._field_names = copy.deepcopy(self._field_names)
other._field_dimensions = copy.deepcopy(self._field_dimensions)
other._cache = copy.deepcopy(self._cache)
Expand All @@ -151,6 +152,18 @@ def __repr__(self):
"""
return f"<{self.__class__.__name__}({self._swigptr})>"

def __eq__(self, other):
"""
Equality check

:param other: other object

:returns: whether other object is equal to this object
"""
if not isinstance(other, self.__class__):
return False
return self._swigptr == other._swigptr


class ReturnDataView(SwigPtrView):
"""
Expand Down Expand Up @@ -340,6 +353,9 @@ class ExpDataView(SwigPtrView):
"""
Interface class for C++ Exp Data objects that avoids possibly costly
copies of member data.

NOTE: This currently assumes that the underlying :class:`ExpData`
does not change after instantiating an :class:`ExpDataView`.
"""

_field_names = [
Expand Down
28 changes: 28 additions & 0 deletions python/tests/test_swig_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numbers

import amici
import numpy as np


def test_version_number(pysb_example_presimulation_module):
Expand Down Expand Up @@ -442,3 +443,30 @@ def test_edata_repr():
assert expected_str in repr(e)
# avoid double delete!!
edata_ptr.release()


def test_edata_equality_operator():
e1 = amici.ExpData(1, 2, 3, [3])
e2 = amici.ExpData(1, 2, 3, [3])
assert e1 == e2
# check that comparison with other types works
# this is not implemented by swig by default
assert e1 != 1


def test_expdata_and_expdataview_are_deepcopyable():
edata1 = amici.ExpData(3, 2, 3, range(4))
edata1.setObservedData(np.zeros((3, 4)).flatten())

# ExpData
edata2 = copy.deepcopy(edata1)
assert edata1 == edata2
assert edata1.this != edata2.this
edata2.setTimepoints([0])
assert edata1 != edata2

# ExpDataView
ev1 = amici.ExpDataView(edata1)
ev2 = copy.deepcopy(ev1)
assert ev2._swigptr.this != ev1._swigptr.this
assert ev1 == ev2
3 changes: 3 additions & 0 deletions swig/edata.i
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def _edata_repr(self: "ExpData"):
%pythoncode %{
def __repr__(self):
return _edata_repr(self)

def __eq__(self, other):
return isinstance(other, self.__class__) and __eq__(self, other)
%}
};
%extend std::unique_ptr<amici::ExpData> {
Expand Down
Loading