Vizualization

3. Vizualization#

Functions to help visualize datasets.

mbo_utilities.save_mp4(fname: str | Path | ndarray, images, framerate=60, speedup=1, chunk_size=100, cmap='gray', win=7, vcodec='libx264', normalize=True)[source]#

Save a video from a 3D array or TIFF stack to .mp4.

Parameters:
fnamestr

Output video file name.

imagesnumpy.ndarray or str

Input 3D array (T x H x W) or a file path to a TIFF stack.

framerateint, optional

Original framerate of the video, by default 60.

speedupint, optional

Factor to increase the playback speed, by default 1 (no speedup).

chunk_sizeint, optional

Number of frames to process and write in a single chunk, by default 100.

cmapstr, optional

Colormap to apply to the video frames, by default “gray”. Must be a valid Matplotlib colormap name.

winint, optional

Temporal averaging window size. If win > 1, frames are averaged over the specified window using convolution. By default, 7.

vcodecstr, optional

Video codec to use, by default ‘libx264’.

normalizebool, optional

Flag to min-max normalize the video frames, by default True.

Raises:
FileNotFoundError

If the input file does not exist when images is provided as a file path.

ValueError

If images is not a valid 3D NumPy array or a file path to a TIFF stack.

Notes

  • The input array images must have the shape (T, H, W), where T is the number of frames, H is the height, and W is the width.

  • The win parameter performs temporal smoothing by averaging over adjacent frames.

Examples

Save a video from a 3D NumPy array with a gray colormap and 2x speedup:

>>> import numpy as np
>>> images = np.random.rand(100, 600, 576) * 255
>>> save_mp4('output.mp4', images, framerate=17, cmap='gray', speedup=2)

Save a video with temporal averaging applied over a 5-frame window at 4x speed:

>>> save_mp4('output_smoothed.mp4', images, framerate=30, speedup=4, cmap='gray', win=5)

Save a video from a TIFF stack:

>>> save_mp4('output.mp4', 'path/to/stack.tiff', framerate=60, cmap='gray')
mbo_utilities.save_png(fname, data)[source]#

Saves a given image array as a PNG file using Matplotlib.

Parameters:
fnamestr or Path

The file name (or full path) where the PNG image will be saved.

dataarray-like

The image data to be visualized and saved. Can be any 2D or 3D array that Matplotlib can display.

Examples

>>> import mbo_utilities as mbo
>>> import tifffile
>>> data = tifffile.memmap("path/to/plane_0.tiff")
>>> frame = data[0, ...]
>>> mbo.save_png("plane_0_frame_1.png", frame)
mbo_utilities.run_gui(data_in: None | str | Path | ScanMultiROIReordered | ndarray = None, **kwargs)[source]#

Open a GUI to preview data.