Skip to content
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

Add render interval slider to control visualization update frequency #2596

Merged
merged 12 commits into from
Jan 10, 2025

Conversation

HMNS19
Copy link
Contributor

@HMNS19 HMNS19 commented Jan 4, 2025

Summary

This PR adds a render interval slider to the SolaraViz visualization system, allowing users to control how frequently plots and visualizations are updated during model simulation. This feature significantly improves performance when working with complex visualizations or multiple plots.

Motive

When running models with multiple or complex visualizations, plotting components (especially Matplotlib) can become a performance bottleneck. This enhancement allows users to reduce visualization update frequency while maintaining full simulation speed, addressing the performance issues raised in #2579.

Implementation

  • Added a render_interval parameter to SolaraViz that defaults to 1
  • Implemented a slider control (1-100, step size 2) to adjust the render interval
  • Modified the do_step() function in both ModelController and SimulatorController to:
    • Run multiple simulation steps based on the render interval
    • Only update visualizations after completing all steps in the interval
  • Added proper documentation and type hints for the new parameter

Usage Examples

The render interval slider appears automatically in the visualization interface.

  • Before
    393637466-8cbd2dde-e7a7-4776-b36f-faebe153117c
  • After
    Screenshot 2025-01-10 200323

Additional Notes

  • The render interval is separate from the play interval - it controls visualization updates while the play interval controls simulation speed
  • The implementation maintains backward compatibility and doesn't affect model execution logic

HMNS19 and others added 2 commits January 4, 2025 20:15
sliders created for step interval to display graph after specified number of steps
Copy link

github-actions bot commented Jan 4, 2025

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.6% [-0.1%, +1.4%] 🔵 -0.0% [-0.1%, +0.1%]
BoltzmannWealth large 🔵 -0.5% [-0.9%, -0.1%] 🔵 +0.5% [-0.1%, +1.1%]
Schelling small 🔵 +1.3% [+1.0%, +1.7%] 🔵 +0.1% [-0.1%, +0.2%]
Schelling large 🔵 +0.6% [+0.3%, +1.0%] 🔵 -1.1% [-1.9%, -0.4%]
WolfSheep small 🔵 -0.3% [-0.8%, +0.3%] 🔵 +1.5% [-3.3%, +5.8%]
WolfSheep large 🔵 -0.6% [-1.4%, -0.0%] 🔵 +0.3% [-1.1%, +1.6%]
BoidFlockers small 🔵 -1.5% [-2.1%, -1.0%] 🔵 +0.4% [-0.3%, +1.2%]
BoidFlockers large 🔵 -1.2% [-1.9%, -0.6%] 🔵 -0.8% [-1.2%, -0.4%]

HMNS19 and others added 2 commits January 5, 2025 00:45
forgot to commit changes ofsolara_viz.py in previous commit
@EwoutH
Copy link
Member

EwoutH commented Jan 7, 2025

@HMNS19 thanks for the PR! Is it already ready for a first round of review?

@HMNS19
Copy link
Contributor Author

HMNS19 commented Jan 7, 2025

@EwoutH The work is almost ready for the first round of review. For the most part, the execution speed has improved, but it’s not as fast as expected for a few models. I’m currently working on optimizing those. I should have it ready in a day or two. Let me know if you'd like me to proceed with the review or wait until it's fully optimized.

@EwoutH
Copy link
Member

EwoutH commented Jan 7, 2025

Thanks for working on it and providing an update. One thing I notice is that you change every example model. Ideally, I would like a generalized solution, directly in SolaraViz.

Like in this PR:

CC @Corvince @AdamZh0u

@HMNS19
Copy link
Contributor Author

HMNS19 commented Jan 7, 2025

Understood, will work on it. Thank you for the feedback and the reference!

@HMNS19 HMNS19 marked this pull request as ready for review January 9, 2025 06:12
Copy link

github-actions bot commented Jan 9, 2025

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -4.0% [-6.1%, -1.7%] 🔵 +0.8% [+0.7%, +0.9%]
BoltzmannWealth large 🔵 -1.0% [-1.3%, -0.6%] 🔵 +0.4% [-0.9%, +1.5%]
Schelling small 🔵 -0.8% [-1.2%, -0.5%] 🔵 +0.0% [-0.1%, +0.1%]
Schelling large 🔵 -0.9% [-1.2%, -0.6%] 🔵 +1.7% [+1.2%, +2.1%]
WolfSheep small 🔵 -0.6% [-1.0%, -0.1%] 🟢 -15.0% [-19.7%, -10.2%]
WolfSheep large 🔵 -1.1% [-1.6%, -0.6%] 🔵 +0.5% [-0.3%, +1.5%]
BoidFlockers small 🔵 +2.3% [+1.6%, +3.0%] 🔵 +2.4% [+1.5%, +3.3%]
BoidFlockers large 🔵 +2.3% [+1.6%, +3.0%] 🔵 +3.5% [+2.9%, +4.0%]

@EwoutH
Copy link
Member

EwoutH commented Jan 9, 2025

Thanks, code looks good, have you tested it?

I will review later today.

@EwoutH EwoutH requested a review from Corvince January 9, 2025 07:43
@HMNS19
Copy link
Contributor Author

HMNS19 commented Jan 9, 2025

Yes, I have tested the example models, and they are working as expected. Most of them respond with very minimal latency after clicking the step button. However, the Epstein model takes around 2 seconds of latency when the step interval is set to a high value like 100. I suspect this might be due to factors unrelated to the slider feature.

Revert unintended changes to mesa\visualization\components\__init__.py
@EwoutH
Copy link
Member

EwoutH commented Jan 9, 2025

This works perfect, and is really useful! Glad to see it could be done the same way as in #2540.

I do find the naming currently not the most intuitive, both of play_interval and step_interval. Maybe we can think of some better names, any suggestions?

@EwoutH
Copy link
Member

EwoutH commented Jan 9, 2025

Maybe render_interval?

@HMNS19
Copy link
Contributor Author

HMNS19 commented Jan 9, 2025

Sure, renaming step_interval to render_interval makes much more sense. However, I’m wondering what we should do about the step button in that case. Since ideally clicking on step should increment step by 1, but it currently depends on the value of the step_interval slider. Should we modify its name to match render_interval so the users don't get confused about why the step button's value also changes.
And if we keep the step button to increment the step by 1 regardless of the slider value, it could cause a mismatch between the current step and the current plot, as the value of the slider would differ.

Would you like me to proceed with any changes based on this?

@EwoutH
Copy link
Member

EwoutH commented Jan 9, 2025

That's a very good catch. For now, I'm okay with leaving it as is, to not make this PR too big. But maybe document this well in the docstring / comments.

CC @quaquel @Corvince.

@EwoutH
Copy link
Member

EwoutH commented Jan 10, 2025

I would like to call it render_interval by the way.

@HMNS19
Copy link
Contributor Author

HMNS19 commented Jan 10, 2025

I've made the necessary changes and renamed it to render_interval as you suggested. Let me know if any other changes are needed!

@EwoutH EwoutH changed the title Add step interval slider for updating visualization (#2579) Add render interval slider to control visualization update frequency Jan 10, 2025
@EwoutH
Copy link
Member

EwoutH commented Jan 10, 2025

@HMNS19 Looks good! I cleaned up the PR a little bit (you changed some unrelated files) and updated the PR title and description.

Finally, could you a screenshot of how the slider now looks to the PR description?

@EwoutH EwoutH added feature Release notes label enhancement Release notes label and removed feature Release notes label labels Jan 10, 2025
@EwoutH EwoutH merged commit 3a85213 into projectmesa:main Jan 10, 2025
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Release notes label visualisation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants