Save plot animations as video in Python
For time-resolved simulations, you may want to save a dynamic (animated) plot as a video so that it can be played back in sync with other simulation variables. This guide describes how to accomplish this with the “double pendulum simulation” as an example.
Nominal has 1st class support for video ingestion, analysis, time sychronization across sensor channels, and automated checks that signal when a video feature is out-of-spec.
This guide demonstrates how to upload a video file to Nominal in Python. Other guides in the Video section demonstrate basic Python video analysis and computer vision for video pre-processing prior to Nominal upload.
Double pendulum simulation
The double pendulum consists of two pendulums attached end to end, where the motion of one pendulum influences the other, creating complex, chaotic dynamics. It is a classic problem in physics that demonstrates how even simple systems can exhibit unpredictable behavior due to sensitivity to initial conditions.
If you’ve never seen the double pendulum simulation, below is a time-resolved animation of its solution. Note that the simulation length is 25 seconds.
Convert animation to video
To convert the Plotly animation to a video, we’ll install the imagio
library with the ffmpeg
plugin:
pip3 install imageio[ffmpeg]
Prior to running the above command, you’ll also want to install the underlying ffmpeg
library.
On a Mac you can install this with brew install ffmpeg
. For installation instructions for
other operating systems, please see the official FFmpeg download page.
For more information about this plugin, please refer to the imageio docs.
Now, we’ll modify the simulation code above to to save an image for each animation frame
with Plotly’s write_image()
function. At the end of the simulation, we’ll stitch these
images together into a video with imageio’s
get_writer()
function.
Note that the simulation video has been saved to double_pendulum.mp4.
Display video inline
If you’re working in Jupyter notebook, here’s a shortcut to display the video inline in your notebook.
Save simulation channels
We’ll run the same simulation a final time to extract channels such as the pendulum fulcrum’s heights, momentums, PEs and KEs, etc.
Simulations are often time-consuming to run. In practice, you would want to extract the simulation result, video frames, and important channel values in a single pass. For the pedagogical purposes of this tutorial, however, we’ve separated the extraction of these 3 artifacts into 3 identical but separate simulation runs.
Note that we saved the channel data in a CSV file - double_pendulum_full_headers.csv.
Finally, let’s upload these channels and video to Nominal so that we can visualize them together.
Connect to Nominal
Get your Nominal API token from your User settings page.
See the Quickstart for more details on connecting to Nominal from Python.
Create a Run
First, we’ll create a Run to which we’ll attach our video and channel data.
In Nominal, Runs are containers of multimodal test data - including Datasets, Videos, Logs, and database connections.
To see your organization’s latest Runs, head over to the Runs page
Note that create_run_csv()
automatically adds the CSV file to the Run and sets the Run’s start and end time.
Add Video to Run
Nominal Video uploads need an absolute start time. We’ll grab this start time from the channel dataframe:
2024-10-16T22:47:54.600400
Adjust Video Start
Nominal has many tools to precisely sync video playback with channel data that was either extracted from the video or recorded independently at the same time as the video.
If you select a video on the video datasets page (login required), then you can adjust video metadata such as it start timestamp.
Create a Workbook
Our simulation video and channel data is now uploaded on the Nominal platform, where it can be collaboratively analyzed and benchmarked against real-world data.
Below is an example Nominal Workbook that syncs the pendulum fulcrum heights with the pendulum simulation video playback:
Other channels such as pendulum fulcrum momentum, acceleration, and kinetic energy can also be plotted. Workbook transforms and formulas can be used to calculate derived channels such as the RMS acceleration.
As an exercise, consider solving the simulation with a different ODE solver and comparing the results in a Nominal Workbook.