Skip to content

Data input and output

Dealing with volumetric data can be done by fibretracker for the most common image formats available.

Currently, it is possible to directly load tiff, h5, nii,txm and common PIL formats using one single function.

fibretracker.io

fibretracker.io.load

load(path, dataset_name=None, return_metadata=False, contains=None, force_load=False, dim_order=(2, 1, 0), **kwargs)

Load data from the specified file or directory.

Parameters:

Name Type Description Default
path str or PathLike

The path to the file or directory.

required
dataset_name str

Specifies the name of the dataset to be loaded in case multiple dataset exist within the same file. Default is None (only for HDF5 files)

None
return_metadata bool

Specifies whether to return metadata or not. Default is False (only for HDF5 and TXRM/TXM/XRM files)

False
contains str

Specifies a part of the name that is common for the TIFF file stack to be loaded (only for TIFF stacks). Default is None.

None
force_load bool

If the file size exceeds available memory, a MemoryError is raised. If force_load is True, the error is changed to warning and the loader tries to load it anyway. Default is False.

False
dim_order tuple

The order of the dimensions in the volume for .vol files. Default is (2,1,0) which corresponds to (z,y,x)

(2, 1, 0)
**kwargs

Additional keyword arguments to be passed

{}

Returns:

Name Type Description
vol ndarray

The loaded volume If return_metadata=True and file format is either HDF5, NIfTI or TXRM/TXM/XRM, returns tuple (volume, metadata).

Raises:

Type Description
MemoryError

if the given file size exceeds available memory

Example

# Load volume from a single file
import fibretracker as ft

vol = ft.io.load(data_path)
# Load a stack of TIFF files as a volume
import fibretracker as ft

vol = ft.io.load(data_path, contains='.tif')

Source code in fibretracker/io/read_file.py
def load(
    path,
    dataset_name=None,
    return_metadata=False,
    contains=None,
    force_load: bool = False,
    dim_order=(2, 1, 0),
    **kwargs,
):
    """
    Load data from the specified file or directory.

    Args:
        path (str or os.PathLike): The path to the file or directory.
        dataset_name (str, optional): Specifies the name of the dataset to be loaded
            in case multiple dataset exist within the same file. Default is None (only for HDF5 files)
        return_metadata (bool, optional): Specifies whether to return metadata or not. Default is False (only for HDF5 and TXRM/TXM/XRM files)
        contains (str, optional): Specifies a part of the name that is common for the TIFF file stack to be loaded (only for TIFF stacks).
            Default is None.
        force_load (bool, optional): If the file size exceeds available memory, a MemoryError is raised.
            If force_load is True, the error is changed to warning and the loader tries to load it anyway. Default is False.
        dim_order (tuple, optional): The order of the dimensions in the volume for .vol files. Default is (2,1,0) which corresponds to (z,y,x)
        **kwargs: Additional keyword arguments to be passed
        to the DataLoader constructor.

    Returns:
        vol (numpy.ndarray): The loaded volume
            If `return_metadata=True` and file format is either HDF5, NIfTI or TXRM/TXM/XRM, returns `tuple` (volume, metadata).

    Raises:
        MemoryError: if the given file size exceeds available memory

    Example:
        ```python
        # Load volume from a single file
        import fibretracker as ft

        vol = ft.io.load(data_path)
        ```
        ```python
        # Load a stack of TIFF files as a volume
        import fibretracker as ft

        vol = ft.io.load(data_path, contains='.tif')
        ```

    """

    loader = DataLoader(
        dataset_name=dataset_name,
        return_metadata=return_metadata,
        contains=contains,
        force_load=force_load,
        dim_order=dim_order,
        **kwargs,
    )

    data = loader.load(path)

    return data

fibretracker.io.normalize

normalize(vol)

Normalize the volume to the range [0, 1] using min-max scaling.

Parameters:

Name Type Description Default
vol ndarray

The volume to normalize.

required

Returns:

Name Type Description
norm_vol ndarray

The normalized volume.

Example
import fibretracker as ft

norm_vol = ft.io.normalize(vol)
Source code in fibretracker/io/read_file.py
def normalize(
        vol: np.ndarray
        ):
    """Normalize the volume to the range [0, 1] using min-max scaling.

    Args:
        vol (numpy.ndarray): The volume to normalize.

    Returns:
        norm_vol (numpy.ndarray): The normalized volume.

    Example:
        ```python
        import fibretracker as ft

        norm_vol = ft.io.normalize(vol)
        ```
    """
    norm_vol = (vol - np.min(vol)) / (np.max(vol) - np.min(vol))
    return norm_vol