|
24 | 24 | from ome_zarr.writer import write_multiscales_metadata |
25 | 25 | from skimage.io import imread as sk_imread |
26 | 26 | from zarr.errors import ContainsGroupError |
| 27 | +from zarr.storage import contains_group |
27 | 28 |
|
28 | 29 | from aind_smartspim_data_transformation.compress.zarr_writer import ( |
29 | 30 | BlockedArrayWriter, |
@@ -505,22 +506,28 @@ def safe_create_zarr_group(store, path: str = "", **kwargs) -> zarr.Group: |
505 | 506 |
|
506 | 507 | Parameters |
507 | 508 | ---------- |
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) |
513 | 513 |
|
514 | 514 | Returns |
515 | 515 | ------- |
516 | | - Zarr.group |
517 | | - Zarr group pointing to where the data is written |
| 516 | + zarr.Group |
| 517 | + The Zarr group object |
518 | 518 | """ |
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 |
523 | 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+") |
524 | 531 |
|
525 | 532 |
|
526 | 533 | def smartspim_channel_zarr_writer( |
|
0 commit comments