Skip to content

Added Basic Visualization tests #2767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Sahil-Chhoker
Copy link
Collaborator

@Sahil-Chhoker Sahil-Chhoker commented May 4, 2025

Summary

Adds visualization tests for example models.

Motive

Part of #2741.

Implementation

Adds basic tests to confirm the data is passed onto the the plot components and space component is changing with step or not.

Additional Notes

Ran every test 15 times (total 135 times), found no flaky tests.
Screenshot 2025-05-04 130828

Summary by CodeRabbit

  • Tests

    • Added automated tests to verify that visualizations for multiple agent-based models update correctly as the models progress.
    • Introduced visual regression checks using Playwright for models such as Schelling, Wolf-Sheep, Boid Flockers, Boltzmann Wealth, Virus on Network, Conway’s Game of Life, Epstein Civil Violence, Sugarscape G1mt, and Prisoner’s Dilemma.
  • Chores

    • Expanded development dependencies to include additional testing tools for Playwright and IPython widgets.

Copy link

coderabbitai bot commented May 4, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes introduce a new visualization test suite for various Mesa agent-based models in a new test file. This suite uses Playwright and Solara to render and capture screenshots of visualizations before and after model progression, verifying that the visual output changes as expected. The test suite covers multiple example models, each with its own setup and portrayal logic. Additionally, the development dependencies in the pyproject.toml file are updated to include pytest-playwright, pytest-ipywidgets, and playwright to support these new testing capabilities.

Changes

File(s) Change Summary
pyproject.toml Added pytest-playwright, pytest-ipywidgets, and playwright to the dev dependency group.
tests/test_examples_viz.py Added new test suite for Mesa model visualizations. Includes a helper function and individual tests for various example models.

Sequence Diagram(s)

sequenceDiagram
    participant Tester as pytest
    participant Model as Mesa Model
    participant Viz as SpaceMatplotlib/PlotMatplotlib
    participant Solara as Solara Test Fixture
    participant Browser as Playwright Page

    Tester->>Model: Initialize with seed
    Tester->>Viz: Create initial visualization
    Viz->>Solara: Display visualization
    Solara->>Browser: Render and capture screenshot
    Tester->>Model: Step model N times
    Tester->>Viz: Create updated visualization
    Viz->>Solara: Display updated visualization
    Solara->>Browser: Render and capture updated screenshot
    Tester->>Tester: Compare screenshots (assert visual change)
Loading

Poem

In the warren of tests, a new suite appears,
With Playwright and Solara, it conquers old fears.
Models parade, their visuals in tow,
Snapshots are taken, before and aglow.
With every step forward, the agents all dance—
Hopping through pixels, they change at a glance!
🐇✨


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Sahil-Chhoker
Copy link
Collaborator Author

@coderabbitai full review

Copy link

coderabbitai bot commented May 4, 2025

✅ Actions performed

Full review triggered.

@Sahil-Chhoker Sahil-Chhoker requested a review from EwoutH May 4, 2025 07:55
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (2)
tests/test_examples_viz.py (2)

63-70: Potential memory-leak of Matplotlib figures

You create two new SpaceMatplotlib and optionally PlotMatplotlib instances per test without closing the previous figures; after dozens of tests this can exhaust the Matplotlib figure manager and memory.

Add an explicit plt.close('all') (or call the components’ close() method if they expose one) right after you finish the first screenshot and again after the second.


26-34: Minor: default steps=5 may hide bugs in slow-evolving models

Some models (e.g. Conway’s with sparse seeds) need more than 5 steps for visible change. Consider exposing steps as a parameter in each test call so that individual tests can override the default when required.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 94c56a3 and f8a7a4f.

📒 Files selected for processing (2)
  • pyproject.toml (1 hunks)
  • tests/test_examples_viz.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build (windows, 3.13)
🔇 Additional comments (2)
pyproject.toml (1)

66-69: Add Playwright browser installation step & consider pinning versions

  1. Adding playwright to dev is necessary for the new screenshot tests, but the tests will still fail on fresh CI runners unless the browsers are installed (playwright install --with-deps or python -m playwright install).
    • Please ensure the CI pipeline (or a post-install hook) downloads the browsers or the tests will error with “Executable doesn’t exist”.
  2. None of the three new dev dependencies is version-pinned while most of your runtime deps are. Un-pinned UI-automation libraries tend to ship breaking changes frequently; consider pinning to the latest known-good versions or adding an upper bound.

Would you like a small CI snippet showing how to cache & install the Playwright browsers?

tests/test_examples_viz.py (1)

90-97: Base-64 inequality check can still be flaky on headless renderers

Anti-aliasing, timestamps, or background metadata may change between runs even if the visual content is identical, giving false positives/negatives. If you encounter flakes later:

• Hash the image array after stripping metadata (e.g. via Pillow).
• Or compare perceptual hashes (e.g. imagehash.phash) to tolerate pixel-level noise while still catching genuine changes.

Copy link

github-actions bot commented May 4, 2025

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +1.1% [-0.1%, +2.2%] 🔵 -0.7% [-0.9%, -0.5%]
BoltzmannWealth large 🔵 +0.3% [-0.5%, +1.1%] 🔵 +0.1% [-2.0%, +2.1%]
Schelling small 🔵 -0.1% [-0.3%, +0.1%] 🔵 -0.7% [-1.0%, -0.4%]
Schelling large 🔵 -0.0% [-0.6%, +0.6%] 🔵 -1.5% [-3.1%, -0.1%]
WolfSheep small 🔵 +0.2% [-0.2%, +0.6%] 🔵 +0.1% [-0.2%, +0.3%]
WolfSheep large 🔵 +1.7% [+0.9%, +2.5%] 🔵 +1.0% [-0.6%, +2.3%]
BoidFlockers small 🔵 +0.4% [-0.4%, +1.2%] 🔵 +0.8% [+0.5%, +1.0%]
BoidFlockers large 🔵 +0.4% [-0.2%, +1.0%] 🔵 -0.1% [-0.5%, +0.3%]

@Sahil-Chhoker
Copy link
Collaborator Author

Sahil-Chhoker commented May 4, 2025

image

Tests became a lot faster by only replacing time.sleep(0.2) with page_session.wait_for_selector("img")

(I run one test 15 times to check if the test is flaky or not, but there are only 9 tests)

@EwoutH EwoutH requested a review from Corvince May 4, 2025 08:27
@EwoutH
Copy link
Member

EwoutH commented May 4, 2025

Thanks a lot for this PR!

@Corvince any chance you could review?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants