Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Nithin9585
Copy link
Contributor

Fixes #2987

This PR resolves an IndexError that occurs in batch_run() when models use sparse data collection (collecting data only at specific steps rather than every step).

Problem

The _collect_data() function in batchrunner.py incorrectly used step numbers as list indices:

model_data = {param: values[step] for param, values in dc.model_vars.items()}

When a model collects data sparsely (e.g., only at steps 0, 5, 10), the model_vars list has only 3 items (indices 0, 1, 2). Attempting to access values[5] or values[10] causes an IndexError.

Solution

Changed the implementation to map step numbers to their corresponding collection indices:

available_steps = sorted(dc._agent_records.keys())
if step not in available_steps:
    step = max((s for s in available_steps if s <= step), default=0)

try:
    collection_index = available_steps.index(step)
except ValueError:
    collection_index = 0

model_data = {param: values[collection_index] for param, values in dc.model_vars.items()}

Testing

Added test_batch_run_sparse_collection() to verify the fix works with models that collect data every N steps.

Test output:

tests/test_batch_run.py::test_batch_run_sparse_collection PASSED [100%]

Changes

  • mesa/batchrunner.py: Fixed the IndexError in _collect_data()
  • tests/test_batch_run.py: Added regression test for sparse data collection

@github-actions
Copy link

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.8% [+0.4%, +1.3%] 🔵 -1.0% [-1.2%, -0.9%]
BoltzmannWealth large 🔵 +1.3% [-0.6%, +4.6%] 🔵 -1.0% [-2.9%, +0.5%]
Schelling small 🔵 -0.3% [-1.3%, +0.7%] 🔵 -1.1% [-1.7%, -0.4%]
Schelling large 🔵 -0.3% [-1.7%, +1.8%] 🔵 -1.4% [-2.3%, -0.6%]
WolfSheep small 🔵 +0.2% [-0.6%, +0.8%] 🔵 -0.4% [-0.8%, +0.0%]
WolfSheep large 🔵 -1.1% [-4.6%, +1.2%] 🔵 -0.2% [-0.9%, +0.5%]
BoidFlockers small 🔵 -0.7% [-1.0%, -0.5%] 🔵 +0.0% [-0.2%, +0.3%]
BoidFlockers large 🔵 -1.2% [-1.9%, -0.5%] 🔵 -0.3% [-0.7%, +0.1%]

@Nithin9585 Nithin9585 force-pushed the fix-batchrunner-sparse-collection branch from ee4f9b5 to 43b87aa Compare December 22, 2025 11:40
@falloficarus22
Copy link
Contributor

falloficarus22 commented Dec 22, 2025

Hey, isn't it better to simply use values[-1] to get the latest collected data instead of trying to map steps to indices. This would be safer and work for all cases.

@Nithin9585
Copy link
Contributor Author

@falloficarus22 Good point! But if data exists at steps [0, 5, 10] and we request step 3, wouldn't values[-1] return step 10's data when we actually want step 0's?

How would your approach handle different requested steps?

@falloficarus22
Copy link
Contributor

@falloficarus22 Good point! But if data exists at steps [0, 5, 10] and we request step 3, wouldn't values[-1] return step 10's data when we actually want step 0's?

How would your approach handle different requested steps?

Well I thought it was possible to do with values[-1] and tried to apply the changes locally and I'm running into multiple errors.
So, I guess this approach is fine.

Your pre-commit is failing (maybe you didn't ruff check and ruff format the code).

@Nithin9585 Nithin9585 force-pushed the fix-batchrunner-sparse-collection branch from e69551a to dae37fe Compare December 23, 2025 10:25
@Nithin9585
Copy link
Contributor Author

@quaquel @EwoutH can you please review this pr ?

@quaquel
Copy link
Member

quaquel commented Jan 5, 2026

I tried to resolve the conflicts with main, but failed. Tests are falling at the moment. Do you have time to investigate?

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.

BatchRunner crashes with sparse data collection (IndexError)

3 participants