Skip to content
Snippets Groups Projects
Commit 74bffb97 authored by fima's avatar fima :beers:
Browse files

Merge branch 'unit_tests_for_qim3d.io.load()' into 'main'

Added first round of unit tests for qim3d.io.load()

See merge request !22
parents 8ec69a25 c512d559
No related branches found
No related tags found
1 merge request!22Added first round of unit tests for qim3d.io.load()
"""Provides functionality for loading data from various file formats.""" """Provides functionality for loading data from various file formats."""
import os import os
import sys
import difflib import difflib
import tifffile import tifffile
import h5py import h5py
...@@ -16,25 +15,24 @@ from qim3d.utils.system import Memory ...@@ -16,25 +15,24 @@ from qim3d.utils.system import Memory
class DataLoader: class DataLoader:
"""Utility class for loading data from different file formats. """Utility class for loading data from different file formats.
Args:
virtual_stack (bool, optional): Specifies whether to use virtual stack
when loading files. Default is False.
Attributes: Attributes:
virtual_stack (bool): Specifies whether virtual stack is enabled. virtual_stack (bool): Specifies whether virtual stack is enabled.
dataset_name (str): Specifies the name of the dataset to be loaded
(only relevant for HDF5 files)
return_metadata (bool): Specifies if metadata is returned or not
(only relevant for HDF5 and TXRM/TXM/XRM files)
contains (str): Specifies a part of the name that is common for the
TIFF file stack to be loaded (only relevant for TIFF stacks)
Methods: Methods:
load_tiff(path): Load a TIFF file from the specified path. load_tiff(path): Load a TIFF file from the specified path.
load_h5(path): Load an HDF5 file from the specified path. load_h5(path): Load an HDF5 file from the specified path.
load_tiff_stack(path): Load a stack of TIFF files from the specified path. load_tiff_stack(path): Load a stack of TIFF files from the specified path.
load_txrm(path): Load a TXRM/TXM/XRM file from the specified path load_txrm(path): Load a TXRM/TXM/XRM file from the specified path
load(path): Load a file or directory based on the given path. load(path): Load a file or directory based on the given path
Raises:
ValueError: If the file format is not supported or the path is invalid.
Example: Example:
loader = DataLoader(virtual_stack=True) loader = qim3d.io.DataLoader(virtual_stack=True)
data = loader.load_tiff("image.tif") data = loader.load_tiff("image.tif")
""" """
...@@ -42,15 +40,14 @@ class DataLoader: ...@@ -42,15 +40,14 @@ class DataLoader:
"""Initializes a new instance of the DataLoader class. """Initializes a new instance of the DataLoader class.
Args: Args:
path (str): The path to the file or directory.
virtual_stack (bool, optional): Specifies whether to use virtual virtual_stack (bool, optional): Specifies whether to use virtual
stack when loading files. Default is False. stack when loading files. Default is False.
dataset_name (str, optional): Specifies the name of the dataset to be loaded 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) 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 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) 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.
""" """
# Virtual stack is False by default
self.virtual_stack = kwargs.get("virtual_stack", False) self.virtual_stack = kwargs.get("virtual_stack", False)
self.dataset_name = kwargs.get("dataset_name", None) self.dataset_name = kwargs.get("dataset_name", None)
self.return_metadata = kwargs.get("return_metadata", False) self.return_metadata = kwargs.get("return_metadata", False)
...@@ -258,11 +255,11 @@ class DataLoader: ...@@ -258,11 +255,11 @@ class DataLoader:
numpy.ndarray: The loaded volume as a NumPy array. numpy.ndarray: The loaded volume as a NumPy array.
Raises: Raises:
ValueError: If the format is not supported or the path is invalid. ValueError: If the format is not supported
FileNotFoundError: If the file or directory does not exist. ValueError: If the file or directory does not exist.
Example: Example:
loader = DataLoader() loader = qim3d.io.DataLoader()
data = loader.load("image.tif") data = loader.load("image.tif")
""" """
...@@ -286,7 +283,7 @@ class DataLoader: ...@@ -286,7 +283,7 @@ class DataLoader:
else: else:
# Find the closest matching path to warn the user # Find the closest matching path to warn the user
parent_dir = os.path.dirname(path) or '.' parent_dir = os.path.dirname(path) or '.'
parent_files = os.listdir(parent_dir) parent_files = os.listdir(parent_dir) if os.path.isdir(parent_dir) else ''
valid_paths = [os.path.join(parent_dir, file) for file in parent_files] valid_paths = [os.path.join(parent_dir, file) for file in parent_files]
similar_paths = difflib.get_close_matches(path, valid_paths) similar_paths = difflib.get_close_matches(path, valid_paths)
if similar_paths: if similar_paths:
...@@ -317,24 +314,21 @@ def load( ...@@ -317,24 +314,21 @@ def load(
Args: Args:
path (str): The path to the file or directory. path (str): The path to the file or directory.
virtual_stack (bool, optional): Specifies whether to use virtual virtual_stack (bool, optional): Specifies whether to use virtual
stack when loading TIFF and HDF5 files. Default is False. stack when loading files. Default is False.
dataset_name (str, optional): Specifies the name of the dataset to be loaded 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) 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 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) 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.
**kwargs: Additional keyword arguments to be passed **kwargs: Additional keyword arguments to be passed
to the DataLoader constructor. to the DataLoader constructor.
Returns: Returns:
numpy.ndarray: The loaded volume as a NumPy array. numpy.ndarray: The loaded volume as a NumPy array.
If 'return_metadata' is True and file format is either HDF5 or TXRM/TXM/XRM, returns a tuple (volume, metadata).
Raises:
ValueError: If the file format is not supported or the path is invalid.
NotImplementedError: If loading from a directory is not implemented yet.
FileNotFoundError: If the file or directory does not exist.
Example: Example:
data = load("image.tif", virtual_stack=True) data = qim3d.io.load("image.tif", virtual_stack=True)
""" """
loader = DataLoader( loader = DataLoader(
......
import qim3d
import numpy as np
from pathlib import Path
import os
import pytest
# Load blobs volume into memory
vol = qim3d.examples.blobs_256x256
# Ceate memory map to blobs
blobs_path = str(Path(qim3d.__file__).parents[0] / "img_examples" / "blobs_256x256.tif")
vol_memmap = qim3d.io.load(blobs_path,virtual_stack=True)
def test_load_shape():
assert vol.shape == vol_memmap.shape == (256,256)
def test_load_type():
assert isinstance(vol,np.ndarray)
def test_load_type_memmap():
assert isinstance(vol_memmap,np.memmap)
def test_invalid_path():
invalid_path = os.path.join('this','path','doesnt','exist.tif')
with pytest.raises(ValueError,match='Invalid path'):
qim3d.io.load(invalid_path)
def test_did_you_mean():
# Remove last two characters from the path
blobs_path_misspelled = blobs_path[:-2]
with pytest.raises(ValueError,match=f"Invalid path.\nDid you mean '{blobs_path}'?"):
qim3d.io.load(blobs_path_misspelled)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment