|
23 | 23 | from ome_zarr.io import parse_url |
24 | 24 | from ome_zarr.writer import write_multiscales_metadata |
25 | 25 | from skimage.io import imread as sk_imread |
| 26 | +from zarr.errors import ContainsGroupError |
| 27 | +from zarr.storage import contains_group |
26 | 28 |
|
27 | 29 | from aind_smartspim_data_transformation.compress.zarr_writer import ( |
28 | 30 | BlockedArrayWriter, |
@@ -498,6 +500,36 @@ def lazy_tiff_reader( |
498 | 500 | return Array(dask_arr, name, chunks, dtype) |
499 | 501 |
|
500 | 502 |
|
| 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 | + |
501 | 533 | def smartspim_channel_zarr_writer( |
502 | 534 | image_data: ArrayLike, |
503 | 535 | output_path: PathLike, |
@@ -567,7 +599,7 @@ def smartspim_channel_zarr_writer( |
567 | 599 |
|
568 | 600 | # Creating Zarr dataset |
569 | 601 | 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) |
571 | 603 |
|
572 | 604 | # Using 1 thread since is in single machine. |
573 | 605 | # Avoiding the use of multithreaded due to GIL |
|
0 commit comments