Skip to content

mixed-face-colors patch - #3866

Merged
tpike3 merged 4 commits into
mesa:mainfrom
dietcoke-17:array-fallback
Sep 20, 2026
Merged

tpike3 merged 4 commits into
mesa:mainfrom
dietcoke-17:array-fallback

Conversation

@dietcoke-17

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

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

Summary

Extends the object-array fallback added in #3856 to face colors (c), so mixing plain color-name strings and RGBA tuples across agents doesn't crash collect_agent_data.

Bug

Raised by @Dilraj07 during review of #3856: _to_numpy_argument_array handles mixed color representations for edgecolors, but c (face color) can hit the exact same failure. np.asarray(["blue", (1.0, 0.0, 0.0, 1.0)]) raises ValueError: setting an array element with a sequence... for the same reason the original edgecolors bug did — NumPy can't coerce a plain string next to a 4-tuple into one array.

This is a pre-existing bug, not introduced by #3856 — the same call would have failed on main before that PR too. Filed as a separate follow-up per discussion on #3856 to keep that PR scoped to #2691.

Fix

  • Factored the existing try/np.asarray/except ValueError/manual-object-array logic out of the edgecolors branch into a small _coerce_or_object_array() helper.
  • Reused it for c, so mixed face colors fall back to a 1D object array the same way mixed edgecolors do.
  • Uniform-length RGBA/RGB tuples are untouched — np.asarray already turns those into a valid (N, 4)/(N, 3) array, which is exactly what Matplotlib expects, so only genuinely mixed representations take the fallback path.
  • MatplotlibBackend needs no changes: it already imports _to_numpy_argument_array from mpl_space_drawing.py, so it picks up the fix automatically.

Testing

  • New: test_to_numpy_argument_array_mixed_face_colors — unit test confirming a mixed ["blue", (1.0, 0.0, 0.0, 1.0)] list round-trips correctly, and a uniform RGBA-tuple list still produces a proper (N, 4) array rather than being forced into an object array.
  • New: test_draw_network_with_mixed_face_colors — end-to-end regression test through draw_network with agents split between a plain color name and an RGBA tuple.
  • Also manually confirmed MatplotlibBackend.collect_agent_data handles the same mixed-color case correctly with zero changes to that file.

Additional Notes

Scoped to matplotlib visualization style collection only — no public API, dependency, schema, or CI changes. Builds directly on the _to_numpy_argument_array helper introduced in #3856; please merge that first if it hasn't landed yet, since this branches from it.

@github-actions github-actions Bot added testing Release notes label visualization labels Sep 18, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -6.9% [-8.7%, -5.2%] 🔵 +0.0% [-0.8%, +0.9%]
BoltzmannWealth large 🔵 -1.2% [-1.9%, -0.7%] 🔵 -2.7% [-3.4%, -2.1%]
Schelling small 🔵 -3.8% [-5.3%, -2.3%] 🔵 -1.3% [-2.3%, -0.4%]
Schelling large 🔵 -3.6% [-4.1%, -3.0%] 🟢 -7.7% [-8.7%, -6.4%]
WolfSheep small 🟢 -9.0% [-10.4%, -7.4%] 🔵 +0.3% [-3.9%, +5.1%]
WolfSheep large 🔵 -2.5% [-4.3%, -0.8%] 🔵 -3.6% [-7.0%, -0.5%]
SugarscapeG1mt small 🔵 +0.8% [-0.7%, +2.4%] 🔵 -0.0% [-1.7%, +1.6%]
SugarscapeG1mt large 🔵 +2.2% [-1.5%, +5.7%] 🔵 +0.0% [-1.6%, +1.8%]
BoidFlockers small 🔵 +3.4% [-0.2%, +6.8%] 🔵 +0.4% [-1.3%, +2.4%]
BoidFlockers large 🔵 -0.3% [-5.9%, +4.9%] 🔵 +0.5% [-0.5%, +1.4%]
MultiLevelAllianceModel small 🔴 +9.8% [+6.0%, +13.4%] 🔵 +0.3% [-1.4%, +2.2%]
MultiLevelAllianceModel large 🔵 +1.1% [-2.6%, +4.6%] 🔵 +0.2% [-0.3%, +0.6%]

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

LGTM thanks @dietcoke-17

@tpike3
tpike3 merged commit f376606 into mesa:main Sep 20, 2026
14 of 15 checks passed
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
* mixed-face-colors patch

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update assertion for uniform result shape

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Tom Pike <[email protected]>
(cherry picked from commit f376606)
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.

2 participants