Open nivedkrishnans opened 3 years ago
Temporary solution is to record the browser screen using e.g. https://chrome.google.com/webstore/detail/screen-recorder/hniebljpgcogalllopnjokppmgbhaden.
But given that we use Solara now, it should be easier to capture the Matplotlib space state as pictures that can be turned to animated GIF.
Temporary solution is to record the browser screen using e.g. https://chrome.google.com/webstore/detail/screen-recorder/hniebljpgcogalllopnjokppmgbhaden.
But given that we use Solara now, it should be easier to capture the Matplotlib space state as pictures that can be turned to animated GIF.
The second option (to capture the matplotlib space state as images) sounds good to me. Any hints on where to start with this? I'm happy to work on this feature if it might be useful to others.
A quick hack to test the idea is to add a line after https://github.com/projectmesa/mesa/blob/9c9a02e25df7c83c866ae9a23416c5d32e76a48d/mesa/experimental/jupyter_viz.py#L262 to save the plot to a file, which filename is an increasing number. And then combine the pictures into 1 file via imagemagick
CLI.
You can also have a look at https://matplotlib.org/stable/api/animation_api.html
Especially the examples on the bottom are helpful to create an animation. No need to use solara here, you can also just do your plots normally and call model.step() inside the Animation function.
That animation API uses set_data
(which is more performant than Solara current update method, which recreates the plot object from scratch every time), which Solara doesn't use. It means there needs to be a separate plot object that is updated using this method after a model step. Not ideal, but if it works, it's fine I suppose.
A quick hack to test the idea is to add a line after
https://github.com/projectmesa/mesa/blob/9c9a02e25df7c83c866ae9a23416c5d32e76a48d/mesa/experimental/jupyter_viz.py#L262 to save the plot to a file, which filename is an increasing number. And then combine the pictures into 1 file via
imagemagick
CLI.
This works well 🎉 I think it might be the easiest integration with the existing infrastructure.
space_ax.set_axis_off()
space_fig.savefig(f"space_{model.schedule.steps}.png")
However, I personally prefer to have a separate function to generate gif for a given model. I guess Matplotlib's animation API is the best option then.
Here is the implementation using matplotlib.animation
:
It's actually pretty fast: ~10 seconds for 1000 steps and 5 agents. Please let me know what you think @rht @Corvince!
We could incorporate your code, but there is a possibility that we are migrating the plotting to Altair instead (see #1806). This is still up for discussion.
We could incorporate your code, but there is a possibility that we are migrating the plotting to Altair instead (see #1806). This is still up for discussion.
Just let me know if this or something like it might be useful, I am actively using this to produce gifs to check the model when running on the compute cluster.
What's the problem this feature will solve? A readily obtainable video output of our simulation would make it easier for collaboration between people working on a model as well as for presentations, publications, etc. A normal screen recording will not do well, as the time required for each step may fluctuate during a simulation. NetLogo offers an option to record the simulation region or even the whole interface (with the sliders, graphs, etc).
Describe the solution you'd like Just a thought: It would be better if we can obtain the exact same thing as the browser visualization as a video. Maybe we can screen capture the active elements of the screen during every step and store it temporarily, and at the end of the simulation, stitch them together (say 25 steps per second) and output it into a predefined location/filename.
EDIT: I came across a few python packages that can 'program' svgs. We can assign some shape for every agent and then put them together at the appropriate coordinates to get a vector image for each step. Maybe also display the parameters of the model, etc.