Update dict-based agent portrayals to use AgentPortrayalStyle in tests#2934
Conversation
- 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)
|
Thanks! Would you like to give a shot at fixing all remaining agent_portrayal warnings? |
|
Performance benchmarks:
|
Sure, will try |
…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)
|
Hey @EwoutH , please check if the fixes I applied are correct and worth merging this PR. |
EwoutH
left a comment
There was a problem hiding this comment.
Thanks for this. Already looks cleaner. I have a suggestion on how to handle the deprecated tests.
| # 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 | ||
|
|
There was a problem hiding this comment.
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] == 1That way we make sure that the old behavior also keeps working until we remove it.
There was a problem hiding this comment.
Okay, will work on it.
| # 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 | ||
|
|
There was a problem hiding this comment.
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|
Hey @EwoutH verified the backward compatibilty too. |
|
@Sahil-Chhoker I think it looks good, could you give a final check? |
tests/test_examples_viz.py
Outdated
| 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" | ||
| ) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Thanks for catching that out, I'm trying to fix it now
|
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. |
|
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. |
AgentPortrayalStyle in tests
Thanks! |
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
AgentPortrayalStyleand add tests to confirm the emittedFutureWarningfrom older dict-based agent portrayals.Changes Made
FutureWarning.Impact
Testing
Ran the following tests successfully:
Changes by @Sahil-Chhoker:
Updated the description to match the PR’s functionality.