-
Notifications
You must be signed in to change notification settings - Fork 19
1351 pymio interface for sde sir type models #1371
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
base: main
Are you sure you want to change the base?
Changes from all commits
de1375b
9f98525
10ac915
21ac3b5
73c264c
6bfe38d
52da059
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,74 @@ | ||||||
############################################################################# | ||||||
# Copyright (C) 2020-2025 MEmilio | ||||||
# | ||||||
# Authors: Maximilian Betz | ||||||
# | ||||||
# Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de> | ||||||
# | ||||||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
# you may not use this file except in compliance with the License. | ||||||
# You may obtain a copy of the License at | ||||||
# | ||||||
# http://www.apache.org/licenses/LICENSE-2.0 | ||||||
# | ||||||
# Unless required by applicable law or agreed to in writing, software | ||||||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
# See the License for the specific language governing permissions and | ||||||
# limitations under the License. | ||||||
############################################################################# | ||||||
import argparse | ||||||
|
||||||
import numpy as np | ||||||
|
||||||
from memilio.simulation import AgeGroup, Damping | ||||||
from memilio.simulation.ssir import InfectionState as State | ||||||
from memilio.simulation.ssir import (Model, simulate_stochastic) | ||||||
|
||||||
|
||||||
def run_sde_sir_simulation(): | ||||||
"""Runs the c++ SDE SIR model""" | ||||||
|
||||||
# Define population of age groups | ||||||
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. I don't understand the comment. This is the total population, right? Does the SDE model even have age groups? |
||||||
population = 10000 | ||||||
|
||||||
days = 5. # number of days to simulate | ||||||
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.
Suggested change
|
||||||
dt = 0.1 | ||||||
|
||||||
# Initialize Parameters | ||||||
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.
Suggested change
|
||||||
model = Model() | ||||||
|
||||||
# Compartment transition duration | ||||||
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.
Suggested change
|
||||||
model.parameters.TimeInfected.value = 10. | ||||||
|
||||||
# # Compartment transition propabilities | ||||||
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.
Suggested change
|
||||||
model.parameters.TransmissionProbabilityOnContact.value = 1. | ||||||
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. Transmission probability of 100%? |
||||||
|
||||||
# Initial number of people in each compartment | ||||||
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.
Suggested change
|
||||||
model.populations[State.Infected] = 100 | ||||||
model.populations[State.Recovered] = 1000 | ||||||
model.populations.set_difference_from_total( | ||||||
(State.Susceptible), population) | ||||||
|
||||||
model.parameters.ContactPatterns.baseline = np.ones( | ||||||
(1, 1)) * 2.7 | ||||||
model.parameters.ContactPatterns.minimum = np.zeros( | ||||||
(1, 1)) | ||||||
model.parameters.ContactPatterns.add_damping( | ||||||
Damping(coeffs=np.r_[0.6], t=12.5, level=0, type=0)) | ||||||
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. You only simulate 5 days but add a damping after 12.5 days |
||||||
|
||||||
# Check logical constraints to parameters | ||||||
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.
Suggested change
|
||||||
model.check_constraints() | ||||||
|
||||||
# Run Simulation | ||||||
result = simulate_stochastic(0., days, dt, model) | ||||||
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.
Suggested change
|
||||||
|
||||||
result.print_table(False, ["S", "I", "R"], 16, 5) | ||||||
|
||||||
|
||||||
if __name__ == "__main__": | ||||||
arg_parser = argparse.ArgumentParser( | ||||||
'sde_sir_simple', | ||||||
description='Simple example demonstrating the setup and simulation of the SDE SIR model.') | ||||||
args = arg_parser.parse_args() | ||||||
run_sde_sir_simulation() |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,80 @@ | ||||||
############################################################################# | ||||||
# Copyright (C) 2020-2025 MEmilio | ||||||
# | ||||||
# Authors: Maximilian Betz | ||||||
# | ||||||
# Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de> | ||||||
# | ||||||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
# you may not use this file except in compliance with the License. | ||||||
# You may obtain a copy of the License at | ||||||
# | ||||||
# http://www.apache.org/licenses/LICENSE-2.0 | ||||||
# | ||||||
# Unless required by applicable law or agreed to in writing, software | ||||||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
# See the License for the specific language governing permissions and | ||||||
# limitations under the License. | ||||||
############################################################################# | ||||||
import argparse | ||||||
|
||||||
import numpy as np | ||||||
|
||||||
from memilio.simulation import AgeGroup, Damping | ||||||
from memilio.simulation.ssirs import InfectionState as State | ||||||
from memilio.simulation.ssirs import ( | ||||||
Model, simulate_stochastic, interpolate_simulation_result) | ||||||
|
||||||
|
||||||
def run_sde_sirs_simulation(): | ||||||
"""Runs the c++ SDE SIRs model""" | ||||||
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.
Suggested change
|
||||||
|
||||||
# Define population of age groups | ||||||
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. See comment above |
||||||
population = 10000 | ||||||
|
||||||
days = 5. # number of days to simulate | ||||||
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.
Suggested change
|
||||||
dt = 0.001 | ||||||
|
||||||
# Initialize Parameters | ||||||
model = Model() | ||||||
|
||||||
# Compartment transition duration | ||||||
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. Please adjust the comments according to my review of SIR example |
||||||
model.parameters.TimeInfected.value = 10. | ||||||
model.parameters.TimeImmune.value = 100. | ||||||
|
||||||
# # Compartment transition propabilities | ||||||
model.parameters.TransmissionProbabilityOnContact.value = 1. | ||||||
|
||||||
# Initial number of people in each compartment | ||||||
model.populations[State.Infected] = 100 | ||||||
model.populations[State.Recovered] = 1000 | ||||||
model.populations.set_difference_from_total( | ||||||
(State.Susceptible), population) | ||||||
|
||||||
model.parameters.ContactPatterns.baseline = np.ones( | ||||||
(1, 1)) * 20.7 | ||||||
model.parameters.ContactPatterns.minimum = np.zeros( | ||||||
(1, 1)) | ||||||
model.parameters.ContactPatterns.add_damping( | ||||||
Damping(coeffs=np.r_[0.6], t=12.5, level=0, type=0)) | ||||||
|
||||||
# Check logical constraints to parameters | ||||||
model.check_constraints() | ||||||
|
||||||
# Run Simulation | ||||||
result = simulate_stochastic(0., days, dt, model) | ||||||
|
||||||
# Interpolate results | ||||||
result = interpolate_simulation_result(result) | ||||||
|
||||||
# Print results | ||||||
result.print_table(False, ["Susceptible", "Infected", "Recovered"], 16, 5) | ||||||
|
||||||
|
||||||
if __name__ == "__main__": | ||||||
arg_parser = argparse.ArgumentParser( | ||||||
'sde_sirs_simple', | ||||||
description='Simple example demonstrating the setup and simulation of the SDE SIRS model.') | ||||||
args = arg_parser.parse_args() | ||||||
run_sde_sirs_simulation() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (C) 2020-2025 MEmilio | ||
* | ||
* Authors: Maximilian Betz | ||
* | ||
* Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
//Includes from pymio | ||
#include "pybind_util.h" | ||
#include "utils/index.h" | ||
#include "compartments/compartmental_model.h" | ||
#include "utils/custom_index_array.h" | ||
#include "utils/parameter_set.h" | ||
#include "epidemiology/populations.h" | ||
|
||
//Includes from MEmilio | ||
#include "sde_sir/model.h" | ||
#include "sde_sir/infection_state.h" | ||
#include "memilio/compartments/stochastic_simulation.h" | ||
#include "memilio/compartments/stochastic_model.h" | ||
#include "memilio/data/analyze_result.h" | ||
|
||
#include "pybind11/pybind11.h" | ||
#include <pybind11/stl.h> | ||
#include <pybind11/functional.h> | ||
|
||
namespace py = pybind11; | ||
|
||
namespace pymio | ||
{ | ||
//specialization of pretty_name | ||
template <> | ||
inline std::string pretty_name<mio::ssir::InfectionState>() | ||
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. What is that used for? |
||
{ | ||
return "InfectionState"; | ||
} | ||
|
||
} // namespace pymio | ||
|
||
PYBIND11_MODULE(_simulation_ssir, m) | ||
{ | ||
m.def("interpolate_simulation_result", | ||
static_cast<mio::TimeSeries<double> (*)(const mio::TimeSeries<double>&, const double)>( | ||
&mio::interpolate_simulation_result), | ||
py::arg("ts"), py::arg("abs_tol") = 1e-14); | ||
|
||
m.def("interpolate_simulation_result", | ||
static_cast<mio::TimeSeries<double> (*)(const mio::TimeSeries<double>&, const std::vector<double>&)>( | ||
&mio::interpolate_simulation_result), | ||
py::arg("ts"), py::arg("interpolation_times")); | ||
|
||
m.def("interpolate_ensemble_results", &mio::interpolate_ensemble_results<mio::TimeSeries<double>>); | ||
|
||
pymio::iterable_enum<mio::ssir::InfectionState>(m, "InfectionState") | ||
.value("Susceptible", mio::ssir::InfectionState::Susceptible) | ||
.value("Infected", mio::ssir::InfectionState::Infected) | ||
.value("Recovered", mio::ssir::InfectionState::Recovered); | ||
|
||
pymio::bind_ParameterSet<mio::ssir::ParametersBase, pymio::EnablePickling::Never>(m, "ParametersBase"); | ||
|
||
pymio::bind_class<mio::ssir::Parameters, pymio::EnablePickling::Required, mio::ssir::ParametersBase>(m, | ||
"Parameters") | ||
.def(py::init<>()) | ||
.def("check_constraints", &mio::ssir::Parameters::check_constraints) | ||
.def("apply_constraints", &mio::ssir::Parameters::apply_constraints); | ||
|
||
using Populations = mio::Populations<double, mio::ssir::InfectionState>; | ||
pymio::bind_Population(m, "Populations", mio::Tag<mio::ssir::Model::Populations>{}); | ||
pymio::bind_CompartmentalModel<mio::ssir::InfectionState, Populations, mio::ssir::Parameters, | ||
pymio::EnablePickling::Never>(m, "ModelBase"); | ||
pymio::bind_class<mio::ssir::Model, pymio::EnablePickling::Never, | ||
mio::CompartmentalModel<double, mio::ssir::InfectionState, Populations, mio::ssir::Parameters>>( | ||
m, "Model") | ||
.def(py::init<>()); | ||
|
||
m.def( | ||
"simulate_stochastic", | ||
[](double t0, double tmax, double dt, mio::ssir::Model const& model) { | ||
return mio::simulate_stochastic<double, mio::ssir::Model>(t0, tmax, dt, model); | ||
}, | ||
"Simulates an SDE SIR model from t0 to tmax.", py::arg("t0"), py::arg("tmax"), py::arg("dt"), py::arg("model")); | ||
|
||
m.attr("__version__") = "dev"; | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,97 @@ | ||||||
/* | ||||||
* Copyright (C) 2020-2025 MEmilio | ||||||
* | ||||||
* Authors: Maximilian Betz | ||||||
* | ||||||
* Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de> | ||||||
* | ||||||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
* you may not use this file except in compliance with the License. | ||||||
* You may obtain a copy of the License at | ||||||
* | ||||||
* http://www.apache.org/licenses/LICENSE-2.0 | ||||||
* | ||||||
* Unless required by applicable law or agreed to in writing, software | ||||||
* distributed under the License is distributed on an "AS IS" BASIS, | ||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
* See the License for the specific language governing permissions and | ||||||
* limitations under the License. | ||||||
*/ | ||||||
|
||||||
//Includes from pymio | ||||||
#include "pybind_util.h" | ||||||
#include "utils/index.h" | ||||||
#include "compartments/compartmental_model.h" | ||||||
#include "utils/custom_index_array.h" | ||||||
#include "utils/parameter_set.h" | ||||||
#include "epidemiology/populations.h" | ||||||
|
||||||
//Includes from MEmilio | ||||||
#include "sde_sirs/model.h" | ||||||
#include "sde_sirs/infection_state.h" | ||||||
#include "memilio/compartments/stochastic_simulation.h" | ||||||
#include "memilio/compartments/stochastic_model.h" | ||||||
#include "memilio/data/analyze_result.h" | ||||||
|
||||||
#include "pybind11/pybind11.h" | ||||||
#include <pybind11/stl.h> | ||||||
#include <pybind11/functional.h> | ||||||
|
||||||
namespace py = pybind11; | ||||||
|
||||||
namespace pymio | ||||||
{ | ||||||
//specialization of pretty_name | ||||||
template <> | ||||||
inline std::string pretty_name<mio::ssirs::InfectionState>() | ||||||
{ | ||||||
return "InfectionState"; | ||||||
} | ||||||
|
||||||
} // namespace pymio | ||||||
|
||||||
PYBIND11_MODULE(_simulation_ssirs, m) | ||||||
{ | ||||||
m.def("interpolate_simulation_result", | ||||||
static_cast<mio::TimeSeries<double> (*)(const mio::TimeSeries<double>&, const double)>( | ||||||
&mio::interpolate_simulation_result), | ||||||
py::arg("ts"), py::arg("abs_tol") = 1e-14); | ||||||
|
||||||
m.def("interpolate_simulation_result", | ||||||
static_cast<mio::TimeSeries<double> (*)(const mio::TimeSeries<double>&, const std::vector<double>&)>( | ||||||
&mio::interpolate_simulation_result), | ||||||
py::arg("ts"), py::arg("interpolation_times")); | ||||||
|
||||||
m.def("interpolate_ensemble_results", &mio::interpolate_ensemble_results<mio::TimeSeries<double>>); | ||||||
|
||||||
pymio::iterable_enum<mio::ssirs::InfectionState>(m, "InfectionState") | ||||||
.value("Susceptible", mio::ssirs::InfectionState::Susceptible) | ||||||
.value("Infected", mio::ssirs::InfectionState::Infected) | ||||||
.value("Recovered", mio::ssirs::InfectionState::Recovered); | ||||||
|
||||||
pymio::bind_ParameterSet<mio::ssirs::ParametersBase, pymio::EnablePickling::Never>(m, "ParametersBase"); | ||||||
|
||||||
pymio::bind_class<mio::ssirs::Parameters, pymio::EnablePickling::Required, mio::ssirs::ParametersBase>(m, | ||||||
"Parameters") | ||||||
.def(py::init<>()) | ||||||
.def("check_constraints", &mio::ssirs::Parameters::check_constraints) | ||||||
.def("apply_constraints", &mio::ssirs::Parameters::apply_constraints); | ||||||
|
||||||
using Populations = mio::Populations<double, mio::ssirs::InfectionState>; | ||||||
pymio::bind_Population(m, "Populations", mio::Tag<mio::ssirs::Model::Populations>{}); | ||||||
pymio::bind_CompartmentalModel<mio::ssirs::InfectionState, Populations, mio::ssirs::Parameters, | ||||||
pymio::EnablePickling::Never>(m, "ModelBase"); | ||||||
pymio::bind_class<mio::ssirs::Model, pymio::EnablePickling::Never, | ||||||
mio::CompartmentalModel<double, mio::ssirs::InfectionState, Populations, mio::ssirs::Parameters>>( | ||||||
m, "Model") | ||||||
.def(py::init<>()); | ||||||
|
||||||
m.def( | ||||||
"simulate_stochastic", | ||||||
[](double t0, double tmax, double dt, mio::ssirs::Model const& model) { | ||||||
return mio::simulate_stochastic<double, mio::ssirs::Model>(t0, tmax, dt, model); | ||||||
}, | ||||||
"Simulates an SDE SIR model from t0 to tmax.", py::arg("t0"), py::arg("tmax"), py::arg("dt"), py::arg("model")); | ||||||
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.
Suggested change
|
||||||
|
||||||
m.attr("__version__") = "dev"; | ||||||
} |
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.