Skip to content

Commit a52981e

Browse files
committed
updating zarr safe creation to use contains_group native function
1 parent 4771d03 commit a52981e

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

src/aind_smartspim_data_transformation/compress/png_to_zarr.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from ome_zarr.writer import write_multiscales_metadata
2525
from skimage.io import imread as sk_imread
2626
from zarr.errors import ContainsGroupError
27+
from zarr.storage import contains_group
2728

2829
from aind_smartspim_data_transformation.compress.zarr_writer import (
2930
BlockedArrayWriter,
@@ -505,22 +506,28 @@ def safe_create_zarr_group(store, path: str = "", **kwargs) -> zarr.Group:
505506
506507
Parameters
507508
----------
508-
store
509-
Zarr store
510-
path: str
511-
Path to the creation of the zarr group
512-
Default: ''
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)
513513
514514
Returns
515515
-------
516-
Zarr.group
517-
Zarr group pointing to where the data is written
516+
zarr.Group
517+
The Zarr group object
518518
"""
519-
try:
520-
return zarr.group(store=store, path=path, overwrite=False, **kwargs)
521-
except ContainsGroupError:
522-
# Group already exists, which is expected with multiple workers
519+
if contains_group(store, path=path):
520+
# Group already exists; open in read/write mode
523521
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+")
524531

525532

526533
def smartspim_channel_zarr_writer(

0 commit comments

Comments
 (0)