Skip to content

Fix partial network edgecolors in matplotlib visualization - #3712

Closed
pragnyanramtha wants to merge 2 commits into
mesa:mainfrom
pragnyanramtha:fix-2691-inconsistent-edgecolors
Closed

pragnyanramtha wants to merge 2 commits into
mesa:mainfrom
pragnyanramtha:fix-2691-inconsistent-edgecolors

Conversation

@pragnyanramtha

Copy link
Copy Markdown
Contributor

Note

Use this template for bug fixes only. For enhancements/new features, use the feature template and get maintainer approval in an issue/discussion before opening a PR.

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

Fixes a matplotlib network visualization crash when only some agent portrayals include edgecolors.

Bug / Issue

Fixes #2691.

draw_network expects per-agent style arrays to stay aligned. When an agent portrayal omitted edgecolors, the collectors skipped that entry, so the edgecolor array could be shorter than the node collection. Mixed string and RGBA tuple edgecolor values also need a one-dimensional object array so Matplotlib can consume them consistently.

Implementation

  • Collect one edgecolors entry per agent whenever any agent supplies edgecolors.
  • Use Matplotlib "none" for agents that omit edgecolors.
  • Preserve normal NumPy conversion where possible, and fall back to a one-dimensional object array for mixed color specifications such as RGBA tuples plus "none".
  • Apply the same behavior to both draw_network and MatplotlibBackend.collect_agent_data.
  • Add regression tests for partial edgecolors with string colors and RGBA tuple colors.

Testing

  • uv run --extra viz --extra network --with pytest pytest -p no:cacheprovider tests/visualization/test_components_matplotlib.py::test_draw_network_with_partial_edgecolors tests/visualization/test_backends.py::test_matplotlib_backend_collects_partial_edgecolors -q -> 4 passed
  • uv run --extra viz --extra network --with pytest pytest -p no:cacheprovider tests/visualization/test_components_matplotlib.py tests/visualization/test_backends.py -q -> 30 passed
  • uv run --extra viz --extra network --with pytest --with ruff ruff check --no-cache mesa/visualization/mpl_space_drawing.py mesa/visualization/backends/matplotlib_backend.py tests/visualization/test_components_matplotlib.py tests/visualization/test_backends.py -> passed
  • git diff --check -> passed

No screenshots are attached because the regression was an exception before rendering completed; the automated Matplotlib tests cover the before/after failure mode.

Additional Notes

This is scoped to matplotlib visualization style collection. It does not change public APIs, dependencies, schemas, or CI configuration.

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.2% [-0.6%, +0.2%] 🔵 -1.1% [-1.2%, -1.0%]
BoltzmannWealth large 🔵 +0.8% [+0.4%, +1.2%] 🔵 -1.5% [-1.7%, -1.3%]
Schelling small 🔵 +2.0% [+1.6%, +2.3%] 🔵 +1.1% [+0.3%, +1.9%]
Schelling large 🔵 +0.7% [+0.1%, +1.0%] 🔵 +0.5% [-0.3%, +1.3%]
WolfSheep small 🔵 -0.2% [-0.6%, +0.2%] 🔵 +2.1% [-2.2%, +6.9%]
WolfSheep large 🔵 -0.4% [-1.0%, +0.1%] 🔵 -0.5% [-1.1%, +0.1%]
SugarscapeG1mt small 🔵 +0.1% [-0.0%, +0.2%] 🔵 +1.3% [-0.3%, +3.0%]
SugarscapeG1mt large 🔵 +0.0% [-0.4%, +0.4%] 🔵 +0.5% [-1.8%, +2.8%]
BoidFlockers small 🔵 -0.4% [-0.7%, -0.1%] 🔵 -0.4% [-0.9%, +0.2%]
BoidFlockers large 🔵 -0.5% [-0.8%, -0.2%] 🔵 -0.2% [-0.3%, -0.1%]

@pragnyanramtha
pragnyanramtha marked this pull request as ready for review May 15, 2026 08:44
Copilot AI review requested due to automatic review settings May 15, 2026 08:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes matplotlib network visualization when only some agents provide edgecolors, keeping style arrays aligned for scatter plotting and supporting mixed color specifications.

Changes:

  • Always collects an edgecolor slot per agent when any agent specifies edgecolors.
  • Fills omitted edgecolors with Matplotlib "none".
  • Adds regression coverage for partial edgecolors using string and RGBA tuple values.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
mesa/visualization/mpl_space_drawing.py Aligns collected edgecolors with agent data and handles mixed color array conversion.
mesa/visualization/backends/matplotlib_backend.py Applies the same aligned edgecolor collection logic in the backend collector.
tests/visualization/test_components_matplotlib.py Adds network drawing regression tests for partial edgecolors.
tests/visualization/test_backends.py Adds backend data collection regression tests for partial edgecolors.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.0% [-1.5%, -0.6%] 🔵 -0.9% [-1.0%, -0.7%]
BoltzmannWealth large 🔵 +1.0% [+0.7%, +1.4%] 🔵 -0.5% [-0.8%, -0.2%]
Schelling small 🔵 +0.6% [+0.2%, +1.1%] 🔵 +0.4% [-0.4%, +1.2%]
Schelling large 🔵 +1.3% [+0.9%, +1.8%] 🔵 +3.5% [+2.8%, +4.3%]
WolfSheep small 🔵 +0.4% [-0.0%, +0.9%] 🔵 -1.4% [-5.3%, +2.3%]
WolfSheep large 🔵 -0.1% [-0.4%, +0.1%] 🔵 -0.4% [-1.5%, +0.6%]
SugarscapeG1mt small 🔵 +0.8% [+0.4%, +1.1%] 🔵 +0.8% [-1.1%, +3.0%]
SugarscapeG1mt large 🔵 -0.7% [-1.2%, -0.2%] 🔵 -1.8% [-3.5%, +0.2%]
BoidFlockers small 🔵 +2.8% [+2.5%, +3.1%] 🔵 +0.7% [+0.3%, +1.1%]
BoidFlockers large 🔵 +3.5% [+2.9%, +3.9%] 🔵 +1.6% [+1.1%, +2.0%]

@quaquel quaquel added the bug Release notes label label May 18, 2026
@tpike3

tpike3 commented Jun 7, 2026

Copy link
Copy Markdown
Member

@pragnyanramtha.... apologies for the delay in the review. Thanks for finding this and coming up with a good solution. My only concern is the readability and we seem to be violating the DRY (dont repeat yourself) principle as Mesa has effectively the same code 2X.

Would you be able to make a common helper function?

Something like....

def _to_numpy_argument_array(key, value):
    if key == "marker":
        return np.asarray(value, dtype=object)

    if key == "edgecolors":
        if not any(edgecolor is not None for edgecolor in value):
            return np.asarray([])

        normalized = [
            edgecolor if edgecolor is not None else "none"
            for edgecolor in value
        ]
        try:
            return np.asarray(normalized)
        except ValueError:
            arr = np.empty(len(normalized), dtype=object)
            arr[:] = normalized
            return arr

    return np.asarray(value)

Then call it ...

data = {key: _to_numpy_argument_array(key, value) for key, value in arguments.items()}

I didn't dig too deeply and my knowledge of the visualization library is a bit rusty, so let me know if I am missing any complications with this idea.

@tpike3 tpike3 self-assigned this Sep 2, 2026
@tpike3

tpike3 commented Sep 4, 2026

Copy link
Copy Markdown
Member

@pragnyanramtha Please let us know if you are planning on completing this PR. No worries if not, we will build on your work and complete it. Goal is to have is merged by 25 Sept.

@dietcoke-17

Copy link
Copy Markdown
Contributor

@pragnyanramtha Please let us know if you are planning on completing this PR. No worries if not, we will build on your work and complete it. Goal is to have is merged by 25 Sept.

Hello sir can I start working this looks interesting to me?

@tpike3

tpike3 commented Sep 12, 2026

Copy link
Copy Markdown
Member

@pragnyanramtha Please let us know if you are planning on completing this PR. No worries if not, we will build on your work and complete it. Goal is to have is merged by 25 Sept.

Hello sir can I start working this looks interesting to me?

@dietcoke-17 Sure if you would like

This was referenced Sep 13, 2026
@quaquel

quaquel commented Sep 15, 2026

Copy link
Copy Markdown
Member

superseded by #3856 3856

@quaquel quaquel closed this Sep 15, 2026
@github-actions github-actions Bot added testing Release notes label visualization labels Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Network Visualization error when Agent Portrayal has inconsistent fields

5 participants