From 637f8846ef11faedd08a864ea4960f9266add254 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Mon, 18 Dec 2023 12:02:29 +0100 Subject: [PATCH 1/3] Make Model, ModelPtr deepcopyable --- python/tests/test_swig_interface.py | 13 +++++++++++++ swig/model.i | 14 +++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/python/tests/test_swig_interface.py b/python/tests/test_swig_interface.py index 6a7cfec855..8d3358b509 100644 --- a/python/tests/test_swig_interface.py +++ b/python/tests/test_swig_interface.py @@ -471,3 +471,16 @@ def test_expdata_and_expdataview_are_deepcopyable(): ev2 = copy.deepcopy(ev1) assert ev2._swigptr.this != ev1._swigptr.this assert ev1 == ev2 + + +def test_model_is_deepcopyable(pysb_example_presimulation_module): + model_module = pysb_example_presimulation_module + for model1 in ( + model_module.getModel(), + amici.ModelPtr(model_module.getModel()), + ): + model2 = copy.deepcopy(model1) + assert model1.this != model2.this + assert model1.getT0() == model2.getT0() + model2.setT0(100 * model2.getT0()) + assert model1.getT0() != model2.getT0() diff --git a/swig/model.i b/swig/model.i index 3063590c21..36d43310ef 100644 --- a/swig/model.i +++ b/swig/model.i @@ -94,10 +94,22 @@ using namespace amici; %ignore fdx_rdatadx_solver; %ignore fdsigmaydy; +%newobject amici::Model::clone; +%extend amici::Model { +%pythoncode %{ +def __deepcopy__(self, memo): + return self.clone() +%} +}; +%extend std::unique_ptr { +%pythoncode %{ +def __deepcopy__(self, memo): + return self.clone() +%} +}; -%newobject amici::Model::clone; // Process symbols in header %include "amici/model.h" From 1fc9e28b8950dd193c37bcfa55564526dbb0ec4d Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Mon, 18 Dec 2023 13:18:55 +0100 Subject: [PATCH 2/3] .. --- python/tests/test_swig_interface.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/tests/test_swig_interface.py b/python/tests/test_swig_interface.py index 8d3358b509..28d7305dd9 100644 --- a/python/tests/test_swig_interface.py +++ b/python/tests/test_swig_interface.py @@ -481,6 +481,6 @@ def test_model_is_deepcopyable(pysb_example_presimulation_module): ): model2 = copy.deepcopy(model1) assert model1.this != model2.this - assert model1.getT0() == model2.getT0() - model2.setT0(100 * model2.getT0()) - assert model1.getT0() != model2.getT0() + assert model1.t0() == model2.t0() + model2.setT0(100 * model2.t0()) + assert model1.t0() != model2.t0() From d054812d22349b060be22a373736ef2fe80c238a Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Mon, 18 Dec 2023 18:15:33 +0100 Subject: [PATCH 3/3] .. --- python/tests/test_swig_interface.py | 5 ++--- swig/amici.i | 1 + swig/model.i | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/python/tests/test_swig_interface.py b/python/tests/test_swig_interface.py index 0dc242d3e8..3eabafd49b 100644 --- a/python/tests/test_swig_interface.py +++ b/python/tests/test_swig_interface.py @@ -489,7 +489,6 @@ def test_solvers_are_deepcopyable(): ) - def test_model_is_deepcopyable(pysb_example_presimulation_module): model_module = pysb_example_presimulation_module for model1 in ( @@ -499,5 +498,5 @@ def test_model_is_deepcopyable(pysb_example_presimulation_module): model2 = copy.deepcopy(model1) assert model1.this != model2.this assert model1.t0() == model2.t0() - model2.setT0(100 * model2.t0()) - assert model1.t0() != model2.t0() \ No newline at end of file + model2.setT0(100 + model2.t0()) + assert model1.t0() != model2.t0() diff --git a/swig/amici.i b/swig/amici.i index 9eac8e5046..3aebfc2912 100644 --- a/swig/amici.i +++ b/swig/amici.i @@ -16,6 +16,7 @@ nonstandard type conversions. // typemaps for docstrings %typemap(doctype) std::unique_ptr< amici::ExpData >::pointer "ExpData"; +%typemap(doctype) std::unique_ptr< amici::Model > "ModelPtr"; %typemap(doctype) std::unique_ptr< amici::Solver > "SolverPtr"; %typemap(doctype) std::vector< amici::realtype,std::allocator< amici::realtype > > "DoubleVector"; %typemap(doctype) std::vector< double,std::allocator< double > > "DoubleVector"; diff --git a/swig/model.i b/swig/model.i index 36d43310ef..ee3286e1a4 100644 --- a/swig/model.i +++ b/swig/model.i @@ -110,6 +110,5 @@ def __deepcopy__(self, memo): %} }; - // Process symbols in header %include "amici/model.h"