Skip to content
Snippets Groups Projects
Commit effcf627 authored by Felipe Delestro Matos's avatar Felipe Delestro Matos
Browse files

load module docstrings

parent 5e0012c3
Branches
Tags
No related merge requests found
import tifffile """Provides functionality for loading data from various file formats."""
import h5py
import os import os
import sys import sys
import difflib import difflib
import tifffile
import h5py
from qim3d.io.logger import log from qim3d.io.logger import log
from qim3d.tools.internal_tools import sizeof from qim3d.tools.internal_tools import sizeof
class DataLoader: class DataLoader:
"""Utility class for loading data from different file formats.
Args:
virtual_stack (bool, optional): Specifies whether to use virtual stack
when loading TIFF files. Default is False.
Attributes:
virtual_stack (bool): Specifies whether virtual stack is enabled.
Methods:
load_tiff(path): Load a TIFF file from the specified path.
load_h5(path): Load an HDF5 file from the specified 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:
loader = DataLoader(virtual_stack=True)
data = loader.load_tiff("image.tif")
"""
def __init__(self, **kwargs): def __init__(self, **kwargs):
"""Initializes a new instance of the DataLoader class.
Args:
virtual_stack (bool, optional): Specifies whether to use virtual
stack when loading TIFF files. Default is False.
"""
# Virtual stack is False by default # Virtual stack is False by default
self.virtual_stack = kwargs.get("virtual_stack", False) self.virtual_stack = kwargs.get("virtual_stack", False)
def load_tiff(self, path): def load_tiff(self, path):
"""Load a TIFF file from the specified path.
Args:
path (str): The path to the TIFF file.
Returns:
numpy.ndarray: The loaded volume as a NumPy array.
Raises:
FileNotFoundError: If the file does not exist.
Example:
loader = DataLoader()
data = loader.load_tiff("image.tif")
"""
if self.virtual_stack: if self.virtual_stack:
log.info("Using virtual stack") log.info("Using virtual stack")
vol = tifffile.memmap(path) vol = tifffile.memmap(path)
else: else:
vol = tifffile.imread(path) vol = tifffile.imread(path)
log.info(f"Loaded shape: {vol.shape}") log.info("Loaded shape: %s", vol.shape)
log.info(f"Using {sizeof(sys.getsizeof(vol))} of memory") log.info("Using %s of memory", sizeof(sys.getsizeof(vol)))
return vol return vol
def load_h5(self, path): def load_h5(self, path):
"""Load an HDF5 file from the specified path.
Args:
path (str): The path to the HDF5 file.
Returns:
numpy.ndarray: The loaded volume as a NumPy array.
Raises:
FileNotFoundError: If the file does not exist.
Example:
loader = DataLoader()
data = loader.load_h5("data.h5")
"""
with h5py.File(path, "r") as f: with h5py.File(path, "r") as f:
vol = f["data"][:] vol = f["data"][:]
return vol return vol
def load(self, path): def load(self, path):
"""
Load a file or directory based on the given path.
Args:
path (str): The path to the file or directory.
Returns:
numpy.ndarray: The loaded volume as a NumPy array.
Raises:
ValueError: If the format is not supported or the path is invalid.
FileNotFoundError: If the file or directory does not exist.
Example:
loader = DataLoader()
data = loader.load("image.tif")
"""
# Load a file # Load a file
if os.path.isfile(path): if os.path.isfile(path):
# Choose the loader based on the file extension # Choose the loader based on the file extension
...@@ -60,6 +137,28 @@ class DataLoader: ...@@ -60,6 +137,28 @@ class DataLoader:
def load(path, virtual_stack=False, **kwargs): def load(path, virtual_stack=False, **kwargs):
"""
Load data from the specified file or directory.
Args:
path (str): The path to the file or directory.
virtual_stack (bool, optional): Specifies whether to use virtual
stack when loading TIFF files. Default is False.
**kwargs: Additional keyword arguments to be passed
to the DataLoader constructor.
Returns:
numpy.ndarray: The loaded volume as a NumPy array.
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:
data = load("image.tif", virtual_stack=True)
"""
loader = DataLoader(virtual_stack=virtual_stack, **kwargs) loader = DataLoader(virtual_stack=virtual_stack, **kwargs)
return loader.load(path) return loader.load(path)
""" Provides a collection of internal utility functions. """ Provides a collection of internal utility functions."""
This module contains various utility functions that are intended for internal
use within the project. These functions cover a range of tasks such as size
formatting, file manipulation, network operations, and more. They are designed
to assist with common tasks and provide convenient functionality for other
components of the project.
Example:
# Importing the module
import internal_tools
# Using the functions
size = internal_tools.sizeof(1024)
internal_tools.mock_write_file("example.txt")
ip_address = internal_tools.get_local_ip()
internal_tools.gradio_header("Gradio server", 4242)
"""
import socket import socket
import hashlib import hashlib
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment