Skip to content

Support AgentPortrayalStyle in Altair visualization components#2985

Merged
EwoutH merged 2 commits intomesa:mainfrom
EwoutH:altair_portrayal
Dec 23, 2025
Merged

Support AgentPortrayalStyle in Altair visualization components#2985
EwoutH merged 2 commits intomesa:mainfrom
EwoutH:altair_portrayal

Conversation

@EwoutH
Copy link
Copy Markdown
Member

@EwoutH EwoutH commented Dec 22, 2025

Problem

The Altair visualization components (altair_components.py) were not updated to support the new AgentPortrayalStyle class introduced in the visualization refactoring. When users returned an AgentPortrayalStyle instance from their agent_portrayal function, the code attempted dictionary-style assignment (agent_data["x"] = x), causing a TypeError: 'AgentPortrayalStyle' object does not support item assignment.

This also triggered a deprecation warning in tests that were still using the old dict-based portrayal format.

Changes

  • Added _portrayal_to_dict() helper function to convert AgentPortrayalStyle instances to dict format for Altair chart generation
  • Updated _get_agent_data_old__discrete_space(), _get_agent_data_new_discrete_space(), and _get_agent_data_continuous_space() to use the helper function
  • Updated test_call_space_drawer to use AgentPortrayalStyle instead of deprecated dict format
  • Maintains full backward compatibility with existing dict-based portrayals

Technical Details

Altair requires dict-formatted data for chart generation and only uses color and size fields from the agent portrayal (other fields become tooltips). The helper function extracts these fields from AgentPortrayalStyle instances while preserving the original dict-based behavior for legacy code.

Follow-up on #2786.

…l_space_drawer

Update agent_portrayal in test_call_space_drawer to use AgentPortrayalStyle instead of dict
@EwoutH EwoutH requested a review from Sahil-Chhoker December 22, 2025 09:25
@EwoutH EwoutH added the enhancement Release notes label label Dec 22, 2025
Add _portrayal_to_dict() helper to convert AgentPortrayalStyle to dict
format for Altair charts. Update tests to use new portrayal API.
@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.4% [-2.4%, -0.2%] 🔵 -2.1% [-2.5%, -1.8%]
BoltzmannWealth large 🔵 -1.1% [-5.1%, +3.6%] 🔵 -4.1% [-5.4%, -2.7%]
Schelling small 🔵 +0.4% [-0.7%, +1.5%] 🔵 +0.5% [-0.1%, +1.1%]
Schelling large 🔵 +2.5% [+1.4%, +4.2%] 🔵 +3.6% [+2.2%, +4.9%]
WolfSheep small 🔵 -0.0% [-0.9%, +0.7%] 🔵 -1.1% [-1.4%, -0.8%]
WolfSheep large 🔵 +0.3% [-2.4%, +2.2%] 🔴 +4.0% [+3.0%, +5.0%]
BoidFlockers small 🔵 -0.5% [-1.3%, +0.4%] 🔵 +0.2% [-0.2%, +0.6%]
BoidFlockers large 🔵 -2.8% [-3.5%, -2.2%] 🔵 -1.3% [-1.7%, -0.9%]

Copy link
Copy Markdown
Collaborator

@Sahil-Chhoker Sahil-Chhoker left a comment

Choose a reason for hiding this comment

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

LGTM!

@EwoutH EwoutH merged commit 425fdb2 into mesa:main Dec 23, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants