Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions cpp/memilio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ add_library(memilio
io/history.h
io/mobility_io.h
io/mobility_io.cpp
io/parameters_io.h
io/parameters_io.cpp
io/result_io.h
io/result_io.cpp
io/epi_data.h
Expand Down
72 changes: 72 additions & 0 deletions cpp/memilio/io/parameters_io.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2020-2025 MEmilio
*
* Authors: Henrik Zunker
*
* 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.
*/

#include "memilio/config.h"

#ifdef MEMILIO_HAS_JSONCPP

#include "memilio/io/epi_data.h"
#include "memilio/io/result_io.h"
#include "json/value.h"
#include <string>
#include <vector>

namespace mio
{
IOResult<std::vector<std::vector<double>>> read_population_data(const std::vector<PopulationDataEntry>& population_data,
const std::vector<int>& vregion)
{
std::vector<std::vector<double>> vnum_population(
vregion.size(), std::vector<double>(ConfirmedCasesDataEntry::age_group_names.size(), 0.0));

for (auto&& county_entry : population_data) {
//accumulate population of states or country from population of counties
if (!county_entry.county_id && !county_entry.district_id) {
return failure(StatusCode::InvalidFileFormat, "File with county population expected.");
}
//find region that this county belongs to
//all counties belong to the country (id = 0)
auto it = std::find_if(vregion.begin(), vregion.end(), [&county_entry](auto r) {
return r == 0 ||
(county_entry.county_id &&
regions::StateId(r) == regions::get_state_id(int(*county_entry.county_id))) ||
(county_entry.county_id && regions::CountyId(r) == *county_entry.county_id) ||
(county_entry.district_id && regions::DistrictId(r) == *county_entry.district_id);
});
if (it != vregion.end()) {
auto region_idx = size_t(it - vregion.begin());
auto& num_population = vnum_population[region_idx];
for (size_t age = 0; age < num_population.size(); age++) {
num_population[age] += county_entry.population[AgeGroup(age)];
}
}
}

return success(vnum_population);
}

IOResult<std::vector<std::vector<double>>> read_population_data(const std::string& path,
const std::vector<int>& vregion)
{
BOOST_OUTCOME_TRY(auto&& population_data, mio::read_population_data(path));
return read_population_data(population_data, vregion);
}
} // namespace mio
#endif //MEMILIO_HAS_JSONCPP
143 changes: 143 additions & 0 deletions cpp/memilio/io/parameters_io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright (C) 2020-2025 MEmilio
*
* Authors: Henrik Zunker
*
* 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.
*/
#ifndef MEMILIO_IO_PARAMETER_H
#define MEMILIO_IO_PARAMETER_H

#include "memilio/config.h"

#ifdef MEMILIO_HAS_JSONCPP

#include "memilio/io/epi_data.h"
#include "memilio/io/result_io.h"
#include "json/value.h"
#include <string>
#include <vector>

namespace mio
{
/**
* @brief Gets the region ID (county, state, or district) of an EpiDataEntry. If none are available,
* it defaults to 0 which is representing the whole country.
*
* If none are available, it defaults to 0 which is representing the whole country.
*
* @tparam EpiDataEntry The type of the data entry.
* @param data_entry The (RKI) data entry to extract the region ID from.
* @return The region ID as integer, or 0 if no specific region information is available.
*/
template <class EpiDataEntry>
int get_region_id(const EpiDataEntry& data_entry)
{
return data_entry.county_id ? data_entry.county_id->get()
: (data_entry.state_id ? data_entry.state_id->get()
: (data_entry.district_id ? data_entry.district_id->get() : 0));
}

/**
* @brief Extracts the number of individuals in critical condition (ICU) for each region
* on a specified date from the provided DIVI data.
*
* @tparam FP Floating point type (default: double).
*
* @param[in] divi_data Vector of DIVI data entries containing date, region, and ICU information.
* @param[in] vregion Vector of region IDs for which the data is computed.
* @param[in] date Date for which the ICU data is computed.
* @param[in, out] vnum_icu Output vector containing the number of ICU cases for each region.
*
* @return An IOResult indicating success or failure.
*/
template <typename FP = double>
IOResult<void> compute_divi_data(const std::vector<DiviEntry>& divi_data, const std::vector<int>& vregion, Date date,
std::vector<FP>& vnum_icu)
{
auto max_date_entry = std::max_element(divi_data.begin(), divi_data.end(), [](auto&& a, auto&& b) {
return a.date < b.date;
});
if (max_date_entry == divi_data.end()) {
log_error("DIVI data is empty.");
return failure(StatusCode::InvalidValue, "DIVI data is empty.");
}
auto max_date = max_date_entry->date;
if (max_date < date) {
log_error("DIVI data does not contain the specified date.");
return failure(StatusCode::OutOfRange, "DIVI data does not contain the specified date.");

Check warning on line 80 in cpp/memilio/io/parameters_io.h

View check run for this annotation

Codecov / codecov/patch

cpp/memilio/io/parameters_io.h#L79-L80

Added lines #L79 - L80 were not covered by tests
}

for (auto&& entry : divi_data) {
auto it = std::find_if(vregion.begin(), vregion.end(), [&entry](auto r) {
return r == 0 || r == get_region_id(entry);
});
auto date_df = entry.date;
if (it != vregion.end() && date_df == date) {
auto region_idx = size_t(it - vregion.begin());
vnum_icu[region_idx] = entry.num_icu;
}
}

return success();
}

/**
* @brief Reads DIVI data from a file and computes the ICU data for specified regions and date.
*
* @tparam FP Floating point type (default: double).
*
* @param[in] path Path to the file containing DIVI data.
* @param[in] vregion Vector of region IDs for which the data is computed.
* @param[in] date Date for which the ICU data is computed.
* @param[in, out] vnum_icu Output vector containing the number of ICU cases for each region.
*
* @return An IOResult indicating success or failure.
*/
template <typename FP = double>
IOResult<void> read_divi_data(const std::string& path, const std::vector<int>& vregion, Date date,
std::vector<FP>& vnum_icu)
{
BOOST_OUTCOME_TRY(auto&& divi_data, mio::read_divi_data(path));
return compute_divi_data(divi_data, vregion, date, vnum_icu);
}

/**
* @brief Reads population data from a vector of population data entries.
*
* @param[in] population_data Vector of population data entries.
* @param[in] vregion Vector of keys representing the regions of interest.
* @return An IOResult containing a vector of vectors, where each inner vector represents the population
* distribution across age groups for a specific region, or an error if the function fails.
*/
IOResult<std::vector<std::vector<double>>> read_population_data(const std::vector<PopulationDataEntry>& population_data,
const std::vector<int>& vregion);

/**
* @brief Reads population data from census data.
*
* @param[in] path Path to the population data file.
* @param[in] vregion Vector of keys representing the regions of interest.
* @return An IOResult containing a vector of vectors, where each inner vector represents the population
* distribution across age groups for a specific region, or an error if the function fails.
*/
IOResult<std::vector<std::vector<double>>> read_population_data(const std::string& path,
const std::vector<int>& vregion);

} // namespace mio

#endif //MEMILIO_HAS_JSONCPP

#endif //MEMILIO_IO_PARAMETER_H
80 changes: 1 addition & 79 deletions cpp/models/ode_secir/parameters_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ GCC_CLANG_DIAGNOSTIC(ignored "-Wmaybe-uninitialized")

#include "ode_secir/parameters_io.h"
#include "memilio/io/epi_data.h"
#include "memilio/io/parameters_io.h"
#include "memilio/io/io.h"
#include "memilio/utils/stl_util.h"
#include "memilio/utils/date.h"
Expand All @@ -42,15 +43,6 @@ namespace osecir

namespace details
{
//district, county or state id of a data entry if available, 0 (for whole country) otherwise
//used to compare data entries to integer ids in STL algorithms
template <class EpiDataEntry>
int get_region_id(const EpiDataEntry& entry)
{
return entry.county_id
? entry.county_id->get()
: (entry.state_id ? entry.state_id->get() : (entry.district_id ? entry.district_id->get() : 0));
}
//overload for integers, so the comparison of data entry to integers is symmetric (required by e.g. equal_range)
int get_region_id(int id)
{
Expand Down Expand Up @@ -195,76 +187,6 @@ IOResult<void> read_confirmed_cases_data(
return success();
}

IOResult<void> read_divi_data(const std::string& path, const std::vector<int>& vregion, Date date,
std::vector<double>& vnum_icu)
{
BOOST_OUTCOME_TRY(auto&& divi_data, mio::read_divi_data(path));

auto max_date_entry = std::max_element(divi_data.begin(), divi_data.end(), [](auto&& a, auto&& b) {
return a.date < b.date;
});
if (max_date_entry == divi_data.end()) {
log_error("DIVI data file is empty.");
return failure(StatusCode::InvalidFileFormat, path + ", file is empty.");
}
auto max_date = max_date_entry->date;
if (max_date < date) {
log_error("Specified date does not exist in DIVI data.");
return failure(StatusCode::OutOfRange, path + ", specified date does not exist in DIVI data.");
}

for (auto&& entry : divi_data) {
auto it = std::find_if(vregion.begin(), vregion.end(), [&entry](auto r) {
return r == 0 || r == get_region_id(entry);
});
auto date_df = entry.date;
if (it != vregion.end() && date_df == date) {
auto region_idx = size_t(it - vregion.begin());
vnum_icu[region_idx] = entry.num_icu;
}
}

return success();
}

IOResult<std::vector<std::vector<double>>>
read_population_data(const std::string& path, const std::vector<int>& vregion, bool accumulate_age_groups)
{
BOOST_OUTCOME_TRY(auto&& population_data, mio::read_population_data(path, !accumulate_age_groups));
//if we set up the model for one age group, the population data should be read in with the
//age groups given in the population data json file and are accumulated later
//otherwise the populations are directly saved for the correct model age groups
size_t age_group_size = accumulate_age_groups ? PopulationDataEntry::age_group_names.size()
: ConfirmedCasesDataEntry::age_group_names.size();
std::vector<std::vector<double>> vnum_population(vregion.size(), std::vector<double>(age_group_size, 0.0));

for (auto&& entry : population_data) {
auto it = std::find_if(vregion.begin(), vregion.end(), [&entry](auto r) {
return r == 0 || (entry.county_id && regions::StateId(r) == regions::get_state_id(int(*entry.county_id))) ||
(entry.county_id && regions::CountyId(r) == *entry.county_id) ||
(entry.district_id && regions::DistrictId(r) == *entry.district_id);
});
if (it != vregion.end()) {
auto region_idx = size_t(it - vregion.begin());
auto& num_population = vnum_population[region_idx];
for (size_t age = 0; age < num_population.size(); age++) {
num_population[age] += entry.population[AgeGroup(age)];
}
}
}
if (accumulate_age_groups) {
std::vector<std::vector<double>> vnum_pop_acc(vregion.size(), std::vector<double>(1, 0));
for (size_t region = 0; region < vregion.size(); ++region) {
vnum_pop_acc[region][0] =
std::accumulate(vnum_population[region].begin(), vnum_population[region].end(), 0.0);
}
return success(vnum_pop_acc);
}
else {
return success(vnum_population);
}
}

} // namespace details
} // namespace osecir
} // namespace mio
Expand Down
27 changes: 3 additions & 24 deletions cpp/models/ode_secir/parameters_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "ode_secir/model.h"
#include "memilio/io/epi_data.h"
#include "memilio/io/parameters_io.h"
#include "memilio/io/result_io.h"

namespace mio
Expand Down Expand Up @@ -170,16 +171,6 @@ IOResult<void> set_confirmed_cases_data(std::vector<Model<FP>>& model, const std
return success();
}

/**
* @brief Reads number of ICU patients from DIVI register into Parameters.
* @param[in] path Path to DIVI file.
* @param[in] vregion Keys of the region of interest.
* @param date Date for which we initialize.
* @param vnum_icu Number of ICU patients.
*/
IOResult<void> read_divi_data(const std::string& path, const std::vector<int>& vregion, Date date,
std::vector<double>& vnum_icu);

/**
* @brief Sets populations data from DIVI register into Model.
* @tparam FP floating point data type, e.g., double.
Expand Down Expand Up @@ -225,16 +216,6 @@ IOResult<void> set_divi_data(std::vector<Model<FP>>& model, const std::string& p
return success();
}

/**
* @brief Reads population data from census data.
* @tparam FP floating point data type, e.g., double.
* @param[in] path Path to RKI file.
* @param[in] vregion Vector of keys of the regions of interest.
* @param[in] accumulate_age_groups Specifies whether population data should be accumulated to one age group.
*/
IOResult<std::vector<std::vector<double>>>
read_population_data(const std::string& path, const std::vector<int>& vregion, bool accumulate_age_groups = false);

/**
* @brief Sets population data from census data which has been read into num_population.
* @tparam FP floating point data type, e.g., double.
Expand Down Expand Up @@ -270,9 +251,7 @@ template <typename FP = double>
IOResult<void> set_population_data(std::vector<Model<FP>>& model, const std::string& path,
const std::vector<int>& vregion)
{
// Specifies whether population data should be accumulated to one age group.
const bool is_single_age_group = static_cast<size_t>(model[0].parameters.get_num_groups()) == 1;
BOOST_OUTCOME_TRY(const auto&& num_population, read_population_data(path, vregion, is_single_age_group));
BOOST_OUTCOME_TRY(const auto&& num_population, read_population_data(path, vregion));
BOOST_OUTCOME_TRY(set_population_data(model, num_population, vregion));
return success();
}
Expand Down Expand Up @@ -310,7 +289,7 @@ IOResult<void> export_input_data_county_timeseries(
std::vector<TimeSeries<double>> extrapolated_data(
region.size(), TimeSeries<double>::zero(num_days + 1, (size_t)InfectionState::Count * num_age_groups));

BOOST_OUTCOME_TRY(auto&& num_population, details::read_population_data(population_data_path, region));
BOOST_OUTCOME_TRY(auto&& num_population, mio::read_population_data(population_data_path, region));
BOOST_OUTCOME_TRY(auto&& case_data, mio::read_confirmed_cases_data(confirmed_cases_path));

for (int t = 0; t <= num_days; ++t) {
Expand Down
Loading