Skip to content

Commit

Permalink
Add a ZARR to TIFF converter
Browse files Browse the repository at this point in the history
  • Loading branch information
Torec Luik committed Sep 21, 2023
1 parent 56aaa7b commit 0cd2b2a
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
36 changes: 34 additions & 2 deletions omero_slurm_client/slurm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,40 @@ def setup_slurm(self):
elif self.slurm_script_path:
# generate scripts
self.update_slurm_scripts(generate_jobs=True)

# 3. Download workflow images

# 3. Setup converters
slurm_convert_path = self.slurm_images_path + "/converters"
convert_cmds = []
if self.slurm_images_path and slurm_convert_path:
convert_cmds.append(f"mkdir -p {slurm_convert_path}")
r = self.run_commands(convert_cmds)
# currently known converters
# 3a. ZARR to TIFF
convert_name = "convert_zarr_to_tiff"
convert_py = f"{convert_name}.py"
convert_script_local = files("resources").joinpath(
convert_py)
convert_def = f"{convert_name}.def"
convert_def_local = files("resources").joinpath(
convert_def)
copy_script = self.put(local=convert_script_local,
remote=slurm_convert_path)
copy_def = self.put(local=convert_def_local,
remote=slurm_convert_path)
if not copy_def.ok or not copy_script.ok:
raise SSHException(f"Failed to copy the ZARR-to-TIFF converter \
scripts: {copy_script} / {copy_def}")
# Build singularity container from definition
with self.cd(slurm_convert_path):
convert_cmds = []
if self.slurm_images_path:
# TODO Change the tmp dir
# export SINGULARITY_TMPDIR=~/my-scratch/tmp;
convert_cmds.append(
f"singularity build {convert_name}.sif {convert_def}")
r = self.run_commands(convert_cmds)

# 4. Download workflow images
# Create specific workflow dirs
with self.cd(self.slurm_images_path):
if self.slurm_model_paths:
Expand Down
14 changes: 14 additions & 0 deletions resources/convert_zarr_to_tiff.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Bootstrap: docker
From: python:3.9-slim

%post
# Install required libraries with specific versions
pip install zarr==2.8.3 tifffile==2020.9.3 dask==2021.3.0


%files
convert_zarr_to_tiff.py /app/convert_zarr_to_tiff.py

%runscript
# Run the script with the provided arguments
exec python /app/convert_zarr_to_tiff.py "$@"
59 changes: 59 additions & 0 deletions resources/convert_zarr_to_tiff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import argparse
import os
import zarr
import tifffile as tf
import dask.array as da
import logging


def convert_zarr_to_tiff(zarr_file_path, key=None, output_file=None):
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

try:
# Open the Zarr file
zarr_file = zarr.open(zarr_file_path, mode='r')

# Get the available keys
available_keys = list(zarr_file.keys())

if len(available_keys) == 0:
raise ValueError("No keys found in the Zarr file.")

# Determine the key to use for conversion
if key is None:
# If no key is specified, use the first available key
key = available_keys[0]
elif key not in available_keys:
raise ValueError(f"Specified key '{key}' not found in the Zarr file.")

# Create a Dask array from the specified Zarr key (lazy loading)
dask_image_data = da.from_zarr(zarr_file[key])

# Generate the default output file name based on the input file name and key
input_file_name = os.path.basename(zarr_file_path)
if output_file is None:
output_file = os.path.splitext(input_file_name)[0] + f".{key}.tif"

# Write the Dask array to the TIFF file
tf.imwrite(output_file, dask_image_data)

logger.info(f"Conversion completed successfully with key: '{key}'.")
logger.info(f"Output TIFF file: '{output_file}'")

except Exception as e:
# Log the error message and raise the exception
logger.error(f"An error occurred: {e}")
raise


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert a Zarr file to a TIFF file.")
parser.add_argument("zarr_file", help="Path to the input Zarr file")
parser.add_argument("--key", help="Key name for the Zarr dataset to convert")
parser.add_argument("--output", help="Path to the output TIFF file (optional)")

args = parser.parse_args()

convert_zarr_to_tiff(args.zarr_file, args.key, args.output)

0 comments on commit 0cd2b2a

Please sign in to comment.