You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
wolf sheep example crashes when i try to run it with grass regrowth enabled=False #3597
It looks like the DataCollector stops tracking the 'Grass' metric when grass_regrowth is set to False, but the matplotlib_components.py visualizer is still blindly attempting to plot it, throwing the KeyError.
I'd love to take this one on. I can put together a PR that adds a safety check in the plotting component to verify the metric exists in the dataframe columns before attempting to plot it. Let me know if I'm good to open a PR for this!
If you run the model headlessly (bypassing the UI crash), it dies with a StopIteration error because Sheep.feed() assumes a grass patch exists and calls next() on an empty generator without a default fallback.
I passed None as a default to next() in Sheep.feed(), and add a quick if grass_patch: check.
Reproduced both crashes locally and opened a fix PR.
Bug 1 (StopIteration): Sheep.feed() calls next(generator) with no default. When grass=False no GrassPatch exists in any cell so the generator is always empty. Fix: next((gen), None) + if grass_patch is not None guard.
Bug 2 (KeyError: 'Grass'): DataCollector only registers the "Grass" reporter when grass=True (model.py line 87). PlotMatplotlib was plotting all measure keys unconditionally. Fix: if m in df.columns guard — generalises to any model with conditionally-collected metrics.
Both fixes are additive guards only, no change to the grass=True path. Two regression tests added.
Here's the fixed model running interactively with grass=False in the Solara visualizer — Wolves and Sheep population curves shown correctly, no crash, Grass metric absent from both the plot and the sidebar as expected.
@DipayanDasgupta i assumed setting regrowth to False means we start out with some initial grass and then it doesn’t grow back, is grass just absent from the get go in your solution?
@disgruntled-penguin good point . You're right that semantically grass=False could mean "grass exists but doesn't regrow." In the current Mesa implementation though, grass=False means no GrassPatch agents are created at all (model.py line 122: if self.grass: # Create grass patches). So grass is absent from the start entirely. The parameter name grass controls whether the grass mechanic exists, not just whether it regrows — grass_regrowth_time controls the regrowth rate separately. The crash happens because Sheep.feed() assumed GrassPatch agents always exist in cells, which isn't true when grass=False.
Activity
laxmi2577 commentedon Mar 22, 2026
Hey @disgruntled-penguin, thanks for the clear traceback!
It looks like the DataCollector stops tracking the 'Grass' metric when grass_regrowth is set to False, but the matplotlib_components.py visualizer is still blindly attempting to plot it, throwing the KeyError.
I'd love to take this one on. I can put together a PR that adds a safety check in the plotting component to verify the metric exists in the dataframe columns before attempting to plot it. Let me know if I'm good to open a PR for this!
quaquel commentedon Mar 22, 2026
I think I have seen this issue before at some point. Is this on main or on 3.5?
codebreaker32 commentedon Mar 22, 2026
I checked it, prominent in both.
quaquel commentedon Mar 22, 2026
Thanks for checking. @laxmi2577 diagnosis makes sense to me. The main question then becomes what an elegant solution is for the problem.
codebreaker32 commentedon Mar 22, 2026
I went ahead and tried to fix this, there were two separate issues:
Added quick if check
mesa/mesa/visualization/components/matplotlib_components.py
Lines 157 to 167 in 71cfd39
to
StopIterationerror becauseSheep.feed()assumes a grass patch exists and calls next() on an empty generator without a default fallback.I passed
Noneas a default tonext()inSheep.feed(), and add a quickif grass_patch:check.But then after reset, background disppeared
codebreaker32 commentedon Mar 22, 2026
This looks somewhat similar to what we encountered in Network(#3323 and #3345)
Fix mesa#3597: safe Sheep.feed() when grass=False, skip missing plot …
DipayanDasgupta commentedon Mar 22, 2026
Reproduced both crashes locally and opened a fix PR.
Bug 1 (StopIteration): Sheep.feed() calls next(generator) with no default. When grass=False no GrassPatch exists in any cell so the generator is always empty. Fix: next((gen), None) + if grass_patch is not None guard.
Bug 2 (KeyError: 'Grass'): DataCollector only registers the "Grass" reporter when grass=True (model.py line 87). PlotMatplotlib was plotting all measure keys unconditionally. Fix: if m in df.columns guard — generalises to any model with conditionally-collected metrics.
Both fixes are additive guards only, no change to the grass=True path. Two regression tests added.
Add wolf_sheep_investigation: reproduce and fix Issue #3597
DipayanDasgupta commentedon Mar 22, 2026
Here's the fixed model running interactively with grass=False in the Solara visualizer — Wolves and Sheep population curves shown correctly, no crash, Grass metric absent from both the plot and the sidebar as expected.
disgruntled-penguin commentedon Mar 22, 2026
@DipayanDasgupta i assumed setting regrowth to False means we start out with some initial grass and then it doesn’t grow back, is grass just absent from the get go in your solution?
Fix wolf_sheep example crash when grass=False
Fix wolf_sheep example crash when grass=False
Fix wolf_sheep example crash when grass=False
DipayanDasgupta commentedon Mar 23, 2026
@disgruntled-penguin good point . You're right that semantically grass=False could mean "grass exists but doesn't regrow." In the current Mesa implementation though, grass=False means no GrassPatch agents are created at all (model.py line 122: if self.grass: # Create grass patches). So grass is absent from the start entirely. The parameter name grass controls whether the grass mechanic exists, not just whether it regrows — grass_regrowth_time controls the regrowth rate separately. The crash happens because Sheep.feed() assumed GrassPatch agents always exist in cells, which isn't true when grass=False.
Fix mesa#3597: safe Sheep.feed() when grass=False — next() default None
Fix mesa#3597: PlotMatplotlib skips measure keys absent from dataframe
Fix mesa#3597: safe Sheep.feed() when grass=False, skip missing plot …
Fix mesa#3597: skip missing measures in PlotMatplotlib, add regressio…
Fix mesa#3597: safe Sheep.feed() when grass=False, add regression test
Fix #3597: safe Sheep.feed() when grass=False, add regression test (#…
tests: regression tests for mesa#3597 (PlotMatplotlib missing column)
Fix mesa#3597: safe Sheep.feed() when grass=False, add regression test (
Fix mesa#3597: safe Sheep.feed() when grass=False, add regression test (
Fix #3597: safe Sheep.feed() when grass=False, add regression test (#…