|
5 | 5 | instead we're providing tools to hack instances of it |
6 | 6 | """ |
7 | 7 | import numpy as np |
8 | | -from zarr.core import Array |
| 8 | +import zarr |
| 9 | + |
| 10 | +from packaging import version |
9 | 11 |
|
| 12 | +from zarr.core import Array |
10 | 13 | # import other zarr gubbins used in the methods we override |
11 | 14 | from zarr.indexing import ( |
12 | 15 | OrthogonalIndexer, |
@@ -171,18 +174,32 @@ def as_get_selection(self, indexer, out=None, |
171 | 174 | chunks_info = [] |
172 | 175 | chunks_locs = [] |
173 | 176 |
|
| 177 | + # note: Zarr API has changed with zarr=2.15 |
| 178 | + # hasattr(self.chunk_store, "getitems") = True for zarr >= 2.15 |
| 179 | + zarr_version = zarr.__version__ |
| 180 | + zarr_api_change_version = "2.15" |
| 181 | + if version.parse(zarr_version) < version.parse(zarr_api_change_version): |
| 182 | + att_getitems = not hasattr(self.chunk_store, "getitems") |
| 183 | + elif version.parse(zarr_version) >= version.parse(zarr_api_change_version): |
| 184 | + att_getitems = hasattr(self.chunk_store, "getitems") |
174 | 185 | # iterate over chunks |
175 | | - if not hasattr(self.chunk_store, "getitems") or \ |
176 | | - any(map(lambda x: x == 0, self.shape)): |
| 186 | + if att_getitems or any(map(lambda x: x == 0, self.shape)): |
177 | 187 | # sequentially get one key at a time from storage |
178 | 188 | for chunk_coords, chunk_selection, out_selection in indexer: |
179 | 189 |
|
180 | | - # load chunk selection into output array |
181 | | - pci = self._chunk_getitem(chunk_coords, chunk_selection, out, out_selection, |
182 | | - drop_axes=indexer.drop_axes, fields=fields) |
183 | | - |
184 | | - chunks_info.append(pci) |
185 | | - chunks_locs.append(chunk_coords) |
| 190 | + if version.parse(zarr_version) < version.parse(zarr_api_change_version): |
| 191 | + # load chunk selection into output array |
| 192 | + pci = self._chunk_getitem(chunk_coords, chunk_selection, out, out_selection, |
| 193 | + drop_axes=indexer.drop_axes, fields=fields) |
| 194 | + chunks_info.append(pci) |
| 195 | + chunks_locs.append(chunk_coords) |
| 196 | + elif version.parse(zarr_version) >= version.parse(zarr_api_change_version): |
| 197 | + chunk_coords = [chunk_coords] |
| 198 | + # load chunk selection into output array |
| 199 | + pci = self._chunk_getitems(chunk_coords, chunk_selection, out, out_selection, |
| 200 | + drop_axes=indexer.drop_axes, fields=fields) |
| 201 | + chunks_info.append(pci) |
| 202 | + chunks_locs.append(chunk_coords[0]) |
186 | 203 | else: |
187 | 204 | # allow storage to get multiple items at once |
188 | 205 | lchunk_coords, lchunk_selection, lout_selection = zip(*indexer) |
|
0 commit comments