Fix: IndexError in batch_run with sparse data collection#2988
Fix: IndexError in batch_run with sparse data collection#2988
Conversation
|
Performance benchmarks:
|
ee4f9b5 to
43b87aa
Compare
|
Hey, isn't it better to simply use |
|
@falloficarus22 Good point! But if data exists at steps [0, 5, 10] and we request step 3, wouldn't How would your approach handle different requested steps? |
Well I thought it was possible to do with Your pre-commit is failing (maybe you didn't |
e69551a to
dae37fe
Compare
|
I tried to resolve the conflicts with main, but failed. Tests are falling at the moment. Do you have time to investigate? |
Resolves test failures after merge with main by properly integrating two fixes that were conflicting: 1. Sparse collection fix (PR mesa#2988 for issue mesa#2987): Handle models that collect data irregularly (e.g., only at steps 0, 5, 10) 2. Time dilation fix (PR mesa#3058): Handle DataCollector.collect() called multiple times per step Changes: - Prioritize _collection_steps when available (modern DataCollector) - Fall back to sparse collection logic when step not found - Support legacy DataCollectors without _collection_steps All 12 batch_run tests now pass, including both test_batch_run_sparse_collection and test_batch_run_time_dilation.
Adds test_batch_run_empty_collection_edge_case to cover the scenario where data is requested before any collection has occurred. This tests the IndexError exception handler in _collect_data when idx is out of bounds and model_vars is empty (lines 277-282 in batchrunner.py). This improves patch coverage for the merge conflict fix.
|
@quaquel I've resolved the merge conflict! |
Thanks! I'll try to look at later today. |
|
Thanks for the review, and reviewing. I think this is a bug fix right, and should be labeled as such? |
|
it's not so much a bug fix but an enhancement of what used to be an IndexError (at least in my understanding) |
Fixes #2987
This PR resolves an
IndexErrorthat occurs inbatch_run()when models use sparse data collection (collecting data only at specific steps rather than every step).Problem
The
_collect_data()function inbatchrunner.pyincorrectly used step numbers as list indices:When a model collects data sparsely (e.g., only at steps 0, 5, 10), the
model_varslist has only 3 items (indices 0, 1, 2). Attempting to accessvalues[5]orvalues[10]causes anIndexError.Solution
Changed the implementation to map step numbers to their corresponding collection indices:
Testing
Added
test_batch_run_sparse_collection()to verify the fix works with models that collect data every N steps.Test output:
Changes
mesa/batchrunner.py: Fixed the IndexError in_collect_data()tests/test_batch_run.py: Added regression test for sparse data collection