|
14 | 14 | from upath import UPath |
15 | 15 |
|
16 | 16 | from ..geometry import Mag, NDBoundingBox, Vec3Int, Vec3IntLike |
17 | | -from ._array import ArrayException, BaseArray, DataFormat |
| 17 | +from ._array import ArrayException, BaseArray, DataFormat, ZarritaArray |
18 | 18 | from ._downsampling_utils import ( |
19 | 19 | calculate_default_coarsest_mag, |
20 | 20 | calculate_mags_to_downsample, |
|
44 | 44 | get_executor_for_args, |
45 | 45 | is_fs_path, |
46 | 46 | is_remote_path, |
| 47 | + movetree, |
47 | 48 | named_partial, |
48 | 49 | rmtree, |
49 | 50 | strip_trailing_slash, |
@@ -753,6 +754,50 @@ def add_fs_copy_mag( |
753 | 754 |
|
754 | 755 | return mag |
755 | 756 |
|
| 757 | + def add_mag_from_zarrarray( |
| 758 | + self, |
| 759 | + mag: Union[int, str, list, tuple, np.ndarray, Mag], |
| 760 | + path: PathLike, |
| 761 | + move: bool = False, |
| 762 | + extend_layer_bounding_box: bool = True, |
| 763 | + ) -> MagView: |
| 764 | + """ |
| 765 | + Copies the data at `path` to the current layer of the dataset |
| 766 | + via the filesystem and adds it as `mag`. When `move` flag is set |
| 767 | + the array is moved, otherwise a copy of the zarrarray is created. |
| 768 | + """ |
| 769 | + self.dataset._ensure_writable() |
| 770 | + source_path = Path(path) |
| 771 | + |
| 772 | + try: |
| 773 | + ZarritaArray.open(source_path) |
| 774 | + except ArrayException as e: |
| 775 | + raise ValueError( |
| 776 | + "The given path does not lead to a valid Zarr Array: " |
| 777 | + ) from e |
| 778 | + else: |
| 779 | + mag = Mag(mag) |
| 780 | + self._assert_mag_does_not_exist_yet(mag) |
| 781 | + if move: |
| 782 | + movetree(source_path, self.path / str(mag)) |
| 783 | + else: |
| 784 | + copytree(source_path, self.path / str(mag)) |
| 785 | + |
| 786 | + mag_view = self.add_mag_for_existing_files(mag) |
| 787 | + |
| 788 | + if extend_layer_bounding_box: |
| 789 | + # assumption: the topleft of the bounding box is still the same, the size might differ |
| 790 | + # axes of the layer and the zarr array provided are the same |
| 791 | + zarray_size = ( |
| 792 | + mag_view.info.shape[mag_view.info.dimension_names.index(axis)] |
| 793 | + for axis in self.bounding_box.axes |
| 794 | + if axis != "c" |
| 795 | + ) |
| 796 | + size = self.bounding_box.size.pairmax(zarray_size) |
| 797 | + self.bounding_box = self.bounding_box.with_size(size) |
| 798 | + |
| 799 | + return mag_view |
| 800 | + |
756 | 801 | def _create_dir_for_mag( |
757 | 802 | self, mag: Union[int, str, list, tuple, np.ndarray, Mag] |
758 | 803 | ) -> None: |
|
0 commit comments