Skip to content

Warn once per call for dict agent_portrayal - #3803

Merged
quaquel merged 1 commit into
mesa:mainfrom
NoiceHax:fix/issue-3455
Aug 18, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
NoiceHax:fix/issue-3455

Conversation

@NoiceHax

Copy link
Copy Markdown
Contributor

Fixes #3455.

The FutureWarning that asks you to stop returning a dict from agent_portrayal sits inside the per agent loop in collect_agent_data. With a few thousand agents you get a few thousand copies of the same message, and under Solara that repeats on every render, so the console fills up and drawing gets slower for no reason.

This adds a local flag before each of the three loops (the matplotlib backend, the altair backend, and mpl_space_drawing.collect_agent_data) and only warns the first time a dict portrayal turns up in that call. The dict to AgentPortrayalStyle conversion is unchanged, so nothing about the drawn output moves. I left the per agent UserWarning about ignored keys alone, since different agents can return different extra keys and folding those together would drop information.

Added tests that a multi agent dict portrayal produces exactly one FutureWarning at each of the three sites. tests/visualization passes with -Werror.

The FutureWarning for returning a dict from agent_portrayal was raised
inside the per-agent loop in collect_agent_data, so a model with a few
thousand agents produced a few thousand identical warnings on every
render. Under Solara that is once per step, which buries anything else
in the console and slows the render down.

Hoist it with a local flag in each of the three loops (both backends
and mpl_space_drawing) so it fires on the first dict portrayal seen and
not again for that call. The dict to AgentPortrayalStyle conversion is
untouched, and the per-agent UserWarning about ignored keys is left as
is since it can differ between agents.

Closes mesa#3455
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.3% [-1.9%, -0.6%] 🔵 -0.9% [-1.0%, -0.7%]
BoltzmannWealth large 🔵 -0.0% [-0.6%, +0.6%] 🔵 -1.0% [-1.8%, -0.1%]
Schelling small 🔵 +0.7% [+0.2%, +1.1%] 🔵 +0.1% [-0.8%, +1.0%]
Schelling large 🔵 -0.5% [-1.0%, -0.1%] 🔵 +0.9% [-2.6%, +4.5%]
WolfSheep small 🔵 +1.9% [+1.5%, +2.4%] 🔵 +1.8% [-1.9%, +5.8%]
WolfSheep large 🔵 -0.2% [-0.8%, +0.3%] 🔵 +1.0% [-0.1%, +2.2%]
SugarscapeG1mt small 🔵 +1.4% [+1.0%, +1.8%] 🔵 +1.6% [-0.1%, +3.3%]
SugarscapeG1mt large 🔵 -0.1% [-0.9%, +0.7%] 🔵 +1.8% [+0.3%, +3.2%]
BoidFlockers small 🔵 -0.9% [-1.3%, -0.5%] 🔵 +0.8% [+0.3%, +1.3%]
BoidFlockers large 🔵 +0.6% [-0.0%, +1.2%] 🔵 +0.7% [+0.5%, +0.9%]

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

Looks fine to me

@quaquel quaquel added the enhancement Release notes label label Aug 18, 2026
@quaquel
quaquel merged commit eed8b2a into mesa:main Aug 18, 2026
13 checks passed
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
The FutureWarning for returning a dict from agent_portrayal was raised
inside the per-agent loop in collect_agent_data, so a model with a few
thousand agents produced a few thousand identical warnings on every
render. Under Solara that is once per step, which buries anything else
in the console and slows the render down.

Hoist it with a local flag in each of the three loops (both backends
and mpl_space_drawing) so it fires on the first dict portrayal seen and
not again for that call. The dict to AgentPortrayalStyle conversion is
untouched, and the per-agent UserWarning about ignored keys is left as
is since it can differ between agents.

Closes #3455

(cherry picked from commit eed8b2a)
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.

Agent portrayal deprecation warning floods console (emitted per agent per render)

2 participants