Skip to content

1210 abm paper logger - #1507

Open
xsaschako wants to merge 43 commits into
mainfrom
1210-abm-paper-logger
Open

xsaschako wants to merge 43 commits into
mainfrom
1210-abm-paper-logger

Conversation

@xsaschako

@xsaschako xsaschako commented Mar 9, 2026 •

Copy link
Copy Markdown
Member

Changes and Information

Please briefly list the changes (main added features, changed items, or corrected bugs) made:

  • ad vizualizations
  • added logger
  • added one logger to the parameter study example

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

  • Every addressed issue is linked (use the "Closes #ISSUE" keyword below)
  • New code adheres to coding guidelines
  • No large data files have been added (files should in sum not exceed 100 KB, avoid PDFs, Word docs, etc.)
  • Tests are added for new functionality and a local test run was successful (with and without OpenMP)
  • Appropriate documentation within the code (Doxygen) for new functionality has been added in the code
  • Appropriate external documentation (ReadTheDocs) for new functionality has been added to the online documentation
  • Proper attention to licenses, especially no new third-party software with conflicting license has been added
  • (For ABM development) Checked benchmark results and ran and posted a local test above from before and after development to ensure performance is monitored.

Checks by code reviewer(s)

  • Corresponding issue(s) is/are linked and addressed
  • Code is clean of development artifacts (no deactivated or commented code lines, no debugging printouts, etc.)
  • Appropriate unit tests have been added, CI passes, code coverage and performance is acceptable (did not decrease)
  • No large data files added in the whole history of commits(files should in sum not exceed 100 KB, avoid PDFs, Word docs, etc.)
  • On merge, add 2-5 lines with the changes (main added features, changed items, or corrected bugs) to the merge-commit-message. This can be taken from the briefly-list-the-changes above (best case) or the separate commit messages (worst case).

xsaschako added 30 commits May 27, 2025 21:02
…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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 unused LogInfectionStatePerAgeGroup) logger structs in common_abm_loggers.h
  • Added m_t_prev tracking to Simulation class and integrated the new detailed logger into ResultSimulation
  • Updated abm_parameter_study.cpp example to collect and save both aggregated and detailed results in separate subdirectories
  • Added new Python plotting module plotAbmInfectionStates.py with 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.

Comment thread cpp/models/abm/result_simulation.h Outdated
Comment thread pycode/memilio-plot/tests/test_plot_plotAbmInfectionStates.py Outdated
Comment thread cpp/models/abm/result_simulation.h Outdated
Comment thread pycode/memilio-plot/memilio/plot/plotAbmInfectionStates.py Outdated
Comment thread pycode/memilio-plot/memilio/plot/plotAbmInfectionStates.py
Comment thread cpp/models/abm/result_simulation.h Outdated
Comment thread pycode/memilio-plot/memilio/plot/plotAbmInfectionStates.py Outdated
Comment thread cpp/models/abm/simulation.h Outdated
Comment thread cpp/models/abm/common_abm_loggers.h
Comment thread cpp/examples/abm_parameter_study.cpp Outdated
@codecov

codecov Bot commented Mar 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.47%. Comparing base (0888d6c) to head (64998ea).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DavidKerkmann DavidKerkmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I have a few comments which should be discussed/resolved.
Furthermore, I think fixing #1406 here would make a lot of sense.

Comment thread cpp/models/abm/simulation.h Outdated
{
return m_t_prev;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's equivalent, but I somewhat prefer to use 'get_time() - get_time_step()' and make a method for the latter one.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread cpp/examples/abm_parameter_study.cpp Outdated
Comment on lines +253 to +260

// 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()));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this in mio::unused?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there was no good reason. Both save_result calls now check the result and print the error, like in ode_secir_parameter_study.cpp.

Comment thread cpp/models/abm/result_simulation.h Outdated
Comment on lines +67 to +72
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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to have a similar test for the other new logger logInfectionStatePerLocationTypePerAgeGroup?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@xsaschako xsaschako left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See changes

also I made a new PR request for #1406
for this logger, which is the inly one that uses it, LogInfectionState, to be independent of it.

Comment thread cpp/models/abm/simulation.h Outdated
{
return m_t_prev;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread cpp/examples/abm_parameter_study.cpp Outdated
Comment on lines +253 to +260

// 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()));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there was no good reason. Both save_result calls now check the result and print the error, like in ode_secir_parameter_study.cpp.

Comment thread cpp/models/abm/result_simulation.h Outdated
Comment on lines +67 to +72
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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that’s covered by TestSimulation.logInfectionStatePerAgeGroup. I also reworked the test for LogInfectionPerLocationTypePerAgeGroup (see below).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ABM visualization from paper to MEmilio-plot ABM Paper: Logger

3 participants