Skip to content
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

932 Get_default of Parameter HighViralLoadProtectionFactor doesn't work as expected #1068

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1211176
Ensure any instance of HighViralLoadProtectionFactor is always initia…
khoanguyen-dev Jul 11, 2024
55076af
Add test for set_space_per_member()
khoanguyen-dev Jul 12, 2024
93846dc
Add test for getPersonalProtectiveFactor
khoanguyen-dev Jul 17, 2024
8443b9b
Merge branch 'main' into 932-get_default-of-parameter-highviralloadpr…
khoanguyen-dev Jul 25, 2024
e0b76ab
Add ExposureType to HighViralLoadProtectionFactor
khoanguyen-dev Jul 25, 2024
e9e0171
Merge branch 'main' into 932-get_default-of-parameter-highviralloadpr…
khoanguyen-dev Aug 5, 2024
5746957
Add data from studies
khoanguyen-dev Aug 5, 2024
0e27395
Add data for abm_braunschweig.cpp
khoanguyen-dev Aug 5, 2024
bd61acf
Remove setting for HighViralLoadProtectionFactor in abm_braunschweig.cpp
khoanguyen-dev Aug 6, 2024
d161958
merge main and fix parameter (de)serialize
DavidKerkmann Aug 7, 2024
f82016d
Fix python bindings
DavidKerkmann Aug 7, 2024
2b4ef0f
Fix python bindings
DavidKerkmann Aug 7, 2024
a39c936
Fix python bindings v3
DavidKerkmann Aug 7, 2024
b9c3ffa
Fix pybinds finally
DavidKerkmann Aug 8, 2024
c178137
Merge branch 'main' into 932-get_default-of-parameter-highviralloadpr…
khoanguyen-dev Oct 11, 2024
3549688
Fix error with merge
khoanguyen-dev Oct 11, 2024
adc1215
Fix error with merge
khoanguyen-dev Oct 11, 2024
a2418d0
Add back correct python bindings of the TestParameters
khoanguyen-dev Oct 17, 2024
ed31f25
Small updates for a commenting error and pycode
khoanguyen-dev Oct 18, 2024
31e1a14
Apply suggestions from code review
DavidKerkmann Nov 14, 2024
e65e51c
merge with main
DavidKerkmann Nov 14, 2024
6a1f99d
Fix test
DavidKerkmann Nov 14, 2024
383ec18
Changed Vaccine to ExposureEvent
DavidKerkmann Nov 15, 2024
536bd2b
Fix import
DavidKerkmann Nov 15, 2024
77072b4
Fix naming
DavidKerkmann Nov 15, 2024
37a12ea
Fix errors
DavidKerkmann Nov 15, 2024
b3ea77d
Yet another fix
DavidKerkmann Nov 15, 2024
6839184
Changes according to discussion
DavidKerkmann Nov 15, 2024
600ab95
Fix error
DavidKerkmann Nov 15, 2024
02e6884
Fix pybinds
DavidKerkmann Nov 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cpp/models/abm/infection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ Infection::Infection(PersonalRandomNumberGenerator& rng, VirusVariant virus, Age
auto vl_params = params.get<ViralLoadDistributions>()[{virus, age}];
ScalarType high_viral_load_factor = 1;
if (latest_exposure.first != ExposureType::NoProtection) {
high_viral_load_factor -=
params.get<HighViralLoadProtectionFactor>()(init_date.days() - latest_exposure.second.days());
high_viral_load_factor -= params.get<HighViralLoadProtectionFactor>()[{latest_exposure.first, age, virus}](
init_date.days() - latest_exposure.second.days());
}
m_viral_load.peak = vl_params.viral_load_peak.get_distribution_instance()(rng, vl_params.viral_load_peak.params) *
high_viral_load_factor;
Expand Down
9 changes: 5 additions & 4 deletions cpp/models/abm/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,14 @@ struct SeverityProtectionFactor {
};

/**
* @brief Personal protective factor against high viral load. Its value is between 0 and 1.
* @brief Personal protective factor against high viral load, which depends on #ExposureType,
* #AgeGroup and #VirusVariant. Its value is between 0 and 1.
*/
struct HighViralLoadProtectionFactor {
using Type = InputFunctionForProtectionLevel;
static auto get_default()
using Type = CustomIndexArray<InputFunctionForProtectionLevel, ExposureType, AgeGroup, VirusVariant>;
static auto get_default(AgeGroup size)
DavidKerkmann marked this conversation as resolved.
Show resolved Hide resolved
{
return Type([](ScalarType /*days*/) -> ScalarType {
return Type({ExposureType::Count, size, VirusVariant::Count}, [](ScalarType /*days*/) -> ScalarType {
return 0;
});
DavidKerkmann marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
6 changes: 0 additions & 6 deletions cpp/simulations/abm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,6 @@ void set_parameters(mio::abm::Parameters params)

params.set<mio::abm::IncubationPeriod>({{mio::abm::VirusVariant::Count, mio::AgeGroup(num_age_groups)}, 4.});

// Set protection level from high viral load. Information based on: https://doi.org/10.1093/cid/ciaa886
params.get<mio::abm::HighViralLoadProtectionFactor>() = [](ScalarType days) -> ScalarType {
return mio::linear_interpolation_of_data_set<ScalarType, ScalarType>(
{{0, 0.863}, {1, 0.969}, {7, 0.029}, {10, 0.002}, {14, 0.0014}, {21, 0}}, days);
};

//0-4
params.get<mio::abm::InfectedNoSymptomsToSymptoms>()[{mio::abm::VirusVariant::Wildtype, age_group_0_to_4}] = 0.276;
params.get<mio::abm::InfectedNoSymptomsToRecovered>()[{mio::abm::VirusVariant::Wildtype, age_group_0_to_4}] = 0.092;
Expand Down
6 changes: 0 additions & 6 deletions cpp/simulations/abm_braunschweig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,6 @@ void set_parameters(mio::abm::Parameters params)

params.set<mio::abm::IncubationPeriod>({{mio::abm::VirusVariant::Count, mio::AgeGroup(num_age_groups)}, 4.});

// Set protection level from high viral load. Information based on: https://doi.org/10.1093/cid/ciaa886
params.get<mio::abm::HighViralLoadProtectionFactor>() = [](ScalarType days) -> ScalarType {
return mio::linear_interpolation_of_data_set<ScalarType, ScalarType>(
{{0, 0.863}, {1, 0.969}, {7, 0.029}, {10, 0.002}, {14, 0.0014}, {21, 0}}, days);
};

//0-4
params.get<mio::abm::InfectedNoSymptomsToSymptoms>()[{mio::abm::VirusVariant::Wildtype, age_group_0_to_4}] = 0.276;
params.get<mio::abm::InfectedNoSymptomsToRecovered>()[{mio::abm::VirusVariant::Wildtype, age_group_0_to_4}] = 0.092;
Expand Down
58 changes: 35 additions & 23 deletions cpp/tests/test_abm_infection.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (C) 2020-2024 MEmilio
*
* Authors: David Kerkmann
* Authors: David Kerkmann, Khoa Nguyen
*
* Contact: Martin J. Kuehn <[email protected]>
*
Expand Down Expand Up @@ -82,7 +82,8 @@ TEST(TestInfection, init)
virus_variant_test}] = [](ScalarType days) -> ScalarType {
return mio::linear_interpolation_of_data_set<ScalarType, ScalarType>({{0, 0.91}, {30, 0.81}}, days);
};
params.get<mio::abm::HighViralLoadProtectionFactor>() = [](ScalarType days) -> ScalarType {
params.get<mio::abm::HighViralLoadProtectionFactor>()[{mio::abm::ExposureType::GenericVaccine, age_group_test,
virus_variant_test}] = [](ScalarType days) -> ScalarType {
return mio::linear_interpolation_of_data_set<ScalarType, ScalarType>({{0, 0.91}, {30, 0.81}}, days);
};
auto infection_w_previous_exp =
Expand Down Expand Up @@ -183,12 +184,15 @@ TEST(TestInfection, getPersonalProtectiveFactor)

mio::abm::Parameters params = mio::abm::Parameters(num_age_groups);
// Test default parameter functions
auto defaut_infection_protection = params.get<mio::abm::InfectionProtectionFactor>()[{
auto defaut_infection_protection = params.get<mio::abm::InfectionProtectionFactor>()[{
mio::abm::ExposureType::GenericVaccine, mio::AgeGroup(0), mio::abm::VirusVariant::Wildtype}](0);
auto defaut_severity_protection = params.get<mio::abm::SeverityProtectionFactor>()[{
auto defaut_severity_protection = params.get<mio::abm::SeverityProtectionFactor>()[{
mio::abm::ExposureType::GenericVaccine, mio::AgeGroup(0), mio::abm::VirusVariant::Wildtype}](0);
ASSERT_NEAR(defaut_infection_protection, 0, 0.0001);
ASSERT_NEAR(defaut_severity_protection, 0, 0.0001);
auto defaut_high_viral_load_protection = params.get<mio::abm::HighViralLoadProtectionFactor>()[{
mio::abm::ExposureType::GenericVaccine, mio::AgeGroup(0), mio::abm::VirusVariant::Wildtype}](0);
EXPECT_NEAR(defaut_infection_protection, 0, 0.0001);
EXPECT_NEAR(defaut_severity_protection, 0, 0.0001);
EXPECT_NEAR(defaut_high_viral_load_protection, 0, 0.0001);

// Test linear interpolation with one node
mio::set_log_level(mio::LogLevel::critical); //this throws an error either way
Expand All @@ -198,7 +202,7 @@ TEST(TestInfection, getPersonalProtectiveFactor)
return mio::linear_interpolation_of_data_set<ScalarType, ScalarType>({{2, 0.91}}, days);
};
auto t = mio::abm::TimePoint(6 * 24 * 60 * 60);
ASSERT_NEAR(person.get_protection_factor(t, mio::abm::VirusVariant::Wildtype, params), 0, 0.001);
EXPECT_NEAR(person.get_protection_factor(t, mio::abm::VirusVariant::Wildtype, params), 0, 0.001);
mio::set_log_level(mio::LogLevel::warn); //this throws an error either way
params.get<mio::abm::InfectionProtectionFactor>()[{mio::abm::ExposureType::GenericVaccine, person.get_age(),
mio::abm::VirusVariant::Wildtype}] =
Expand All @@ -210,7 +214,9 @@ TEST(TestInfection, getPersonalProtectiveFactor)
[](ScalarType days) -> ScalarType {
return mio::linear_interpolation_of_data_set<ScalarType, ScalarType>({{2, 0.91}, {30, 0.81}}, days);
};
params.get<mio::abm::HighViralLoadProtectionFactor>() = [](ScalarType days) -> ScalarType {
params.get<mio::abm::HighViralLoadProtectionFactor>()[{mio::abm::ExposureType::GenericVaccine, person.get_age(),
mio::abm::VirusVariant::Wildtype}] =
[](ScalarType days) -> ScalarType {
return mio::linear_interpolation_of_data_set<ScalarType, ScalarType>({{2, 0.91}, {30, 0.81}}, days);
};

Expand All @@ -219,47 +225,53 @@ TEST(TestInfection, getPersonalProtectiveFactor)
auto infection_protection_factor = params.get<mio::abm::InfectionProtectionFactor>()[{
latest_protection.first, age_group_15_to_34, mio::abm::VirusVariant::Wildtype}](
t.days() - latest_protection.second.days());
ASSERT_NEAR(infection_protection_factor, 0.91, 0.0001);
ASSERT_NEAR(person.get_protection_factor(t, mio::abm::VirusVariant::Wildtype, params), 0.91, 0.0001);
EXPECT_NEAR(infection_protection_factor, 0.91, 0.0001);
EXPECT_NEAR(person.get_protection_factor(t, mio::abm::VirusVariant::Wildtype, params), 0.91, 0.0001);

t = mio::abm::TimePoint(0) + mio::abm::days(15);
infection_protection_factor = params.get<mio::abm::InfectionProtectionFactor>()[{
latest_protection.first, age_group_15_to_34, mio::abm::VirusVariant::Wildtype}](
t.days() - latest_protection.second.days());
ASSERT_NEAR(infection_protection_factor, 0.8635, 0.0001);
ASSERT_NEAR(person.get_protection_factor(t, mio::abm::VirusVariant::Wildtype, params), 0.8635, 0.0001);
EXPECT_NEAR(infection_protection_factor, 0.8635, 0.0001);
EXPECT_NEAR(person.get_protection_factor(t, mio::abm::VirusVariant::Wildtype, params), 0.8635, 0.0001);

t = mio::abm::TimePoint(0) + mio::abm::days(40);
infection_protection_factor = params.get<mio::abm::InfectionProtectionFactor>()[{
latest_protection.first, age_group_15_to_34, mio::abm::VirusVariant::Wildtype}](
t.days() - latest_protection.second.days());
ASSERT_NEAR(infection_protection_factor, 0, 0.0001);
ASSERT_NEAR(person.get_protection_factor(t, mio::abm::VirusVariant::Wildtype, params), 0, 0.0001);
EXPECT_NEAR(infection_protection_factor, 0, 0.0001);
EXPECT_NEAR(person.get_protection_factor(t, mio::abm::VirusVariant::Wildtype, params), 0, 0.0001);

// Test Parameter SeverityProtectionFactor
t = mio::abm::TimePoint(0) + mio::abm::days(2);
auto severity_protection_factor = params.get<mio::abm::SeverityProtectionFactor>()[{
latest_protection.first, age_group_15_to_34, mio::abm::VirusVariant::Wildtype}](
t.days() - latest_protection.second.days());
ASSERT_NEAR(severity_protection_factor, 0.91, 0.0001);
EXPECT_NEAR(severity_protection_factor, 0.91, 0.0001);

t = mio::abm::TimePoint(0) + mio::abm::days(15);
severity_protection_factor = params.get<mio::abm::SeverityProtectionFactor>()[{
latest_protection.first, age_group_15_to_34, mio::abm::VirusVariant::Wildtype}](
t.days() - latest_protection.second.days());
ASSERT_NEAR(severity_protection_factor, 0.8635, 0.0001);
EXPECT_NEAR(severity_protection_factor, 0.8635, 0.0001);

t = mio::abm::TimePoint(0) + mio::abm::days(40);
severity_protection_factor = params.get<mio::abm::SeverityProtectionFactor>()[{
latest_protection.first, age_group_15_to_34, mio::abm::VirusVariant::Wildtype}](
t.days() - latest_protection.second.days());
ASSERT_NEAR(severity_protection_factor, 0, 0.0001);
EXPECT_NEAR(severity_protection_factor, 0, 0.0001);

// Test Parameter HighViralLoadProtectionFactor
t = mio::abm::TimePoint(0) + mio::abm::days(2);
ASSERT_NEAR(params.get<mio::abm::HighViralLoadProtectionFactor>()(t.days()), 0.91, 0.0001);
t = mio::abm::TimePoint(0) + mio::abm::days(15);
ASSERT_NEAR(params.get<mio::abm::HighViralLoadProtectionFactor>()(t.days()), 0.8635, 0.0001);
t = mio::abm::TimePoint(0) + mio::abm::days(40);
ASSERT_NEAR(params.get<mio::abm::HighViralLoadProtectionFactor>()(t.days()), 0, 0.0001);
t = mio::abm::TimePoint(0) + mio::abm::days(2);
auto high_viral_protection_factor = params.get<mio::abm::HighViralLoadProtectionFactor>()[{
latest_protection.first, age_group_15_to_34, mio::abm::VirusVariant::Wildtype}](t.days());
EXPECT_NEAR(high_viral_protection_factor, 0.91, 0.0001);
t = mio::abm::TimePoint(0) + mio::abm::days(15);
high_viral_protection_factor = params.get<mio::abm::HighViralLoadProtectionFactor>()[{
latest_protection.first, age_group_15_to_34, mio::abm::VirusVariant::Wildtype}](t.days());
EXPECT_NEAR(high_viral_protection_factor, 0.8635, 0.0001);
t = mio::abm::TimePoint(0) + mio::abm::days(40);
high_viral_protection_factor = params.get<mio::abm::HighViralLoadProtectionFactor>()[{
latest_protection.first, age_group_15_to_34, mio::abm::VirusVariant::Wildtype}](t.days());
EXPECT_NEAR(high_viral_protection_factor, 0, 0.0001);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
//Includes from MEmilio
#include "abm/simulation.h"

#include "pybind11/pybind11.h"
#include "pybind11/attr.h"
#include "pybind11/cast.h"
#include "pybind11/pybind11.h"
#include "pybind11/operators.h"
#include <cstdint>
#include <type_traits>
Expand Down Expand Up @@ -72,23 +72,6 @@ PYBIND11_MODULE(_simulation_abm, m)
.value("Antigen", mio::abm::TestType::Antigen)
.value("PCR", mio::abm::TestType::PCR);

pymio::bind_class<mio::abm::TestParameters, pymio::EnablePickling::Never>(m, "TestParameters")
.def(py::init<double, double>())
.def_readwrite("sensitivity", &mio::abm::TestParameters::sensitivity)
.def_readwrite("specificity", &mio::abm::TestParameters::specificity);

pymio::bind_CustomIndexArray<mio::UncertainValue<double>, mio::abm::VirusVariant, mio::AgeGroup>(
m, "_AgeParameterArray");
pymio::bind_CustomIndexArray<mio::abm::TestParameters, mio::abm::TestType>(m, "_TestData");
pymio::bind_Index<mio::abm::ExposureType>(m, "ExposureTypeIndex");
pymio::bind_ParameterSet<mio::abm::ParametersBase, pymio::EnablePickling::Never>(m, "ParametersBase");
pymio::bind_class<mio::abm::Parameters, pymio::EnablePickling::Never, mio::abm::ParametersBase>(m, "Parameters")
.def(py::init<int>())
.def("check_constraints", &mio::abm::Parameters::check_constraints);

pymio::bind_ParameterSet<mio::abm::LocalInfectionParameters, pymio::EnablePickling::Never>(
m, "LocalInfectionParameters")
.def(py::init<size_t>());

pymio::bind_class<mio::abm::TimeSpan, pymio::EnablePickling::Never>(m, "TimeSpan")
.def(py::init<int>(), py::arg("seconds") = 0)
Expand Down Expand Up @@ -134,6 +117,26 @@ PYBIND11_MODULE(_simulation_abm, m)
.def(py::self += mio::abm::TimeSpan{})
.def(py::self - mio::abm::TimeSpan{})
.def(py::self -= mio::abm::TimeSpan{});

pymio::bind_class<mio::abm::TestParameters, pymio::EnablePickling::Never>(m, "TestParameters")
.def(py::init<double, double, mio::abm::TimeSpan, mio::abm::TestType>())
.def_readwrite("sensitivity", &mio::abm::TestParameters::sensitivity)
.def_readwrite("specificity", &mio::abm::TestParameters::specificity)
.def_readwrite("required_time", &mio::abm::TestParameters::required_time)
.def_readwrite("type", &mio::abm::TestParameters::type);

pymio::bind_CustomIndexArray<mio::UncertainValue<double>, mio::abm::VirusVariant, mio::AgeGroup>(
m, "_AgeParameterArray");
pymio::bind_CustomIndexArray<mio::abm::TestParameters, mio::abm::TestType>(m, "_TestData");
pymio::bind_Index<mio::abm::ExposureType>(m, "ExposureTypeIndex");
pymio::bind_ParameterSet<mio::abm::ParametersBase, pymio::EnablePickling::Never>(m, "ParametersBase");
pymio::bind_class<mio::abm::Parameters, pymio::EnablePickling::Never, mio::abm::ParametersBase>(m, "Parameters")
.def(py::init<int>())
.def("check_constraints", &mio::abm::Parameters::check_constraints);
reneSchm marked this conversation as resolved.
Show resolved Hide resolved

pymio::bind_ParameterSet<mio::abm::LocalInfectionParameters, pymio::EnablePickling::Never>(
m, "LocalInfectionParameters")
.def(py::init<size_t>());

pymio::bind_class<mio::abm::LocationId, pymio::EnablePickling::Never>(m, "LocationId")
.def(py::init<uint32_t>(), py::arg("id"))
Expand Down
Loading