Skip to content

Fix IndexError in Altair visualization when no agents are present - #3685

Merged
EwoutH merged 9 commits into
mesa:mainfrom
SiddharthRiot:fix-empty-agent-altair
Apr 10, 2026
Merged

EwoutH merged 9 commits into
mesa:mainfrom
SiddharthRiot:fix-empty-agent-altair

Conversation

@SiddharthRiot

Copy link
Copy Markdown
Contributor

Summary

Fixes a crash in the Altair-based space visualization when the model contains no agents.


Bug / Issue

Fixes #3682

When all_agent_data is empty, the code attempts to access all_agent_data[0], causing an IndexError: list index out of range.

Expected behavior:

  • Visualization should not crash for empty grids
  • Should return an empty chart instead

Implementation

Added a guard clause in _draw_grid() to handle the case where all_agent_data is empty.

if not all_agent_data:
    return alt.Chart(pd.DataFrame()).mark_point().properties(width=300, height=300)

Testing

  • Reproduced the issue using an empty grid model
  • Confirmed that the visualization previously crashed with IndexError
  • After the fix, no error occurs and an empty chart is returned

Additional Notes

This ensures safer handling of edge cases where no agents exist in the model space and aligns with the expected behavior described in the issue.

@github-actions

github-actions Bot commented Apr 2, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.1% [-1.4%, -0.8%] 🔵 -0.2% [-0.4%, +0.1%]
BoltzmannWealth large 🔵 -1.1% [-1.4%, -0.8%] 🔵 -0.5% [-1.0%, +0.1%]
Schelling small 🔵 +0.5% [+0.2%, +0.8%] 🔵 +0.6% [+0.0%, +1.2%]
Schelling large 🔵 +0.2% [-0.3%, +0.8%] 🔵 +2.4% [+1.4%, +3.4%]
WolfSheep small 🔵 -0.8% [-1.2%, -0.5%] 🔵 +4.5% [+0.4%, +8.5%]
WolfSheep large 🔵 -1.4% [-1.8%, -0.9%] 🔵 -0.8% [-1.8%, +0.0%]
SugarscapeG1mt small 🔵 -0.9% [-1.1%, -0.8%] 🔵 +0.0% [-1.9%, +1.8%]
SugarscapeG1mt large 🔵 -2.3% [-2.8%, -1.6%] 🔵 +0.7% [-0.2%, +1.7%]
BoidFlockers small 🔵 -1.4% [-1.6%, -1.2%] 🔵 -1.0% [-1.5%, -0.5%]
BoidFlockers large 🔵 -2.2% [-2.5%, -1.9%] 🔵 -0.6% [-0.7%, -0.4%]

Comment on lines +158 to +159
if not all_agent_data:
return alt.Chart(pd.DataFrame({"x": [], "y": []})).mark_point().properties(width=300, height=300)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the early return for if not all_agent_data here bypasses the property-layer branch entirely, so empty spaces with a property layer will render as a blank point chart instead of showing the property layer. The existing flow clearly supports property-only rendering via altair_components.py, so returning before that logic is a behavioral regression rather than just a crash fix. In other words, this PR fixes “no agents” only for the no agents + no property layer case.

@quaquel quaquel added front end bug Release notes label labels Apr 4, 2026
@EwoutH
EwoutH requested a review from Sahil-Chhoker April 9, 2026 20:24
@EwoutH

EwoutH commented Apr 9, 2026

Copy link
Copy Markdown
Member

Thanks for the PR and the peer review. Appreciated.

@Sahil-Chhoker would you be able to review this?

@Sahil-Chhoker Sahil-Chhoker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good work @SiddharthRiot and @falloficarus22, changes look good to me.

@EwoutH EwoutH 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.

Merging, thanks!

@EwoutH
EwoutH merged commit b387075 into mesa:main Apr 10, 2026
11 checks passed
@EwoutH EwoutH added the backport-candidate PRs we might want to backport to an earlier branch label Apr 10, 2026
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
)

Fixes a crash in the Altair-based space visualization when the model contains no agents.

(cherry picked from commit b387075)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-candidate PRs we might want to backport to an earlier branch bug Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Altair space visualization crashes on empty spaces

5 participants