cellects.io.save
cellects.io.save
This script contains functions save various files.
For example: - videos (e.g. mp4) - arrays (h5) - images (jpg)
remove_h5_key(file_name, key='data')
Remove a specified key from an HDF5 file.
This function opens an HDF5 file in append mode and deletes the specified key if it exists. It handles exceptions related to file not found and other runtime errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
The path to the HDF5 file from which the key should be removed. |
required |
key
|
str
|
The name of the dataset or group to delete from the HDF5 file. Default is "data". |
'data'
|
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the specified file does not exist. |
RuntimeError
|
If any other error occurs during file operations. |
Notes
This function modifies the HDF5 file in place. Ensure you have a backup if necessary.
Source code in src/cellects/io/save.py
save_im(img, full_path=None, cmap=None)
Save an image figure to a file with specified options.
This function creates a matplotlib figure from the given image, optionally applies a colormap, displays it briefly, saves the figure to disk at high resolution, and closes the figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
array_like(M, N, 3)
|
Input image to be saved as a figure. Expected to be in RGB format. |
required |
full_path
|
str
|
The complete file path where the figure will be saved. Must include extension (e.g., '.png', '.jpg'). |
None
|
cmap
|
str or None
|
Colormap to be applied if the image should be displayed with a specific
color map. If |
None
|
Returns:
| Type | Description |
|---|---|
None
|
This function does not return any value. It saves the figure to disk at the specified location. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the directory in |
Examples:
>>> img = np.random.rand(100, 100, 3) * 255
>>> save_im(img, 'test.png')
Creates and saves a figure from the random image to 'test.png'.
>>> save_im(img, 'colored_test.png', cmap='viridis')
Creates and saves a figure from the random image with 'viridis' colormap
to 'colored_test.png'.
Source code in src/cellects/io/save.py
tear_down_temp_files()
Delete temporary experiment files created during tests or documentation creation.
Source code in src/cellects/io/save.py
video_writing_decision(arena_nb, im_or_vid, overwrite_unaltered_videos)
Determine whether to write videos based on existing files and user preferences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arena_nb
|
int
|
Number of arenas to analyze. |
required |
im_or_vid
|
int
|
Indicates whether the analysis should be performed on images or videos. |
required |
overwrite_unaltered_videos
|
bool
|
Flag indicating whether existing unaltered videos should be overwritten. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if videos should be written, False otherwise. |
Source code in src/cellects/io/save.py
vstack_h5_array(file_name, table, key='data')
Stack tables vertically in an HDF5 file.
This function either appends the input table to an existing dataset in the specified HDF5 file or creates a new dataset if the key doesn't exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
Path to the HDF5 file. |
required |
table
|
NDArray[uint8]
|
The table to be stacked vertically with the existing data. |
required |
key
|
str
|
Key under which the dataset will be stored. Defaults to 'data'. |
'data'
|
Examples:
Source code in src/cellects/io/save.py
write_h5(file_name, table, key='data')
Write a file using the h5 format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
Name of the file to write. |
required |
table
|
NDArray[]
|
An array. |
required |
key
|
str
|
The identifier of the data in this h5 file. |
'data'
|
Source code in src/cellects/io/save.py
write_video(np_array, vid_name, is_color=True, fps=40)
Write video from numpy array.
Save a numpy array as a video file. Supports .h5 format for saving raw numpy arrays and various video formats (mp4, avi, mkv) using OpenCV. For video formats, automatically selects a suitable codec and handles file extensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
np_array
|
ndarray of uint8
|
Input array containing video frames. |
required |
vid_name
|
str
|
Filename for the output video. Can include extension or not (defaults to .mp4). |
required |
is_color
|
bool
|
Whether the video should be written in color. Defaults to True. |
True
|
fps
|
int
|
Frame rate for the video in frames per second. Defaults to 40. |
40
|
Examples:
>>> video_array = np.random.randint(0, 255, size=(10, 100, 100, 3), dtype=np.uint8)
>>> write_video(video_array, 'output.mp4', True, 30)
Saves `video_array` as a color video 'output.mp4' with FPS 30.
>>> video_array = np.random.randint(0, 255, size=(10, 100, 100), dtype=np.uint8)
>>> write_video(video_array, 'raw_data.h5')
Saves `video_array` as a raw numpy array file without frame rate.
Source code in src/cellects/io/save.py
write_video_from_images(path_to_images='', vid_name='timelapse.mp4', fps=20, img_extension='', img_radical='', crop_coord=None)
Write a video file from a sequence of images.
Extended Description
This function creates a video from a list of image files in the specified directory. To prevent the most comon issues: - The image list is sorted - mp4 files are removed - If they do not have the same orientation, rotate the images accordingly - Images are cropped - Color vs greyscale is automatically determined
After processing, images are compiled into a video file.
Parameters
path_to_images : str The directory where the images are located. vid_name : str, optional The name of the output video file. Default is 'video.mp4'. fps : int, optional The frames per second for the video. Default is 20. img_extension : str, optional The file extension of the images. Default is an empty string. img_radical : str, optional The common prefix of the image filenames. Default is an empty string. crop_coord : list, optional list containing four crop coordinates: [top, bot, left, right]. Default is None and takes the whole image.
Examples
write_video_from_images('path/to/images', vid_name='timelapse.mp4') This will create a video file named 'timelapse.mp4' from the images in the specified directory.
Source code in src/cellects/io/save.py
write_video_sets(img_list, sizes, vid_names, crop_coord, bounding_boxes, bunch_nb, video_nb_per_bunch, remaining, raw_images, is_landscape, use_list_of_vid, in_colors=False, reduce_image_dim=False, pathway='')
Write video sets from a list of images, applying cropping and optional rotation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_list
|
list
|
List of image file names. |
required |
sizes
|
NDArray
|
Array containing the dimensions of each video frame. |
required |
vid_names
|
list
|
List of video file names to be saved. |
required |
crop_coord
|
dict or tuple
|
Coordinates for cropping regions of interest in images/videos. |
required |
bounding_boxes
|
tuple
|
Bounding box coordinates to extract sub-images from the original images. |
required |
bunch_nb
|
int
|
Number of bunches to divide the videos into. |
required |
video_nb_per_bunch
|
int
|
Number of videos per bunch. |
required |
remaining
|
int
|
Number of videos remaining after the last full bunch. |
required |
raw_images
|
bool
|
Whether the images are in raw format. |
required |
is_landscape
|
bool
|
If true, rotate the images to landscape orientation before processing. |
required |
use_list_of_vid
|
bool
|
Flag indicating if the output should be a list of videos. |
required |
in_colors
|
bool
|
If true, process images with color information. Default is False. |
False
|
reduce_image_dim
|
bool
|
If true, reduce image dimensions. Default is False. |
False
|
pathway
|
str
|
Path where the videos should be saved. Default is an empty string. |
''
|