From adb0c2af5d692c31b6bc5b3e8b685cb0f9b0c0ee Mon Sep 17 00:00:00 2001 From: Fabio Mazza Date: Wed, 14 Sep 2022 14:18:30 +0200 Subject: [PATCH 01/10] more comments, better formatting --- bp.cpp | 24 +++++++++++++----------- bp.h | 8 ++++---- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/bp.cpp b/bp.cpp index 43e8a9d..b894bcd 100644 --- a/bp.cpp +++ b/bp.cpp @@ -123,8 +123,8 @@ void FactorGraph::append_contact(int i, int j, times_t t, real_t lambdaij, real_ { if (i == j) throw invalid_argument("self loops are not allowed"); - add_node(i); - add_node(j); + add_node(i); + add_node(j); Node & fi = nodes[i]; Node & fj = nodes[j]; int qi = fi.times.size(); @@ -144,12 +144,14 @@ void FactorGraph::append_contact(int i, int j, times_t t, real_t lambdaij, real_ Neigh & ni = fi.neighs[ki]; Neigh & nj = fj.neighs[kj]; if (fi.times[qi - 2] < t) { + //extend fi fi.push_back_time(t); - ++qi; + ++qi; } if (fj.times[qj - 2] < t) { + //extend fj fj.push_back_time(t); - ++qj; + ++qj; } if (ni.t.size() < 2 || ni.t[ni.t.size() - 2] < qi - 2) { ni.t.back() = qi - 2; @@ -160,8 +162,8 @@ void FactorGraph::append_contact(int i, int j, times_t t, real_t lambdaij, real_ ni.lambdas.back() = lambdaij; if (lambdaji != DO_NOT_OVERWRITE) nj.lambdas.back() = lambdaji; - ni.lambdas.push_back(0.0); - nj.lambdas.push_back(0.0); + ni.lambdas.push_back(0.0); + nj.lambdas.push_back(0.0); ++ni.msg; ++nj.msg; } else if (ni.t[ni.t.size() - 2] == qi - 2) { @@ -172,12 +174,12 @@ void FactorGraph::append_contact(int i, int j, times_t t, real_t lambdaij, real_ } else { throw invalid_argument("time of contacts should be ordered"); } - // adjust infinite times - for (int k = 0; k < int(fi.neighs.size()); ++k) { - fi.neighs[k].t.back() = qi - 1; + // adjust infinite times + for (int k = 0; k < int(fi.neighs.size()); ++k) { + fi.neighs[k].t.back() = qi - 1; } - for (int k = 0; k < int(fj.neighs.size()); ++k) { - fj.neighs[k].t.back() = qj - 1; + for (int k = 0; k < int(fj.neighs.size()); ++k) { + fj.neighs[k].t.back() = qj - 1; } } diff --git a/bp.h b/bp.h index bdabd28..1096422 100644 --- a/bp.h +++ b/bp.h @@ -70,10 +70,10 @@ struct Node { void push_back_time(times_t t) { times.back() = t; times.push_back(Tinf); - ht.push_back(ht.back()); - hg.push_back(hg.back()); - bt.push_back(bt.back()); - bg.push_back(bg.back()); + ht.push_back(ht.back()); + hg.push_back(hg.back()); + bt.push_back(bt.back()); + bg.push_back(bg.back()); } std::shared_ptr prob_i; std::shared_ptr prob_r; From b37608538b037e9b159d74f14e2e33f6916a74f0 Mon Sep 17 00:00:00 2001 From: Fabio Mazza Date: Wed, 14 Sep 2022 15:31:37 +0200 Subject: [PATCH 02/10] Add copy support for Params --- pysib.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pysib.cpp b/pysib.cpp index 4bf6686..fc5ba50 100644 --- a/pysib.cpp +++ b/pysib.cpp @@ -212,7 +212,15 @@ PYBIND11_MODULE(_sib, m) { .def_readwrite("psus", &Params::psus) .def_readwrite("pautoinf", &Params::pautoinf) .def_readwrite("learn_rate", &Params::learn_rate) - .def("__repr__", &lexical_cast); + .def("__repr__", &lexical_cast) + .def("__copy__", [](const Params &s){ + return Params(s); + }) + .def("__deepcopy__", [](const Params &s, py::dict){ + return Params(s); + }, "memo"_a); + + ; py::class_(m, "FactorGraph", "SIB class representing the graphical model of the epidemics") .def(py::init Date: Wed, 14 Sep 2022 15:31:45 +0200 Subject: [PATCH 03/10] ignore python eggs --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 63bc918..3a56cc0 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ __pycache__/ # python build files build/ sib.egg-info/ + +.eggs From df7ac3565094f2144454bd6fbbb8fdd29fc2fbdc Mon Sep 17 00:00:00 2001 From: Fabio Mazza Date: Wed, 14 Sep 2022 16:08:08 +0200 Subject: [PATCH 04/10] try fix --- pysib.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pysib.cpp b/pysib.cpp index fc5ba50..cef1bc7 100644 --- a/pysib.cpp +++ b/pysib.cpp @@ -218,7 +218,7 @@ PYBIND11_MODULE(_sib, m) { }) .def("__deepcopy__", [](const Params &s, py::dict){ return Params(s); - }, "memo"_a); + }, "memo"); ; From 85d7786ac3cb563b890a90ebda68501ca7672a25 Mon Sep 17 00:00:00 2001 From: Fabio Mazza Date: Wed, 14 Sep 2022 14:29:03 +0000 Subject: [PATCH 05/10] FactorGraph copy --- pysib.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pysib.cpp b/pysib.cpp index cef1bc7..10af15d 100644 --- a/pysib.cpp +++ b/pysib.cpp @@ -220,8 +220,6 @@ PYBIND11_MODULE(_sib, m) { return Params(s); }, "memo"); - ; - py::class_(m, "FactorGraph", "SIB class representing the graphical model of the epidemics") .def(py::init>, @@ -273,7 +271,13 @@ PYBIND11_MODULE(_sib, m) { .def("showmsg", [](FactorGraph & f){f.show_msg(std::cout);}, "show messages for debugging") .def_readonly("nodes", &FactorGraph::nodes, "all nodes in this FactorGraph") - .def_readonly("params", &FactorGraph::params, "parameters"); + .def_readonly("params", &FactorGraph::params, "parameters") + .def("__copy__", [](const FactorGraph &s){ + return FactorGraph(s); + }) + .def("__deepcopy__", [](const FactorGraph &s, py::dict){ + return FactorGraph(s); + }, "memo"); py::class_(m, "Node", "SIB class representing an individual") .def("marginal", &get_marginal, "compute marginal probabilities (pS,pI,pR) corresponding to times n.times[1:]") From 1fb9e44f1ae05caf517eb59ef9215d36cf4e3232 Mon Sep 17 00:00:00 2001 From: Fabio Mazza Date: Wed, 14 Sep 2022 14:39:55 +0000 Subject: [PATCH 06/10] Pickable Test --- pysib.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pysib.cpp b/pysib.cpp index 10af15d..0705983 100644 --- a/pysib.cpp +++ b/pysib.cpp @@ -150,7 +150,19 @@ PYBIND11_MODULE(_sib, m) { .def("__repr__", &lexical_cast) .def_readwrite("ps", &Test::ps, "probability of S") .def_readwrite("pi", &Test::pi, "probability of I") - .def_readwrite("pr", &Test::pr, "probability of R"); + .def_readwrite("pr", &Test::pr, "probability of R") + .def(py::pickle( + [](const Test &t){ //__getstate__ + return py::make_tuple(t.ps, t.pi, t.pr); + }, + [](py::tuple p){ //__set_state__ + if (p.size()!=3) + throw std::runtime_error("Invalid pickling state!"); + Test test = Test(p[0].cast(), p[1].cast(), + p[2].cast()); + return test; + } + )); py::class_>(m, "Proba") .def("__call__", [](Proba const & p, real_t d) { return p(d); } ) From 1eb3bc3560b3bd95342c4d94197ef33f7fb29bd6 Mon Sep 17 00:00:00 2001 From: Fabio Mazza Date: Thu, 15 Sep 2022 09:18:37 +0000 Subject: [PATCH 07/10] fix literal --- pysib.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pysib.cpp b/pysib.cpp index 0705983..1207104 100644 --- a/pysib.cpp +++ b/pysib.cpp @@ -27,7 +27,7 @@ namespace py = pybind11; using namespace std; using boost::lexical_cast; - +using namespace pybind11::literals; @@ -155,7 +155,7 @@ PYBIND11_MODULE(_sib, m) { [](const Test &t){ //__getstate__ return py::make_tuple(t.ps, t.pi, t.pr); }, - [](py::tuple p){ //__set_state__ + [](py::tuple p){ //__setstate__ if (p.size()!=3) throw std::runtime_error("Invalid pickling state!"); Test test = Test(p[0].cast(), p[1].cast(), @@ -230,7 +230,7 @@ PYBIND11_MODULE(_sib, m) { }) .def("__deepcopy__", [](const Params &s, py::dict){ return Params(s); - }, "memo"); + }, "memo"_a); py::class_(m, "FactorGraph", "SIB class representing the graphical model of the epidemics") .def(py::init(m, "Node", "SIB class representing an individual") .def("marginal", &get_marginal, "compute marginal probabilities (pS,pI,pR) corresponding to times n.times[1:]") From 626447099121e31635f2387ce59948388201888f Mon Sep 17 00:00:00 2001 From: Fabio Mazza Date: Thu, 15 Sep 2022 09:46:26 +0000 Subject: [PATCH 08/10] add test to copy for codecov --- test/copy.doctest | 21 +++++++++++++++++++++ test/likelihood.doctest | 13 +++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 test/copy.doctest diff --git a/test/copy.doctest b/test/copy.doctest new file mode 100644 index 0000000..b526cb8 --- /dev/null +++ b/test/copy.doctest @@ -0,0 +1,21 @@ +>>> import sib +>>> import copy +>>> +>>> t = sib.Test(0.1,0.6,0.3) +>>> +>>> copy.copy(t) +Test(ps=0.1, pi=0.6, pr=0.3) +>>> memo = dict() +>>> copy.deepcopy(t, memo) +Test(ps=0.1, pi=0.6, pr=0.3) +>>> +>>> +>>> par = sib.Params(prob_i=sib.Gamma(0.6,0.1),prob_r=sib.Exponential(mu=0.2), +... pseed=0.01, +... psus=0.5, +... pautoinf=1e-6, +... ) +>>> copy.copy(par) +Params(prob_i=Gamma(0.6,0.1),prob_r=Exponential(0.2),pseed=0.01,psus=0.5,pautoinf=1e-06),learn_rate=0) +>>> copy.deepcopy(par, memo) +Params(prob_i=Gamma(0.6,0.1),prob_r=Exponential(0.2),pseed=0.01,psus=0.5,pautoinf=1e-06),learn_rate=0) diff --git a/test/likelihood.doctest b/test/likelihood.doctest index 8fab9e0..058d3da 100644 --- a/test/likelihood.doctest +++ b/test/likelihood.doctest @@ -32,3 +32,16 @@ FactorGraph >>> sib.iterate(f, tol=1e-15, callback=None) >>> f.loglikelihood() -9535.906070631045 + +### test copying of FactorGraph +>>> import copy +>>> copy.copy(f) +FactorGraph + nodes: 101 + edges: 100 (100 asymmetric) + time contacts: 100 +>>> copy.deepcopy(f) +FactorGraph + nodes: 101 + edges: 100 (100 asymmetric) + time contacts: 100 From df0751ed5b43cbe57cd6e81b6bc7a0f90e762c44 Mon Sep 17 00:00:00 2001 From: Fabio Mazza Date: Thu, 27 Oct 2022 15:24:16 +0200 Subject: [PATCH 09/10] Minor changes to doctest --- test/copy.doctest | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/copy.doctest b/test/copy.doctest index b526cb8..94e0f16 100644 --- a/test/copy.doctest +++ b/test/copy.doctest @@ -1,15 +1,13 @@ >>> import sib >>> import copy ->>> + >>> t = sib.Test(0.1,0.6,0.3) ->>> >>> copy.copy(t) Test(ps=0.1, pi=0.6, pr=0.3) >>> memo = dict() >>> copy.deepcopy(t, memo) Test(ps=0.1, pi=0.6, pr=0.3) ->>> ->>> + >>> par = sib.Params(prob_i=sib.Gamma(0.6,0.1),prob_r=sib.Exponential(mu=0.2), ... pseed=0.01, ... psus=0.5, From 73d5dba529da39882260910938f701eeed7009c3 Mon Sep 17 00:00:00 2001 From: Fabio Mazza Date: Fri, 23 Jun 2023 15:21:21 +0000 Subject: [PATCH 10/10] Change numpy types to avoid deprecation --- test/data_load.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/data_load.py b/test/data_load.py index b2fbc88..b5577b2 100644 --- a/test/data_load.py +++ b/test/data_load.py @@ -28,7 +28,7 @@ def save_json(obj,file_,indent=1): ALL_EPI = "all_epidemies" NAMES_COLS_CONTACTS = ["t","i","j","lambda"] -CONTACTS_DTYPES = dict(zip(NAMES_COLS_CONTACTS,(np.int,np.int,np.int,np.float) )) +CONTACTS_DTYPES = dict(zip(NAMES_COLS_CONTACTS,(np.int_,np.int_,np.int_,np.float_) ))