From e5cf148daab178dd96cb3bf631b14ed0a421a562 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Tue, 2 Jan 2024 12:06:41 +0100 Subject: [PATCH] Make Model, ModelPtr deepcopyable (#2247) * Make Model, ModelPtr deepcopyable --- python/tests/test_swig_interface.py | 13 +++++++++++++ swig/amici.i | 1 + swig/model.i | 15 +++++++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/python/tests/test_swig_interface.py b/python/tests/test_swig_interface.py index e8dd478cab..3eabafd49b 100644 --- a/python/tests/test_swig_interface.py +++ b/python/tests/test_swig_interface.py @@ -487,3 +487,16 @@ def test_solvers_are_deepcopyable(): solver1.getRelativeTolerance() != solver2.getRelativeTolerance() ) + + +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.t0() == model2.t0() + 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 3063590c21..ee3286e1a4 100644 --- a/swig/model.i +++ b/swig/model.i @@ -94,10 +94,21 @@ using namespace amici; %ignore fdx_rdatadx_solver; %ignore fdsigmaydy; +%newobject amici::Model::clone; +%extend amici::Model { +%pythoncode %{ +def __deepcopy__(self, memo): + return self.clone() +%} +}; - -%newobject amici::Model::clone; +%extend std::unique_ptr { +%pythoncode %{ +def __deepcopy__(self, memo): + return self.clone() +%} +}; // Process symbols in header %include "amici/model.h"