|
25 | 25 | array_metadata, |
26 | 26 | arrays, |
27 | 27 | basic_indices, |
| 28 | + block_indices, |
28 | 29 | complex_rectilinear_arrays, |
| 30 | + np_array_and_chunks, |
29 | 31 | numpy_arrays, |
30 | 32 | orthogonal_indices, |
31 | 33 | rectilinear_arrays, |
@@ -230,6 +232,63 @@ async def test_vindex(data: st.DataObject) -> None: |
230 | 232 | # note: async vindex setitem not yet implemented |
231 | 233 |
|
232 | 234 |
|
| 235 | +@settings(deadline=None) |
| 236 | +@pytest.mark.filterwarnings("ignore::zarr.core.dtype.common.UnstableSpecificationWarning") |
| 237 | +@given(data=st.data()) |
| 238 | +def test_mask_indexing(data: st.DataObject) -> None: |
| 239 | + zarray = data.draw(st.one_of(simple_arrays(), rectilinear_arrays())) |
| 240 | + nparray = zarray[:] |
| 241 | + mask = data.draw(npst.arrays(dtype=np.bool_, shape=st.just(nparray.shape))) |
| 242 | + |
| 243 | + expected = nparray[mask] |
| 244 | + |
| 245 | + # sync get, via both the dedicated method and the vindex interface |
| 246 | + assert_array_equal(expected, zarray.get_mask_selection(mask)) |
| 247 | + assert_array_equal(expected, zarray.vindex[mask]) |
| 248 | + |
| 249 | + # sync set, via both interfaces |
| 250 | + assume(zarray.shards is None) # GH2834 |
| 251 | + new_data = data.draw(numpy_arrays(shapes=st.just(expected.shape), dtype=nparray.dtype)) |
| 252 | + nparray[mask] = new_data |
| 253 | + zarray.set_mask_selection(mask, new_data) |
| 254 | + assert_array_equal(nparray, zarray[:]) |
| 255 | + |
| 256 | + zarray.vindex[mask] = new_data |
| 257 | + assert_array_equal(nparray, zarray[:]) |
| 258 | + |
| 259 | + |
| 260 | +@settings(deadline=None) |
| 261 | +@pytest.mark.filterwarnings("ignore::zarr.core.dtype.common.UnstableSpecificationWarning") |
| 262 | +@given(data=st.data()) |
| 263 | +def test_block_indexing(data: st.DataObject) -> None: |
| 264 | + # Block indexing addresses whole chunks on a regular grid; the array-space |
| 265 | + # oracle in block_indices() assumes regular, unsharded chunks, so build the |
| 266 | + # array directly from a regular chunking rather than drawing one that might |
| 267 | + # be rectilinear or sharded. |
| 268 | + nparray, chunks = data.draw( |
| 269 | + np_array_and_chunks(arrays=numpy_arrays(shapes=npst.array_shapes(max_dims=4, min_side=1))) |
| 270 | + ) |
| 271 | + store = data.draw(stores) |
| 272 | + zarray = zarr.create_array(store=store, shape=nparray.shape, chunks=chunks, dtype=nparray.dtype) |
| 273 | + zarray[...] = nparray |
| 274 | + |
| 275 | + block_indexer, array_indexer = data.draw(block_indices(shape=nparray.shape, chunks=chunks)) |
| 276 | + expected = nparray[array_indexer] |
| 277 | + |
| 278 | + # sync get, via both the .blocks interface and the dedicated method |
| 279 | + assert_array_equal(expected, zarray.blocks[block_indexer]) |
| 280 | + assert_array_equal(expected, zarray.get_block_selection(block_indexer)) |
| 281 | + |
| 282 | + # sync set, via both interfaces |
| 283 | + new_data = data.draw(numpy_arrays(shapes=st.just(expected.shape), dtype=nparray.dtype)) |
| 284 | + nparray[array_indexer] = new_data |
| 285 | + zarray.blocks[block_indexer] = new_data |
| 286 | + assert_array_equal(nparray, zarray[:]) |
| 287 | + |
| 288 | + zarray.set_block_selection(block_indexer, new_data) |
| 289 | + assert_array_equal(nparray, zarray[:]) |
| 290 | + |
| 291 | + |
233 | 292 | @given(store=stores, meta=array_metadata()) # type: ignore[misc] |
234 | 293 | @pytest.mark.filterwarnings("ignore::zarr.core.dtype.common.UnstableSpecificationWarning") |
235 | 294 | async def test_roundtrip_array_metadata_from_store( |
|
0 commit comments