Fix:Add agenttype_reporters support to batch_run#3095
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where agenttype_reporters data was silently dropped during batch_run() execution, making it impossible to collect type-specific agent data in parameter sweeps and parallel runs.
Key changes:
- Added
agenttype_reportersdata collection logic to_collect_data()inmesa/batchrunner.py - Added two comprehensive tests validating agenttype_reporters functionality in batch runs
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| mesa/batchrunner.py | Extended _collect_data() to iterate through and collect _agenttype_records alongside existing _agent_records collection |
| tests/test_batch_run.py | Added test_batch_run_agenttype_reporters() and test_batch_run_agenttype_and_agent_reporters() to verify agenttype data collection works correctly in batch runs |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Performance benchmarks:
|
There was a problem hiding this comment.
Thanks for fixing this, this was really long overdue and probably should have been caught directly when implementing #2300.
It does show the fragility of our batch run and datacollector functions, and the often implicit interaction between them. That might support #2972 (comment).
For now, this is a great fix, and we will include/backport this in a new 3.x release.
|
Thanks @EwoutH for considering my contribution! I would like to work more on this project, it would be great if you can suggest me something that I can pick up. I would be happy to implement. |
Summary
Fixes
agenttype_reportersnot collecting data when usingbatch_run(), making it impossible to use selective agent data collection with parameter sweeps and parallel execution.Bug / Issue
Closes #2804
Users reported that
agenttype_reportersworked perfectly in regular model runs but failed to collect data when usingbatch_run().What was expected: Data from
agenttype_reportersshould appear in batch_run results, just likeagent_reportersdata does.What actually happened: The agenttype data was silently dropped - no errors just missing columns in the results DataFrame.
Implementation
The issue was in the
_collect_data()helper function inmesa/batchrunner.py. This function extracts data from the DataCollector after model execution, but it only looked at_agent_recordsand completely ignored_agenttype_records.Changes made:
dc._agenttype_recordsin_collect_data()all_agents_datalist for consistent outputIt handles all edge cases:
agent_reportersagenttype_reportersTesting
Added test coverage in
tests/test_batch_run.py:test_batch_run_agenttype_reporters(): Verifies agenttype_reporters data is collected correctlytest_batch_run_agenttype_and_agent_reporters(): Tests mixed usage of both reporter typesTest Results: