|
22 | 22 | from ..utils import is_fs_path |
23 | 23 | from .data_format import DataFormat |
24 | 24 |
|
| 25 | +TS_CONTEXT = tensorstore.Context() |
| 26 | + |
25 | 27 |
|
26 | 28 | def _is_power_of_two(num: int) -> bool: |
27 | 29 | return num & (num - 1) == 0 |
@@ -442,6 +444,14 @@ def _make_kvstore(path: Path) -> str | dict[str, str | list[str]]: |
442 | 444 | else: |
443 | 445 | kvstore_spec["aws_credentials"] = {"type": "default"} |
444 | 446 | return kvstore_spec |
| 447 | + elif isinstance(path, UPath) and path.protocol == "memory": |
| 448 | + # use memory driver (in-memory file systems), e.g. useful for testing |
| 449 | + # attention: this is not a persistent storage and it does not support |
| 450 | + # multiprocessing since memory is not shared between processes |
| 451 | + return { |
| 452 | + "driver": "memory", |
| 453 | + "path": path.path, |
| 454 | + } |
445 | 455 | else: |
446 | 456 | return { |
447 | 457 | "driver": "file", |
@@ -470,6 +480,7 @@ def _open(cls, path: Path) -> Self: |
470 | 480 | }, |
471 | 481 | open=True, |
472 | 482 | create=False, |
| 483 | + context=TS_CONTEXT, |
473 | 484 | ).result() # check that everything exists |
474 | 485 | return cls(path, _array) |
475 | 486 | except Exception as exc: |
@@ -532,7 +543,8 @@ def resize(self, new_bbox: NDBoundingBox) -> None: |
532 | 543 | { |
533 | 544 | "driver": str(self.data_format), |
534 | 545 | "kvstore": self._make_kvstore(self._path), |
535 | | - } |
| 546 | + }, |
| 547 | + context=TS_CONTEXT, |
536 | 548 | ).result() |
537 | 549 | if array.domain != current_array.domain: |
538 | 550 | raise RuntimeError( |
@@ -632,7 +644,8 @@ def _array(self) -> tensorstore.TensorStore: |
632 | 644 | { |
633 | 645 | "driver": str(self.data_format), |
634 | 646 | "kvstore": self._make_kvstore(self._path), |
635 | | - } |
| 647 | + }, |
| 648 | + context=TS_CONTEXT, |
636 | 649 | ).result() |
637 | 650 | except Exception as e: |
638 | 651 | raise ArrayException( |
@@ -780,7 +793,8 @@ def create(cls, path: Path, array_info: ArrayInfo) -> "Zarr3Array": |
780 | 793 | ], |
781 | 794 | }, |
782 | 795 | "create": True, |
783 | | - } |
| 796 | + }, |
| 797 | + context=TS_CONTEXT, |
784 | 798 | ).result() |
785 | 799 | return cls(path, _array) |
786 | 800 |
|
@@ -856,7 +870,8 @@ def create(cls, path: Path, array_info: ArrayInfo) -> "Zarr2Array": |
856 | 870 | "dimension_separator": "/", |
857 | 871 | }, |
858 | 872 | "create": True, |
859 | | - } |
| 873 | + }, |
| 874 | + context=TS_CONTEXT, |
860 | 875 | ).result() |
861 | 876 | return cls(path, _array) |
862 | 877 |
|
|
0 commit comments