zarrs 0.23.9

A library for the Zarr storage format for multidimensional arrays and metadata
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! The `regular_bounded` chunk grid.
//!
//! This chunk grid is the same as `regular`, except that chunks at the edges of the array may be smaller than the specified chunk shape.
//!
//! <div class="warning">
//! This chunk grid is experimental and may be incompatible with other Zarr V3 implementations.
//! </div>
//!
//! ### Compatible Implementations
//! None
//!
//! ### Specification
//! - <https://chunkgrid.zarrs.dev/regular_bounded>
//!
//! ### Chunk Grid `name` Aliases (Zarr V3)
//! - `zarrs.regular_bounded`
//!
//! ### Chunk Grid `configuration` Example - [`RegularBoundedChunkGridConfiguration`]:
//! ```rust
//! # let JSON = r#"
//! {
//!   "chunk_shape": [100, 100]
//! }
//! # "#;
//! # use zarrs::array::chunk_grid::RegularBoundedChunkGridConfiguration;
//! # let configuration: RegularBoundedChunkGridConfiguration = serde_json::from_str(JSON).unwrap();
//! ```

use std::num::NonZeroU64;

use itertools::izip;

/// Configuration parameters for a `regular_bounded` chunk grid.
pub type RegularBoundedChunkGridConfiguration = super::RegularChunkGridConfiguration; // TODO: move to zarrs_metadata_ex on stabilisation

use crate::array::{
    ArrayIndices, ArrayShape, ArraySubset, ChunkShape, IncompatibleDimensionalityError,
};
use zarrs_chunk_grid::{ChunkGrid, ChunkGridPlugin, ChunkGridTraits};
use zarrs_metadata::Configuration;
use zarrs_metadata::v3::MetadataV3;
use zarrs_plugin::PluginCreateError;

zarrs_plugin::impl_extension_aliases!(RegularBoundedChunkGrid,
  v3: "zarrs.regular_bounded", []
);

// Register the chunk grid.
inventory::submit! {
    ChunkGridPlugin::new::<RegularBoundedChunkGrid>()
}

/// A `regular_bounded` chunk grid.
#[allow(clippy::struct_field_names)]
#[derive(Debug, Clone)]
pub struct RegularBoundedChunkGrid {
    array_shape: ArrayShape,
    grid_shape: ArrayShape,
    chunk_shape: ChunkShape,
}

impl RegularBoundedChunkGrid {
    /// Create a new `regular_bounded` chunk grid with chunk shape `chunk_shape`.
    ///
    /// # Errors
    /// Returns a [`IncompatibleDimensionalityError`] if `chunk_shape` is not compatible with the `array_shape`.
    pub fn new(
        array_shape: ArrayShape,
        chunk_shape: ChunkShape,
    ) -> Result<Self, IncompatibleDimensionalityError> {
        if array_shape.len() != chunk_shape.len() {
            return Err(IncompatibleDimensionalityError::new(
                chunk_shape.len(),
                array_shape.len(),
            ));
        }

        let grid_shape = std::iter::zip(&array_shape, chunk_shape.iter())
            .map(|(a, s)| {
                let s = s.get();
                a.div_ceil(s)
            })
            .collect();
        Ok(Self {
            array_shape,
            grid_shape,
            chunk_shape,
        })
    }
}

unsafe impl ChunkGridTraits for RegularBoundedChunkGrid {
    fn create(
        metadata: &MetadataV3,
        array_shape: &ArrayShape,
    ) -> Result<ChunkGrid, PluginCreateError> {
        crate::warn_experimental_extension(metadata.name(), "chunk grid");
        let configuration: RegularBoundedChunkGridConfiguration =
            metadata.to_typed_configuration()?;
        let chunk_grid = RegularBoundedChunkGrid::new(
            array_shape.clone(),
            configuration.chunk_shape,
        )
        .map_err(|_| {
            PluginCreateError::from(
                "`regular_bounded` chunk shape and array shape have inconsistent dimensionality",
            )
        })?;
        Ok(ChunkGrid::new(chunk_grid))
    }

    fn configuration(&self) -> Configuration {
        RegularBoundedChunkGridConfiguration {
            chunk_shape: self.chunk_shape.clone(),
        }
        .into()
    }

    fn dimensionality(&self) -> usize {
        self.chunk_shape.len()
    }

    fn array_shape(&self) -> &[u64] {
        &self.array_shape
    }

    fn grid_shape(&self) -> &[u64] {
        &self.grid_shape
    }

    fn chunk_shape(
        &self,
        chunk_indices: &[u64],
    ) -> Result<Option<ChunkShape>, IncompatibleDimensionalityError> {
        if chunk_indices.len() == self.dimensionality() {
            Ok(izip!(
                self.chunk_shape.as_slice(),
                &self.array_shape,
                chunk_indices
            )
            .map(|(chunk_shape, &array_shape, chunk_indices)| {
                let start = (chunk_indices * chunk_shape.get()).min(array_shape);
                let end = (start + chunk_shape.get()).min(array_shape);
                NonZeroU64::new(end.saturating_sub(start))
            })
            .collect::<Option<Vec<_>>>())
        } else {
            Err(IncompatibleDimensionalityError::new(
                chunk_indices.len(),
                self.dimensionality(),
            ))
        }
    }

    fn chunk_shape_u64(
        &self,
        chunk_indices: &[u64],
    ) -> Result<Option<ArrayShape>, IncompatibleDimensionalityError> {
        if chunk_indices.len() == self.dimensionality() {
            Ok(izip!(
                self.chunk_shape.as_slice(),
                &self.array_shape,
                chunk_indices
            )
            .map(|(chunk_shape, &array_shape, chunk_indices)| {
                let start = (chunk_indices * chunk_shape.get()).min(array_shape);
                let end = (start + chunk_shape.get()).min(array_shape);
                if end > start { Some(end - start) } else { None }
            })
            .collect::<Option<Vec<_>>>())
        } else {
            Err(IncompatibleDimensionalityError::new(
                chunk_indices.len(),
                self.dimensionality(),
            ))
        }
    }

    fn chunk_origin(
        &self,
        chunk_indices: &[u64],
    ) -> Result<Option<ArrayIndices>, IncompatibleDimensionalityError> {
        if chunk_indices.len() == self.dimensionality() {
            Ok(izip!(
                chunk_indices,
                self.chunk_shape.as_slice(),
                &self.array_shape
            )
            .map(|(chunk_index, chunk_shape, &array_shape)| {
                let start = chunk_index * chunk_shape.get();
                if start < array_shape {
                    Some(start)
                } else {
                    None
                }
            })
            .collect::<Option<Vec<_>>>())
        } else {
            Err(IncompatibleDimensionalityError::new(
                chunk_indices.len(),
                self.dimensionality(),
            ))
        }
    }

    fn chunk_indices(
        &self,
        array_indices: &[u64],
    ) -> Result<Option<ArrayIndices>, IncompatibleDimensionalityError> {
        if array_indices.len() == self.dimensionality() {
            Ok(izip!(
                array_indices,
                self.chunk_shape.as_slice(),
                &self.array_shape
            )
            .map(|(array_index, chunk_shape, array_shape)| {
                if array_index < array_shape {
                    Some(array_index / chunk_shape.get())
                } else {
                    None
                }
            })
            .collect::<Option<Vec<_>>>())
        } else {
            Err(IncompatibleDimensionalityError::new(
                array_indices.len(),
                self.dimensionality(),
            ))
        }
    }

    fn chunk_element_indices(
        &self,
        array_indices: &[u64],
    ) -> Result<Option<ArrayIndices>, IncompatibleDimensionalityError> {
        if array_indices.len() == self.dimensionality() {
            Ok(izip!(
                array_indices,
                self.chunk_shape.as_slice(),
                &self.array_shape
            )
            .map(|(array_index, chunk_shape, array_shape)| {
                if array_index < array_shape {
                    Some(array_index % chunk_shape.get())
                } else {
                    None
                }
            })
            .collect::<Option<Vec<_>>>())
        } else {
            Err(IncompatibleDimensionalityError::new(
                array_indices.len(),
                self.dimensionality(),
            ))
        }
    }

    fn subset(
        &self,
        chunk_indices: &[u64],
    ) -> Result<Option<ArraySubset>, IncompatibleDimensionalityError> {
        if chunk_indices.len() == self.dimensionality() {
            let ranges = izip!(
                self.chunk_shape.as_slice(),
                &self.array_shape,
                chunk_indices
            )
            .map(|(chunk_shape, &array_shape, chunk_indices)| {
                let start = (chunk_indices * chunk_shape.get()).min(array_shape);
                let end = (start + chunk_shape.get()).min(array_shape);
                if end > start { Some(start..end) } else { None }
            })
            .collect::<Option<Vec<_>>>();
            if let Some(ranges) = ranges {
                Ok(Some(ArraySubset::new_with_ranges(&ranges)))
            } else {
                Ok(None)
            }
        } else {
            Err(IncompatibleDimensionalityError::new(
                chunk_indices.len(),
                self.dimensionality(),
            ))
        }
    }
}

#[cfg(test)]
mod tests {
    use rayon::iter::ParallelIterator;

    use super::*;
    use crate::array::{ArrayIndicesTinyVec, ArraySubset};

    #[test]
    fn chunk_grid_regular_bounded_metadata() {
        let metadata: MetadataV3 = serde_json::from_str(
            r#"{"name":"zarrs.regular_bounded","configuration":{"chunk_shape":[1,2,3]}}"#,
        )
        .unwrap();
        assert!(RegularBoundedChunkGrid::create(&metadata, &vec![3, 3, 3]).is_ok());
    }

    #[test]
    fn chunk_grid_regular_bounded_metadata_invalid() {
        let metadata: MetadataV3 = serde_json::from_str(
            r#"{"name":"zarrs.regular_bounded","configuration":{"invalid":[1,2,3]}}"#,
        )
        .unwrap();
        assert!(RegularBoundedChunkGrid::create(&metadata, &vec![3, 3, 3]).is_err());
        assert_eq!(
            RegularBoundedChunkGrid::create(&metadata, &vec![3, 3, 3])
                .unwrap_err()
                .to_string(),
            r"configuration is unsupported: unknown field `invalid`, expected `chunk_shape`"
        );
    }

    #[allow(clippy::single_range_in_vec_init)]
    #[test]
    fn chunk_grid_regular_bounded() {
        let array_shape: ArrayShape = vec![5, 7, 52];
        let chunk_shape: ChunkShape = vec![
            NonZeroU64::new(1).unwrap(),
            NonZeroU64::new(2).unwrap(),
            NonZeroU64::new(3).unwrap(),
        ];

        {
            let chunk_grid =
                RegularBoundedChunkGrid::new(array_shape.clone(), chunk_shape.clone()).unwrap();
            assert_eq!(chunk_grid.dimensionality(), 3);
            assert_eq!(chunk_grid.array_shape(), &array_shape);
            assert_eq!(
                chunk_grid.chunk_origin(&[1, 1, 1]).unwrap(),
                Some(vec![1, 2, 3])
            );
            assert_eq!(
                chunk_grid.chunk_shape(&[0, 0, 0]).unwrap(),
                Some(chunk_shape.clone())
            );
            assert_eq!(
                chunk_grid.chunk_shape_u64(&[0, 0, 0]).unwrap(),
                Some(chunk_shape.iter().map(|u| u.get()).collect())
            );
            assert_eq!(
                chunk_grid.chunk_shape(&[0, 3, 17]).unwrap(),
                Some(vec![NonZeroU64::new(1).unwrap(); 3])
            );
            assert_eq!(chunk_grid.chunk_shape(&[5, 0, 0]).unwrap(), None);
            let chunk_grid_shape = chunk_grid.grid_shape();
            assert_eq!(chunk_grid_shape, &[5, 4, 18]);
            let array_index: ArrayIndices = vec![3, 5, 50];
            assert_eq!(
                chunk_grid.chunk_indices(&array_index).unwrap(),
                Some(vec![3, 2, 16])
            );
            assert_eq!(
                chunk_grid.chunk_element_indices(&array_index).unwrap(),
                Some(vec![0, 1, 2])
            );

            assert_eq!(
                chunk_grid.chunks_subset(&[1..3, 1..2, 5..8],).unwrap(),
                Some(ArraySubset::new_with_ranges(&[1..3, 2..4, 15..24]))
            );

            assert!(chunk_grid.chunks_subset(&[1..3]).is_err());

            assert!(
                chunk_grid
                    .chunks_subset(&[0..0, 0..0, 0..0],)
                    .unwrap()
                    .unwrap()
                    .is_empty()
            );
        }

        assert!(RegularBoundedChunkGrid::new(vec![0; 1], chunk_shape.clone()).is_err());
    }

    #[test]
    fn chunk_grid_regular_bounded_out_of_bounds() {
        let array_shape: ArrayShape = vec![5, 7, 52];
        let chunk_shape: ChunkShape = vec![
            NonZeroU64::new(1).unwrap(),
            NonZeroU64::new(2).unwrap(),
            NonZeroU64::new(3).unwrap(),
        ];
        let chunk_grid = RegularBoundedChunkGrid::new(array_shape, chunk_shape).unwrap();

        let array_indices: ArrayIndices = vec![3, 5, 53];
        assert_eq!(chunk_grid.chunk_indices(&array_indices).unwrap(), None);

        let chunk_indices: ArrayShape = vec![6, 1, 1];
        assert!(!chunk_grid.chunk_indices_inbounds(&chunk_indices));
        assert_eq!(chunk_grid.chunk_origin(&chunk_indices).unwrap(), None);
    }

    #[test]
    fn chunk_grid_regular_bounded_unlimited() {
        let array_shape: ArrayShape = vec![5, 7, 0];
        let chunk_shape: ChunkShape = vec![
            NonZeroU64::new(1).unwrap(),
            NonZeroU64::new(2).unwrap(),
            NonZeroU64::new(3).unwrap(),
        ];
        let chunk_grid = RegularBoundedChunkGrid::new(array_shape, chunk_shape).unwrap();

        let array_indices: ArrayIndices = vec![3, 5, 1000];
        assert_eq!(chunk_grid.chunk_indices(&array_indices).unwrap(), None);

        assert_eq!(chunk_grid.grid_shape(), &[5, 4, 0]);

        let chunk_indices: ArrayShape = vec![3, 1, 1000];
        assert!(chunk_grid.chunk_indices_inbounds(&chunk_indices));
    }

    #[test]
    fn chunk_grid_regular_bounded_iterators() {
        let array_shape: ArrayShape = vec![2, 2, 6];
        let chunk_shape: ChunkShape = vec![
            NonZeroU64::new(1).unwrap(),
            NonZeroU64::new(2).unwrap(),
            NonZeroU64::new(3).unwrap(),
        ];
        let chunk_grid = RegularBoundedChunkGrid::new(array_shape, chunk_shape).unwrap();

        let iter = chunk_grid.iter_chunk_indices();
        assert_eq!(
            iter.collect::<Vec<_>>(),
            vec![
                ArrayIndicesTinyVec::Heap(vec![0, 0, 0]),
                ArrayIndicesTinyVec::Heap(vec![0, 0, 1]),
                ArrayIndicesTinyVec::Heap(vec![1, 0, 0]),
                ArrayIndicesTinyVec::Heap(vec![1, 0, 1]),
            ]
        );

        let iter = chunk_grid.par_iter_chunk_indices();
        assert_eq!(
            iter.collect::<Vec<_>>(),
            vec![
                ArrayIndicesTinyVec::Heap(vec![0, 0, 0]),
                ArrayIndicesTinyVec::Heap(vec![0, 0, 1]),
                ArrayIndicesTinyVec::Heap(vec![1, 0, 0]),
                ArrayIndicesTinyVec::Heap(vec![1, 0, 1]),
            ]
        );
    }
}