Skip to content

Update dict-based agent portrayals to use AgentPortrayalStyle in tests#2934

Merged
Sahil-Chhoker merged 11 commits intomesa:mainfrom
falloficarus22:fix-agent-portrayal-deprecation-warnings
Dec 16, 2025
Merged

Update dict-based agent portrayals to use AgentPortrayalStyle in tests#2934
Sahil-Chhoker merged 11 commits intomesa:mainfrom
falloficarus22:fix-agent-portrayal-deprecation-warnings

Conversation

@falloficarus22
Copy link
Copy Markdown
Contributor

@falloficarus22 falloficarus22 commented Dec 8, 2025

Summary

This PR addresses part of issue #2904 (section 1.1) by updating the older visualization test suite to replace deprecated dict-based agent portrayals with AgentPortrayalStyle and add tests to confirm the emitted FutureWarning from older dict-based agent portrayals.

Changes Made

  • tests/test_examples_viz.py: Updated 9 agent portrayal functions to AgentPortrayalStyle.
  • tests/test_solara_viz_updated.py: Updated dict-based portrayal to AgentPortrayalStyle.
  • tests/test_backends.py: Added tests for deprecated dict-based portrayal to confirm they emit FutureWarning.

Impact

  • Eliminates FutureWarning: Returning a dict from agent_portrayal is deprecated messages from the test suite.
  • Verifies deprecated dict-based portrayal still works (backward compatibility) while emitting the expected warning.
  • All visualization tests passing.
  • Code follows new Mesa API standards using AgentPortrayalStyle.

Testing

Ran the following tests successfully:

pytest tests/test_examples_viz.py tests/test_solara_viz_updated.py tests/test_backends.py -v

Changes by @Sahil-Chhoker:

Updated the description to match the PR’s functionality.

- Remove deprecated dict-based portrayal tests from test_backends.py
- Convert dict-based portrayal to AgentPortrayalStyle in test_solara_viz_updated.py
- Eliminates 3 FutureWarning messages from tests suite

Fixes mesa#2904 (partial - section 1.1)
@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Dec 8, 2025

Thanks!

Would you like to give a shot at fixing all remaining agent_portrayal warnings?

@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 8, 2025

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔴 +7.9% [+6.8%, +9.0%] 🔵 -0.2% [-0.4%, -0.0%]
BoltzmannWealth large 🔵 -2.8% [-15.1%, +7.2%] 🔴 +9.1% [+5.5%, +12.8%]
Schelling small 🔵 -0.3% [-0.5%, -0.1%] 🔵 -0.6% [-0.8%, -0.4%]
Schelling large 🔵 +7.9% [-0.2%, +21.5%] 🔴 +22.8% [+7.4%, +45.9%]
WolfSheep small 🔵 +0.1% [-0.2%, +0.4%] 🔵 -1.6% [-1.7%, -1.5%]
WolfSheep large 🔵 +0.1% [-2.3%, +2.2%] 🔵 +13.1% [-28.2%, +59.2%]
BoidFlockers small 🔵 -1.5% [-1.9%, -1.0%] 🔵 +0.1% [-0.1%, +0.3%]
BoidFlockers large 🔵 -2.0% [-2.4%, -1.7%] 🔵 -0.1% [-0.4%, +0.1%]

@falloficarus22
Copy link
Copy Markdown
Contributor Author

Thanks!

Would you like to give a shot at fixing all remaining agent_portrayal warnings?

Sure, will try

falloficarus22 and others added 3 commits December 8, 2025 09:32
…tPortrayalStyle

- Converted 9 agent_portrayal functions from dict-based to AgentPortrayalStyle
- Includes complex portrayals (wolf_sheep, epstein_civil_violence)
- Eliminates remaining FutureWarning messages in example tests

Part of mesa#2904 (Item 1.1)
@falloficarus22
Copy link
Copy Markdown
Contributor Author

Hey @EwoutH , please check if the fixes I applied are correct and worth merging this PR.

Copy link
Copy Markdown
Member

@EwoutH EwoutH left a comment

Choose a reason for hiding this comment

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

Thanks for this. Already looks cleaner. I have a suggestion on how to handle the deprecated tests.

Comment on lines -70 to -76
# Test with dict-based portrayal
def agent_portrayal_dict(agent):
return {"size": 5, "color": "red", "marker": "o"}

data = mb.collect_agent_data(DummySpace(), agent_portrayal_dict)
assert "loc" in data and data["loc"].shape[0] == 1

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.

I think we can do here:

    # Test with dict-based portrayal (deprecated, emits FutureWarning)
    def agent_portrayal_dict(agent):
        return {"size": 5, "color": "red", "marker": "o"}

    with pytest.warns(FutureWarning):
        data = mb.collect_agent_data(DummySpace(), agent_portrayal_dict)

    assert "loc" in data and data["loc"].shape[0] == 1

That way we make sure that the old behavior also keeps working until we remove it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okay, will work on it.

Comment on lines -206 to -212
# Test with dict-based portrayal
def agent_portrayal_dict(agent):
return {"size": 5, "color": "red", "marker": "o"}

data = ab.collect_agent_data(DummySpace(), agent_portrayal_dict)
assert "loc" in data and data["loc"].shape[0] == 1

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.

Idem dito

    # Test with dict-based portrayal (deprecated, emits FutureWarning)
    def agent_portrayal_dict(agent):
        return {"size": 5, "color": "red", "marker": "o"}

    with pytest.warns(FutureWarning):
        data = ab.collect_agent_data(DummySpace(), agent_portrayal_dict)

    assert "loc" in data and data["loc"].shape[0] == 1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This too!

@falloficarus22
Copy link
Copy Markdown
Contributor Author

Hey @EwoutH verified the backward compatibilty too.

@falloficarus22 falloficarus22 requested a review from EwoutH December 9, 2025 12:59
@EwoutH EwoutH requested a review from Sahil-Chhoker December 11, 2025 10:06
@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Dec 11, 2025

@Sahil-Chhoker I think it looks good, could you give a final check?

Comment on lines +195 to +199
color = agent.wealth # we are using a colormap to translate wealth to color
return {"color": color}
return AgentPortrayalStyle(
color="tab:purple" if agent.wealth > 0 else "tab:grey"
)
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.

Why aren't you setting color to agent.wealth here, this is a crucial test to check if the backend is properly converting int values to a colormap, but now you are just returning two colors.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching that out, I'm trying to fix it now

@falloficarus22
Copy link
Copy Markdown
Contributor Author

Hey @Sahil-Chhoker , I've updated the test to use color=agent.wealth. I came across a bug in mpl_space_drawing.py: the backend was attempting to copy the scalar color value (an integer in this case) directly to edgecolors, which caused Matplotlib to crash.

I’ve pushed a fix for that backend issue along with the updated test, so AgentPortrayalStyle now correctly handles scalar values for colormapping."

Let me know if I'm wrong or if I need to change anything.

@Sahil-Chhoker
Copy link
Copy Markdown
Collaborator

Sorry for the delay @falloficarus22, and thanks for tracking the bug, this is indeed the right fix for it but can you make a separate PR with just the fix since this PR is only for the tests, first we'll merge the fix then this PR.

@falloficarus22
Copy link
Copy Markdown
Contributor Author

Sorry for the delay @falloficarus22, and thanks for tracking the bug, this is indeed the right fix for it but can you make a separate PR with just the fix since this PR is only for the tests, first we'll merge the fix then this PR.

Okay, will do.

@Sahil-Chhoker Sahil-Chhoker changed the title Fix agent portrayal dict deprecation warnings in tests Update dict-based agent portrayals to use AgentPortrayalStyle in tests Dec 16, 2025
@Sahil-Chhoker Sahil-Chhoker added the testing Release notes label label Dec 16, 2025
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.

Good Work!

@Sahil-Chhoker Sahil-Chhoker merged commit 4ecebb1 into mesa:main Dec 16, 2025
13 of 14 checks passed
@falloficarus22 falloficarus22 deleted the fix-agent-portrayal-deprecation-warnings branch December 16, 2025 11:50
@falloficarus22
Copy link
Copy Markdown
Contributor Author

Good Work!

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants