Repository navigation
Conversation
…rove function structure, and add new plotting capabilities for location types.
… documentation, and improve function argument handling for better usability
…st cases for loading H5 results and plotting functions
…ments for clarity
…th comparison plots
…arguments for better flexibility
…from-paper-to-memilio-plot
…into 1210-abm-paper-logger
There was a problem hiding this comment.
Pull request overview
This PR adds a new ABM (Agent-Based Model) logger for tracking new infections per location type per age group (LogInfectionPerLocationTypePerAgeGroup), integrates it into ResultSimulation via a new history_detailed history and get_result_detailed() method, updates the abm_parameter_study.cpp example to save both standard and detailed results, and introduces a new Python plotting module (plotAbmInfectionStates.py) with tests for visualizing the results.
Changes:
- Added
LogInfectionPerLocationTypePerAgeGroup(and unusedLogInfectionStatePerAgeGroup) logger structs incommon_abm_loggers.h - Added
m_t_prevtracking toSimulationclass and integrated the new detailed logger intoResultSimulation - Updated
abm_parameter_study.cppexample to collect and save both aggregated and detailed results in separate subdirectories - Added new Python plotting module
plotAbmInfectionStates.pywith functions to visualize infection states and infection-by-location-type data, plus unit tests
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 16 comments.
Show a summary per file
| File | Description |
|---|---|
cpp/models/abm/simulation.h |
Adds m_t_prev member and get_prev_time() to track previous simulation time step, needed by new logger |
cpp/models/abm/common_abm_loggers.h |
Introduces two new logger structs for per-age-group data; one is unused |
cpp/models/abm/result_simulation.h |
Adds history_detailed and get_result_detailed() using the new logger; changes get_result() return type |
cpp/examples/abm_parameter_study.cpp |
Updated example to run 10 simulations over 10 days and save both standard and detailed results |
pycode/memilio-plot/memilio/plot/plotAbmInfectionStates.py |
New Python module for loading and plotting ABM infection state and location-type results |
pycode/memilio-plot/tests/test_plot_plotAbmInfectionStates.py |
Unit tests for the new Python plotting module |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1507 +/- ##
==========================================
+ Coverage 97.44% 97.47% +0.02%
==========================================
Files 190 190
Lines 16035 16066 +31
==========================================
+ Hits 15626 15661 +35
+ Misses 409 405 -4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
DavidKerkmann
left a comment
There was a problem hiding this comment.
Thanks, I have a few comments which should be discussed/resolved.
Furthermore, I think fixing #1406 here would make a lot of sense.
| { | ||
| return m_t_prev; | ||
| } | ||
|
|
There was a problem hiding this comment.
It's equivalent, but I somewhat prefer to use 'get_time() - get_time_step()' and make a method for the latter one.
There was a problem hiding this comment.
Good point! I ended up removing m_t_prev and get_prev_time() entirely and the logger now keeps track of its previous log time itself, so Simulation is unchanged compared to main.
Also made changes, see below for the logger, since it was buggy since it logged the locations after the tilmestep (we had this bug already I kind of remember).
|
|
||
| // save_result splits each row into num_groups groups. Both results are a single vector per time point, | ||
| // so they are written as one group each, whose "Total" holds all entries of that vector. Passing the | ||
| // number of age groups here would instead split those entries across that many groups. | ||
| mio::unused(mio::save_result(ensemble_percentiles, {0}, 1, | ||
| (result_dir_standard / fmt::format("Results_p{:02d}.h5", p)).string())); | ||
| mio::unused(mio::save_result(ensemble_percentiles_detailed, {0}, 1, | ||
| (result_dir_detailed / fmt::format("Results_p{:02d}.h5", p)).string())); |
There was a problem hiding this comment.
Why is this in mio::unused?
There was a problem hiding this comment.
there was no good reason. Both save_result calls now check the result and print the error, like in ode_secir_parameter_study.cpp.
| mio::History<TimeSeriesWriter, LogInfectionState> history{ | ||
| Eigen::Index(InfectionState::Count)}; ///< History used to create the result TimeSeries. | ||
|
|
||
| mio::History<TimeSeriesWriter, LogInfectionPerLocationTypePerAgeGroup> history_detailed{ | ||
| Eigen::Index(LocationType::Count) * | ||
| this->get_model().parameters.get_num_groups()}; ///< History used to create the detailed result TimeSeries. |
There was a problem hiding this comment.
I find it a bit unclean that we have a hardcoded result_simulation which is tailored to the one example (parameter study) with these particular loggers/histories.
It would be better to have a framework that allows us to do the parameter studies in a way where the make_model and the history(-ies) is/are provided instead.
There was a problem hiding this comment.
Agreed. I reverted ResultSimulation to main and moved the study-specific histories into a small StudySimulation wrapper in the example.
| EXPECT_EQ(detailed.get_value(1)[index], 1.0); | ||
| } | ||
|
|
||
| TEST(TestSimulation, logInfectionStatePerAgeGroup) |
There was a problem hiding this comment.
Would it make sense to have a similar test for the other new logger logInfectionStatePerLocationTypePerAgeGroup?
There was a problem hiding this comment.
Yes, that’s covered by TestSimulation.logInfectionStatePerAgeGroup. I also reworked the test for LogInfectionPerLocationTypePerAgeGroup (see below).
…on logger - Revert ResultSimulation and Simulation to main; the parameter study example now uses a small StudySimulation wrapper holding its histories. - LogInfectionPerLocationTypePerAgeGroup keeps its previous log time and location types itself and attributes new infections to the location where the interaction happened, not to where the person moved afterwards. - Gather the detailed results across MPI ranks by returning them from the processing lambda. - Handle the IOResult of save_result instead of discarding it. - LogInfectionState counts in a single pass over all persons (#1406).
| { | ||
| return m_t_prev; | ||
| } | ||
|
|
There was a problem hiding this comment.
Good point! I ended up removing m_t_prev and get_prev_time() entirely and the logger now keeps track of its previous log time itself, so Simulation is unchanged compared to main.
Also made changes, see below for the logger, since it was buggy since it logged the locations after the tilmestep (we had this bug already I kind of remember).
|
|
||
| // save_result splits each row into num_groups groups. Both results are a single vector per time point, | ||
| // so they are written as one group each, whose "Total" holds all entries of that vector. Passing the | ||
| // number of age groups here would instead split those entries across that many groups. | ||
| mio::unused(mio::save_result(ensemble_percentiles, {0}, 1, | ||
| (result_dir_standard / fmt::format("Results_p{:02d}.h5", p)).string())); | ||
| mio::unused(mio::save_result(ensemble_percentiles_detailed, {0}, 1, | ||
| (result_dir_detailed / fmt::format("Results_p{:02d}.h5", p)).string())); |
There was a problem hiding this comment.
there was no good reason. Both save_result calls now check the result and print the error, like in ode_secir_parameter_study.cpp.
| mio::History<TimeSeriesWriter, LogInfectionState> history{ | ||
| Eigen::Index(InfectionState::Count)}; ///< History used to create the result TimeSeries. | ||
|
|
||
| mio::History<TimeSeriesWriter, LogInfectionPerLocationTypePerAgeGroup> history_detailed{ | ||
| Eigen::Index(LocationType::Count) * | ||
| this->get_model().parameters.get_num_groups()}; ///< History used to create the detailed result TimeSeries. |
There was a problem hiding this comment.
Agreed. I reverted ResultSimulation to main and moved the study-specific histories into a small StudySimulation wrapper in the example.
| EXPECT_EQ(detailed.get_value(1)[index], 1.0); | ||
| } | ||
|
|
||
| TEST(TestSimulation, logInfectionStatePerAgeGroup) |
There was a problem hiding this comment.
Yes, that’s covered by TestSimulation.logInfectionStatePerAgeGroup. I also reworked the test for LogInfectionPerLocationTypePerAgeGroup (see below).
Changes and Information
Please briefly list the changes (main added features, changed items, or corrected bugs) made:
If need be, add additional information and what the reviewer should look out for in particular:
Merge Request - Guideline Checklist
Please check our git workflow. Use the draft feature if the Pull Request is not yet ready to review.
Checks by code author
Checks by code reviewer(s)