-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.cpp
286 lines (251 loc) · 11 KB
/
model.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "model.hpp"
#include "agent-meat-state.hpp"
#define MODEL_OF(m, model, description) \
py::class_<epimodels::Model##model<int>, Model<int>>(m, "Model" #model, \
description)
using namespace epiworldpy;
using namespace epiworld;
namespace py = pybind11;
static std::shared_ptr<DataBase<int>> get_db(Model<int> &self) {
return std::shared_ptr<DataBase<int>>(
&self.get_db(), [](DataBase<int> *) { /* do nothing, no delete */ });
}
static void update_susceptible(Agent<int> *p, Model<int> *m) {
Virus<int> *virus = sampler::sample_virus_single<int>(p, m);
if (virus != nullptr) {
p->set_virus(*virus, m);
}
}
static void update_exposed(Agent<int> *p, Model<int> *m) {
if (p->get_virus() == nullptr)
throw std::logic_error(
std::string("Using the -default_update_exposed- on agents WITHOUT "
"viruses makes no sense! ") +
std::string("Agent id ") + std::to_string(p->get_id()) +
std::string(" has no virus registered."));
auto &virus = p->get_virus();
m->array_double_tmp[0u] =
virus->get_prob_death(m) * (1.0 - p->get_death_reduction(virus, m));
m->array_double_tmp[1u] =
1.0 - (1.0 - virus->get_prob_recovery(m)) *
(1.0 - p->get_recovery_enhancer(virus, m));
int which = roulette(2u, m);
if (which < 0) {
return;
}
if (which == 0u) {
p->rm_agent_by_virus(m);
} else {
p->rm_virus(m);
}
}
static void local_default_update_susceptible(Agent<int> *p, Model<int> *m) {
Virus<int> *virus = sampler::sample_virus_single<int>(p, m);
if (virus == nullptr)
return;
p->set_virus(*virus, m);
}
static void local_default_update_exposed(Agent<int> *p, Model<int> *m) {
if (p->get_virus() == nullptr) {
throw std::logic_error(
std::string("Using the -default_update_exposed- on agents WITHOUT "
"viruses makes no sense! ") +
std::string("Agent id ") + std::to_string(p->get_id()) +
std::string(" has no virus registered."));
}
auto &virus = p->get_virus();
m->array_double_tmp[0u] =
virus->get_prob_death(m) * (1.0 - p->get_death_reduction(virus, m));
m->array_double_tmp[1u] =
1.0 - (1.0 - virus->get_prob_recovery(m)) *
(1.0 - p->get_recovery_enhancer(virus, m));
int which = roulette(2u, m);
if (which < 0)
return;
if (which == 0u) {
p->rm_agent_by_virus(m);
} else {
p->rm_virus(m);
}
}
void epiworldpy::export_update_fun(
pybind11::class_<epiworld::UpdateFun<int>> &c) {
c.def_static("default",
[]() {
return (
std::function<void(Agent<int> *, Model<int> *)>)nullptr;
})
.def_static("default_update_susceptible",
[]() {
return (std::function<void(Agent<int> *, Model<int> *)>)
local_default_update_susceptible;
})
.def_static("default_update_exposed", []() {
return (std::function<void(Agent<int> *, Model<int> *)>)
local_default_update_exposed;
});
}
void epiworldpy::export_model(py::class_<epiworld::Model<int>> &c) {
c.def(py::init<>(), "Create a new empty model.")
.def(
"add_state",
static_cast<void (epiworld::Model<>::*)(std::string, UpdateFun<int>)>(
&Model<int>::add_state),
py::arg("lab"), py::arg("fun"))
.def("get_states", &Model<int>::get_states)
.def("get_name", &Model<int>::get_name,
"Get the name of the type of model.")
.def("agents_from_edgelist", &Model<int>::agents_from_edgelist,
"Populatates the model's agents from an edge list.",
py::arg("source"), py::arg("target"), py::arg("size"),
py::arg("directed"))
.def(
"add_virus",
[](Model<int> &model, Virus<int> virus) {
virus.set_state(1, 2, 3);
model.add_virus(virus);
},
"Adds a virus to the model", py::arg("virus"))
.def("add_tool", &Model<int>::add_tool,
"Adds a tool to modify the model.", py::arg("tool"))
.def("add_entity", &Model<int>::add_entity)
.def("get_entity", &Model<int>::get_entity)
.def("agents_smallworld", &Model<int>::agents_smallworld,
"Populate the model without an edgelist.", py::arg("n"),
py::arg("k"), py::arg("d"), py::arg("p"))
.def("print", &Model<int>::print, "Print a summary of the model run.",
py::arg("summary") = true)
.def("run", &Model<int>::run,
"Run the model according to the previously specific parameters.",
py::arg("ndays"), py::arg("seed") = 1u)
.def("get_db", &get_db,
"Get the data from the model run, which may then be queried with "
"associated methods.",
py::return_value_policy::reference);
}
void epiworldpy::export_all_models(pybind11::module &m) {
/* Export models.
*
* TODO: Make Python model proxys inhereit from an interface and do this in
* a loop.
*/
auto diffnet = MODEL_OF(m, DiffNet, "A network diffusion model.");
auto seir = MODEL_OF(m, SEIR,
"A model with four compartments: susceptible, "
"exposed, infectious, and recovered.");
auto seirconn = MODEL_OF(
m, SEIRCONN,
"A model with four compartments: susceptible, exposed, infectious, and "
"recovered.");
auto seird = MODEL_OF(m, SEIRD,
"A model with five compartments: susceptible, "
"exposed, infectious, recovered, and dead.");
auto sir = MODEL_OF(m, SIR,
"A model with three compartments: "
"susceptible, infectious, and recovered.");
auto sirconn = MODEL_OF(m, SIRCONN,
"A model with three compartments: susceptible, "
"infectious, and recovered.");
auto sird = MODEL_OF(m, SIRD,
"A model with four compartments: susceptible, "
"infectious, recovered, and dead.");
auto sirdconn = MODEL_OF(m, SIRDCONN,
"A model with four compartments: susceptible, "
"infectious, recovered, and dead.");
auto sis = MODEL_OF(
m, SIS, "A model wth two compartments: susceptible and infectious.");
auto sisd = MODEL_OF(
m, SISD,
"A model wth three compartments: susceptible, infectious, and death.");
auto surv = MODEL_OF(
m, SURV, "A model where agents may be isolated, even when asymptomatic.");
epiworldpy::export_diffnet(diffnet);
epiworldpy::export_seir(seir);
epiworldpy::export_seirconn(seirconn);
epiworldpy::export_seird(seird);
epiworldpy::export_sir(sir);
epiworldpy::export_sirconn(sirconn);
epiworldpy::export_sird(sird);
epiworldpy::export_sirdconn(sirdconn);
epiworldpy::export_sis(sis);
epiworldpy::export_sisd(sisd);
epiworldpy::export_surv(surv);
}
void epiworldpy::export_diffnet(MODEL_CHILD_TYPE(DiffNet) & c) {
c.def(py::init<std::string, double, double, bool, double *, int,
std::vector<size_t>, std::vector<double>>(),
"Create a new DiffNet model.", py::arg("name"), py::arg("prevalence"),
py::arg("prob_adopt"), py::arg("normalize_exposure"), py::arg("data"),
py::arg("data_ncols"), py::arg("data_cols"), py::arg("params"));
}
void epiworldpy::export_seir(MODEL_CHILD_TYPE(SEIR) & c) {
c.def(py::init<std::string, double, double, double, double>(),
"Create a new SEIR model.", py::arg("name"), py::arg("prevalence"),
py::arg("transmission_rate"), py::arg("incubation_days"),
py::arg("recovery_rate"));
}
void epiworldpy::export_seirconn(MODEL_CHILD_TYPE(SEIRCONN) & c) {
c.def(py::init<std::string, int, double, double, double, double, double>(),
"Create a new SEIRCONN model.", py::arg("name"), py::arg("n"),
py::arg("prevalence"), py::arg("contact_rate"),
py::arg("transmission_rate"), py::arg("incubation_days"),
py::arg("recovery_rate"));
}
void epiworldpy::export_seird(MODEL_CHILD_TYPE(SEIRD) & c) {
c.def(py::init<std::string, double, double, double, double, double>(),
"Create a new SEIRD model.", py::arg("name"), py::arg("prevalence"),
py::arg("transmission_rate"), py::arg("incubation_days"),
py::arg("recovery_rate"), py::arg("death_rate"));
}
void epiworldpy::export_sir(MODEL_CHILD_TYPE(SIR) & c) {
c.def(py::init<std::string, double, double, double>(),
"Create a new SIR model.", py::arg("name"), py::arg("prevalence"),
py::arg("transmission_rate"), py::arg("recovery_rate"));
}
void epiworldpy::export_sirconn(MODEL_CHILD_TYPE(SIRCONN) & c) {
c.def(py::init<std::string, int, double, double, double, double>(),
"Create a new SIRCONN model.", py::arg("name"), py::arg("n"),
py::arg("prevalence"), py::arg("contact_rate"),
py::arg("transmission_rate"), py::arg("recovery_rate"));
}
void epiworldpy::export_sird(MODEL_CHILD_TYPE(SIRD) & c) {
c.def(py::init<std::string, double, double, double, double>(),
"Create a new SIRD model.", py::arg("name"), py::arg("prevalence"),
py::arg("transmission_rate"), py::arg("recovery_rate"),
py::arg("death_rate"));
}
void epiworldpy::export_sirdconn(MODEL_CHILD_TYPE(SIRDCONN) & c) {
c.def(py::init<std::string, int, double, double, double, double, double>(),
"Create a new SIRDCONN model.", py::arg("name"), py::arg("n"),
py::arg("prevalence"), py::arg("contact_rate"),
py::arg("transmission_rate"), py::arg("recovery_rate"),
py::arg("death_rate"));
}
void epiworldpy::export_sis(MODEL_CHILD_TYPE(SIS) & c) {
c.def(py::init<std::string, double, double, double>(),
"Create a new SIS model.", py::arg("name"), py::arg("prevalence"),
py::arg("transmission_rate"), py::arg("recovery_rate"));
}
void epiworldpy::export_sisd(MODEL_CHILD_TYPE(SISD) & c) {
c.def(py::init<std::string, double, double, double, double>(),
"Create a new SISD model.", py::arg("name"), py::arg("prevalence"),
py::arg("transmission_rate"), py::arg("recovery_rate"),
py::arg("death_rate"));
}
void epiworldpy::export_surv(MODEL_CHILD_TYPE(SURV) & c) {
/*
* TODO: I noticed that epiworld::epimodels::ModelSURV is overloaded to
* accept a variable number of arguments, should this be expressed on the
* Python side? It isn't in R, as far as I can tell, and I don't know how
* concerning symmetry is.
*/
c.def(py::init<std::string, double, double, double, double, double, double,
double, double, double, double, double, double>(),
"Create a new SURV model.", py::arg("name"), py::arg("prevalence"),
py::arg("efficacy_vax"), py::arg("latent_period"),
py::arg("prob_symptoms"), py::arg("prop_vaccinated"),
py::arg("prop_vax_redux_transm"), py::arg("infect_period"),
py::arg("prop_vax_redux_infect"), py::arg("surveillance_prob"),
py::arg("transmission_rate"), py::arg("prob_death"),
py::arg("prob_noreinfect"));
}