parallel_animate — API Reference
The public API consists of the Animator base class and the
IndexedFrameParams helper, both importable directly from parallel_animate.
parallel_animate.animator
Animator
Bases: ABC
Base class for creating matplotlib animations with efficient parallel rendering.
Performance note: every frame is fully rasterised — update() mutates the
figure and the whole canvas is then re-drawn and captured. Matplotlib's
blitting optimisation (re-drawing only the artists that changed via
draw_artist/restore_region) is intentionally not used: frames are
rendered independently and, in parallel mode, in separate worker processes,
so there is no persistent inter-frame canvas state to blit against. Speedups
come from rendering frames concurrently across workers rather than from
incremental redraws. To keep each frame cheap, do expensive, frame-invariant
setup once in setup() and only touch what changes in update().
Pickling note: in parallel mode the whole Animator instance is pickled and
copied into every worker process. Anything stored on self (e.g. in
__init__) is therefore serialised once per worker — stashing a large
array such as an entire video tensor on an instance attribute multiplies its
memory across workers and adds pickling overhead. Keep heavy, per-frame data
out of instance attributes: pass it through param_by_frame (the intended
channel), or load/construct it lazily inside setup() so each worker
builds its own copy instead of receiving one over the pickle boundary.
setup
abstractmethod
Set up the figure, axes, and any artists. Store whatever you need as instance attributes (self.ax, self.line, etc.); you will need them in update().
This method MUST not accept any argument other than self (if you need to pass data to setup, store it as instance attributes in init).
This method MUST return the plt.Figure object, and that alone.
This method is called once before the animation loop (once per worker if parallelized), unless reuse_figure_object is False (basically never).
Source code in src/parallel_animate/animator.py
update
abstractmethod
Update the plot for the given frame.
This is called for each frame in the animation. Modify any figure, axes, and artists that you created in setup() and stored as instance attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame_idx
|
int
|
Current frame index (0 to num_frames-1) |
required |
params
|
Any
|
Parameters for this frame from param_by_frame[frame_idx]. Can be any object you want as long as it's pickleable (required to distribute jobs to distributed workers). |
required |
Source code in src/parallel_animate/animator.py
make_video
make_video(
output_file: Path | str,
param_by_frame: Iterable[Any],
fps: int,
n_frames: int | None = None,
num_workers: int = -1,
disable_progress_bar: bool | None = None,
plotting_log_interval: int | None = None,
saving_log_interval: int | None = None,
savefig_params: dict[str, Any] | None = None,
video_mode: str = "auto",
video_quality: int | None = None,
video_preset: str | None = None,
video_extra_ffmpeg_params: list[str] | None = None,
reuse_figure_object: bool = True,
preload_factor: int = 8,
) -> None
Render the animation to a video file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_file
|
Path | str
|
Path to output video file |
required |
param_by_frame
|
Iterable[Any]
|
Iterable of parameters, one per frame |
required |
fps
|
int
|
Frames per second for output video |
required |
n_frames
|
int | None
|
Number of frames to render. If None, use the length of param_by_frame. If param_by_frame does not have len implemented and n_frames is None, the progress bar won't show completion percentage. |
None
|
num_workers
|
int
|
Number of parallel workers. 1 for serial processing (in the main thread), -1 for all CPU cores, -2 for all but one CPU core, etc. |
-1
|
disable_progress_bar
|
bool | None
|
Same behavior as tqdm: if True, disable progress bar. If None, auto-detect based on whether output is a TTY. |
None
|
plotting_log_interval
|
int | None
|
Log progress every N frames in each worker (None = no interval logging). |
None
|
saving_log_interval
|
int | None
|
Log progress every N frames when merging frames into video (None = no interval logging). |
None
|
savefig_params
|
dict[str, Any]
|
Rendering options for each frame
(default: {}). For speed, frames are grabbed directly from the
Agg canvas buffer as raw RGB pixels (no PNG encode/decode or
image-file round-trip), so only |
None
|
video_mode
|
str
|
Encoding backend selection passed to parallel-video-io, one of "auto", "gpu", or "cpu" (default: "auto"). "auto" uses the GPU encoder (FFmpeg/NVENC) when a CUDA device is available and transparently falls back to CPU (libx264) otherwise. "gpu" forces NVENC and "cpu" forces libx264. Output is always an H.264 MP4. |
'auto'
|
video_quality
|
int | None
|
H.264 quantiser scale in the range 0-51, where lower means higher quality / larger files. If None (default), use parallel-video-io's visually-lossless default. |
None
|
video_preset
|
str | None
|
Encoder preset. If None (default), use the encoder's own default. Accepts libx264 presets ("ultrafast" … "placebo") in CPU mode or NVENC presets ("p1" … "p7") in GPU mode. |
None
|
video_extra_ffmpeg_params
|
list[str] | None
|
Extra raw FFmpeg arguments appended to the encode command for advanced tuning (default: None). |
None
|
reuse_figure_object
|
bool
|
If False, the figure will be re-created for each frame (i.e. setup() called every frame). There is basically no reason to set this to False. Use only for testing and benchmarking. |
True
|
preload_factor
|
int
|
Number of workloads to prefetch in the task queue per worker (approximately). I.e. each worker will have up to this many frames (on average) queued ahead of time to work on. Higher values smooth throughput but hold more frames' params in the queue at once; with large per-frame params (e.g. images) this raises peak memory by roughly num_workers * preload_factor frames' worth. |
8
|
Source code in src/parallel_animate/animator.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |