-
Notifications
You must be signed in to change notification settings - Fork 2
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 pickling and copying #1
Open
fabmazz
wants to merge
10
commits into
master
Choose a base branch
from
pickling
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
adb0c2a
more comments, better formatting
fabmazz b376085
Add copy support for Params
fabmazz bc0e3e3
ignore python eggs
fabmazz df7ac35
try fix
fabmazz 85d7786
FactorGraph copy
fabmazz 1fb9e44
Pickable Test
fabmazz 1eb3bc3
fix literal
fabmazz 6264470
add test to copy for codecov
fabmazz df0751e
Minor changes to doctest
fabmazz 73d5dba
Change numpy types to avoid deprecation
fabmazz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,5 @@ __pycache__/ | |
# python build files | ||
build/ | ||
sib.egg-info/ | ||
|
||
.eggs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ namespace py = pybind11; | |
using namespace std; | ||
using boost::lexical_cast; | ||
|
||
|
||
using namespace pybind11::literals; | ||
|
||
|
||
|
||
|
@@ -150,7 +150,19 @@ PYBIND11_MODULE(_sib, m) { | |
.def("__repr__", &lexical_cast<string, Test>) | ||
.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){ //__setstate__ | ||
if (p.size()!=3) | ||
throw std::runtime_error("Invalid pickling state!"); | ||
Test test = Test(p[0].cast<real_t>(), p[1].cast<real_t>(), | ||
p[2].cast<real_t>()); | ||
return test; | ||
} | ||
)); | ||
|
||
py::class_<Proba, shared_ptr<Proba>>(m, "Proba") | ||
.def("__call__", [](Proba const & p, real_t d) { return p(d); } ) | ||
|
@@ -212,7 +224,13 @@ 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<string, Params>); | ||
.def("__repr__", &lexical_cast<string, Params>) | ||
.def("__copy__", [](const Params &s){ | ||
return Params(s); | ||
}) | ||
.def("__deepcopy__", [](const Params &s, py::dict){ | ||
return Params(s); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
}, "memo"_a); | ||
|
||
py::class_<FactorGraph>(m, "FactorGraph", "SIB class representing the graphical model of the epidemics") | ||
.def(py::init<Params const &, | ||
|
@@ -265,7 +283,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"_a); | ||
|
||
py::class_<Node>(m, "Node", "SIB class representing an individual") | ||
.def("marginal", &get_marginal, "compute marginal probabilities (pS,pI,pR) corresponding to times n.times[1:]") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
>>> 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Params
contains smart pointers... are we sure that this is the correct way to do this? In the sense that this will be not be an "honest" deep copy, as e.g.prob_i
andprob_r
will still point to the old python object I think...