-
Notifications
You must be signed in to change notification settings - Fork 19
1108 Streamline code for reading in data in model specific IO files #1163
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
Merged
HenrZu
merged 9 commits into
main
from
1108-Reuse-more-code-in-Initialization-Process-of-ODE-models
Jun 18, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
55bda99
divi io model independent, get_region_id also
HenrZu a841c90
read population data
HenrZu ddc341f
Merge branch 'main' into 1108-Reuse-more-code-in-Initialization-Proce…
HenrZu 6aba288
[ci skip] doc: 2024 -> 2025
HenrZu 6932e59
Merge branch 'main' into 1108-Reuse-more-code-in-Initialization-Proce…
HenrZu 3e7c55c
add tests for cov
HenrZu 0f0d498
Apply suggestions from code review
HenrZu f8f729f
[ci skip] rm space in doc, add param names
HenrZu fd8fb0e
typo
HenrZu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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."); | ||
| } | ||
|
|
||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.