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

Changes in log messages

parent ca6de549
No related branches found
No related tags found
1 merge request!30Save tiff stack
This commit is part of merge request !30. Comments created here will be created in the context of that merge request.
......@@ -46,7 +46,6 @@ class DataSaver:
self.basename = kwargs.get("basename", None)
self.sliced_dim = kwargs.get("sliced_dim", 0)
def save_tiff(self, path, data):
"""Save data to a TIFF file to the given path.
......@@ -65,9 +64,10 @@ class DataSaver:
path (str): The directory to save files to
data (numpy.ndarray): The data to be saved
"""
extension = ".tif"
if data.ndim <= 2:
path = os.path.join(path,self.basename,'.tif')
path = os.path.join(path, self.basename, ".tif")
self.save_tiff(path, data)
else:
# get number of total slices
......@@ -82,12 +82,13 @@ class DataSaver:
for i in range(no_slices):
idx[self.sliced_dim] = i
sliced = data[tuple(idx)]
filename = self.basename + str(i).zfill(zfill_val) + '.tif'
filename = self.basename + str(i).zfill(zfill_val) + extension
filepath = os.path.join(path, filename)
self.save_tiff(filepath, sliced)
pattern_string = filepath[:-(len(extension)+zfill_val)] + "-"*zfill_val + extension
log.info(f"Total of {no_slices} files saved following the pattern '{pattern_string}'")
def save(self, path, data):
"""Save data to the given path.
......@@ -122,8 +123,10 @@ class DataSaver:
return self.save_tiff_stack(path, data)
# If basename is not provided
else:
raise ValueError("Please provide a basename with the keyword argument 'basename' if you want to save\n" +
f" a TIFF stack as several files to '{path}'. Otherwise, please provide a path with a valid filename")
raise ValueError(
f"To save a stack as several TIFF files to the directory '{path}', please provide the keyword argument 'basename'. "
+ "Otherwise, to save a single file, please provide a full path with a filename and valid extension."
)
# If path is not an existing directory
else:
......@@ -135,19 +138,19 @@ class DataSaver:
return self.save_tiff_stack(path, data)
# Check if a parent directory exists
parentdir = os.path.dirname(path) or '.'
parentdir = os.path.dirname(path) or "."
if os.path.isdir(parentdir):
# If there is a file extension in the path
if ext:
# If there is a basename
if self.basename:
# It will be unused and the user is informed accordingly
log.info(
"'basename' argument is unused"
)
log.info("'basename' argument is unused")
# Check if a file with the given path already exists
if os.path.isfile(path) and not self.replace:
raise ValueError("A file with the provided path already exists. To replace it set 'replace=True'")
raise ValueError(
"A file with the provided path already exists. To replace it set 'replace=True'"
)
if path.endswith((".tif", ".tiff")):
return self.save_tiff(path, data)
......@@ -155,20 +158,19 @@ class DataSaver:
raise ValueError("Unsupported file format")
# If there is no file extension in the path
else:
raise ValueError('Please provide a file extension if you want to save as a single file.'+
' Otherwise, please provide a basename to save as a TIFF stack')
raise ValueError(
"Please provide a file extension if you want to save as a single file."
+ " Otherwise, please provide a basename to save as a TIFF stack"
)
else:
raise ValueError(f"The directory {parentdir} does not exist. Please provide a valid directory or specify a basename\n"+
" if you want to save a tiff stack as several files to a folder that does not yet exist")
raise ValueError(
f"The directory '{parentdir}' does not exist.\n"
+ "Please provide a valid directory or specify a basename if you want to save a tiff stack as several files to a folder that does not yet exist"
)
def save(path,
data,
replace=False,
compression=False,
basename=None,
sliced_dim=0,
**kwargs
def save(
path, data, replace=False, compression=False, basename=None, sliced_dim=0, **kwargs
):
"""Save data to a specified file path.
......@@ -195,5 +197,5 @@ def save(path,
compression=compression,
basename=basename,
sliced_dim=sliced_dim,
**kwargs
**kwargs,
).save(path, data)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment