Skip to content

Commit 041fab0

Browse files
authored
Merge pull request #20 from AllenNeuralDynamics/release-v0.1.8
Release v0.1.8
2 parents 2eddd4e + d5d7dc7 commit 041fab0

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Init package"""
22

3-
__version__ = "0.1.7"
3+
__version__ = "0.1.8"

src/aind_smartspim_data_transformation/compress/png_to_zarr.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
from ome_zarr.io import parse_url
2424
from ome_zarr.writer import write_multiscales_metadata
2525
from skimage.io import imread as sk_imread
26+
from zarr.errors import ContainsGroupError
27+
from zarr.storage import contains_group
2628

2729
from aind_smartspim_data_transformation.compress.zarr_writer import (
2830
BlockedArrayWriter,
@@ -498,6 +500,36 @@ def lazy_tiff_reader(
498500
return Array(dask_arr, name, chunks, dtype)
499501

500502

503+
def safe_create_zarr_group(store, path: str = "", **kwargs) -> zarr.Group:
504+
"""
505+
Safe creation of the zarr group.
506+
507+
Parameters
508+
----------
509+
store : MutableMapping or string
510+
Zarr store (e.g., directory, zip file, etc.)
511+
path : str
512+
Path to the group inside the store (default: root group)
513+
514+
Returns
515+
-------
516+
zarr.Group
517+
The Zarr group object
518+
"""
519+
if contains_group(store, path=path):
520+
# Group already exists; open in read/write mode
521+
return zarr.open_group(store=store, path=path, mode="r+")
522+
else:
523+
# Attempt to create;
524+
# catch race condition where another worker just created it
525+
try:
526+
return zarr.group(
527+
store=store, path=path, overwrite=False, **kwargs
528+
)
529+
except ContainsGroupError:
530+
return zarr.open_group(store=store, path=path, mode="r+")
531+
532+
501533
def smartspim_channel_zarr_writer(
502534
image_data: ArrayLike,
503535
output_path: PathLike,
@@ -567,7 +599,7 @@ def smartspim_channel_zarr_writer(
567599

568600
# Creating Zarr dataset
569601
store = parse_url(path=output_path, mode="w").store
570-
root_group = zarr.group(store=store)
602+
root_group = safe_create_zarr_group(store=store)
571603

572604
# Using 1 thread since is in single machine.
573605
# Avoiding the use of multithreaded due to GIL

0 commit comments

Comments
 (0)