Skip to content

Commit deefc27

Browse files
authored
1016 use interpolate_simulation_result for timeseries in secirvvs python (#1017)
- added bindings for using the interpolate_simulation_result fuction with a timeseries obtained by a secirvvs simulation
1 parent 907ebbe commit deefc27

2 files changed

Lines changed: 55 additions & 33 deletions

File tree

pycode/examples/simulation/osecirvvs_simple.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import pandas as pd
2727

2828
from memilio.simulation import AgeGroup, Damping, SimulationDay
29-
from memilio.simulation.osecirvvs import InfectionState
29+
from memilio.simulation.osecirvvs import InfectionState, interpolate_simulation_result
3030
from memilio.simulation.osecirvvs import Model, simulate
3131

3232

@@ -152,7 +152,7 @@ def run_secirvvs_simulation(show_plot=True):
152152
result = simulate(t0, tmax, dt, model)
153153

154154
# # interpolate results
155-
# result = interpolate_simulation_result(result)
155+
result = interpolate_simulation_result(result)
156156

157157
print(result.get_last_value())
158158

pycode/memilio-simulation/memilio/simulation/osecirvvs.cpp

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ void bind_ParameterStudy(py::module_& m, std::string const& name)
7979
py::return_value_policy::reference_internal)
8080
.def_property_readonly("model", py::overload_cast<>(&mio::ParameterStudy<Simulation>::get_model, py::const_),
8181
py::return_value_policy::reference_internal)
82-
.def_property_readonly("model_graph",
83-
py::overload_cast<>(&mio::ParameterStudy<Simulation>::get_model_graph),
82+
.def_property_readonly("model_graph", py::overload_cast<>(&mio::ParameterStudy<Simulation>::get_model_graph),
8483
py::return_value_policy::reference_internal)
8584
.def_property_readonly("model_graph",
8685
py::overload_cast<>(&mio::ParameterStudy<Simulation>::get_model_graph, py::const_),
@@ -89,7 +88,8 @@ void bind_ParameterStudy(py::module_& m, std::string const& name)
8988
"run",
9089
[](mio::ParameterStudy<Simulation>& self,
9190
std::function<void(mio::Graph<mio::SimulationNode<Simulation>, mio::MigrationEdge>, size_t)>
92-
handle_result, bool variant_high) {
91+
handle_result,
92+
bool variant_high) {
9393
self.run(
9494
[variant_high](auto&& g) {
9595
return draw_sample(g, variant_high);
@@ -104,16 +104,19 @@ void bind_ParameterStudy(py::module_& m, std::string const& name)
104104
});
105105
},
106106
py::arg("handle_result_func"), py::arg("variant_high"))
107-
.def("run",
108-
[](mio::ParameterStudy<Simulation>& self, bool variant_high) { //default argument doesn't seem to work with functions
107+
.def(
108+
"run",
109+
[](mio::ParameterStudy<Simulation>& self,
110+
bool variant_high) { //default argument doesn't seem to work with functions
109111
return self.run([variant_high](auto&& g) {
110-
return draw_sample(g, variant_high);
111-
});
112-
},
112+
return draw_sample(g, variant_high);
113+
});
114+
},
113115
py::arg("variant_high"))
114116
.def(
115117
"run_single",
116-
[](mio::ParameterStudy<Simulation>& self, std::function<void(Simulation, size_t)> handle_result, bool variant_high) {
118+
[](mio::ParameterStudy<Simulation>& self, std::function<void(Simulation, size_t)> handle_result,
119+
bool variant_high) {
117120
self.run(
118121
[variant_high](auto&& g) {
119122
return draw_sample(g, variant_high);
@@ -124,12 +127,13 @@ void bind_ParameterStudy(py::module_& m, std::string const& name)
124127
});
125128
},
126129
py::arg("handle_result_func"), py::arg("variant_high"))
127-
.def("run_single",
130+
.def(
131+
"run_single",
128132
[](mio::ParameterStudy<Simulation>& self, bool variant_high) {
129133
return filter_graph_results(self.run([variant_high](auto&& g) {
130134
return draw_sample(g, variant_high);
131135
}));
132-
},
136+
},
133137
py::arg("variant_high"));
134138
}
135139

@@ -165,6 +169,16 @@ PYBIND11_MAKE_OPAQUE(std::vector<mio::Graph<mio::SimulationNode<mio::osecirvvs::
165169

166170
PYBIND11_MODULE(_simulation_osecirvvs, m)
167171
{
172+
m.def("interpolate_simulation_result",
173+
static_cast<mio::TimeSeries<double> (*)(const mio::TimeSeries<double>&, const double)>(
174+
&mio::interpolate_simulation_result),
175+
py::arg("ts"), py::arg("abs_tol") = 1e-14);
176+
177+
m.def("interpolate_simulation_result",
178+
static_cast<mio::TimeSeries<double> (*)(const mio::TimeSeries<double>&, const std::vector<double>&)>(
179+
&mio::interpolate_simulation_result),
180+
py::arg("ts"), py::arg("interpolation_times"));
181+
168182
pymio::iterable_enum<mio::osecirvvs::InfectionState>(m, "InfectionState")
169183
.value("SusceptibleNaive", mio::osecirvvs::InfectionState::SusceptibleNaive)
170184
.value("SusceptiblePartialImmunity", mio::osecirvvs::InfectionState::SusceptiblePartialImmunity)
@@ -202,28 +216,32 @@ PYBIND11_MODULE(_simulation_osecirvvs, m)
202216

203217
py::class_<mio::osecirvvs::Parameters, mio::osecirvvs::ParametersBase>(m, "Parameters")
204218
.def(py::init<mio::AgeGroup>())
205-
.def_property("commuter_nondetection",
219+
.def_property(
220+
"commuter_nondetection",
206221
[](const mio::osecirvvs::Parameters& self) {
207222
return self.get_commuter_nondetection();
208223
},
209224
[](mio::osecirvvs::Parameters& self, double v) {
210225
self.get_commuter_nondetection() = v;
211226
})
212-
.def_property("start_commuter_detection",
227+
.def_property(
228+
"start_commuter_detection",
213229
[](const mio::osecirvvs::Parameters& self) {
214230
return self.get_start_commuter_detection();
215231
},
216232
[](mio::osecirvvs::Parameters& self, double v) {
217233
self.get_start_commuter_detection() = v;
218234
})
219-
.def_property("end_commuter_detection",
235+
.def_property(
236+
"end_commuter_detection",
220237
[](const mio::osecirvvs::Parameters& self) {
221238
return self.get_end_commuter_detection();
222239
},
223240
[](mio::osecirvvs::Parameters& self, double v) {
224241
self.get_end_commuter_detection() = v;
225242
})
226-
.def_property("end_dynamic_npis",
243+
.def_property(
244+
"end_dynamic_npis",
227245
[](const mio::osecirvvs::Parameters& self) {
228246
return self.get_end_dynamic_npis();
229247
},
@@ -269,7 +287,8 @@ PYBIND11_MODULE(_simulation_osecirvvs, m)
269287
//normally, std::vector is bound to any python iterable, but this doesn't work for move-only elements
270288
//Bound the vector as a custom type that serves as output of ParameterStudy::run and input to
271289
//interpolate_ensemble_results
272-
py::bind_vector<std::vector<mio::Graph<mio::SimulationNode<mio::osecirvvs::Simulation<>>, mio::MigrationEdge>>>(m, "EnsembleGraphResults");
290+
py::bind_vector<std::vector<mio::Graph<mio::SimulationNode<mio::osecirvvs::Simulation<>>, mio::MigrationEdge>>>(
291+
m, "EnsembleGraphResults");
273292
bind_ParameterStudy<mio::osecirvvs::Simulation<>>(m, "ParameterStudy");
274293

275294
m.def(
@@ -282,8 +301,8 @@ PYBIND11_MODULE(_simulation_osecirvvs, m)
282301
// These functions are in general not secir dependent, only with the current config
283302
m.def(
284303
"set_nodes",
285-
[](const mio::osecirvvs::Parameters& params, mio::Date start_date, mio::Date end_date, const std::string& data_dir,
286-
const std::string& population_data_path, bool is_node_for_county,
304+
[](const mio::osecirvvs::Parameters& params, mio::Date start_date, mio::Date end_date,
305+
const std::string& data_dir, const std::string& population_data_path, bool is_node_for_county,
287306
mio::Graph<mio::osecirvvs::Model, mio::MigrationParameters>& params_graph,
288307
const std::vector<double>& scaling_factor_inf, double scaling_factor_icu, double tnt_capacity_factor,
289308
int num_days = 0, bool export_time_series = false) {
@@ -303,17 +322,17 @@ PYBIND11_MODULE(_simulation_osecirvvs, m)
303322
[](const std::string& data_dir, mio::Graph<mio::osecirvvs::Model, mio::MigrationParameters>& params_graph,
304323
size_t contact_locations_size) {
305324
auto migrating_comp = {mio::osecirvvs::InfectionState::SusceptibleNaive,
306-
mio::osecirvvs::InfectionState::ExposedNaive,
307-
mio::osecirvvs::InfectionState::InfectedNoSymptomsNaive,
308-
mio::osecirvvs::InfectionState::InfectedSymptomsNaive,
309-
mio::osecirvvs::InfectionState::SusceptibleImprovedImmunity,
310-
mio::osecirvvs::InfectionState::SusceptiblePartialImmunity,
311-
mio::osecirvvs::InfectionState::ExposedPartialImmunity,
312-
mio::osecirvvs::InfectionState::InfectedNoSymptomsPartialImmunity,
313-
mio::osecirvvs::InfectionState::InfectedSymptomsPartialImmunity,
314-
mio::osecirvvs::InfectionState::ExposedImprovedImmunity,
315-
mio::osecirvvs::InfectionState::InfectedNoSymptomsImprovedImmunity,
316-
mio::osecirvvs::InfectionState::InfectedSymptomsImprovedImmunity};
325+
mio::osecirvvs::InfectionState::ExposedNaive,
326+
mio::osecirvvs::InfectionState::InfectedNoSymptomsNaive,
327+
mio::osecirvvs::InfectionState::InfectedSymptomsNaive,
328+
mio::osecirvvs::InfectionState::SusceptibleImprovedImmunity,
329+
mio::osecirvvs::InfectionState::SusceptiblePartialImmunity,
330+
mio::osecirvvs::InfectionState::ExposedPartialImmunity,
331+
mio::osecirvvs::InfectionState::InfectedNoSymptomsPartialImmunity,
332+
mio::osecirvvs::InfectionState::InfectedSymptomsPartialImmunity,
333+
mio::osecirvvs::InfectionState::ExposedImprovedImmunity,
334+
mio::osecirvvs::InfectionState::InfectedNoSymptomsImprovedImmunity,
335+
mio::osecirvvs::InfectionState::InfectedSymptomsImprovedImmunity};
317336
auto weights = std::vector<ScalarType>{0., 0., 1.0, 1.0, 0.33, 0., 0.};
318337
auto result = mio::set_edges<ContactLocation, mio::osecirvvs::Model, mio::MigrationParameters,
319338
mio::MigrationCoefficientGroup, mio::osecirvvs::InfectionState,
@@ -342,9 +361,12 @@ PYBIND11_MODULE(_simulation_osecirvvs, m)
342361
#endif // MEMILIO_HAS_JSONCPP
343362

344363
m.def("interpolate_simulation_result",
345-
py::overload_cast<const mio::Graph<mio::SimulationNode<mio::osecirvvs::Simulation<>>, mio::MigrationEdge>&>(&mio::interpolate_simulation_result<mio::osecirvvs::Simulation<>>));
364+
py::overload_cast<const mio::Graph<mio::SimulationNode<mio::osecirvvs::Simulation<>>, mio::MigrationEdge>&>(
365+
&mio::interpolate_simulation_result<mio::osecirvvs::Simulation<>>));
346366

347-
m.def("interpolate_ensemble_results", &mio::interpolate_ensemble_results<mio::Graph<mio::SimulationNode<mio::osecirvvs::Simulation<>>, mio::MigrationEdge>>);
367+
m.def("interpolate_ensemble_results",
368+
&mio::interpolate_ensemble_results<
369+
mio::Graph<mio::SimulationNode<mio::osecirvvs::Simulation<>>, mio::MigrationEdge>>);
348370

349371
m.attr("__version__") = "dev";
350372
}

0 commit comments

Comments
 (0)