Skip to content
Snippets Groups Projects
Commit 5828cda7 authored by Christian Kento Rasmussen's avatar Christian Kento Rasmussen
Browse files

finalized convert function for zarr to tiff

parent ebc8ab67
No related branches found
No related tags found
4 merge requests!102Conv zarr tiff folders,!100Conv zarr nifti,!99Zarr cli,!96Zarr loading and converting
...@@ -2,4 +2,5 @@ from .loading import DataLoader, load, ImgExamples ...@@ -2,4 +2,5 @@ from .loading import DataLoader, load, ImgExamples
from .downloader import Downloader from .downloader import Downloader
from .saving import DataSaver, save from .saving import DataSaver, save
from .sync import Sync from .sync import Sync
from .convert import convert
from . import logger from . import logger
\ No newline at end of file
...@@ -23,13 +23,14 @@ class Convert: ...@@ -23,13 +23,14 @@ class Convert:
def convert(self, input_path, output_path): def convert(self, input_path, output_path):
# Stringify path in case it is not already a string # Stringify path in case it is not already a string
input_path = stringify_path(input_path) input_path = stringify_path(input_path)
output_path = stringify_path(output_path)
if os.path.isfile(input_path) and os.path.isfile(output_path): if os.path.isfile(input_path):
match input_path, output_path: input_ext = os.path.splitext(input_path)[1]
output_ext = os.path.splitext(output_path)[1]
match input_ext, output_ext:
case (".tif", ".zarr"): case (".tif", ".zarr"):
return self.convert_tif_to_zarr(input_path, output_path) return self.convert_tif_to_zarr(input_path, output_path)
case (".npy", ".zarr"):
return self.convert_npy_to_zarr(input_path, output_path, shape=(64, 64, 64))
case (".zarr", ".tif"): case (".zarr", ".tif"):
return self.convert_zarr_to_tif(input_path, output_path) return self.convert_zarr_to_tif(input_path, output_path)
case _: case _:
...@@ -75,23 +76,6 @@ class Convert: ...@@ -75,23 +76,6 @@ class Convert:
return z return z
def convert_npy_to_zarr(self, npy_path, zarr_path, shape, dtype=np.float32, chunks=(64, 64, 64)):
""" Convert a numpy file to a zarr file
Args:
npy_path (str): path to the numpy file
zarr_path (str): path to the zarr file
chunks (tuple, optional): chunk size for the zarr file. Defaults to (64, 64, 64).
Returns:
zarr.core.Array: zarr array containing the data from the numpy file
"""
vol = np.memmap(npy_path, dtype=dtype, mode='r', shape=shape)
z = zarr.open(zarr_path, mode='w', shape=vol.shape, chunks=chunks, dtype=vol.dtype)
z[:] = vol[:]
return z
def convert_zarr_to_tif(self, zarr_path, tif_path): def convert_zarr_to_tif(self, zarr_path, tif_path):
""" Convert a zarr file to a tiff file """ Convert a zarr file to a tiff file
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment