Skip to content
Snippets Groups Projects

First version of tiff stack loader

1 file
+ 18
4
Compare changes
  • Side-by-side
  • Inline
+ 18
4
@@ -23,7 +23,7 @@ class DataLoader:
Methods:
load_tiff(path): Load a TIFF file from the specified path.
load_h5(path): Load an HDF5 file from the specified path.
load_tiff_dir(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(path): Load a file or directory based on the given path.
Raises:
@@ -122,7 +122,7 @@ class DataLoader:
else:
if self.dataset_name: # Dataset name is provided
similar_names = difflib.get_close_matches(
self.dataset_name, list(datasets.keys())
self.dataset_name, datasets
) # Find closest matching name if any
if similar_names:
suggestion = similar_names[0] # Get the closest match
@@ -157,7 +157,7 @@ class DataLoader:
else:
return vol
def load_tiff_dir(self, path):
def load_tiff_stack(self, path):
"""Load a stack of TIFF files from the specified path.
Args:
@@ -168,6 +168,7 @@ class DataLoader:
Raises:
ValueError: If the 'contains' argument is not specified.
ValueError: If the 'contains' argument matches multiple TIFF stacks in the directory
"""
if not self.contains:
@@ -177,6 +178,19 @@ class DataLoader:
tiff_stack = [file for file in os.listdir(path) if (file.endswith('.tif') or file.endswith('.tiff')) and self.contains in file]
tiff_stack.sort() # Ensure proper ordering
# Check that only one TIFF stack in the directory contains the provided string in its name
tiff_stack_only_letters = []
for filename in tiff_stack:
name = os.path.splitext(filename)[0] # Remove file extension
tiff_stack_only_letters.append(''.join(filter(str.isalpha, name))) # Remove everything else than letters from the name
# Get unique elements from tiff_stack_only_letters
unique_names = list(set(tiff_stack_only_letters))
if len(unique_names)>1:
raise ValueError(f"The provided part of the filename for the TIFF stack matches multiple TIFF stacks: {unique_names}.\nPlease provide a string that is unique for the TIFF stack that is intended to be loaded")
vol = tifffile.imread([os.path.join(path, file) for file in tiff_stack])
if not self.virtual_stack:
@@ -220,7 +234,7 @@ class DataLoader:
# Load a directory
elif os.path.isdir(path):
return self.load_tiff_dir(path)
return self.load_tiff_stack(path)
# Fails
else:
Loading