cellects.io.load
cellects.io.load
This script contains functions to load various files.
For example, - Images (e.g., tif, jpg) - Videos (e.g., mp4) - Arrays (h5)
create_empty_videos(image_list, cr, lose_accuracy_to_save_memory, already_greyscale, csc_dict)
Create empty video arrays based on input parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_list
|
list
|
List of images. |
required |
cr
|
list
|
Crop region defined by [x_start, y_start, x_end, y_end]. |
required |
lose_accuracy_to_save_memory
|
bool
|
Boolean flag to determine if memory should be saved by using uint8 data type. |
required |
already_greyscale
|
bool
|
Boolean flag indicating if the images are already in greyscale format. |
required |
csc_dict
|
dict
|
Dictionary containing color space conversion settings, including 'logical' key. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple containing three elements:
- |
Notes
Performance considerations:
- If lose_accuracy_to_save_memory is True, the function uses np.uint8 for memory efficiency.
- If already_greyscale is False, additional arrays are created to store RGB data.
Source code in src/cellects/io/load.py
extract_time(pathway='', image_list=None, raw_images=False)
Extract timestamps from a list of images.
This function extracts the DateTimeOriginal or datetime values from the EXIF data of a list of image files, and computes the total time in seconds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pathway
|
str
|
Path to the directory containing the images. Default is an empty string. |
''
|
image_list
|
list of str
|
List of image file names. |
None
|
raw_images
|
bool
|
If True, use the exifread library. Otherwise, use the exif library. Default is False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
time |
ndarray of int64
|
Array containing the total time in seconds for each image. |
Examples:
>>> pathway = Path(__name__).resolve().parents[0] / "data" / "single_experiment"
>>> image_list = ['image1.tif', 'image2.tif']
>>> time = extract_time(pathway, image_list)
>>> print(time)
array([0, 0])
Source code in src/cellects/io/load.py
get_h5_keys(file_name)
Retrieve all keys from a given HDF5 file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
The path to the HDF5 file from which keys are to be retrieved. |
required |
Returns:
| Type | Description |
|---|---|
list of str
|
A list containing all the keys present in the specified HDF5 file. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the specified HDF5 file does not exist. |
Source code in src/cellects/io/load.py
is_raw_image(image_path)
Determine if the image path corresponds to a raw image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_path
|
str
|
The file path of the image. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the image is considered raw, False otherwise. |
Examples:
Source code in src/cellects/io/load.py
list_image_dir(path_to_images='', img_extension='', img_radical='')
List files in an image directory based on optional naming patterns (extension and/or radical).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_to_images
|
optional
|
The path to the directory containing images. Default is an empty string. |
''
|
img_extension
|
str
|
The file extension of the images to be listed. Default is an empty string. When let empty, use the extension corresponding to the most numerous image file in the folder. |
''
|
img_radical
|
str
|
The radical part of the filenames to be listed. Default is an empty string. |
''
|
Returns:
| Type | Description |
|---|---|
list
|
A list of image filenames that match the specified criteria, sorted in a natural order. |
Notes
This function uses the natsorted and insensitive_glob utilities to ensure
that filenames are sorted in a human-readable order.
Examples:
>>> pathway = Path(__name__).resolve().parents[0] / "data" / "single_experiment"
>>> image_list = list_image_dir(pathway)
>>> print(image_list)
Source code in src/cellects/io/load.py
read_and_rotate(image_name, prev_img=None, raw_images=False, is_landscape=True, crop_coord=None)
Read and rotate an image based on specified parameters.
This function reads an image from the given file name, optionally rotates
it by 90 degrees clockwise or counterclockwise based on its dimensions and
the is_landscape flag, and applies cropping if specified. It also compares
rotated images against a previous image to choose the best rotation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_name
|
str
|
Name of the image file to read. |
required |
prev_img
|
ndarray
|
Previous image for comparison. Default is |
None
|
raw_images
|
bool
|
Flag to read raw images. Default is |
False
|
is_landscape
|
bool
|
Flag to determine if the image should be considered in landscape mode.
Default is |
True
|
crop_coord
|
ndarray
|
Coordinates for cropping the image. Default is |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Rotated and optionally cropped image. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the specified image file does not exist. |
Examples:
>>> pathway = Path(__name__).resolve().parents[0] / "data" / "single_experiment"
>>> image_name = 'image1.tif'
>>> image = read_and_rotate(pathway /image_name)
>>> print(image.shape)
(245, 300, 3)
Source code in src/cellects/io/load.py
read_h5(file_name, key='data')
Read data array from an HDF5 file.
This function reads a specific dataset from an HDF5 file using the provided key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
The path to the HDF5 file. |
required |
key
|
str
|
The dataset name within the HDF5 file. |
'data'
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The data array from the specified dataset in the HDF5 file. |
Source code in src/cellects/io/load.py
read_one_arena(arena_label, already_greyscale, csc_dict, videos_already_in_ram=None, true_frame_width=None, vid_name=None, background=None, background2=None)
Read a single arena's video data, potentially converting it from color to greyscale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arena_label
|
int
|
The label of the arena. |
required |
already_greyscale
|
bool
|
Whether the video is already in greyscale format. |
required |
csc_dict
|
dict
|
Dictionary containing color space conversion settings. |
required |
videos_already_in_ram
|
ndarray
|
Pre-loaded video frames in memory. Default is None. |
None
|
true_frame_width
|
int
|
The true width of the video frames. Default is None. |
None
|
vid_name
|
str
|
Name of the video file. Default is None. |
None
|
background
|
ndarray
|
Background image for subtractions. Default is None. |
None
|
background2
|
ndarray
|
Second background image for subtractions. Default is None. |
None
|
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple containing: - visu: np.ndarray or None, the visual frame. - converted_video: np.ndarray or None, the video data converted as needed. - converted_video2: np.ndarray or None, additional video data if necessary. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the specified video file does not exist. |
ValueError
|
If the video data shape is invalid. |
Notes
This function assumes that video2numpy is a helper function available in the scope.
For optimal performance, ensure all video data fits in RAM.
Source code in src/cellects/io/load.py
read_rotate_crop_and_reduce_image(image_name, prev_img=None, crop_coord=None, cr=None, raw_images=False, is_landscape=True, reduce_image_dim=False)
Reads, rotates, crops (if specified), and reduces image dimensionality if required.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_name
|
str
|
Name of the image file to read. |
required |
prev_img
|
NDArray
|
Previous image array used for rotation reference or state tracking. |
None
|
crop_coord
|
list
|
List of four integers [x_start, x_end, y_start, y_end] specifying cropping region. If None, no initial crop is applied. |
None
|
cr
|
list
|
List of four integers [x_start, x_end, y_start, y_end] for final cropping after rotation. |
None
|
raw_images
|
bool
|
Flag indicating whether to process raw image data (True) or processed image (False). |
False
|
is_landscape
|
bool
|
Boolean determining if the image is landscape-oriented and requires specific rotation handling. |
True
|
reduce_image_dim
|
bool
|
Whether to reduce the cropped image to a single channel (e.g., grayscale from RGB). |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
img |
NDArray
|
Processed image after rotation, cropping, and optional dimensionality reduction. |
prev_img |
NDArray
|
Copy of the image immediately after rotation but before any cropping operations. |
Examples:
>>> import numpy as np
>>> img = np.random.rand(200, 300, 3)
>>> new_img, prev = read_rotate_crop_and_reduce_image("example.jpg", img, [50, 150, 75, 225], [20, 180, 40, 250], False, True, True)
>>> new_img.shape == (160, 210)
True
>>> prev.shape == (200, 300, 3)
True
Source code in src/cellects/io/load.py
read_tif_stack(vid_name, expected_channels=1)
Read video array from a tif file.
This function reads a specific dataset from a tif file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid_name
|
str
|
The path to the tif stack file. |
required |
expected_channels
|
int
|
The number of channel. |
1
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The data array from the tif file. |
Source code in src/cellects/io/load.py
readim(image_path, raw_image=False)
Read an image from a file and optionally process it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_path
|
str
|
Path to the image file. |
required |
raw_image
|
bool
|
If True, logs an error message indicating that the raw image format cannot be processed. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The decoded image represented as a NumPy array of shape (height, width, channels). |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Notes
Although raw_image is set to False by default, currently it does not perform any raw image processing.
Examples:
[[ 0, 255, 0],
[ 0, 255, 0]],
[[ 0, 0, 255],
[ 0, 0, 255]]], dtype=np.uint8)
Source code in src/cellects/io/load.py
video2numpy(vid_name, conversion_dict=None, background=None, background2=None, true_frame_width=None)
Convert a video file to a NumPy array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vid_name
|
str
|
The path to the video file. Can be a |
required |
conversion_dict
|
dict
|
Dictionary containing color space conversion parameters. |
None
|
background
|
NDArray
|
Background image for processing. |
None
|
background2
|
NDArray
|
Second background image for processing. |
None
|
true_frame_width
|
int
|
True width of the frame. If specified and the current width is double this value, adjusts to true_frame_width. |
None
|
Returns:
| Type | Description |
|---|---|
NDArray or tuple of NDArrays
|
If conversion_dict is None, returns the video as a NumPy array. Otherwise, returns a tuple containing the original video and converted video. |
Notes
This function uses OpenCV to read the contents of a .mp4 video file.