|
3 | 3 | import math |
4 | 4 | import numbers |
5 | 5 | import warnings |
6 | | -from abc import abstractmethod |
7 | | -from dataclasses import dataclass |
8 | | -from typing import TYPE_CHECKING, Any, Literal |
| 6 | +from abc import ABC, abstractmethod |
| 7 | +from typing import TYPE_CHECKING, Any, Literal, Self |
9 | 8 |
|
10 | 9 | import numpy as np |
11 | 10 | import numpy.typing as npt |
|
16 | 15 |
|
17 | 16 | if TYPE_CHECKING: |
18 | 17 | from collections.abc import Iterator |
19 | | - from typing import Self |
20 | 18 |
|
21 | 19 | from zarr.core.array import ShardsLike |
22 | 20 | from zarr.core.common import JSON |
23 | 21 |
|
24 | 22 |
|
25 | | -@dataclass(frozen=True) |
26 | | -class ChunkGrid(Metadata): |
| 23 | +class ChunkGrid(ABC, Metadata): |
| 24 | + @property |
| 25 | + @abstractmethod |
| 26 | + def array_shape(self) -> tuple[int, ...]: |
| 27 | + """The shape of the array this chunk grid is bound to.""" |
| 28 | + ... |
| 29 | + |
27 | 30 | @abstractmethod |
28 | 31 | def to_dict(self) -> dict[str, JSON]: ... |
29 | 32 |
|
30 | 33 | @abstractmethod |
31 | 34 | def update_shape(self, new_shape: tuple[int, ...]) -> Self: |
32 | | - pass |
| 35 | + """Return a new ChunkGrid with the given array_shape.""" |
| 36 | + ... |
33 | 37 |
|
34 | 38 | @abstractmethod |
35 | | - def all_chunk_coords(self, array_shape: tuple[int, ...]) -> Iterator[tuple[int, ...]]: |
36 | | - pass |
| 39 | + def all_chunk_coords(self) -> Iterator[tuple[int, ...]]: ... |
37 | 40 |
|
38 | 41 | @abstractmethod |
39 | | - def get_nchunks(self, array_shape: tuple[int, ...]) -> int: |
40 | | - pass |
| 42 | + def get_nchunks(self) -> int: ... |
41 | 43 |
|
42 | 44 | @abstractmethod |
43 | | - def get_chunk_shape( |
44 | | - self, array_shape: tuple[int, ...], chunk_coord: tuple[int, ...] |
45 | | - ) -> tuple[int, ...]: |
46 | | - """ |
47 | | - Get the shape of a specific chunk. |
48 | | -
|
49 | | - Parameters |
50 | | - ---------- |
51 | | - array_shape : tuple[int, ...] |
52 | | - Shape of the full array. |
53 | | - chunk_coord : tuple[int, ...] |
54 | | - Coordinates of the chunk in the chunk grid. |
55 | | -
|
56 | | - Returns |
57 | | - ------- |
58 | | - tuple[int, ...] |
59 | | - Shape of the chunk at the given coordinates. |
60 | | - """ |
| 45 | + def get_chunk_shape(self, chunk_coord: tuple[int, ...]) -> tuple[int, ...]: |
| 46 | + """Get the shape of a specific chunk.""" |
| 47 | + ... |
61 | 48 |
|
62 | 49 | @abstractmethod |
63 | | - def get_chunk_start( |
64 | | - self, array_shape: tuple[int, ...], chunk_coord: tuple[int, ...] |
65 | | - ) -> tuple[int, ...]: |
66 | | - """ |
67 | | - Get the starting position of a chunk in the array. |
68 | | -
|
69 | | - Parameters |
70 | | - ---------- |
71 | | - array_shape : tuple[int, ...] |
72 | | - Shape of the full array. |
73 | | - chunk_coord : tuple[int, ...] |
74 | | - Coordinates of the chunk in the chunk grid. |
75 | | -
|
76 | | - Returns |
77 | | - ------- |
78 | | - tuple[int, ...] |
79 | | - Starting position (offset) of the chunk in the array. |
80 | | - """ |
| 50 | + def get_chunk_start(self, chunk_coord: tuple[int, ...]) -> tuple[int, ...]: |
| 51 | + """Get the starting position of a chunk in the array.""" |
| 52 | + ... |
81 | 53 |
|
82 | 54 | @abstractmethod |
83 | | - def array_index_to_chunk_coord( |
84 | | - self, array_shape: tuple[int, ...], array_index: tuple[int, ...] |
85 | | - ) -> tuple[int, ...]: |
86 | | - """ |
87 | | - Map an array index to the chunk coordinates that contain it. |
88 | | -
|
89 | | - Parameters |
90 | | - ---------- |
91 | | - array_shape : tuple[int, ...] |
92 | | - Shape of the full array. |
93 | | - array_index : tuple[int, ...] |
94 | | - Index in the array. |
95 | | -
|
96 | | - Returns |
97 | | - ------- |
98 | | - tuple[int, ...] |
99 | | - Coordinates of the chunk containing the array index. |
100 | | - """ |
| 55 | + def array_index_to_chunk_coord(self, array_index: tuple[int, ...]) -> tuple[int, ...]: |
| 56 | + """Map an array index to the chunk coordinates that contain it.""" |
| 57 | + ... |
101 | 58 |
|
102 | 59 | @abstractmethod |
103 | 60 | def array_indices_to_chunk_dim( |
104 | | - self, array_shape: tuple[int, ...], dim: int, indices: npt.NDArray[np.intp] |
| 61 | + self, dim: int, indices: npt.NDArray[np.intp] |
105 | 62 | ) -> npt.NDArray[np.intp]: |
106 | | - """ |
107 | | - Map an array of indices along one dimension to chunk coordinates (vectorized). |
108 | | -
|
109 | | - Parameters |
110 | | - ---------- |
111 | | - array_shape : tuple[int, ...] |
112 | | - Shape of the full array. |
113 | | - dim : int |
114 | | - Dimension index. |
115 | | - indices : np.ndarray |
116 | | - Array of indices along the given dimension. |
117 | | -
|
118 | | - Returns |
119 | | - ------- |
120 | | - np.ndarray |
121 | | - Array of chunk coordinates, same shape as indices. |
122 | | - """ |
| 63 | + """Map an array of indices along one dimension to chunk coordinates (vectorized).""" |
| 64 | + ... |
123 | 65 |
|
124 | 66 | @abstractmethod |
125 | | - def chunks_per_dim(self, array_shape: tuple[int, ...], dim: int) -> int: |
126 | | - """ |
127 | | - Get the number of chunks along a specific dimension. |
128 | | -
|
129 | | - Parameters |
130 | | - ---------- |
131 | | - array_shape : tuple[int, ...] |
132 | | - Shape of the full array. |
133 | | - dim : int |
134 | | - Dimension index. |
135 | | -
|
136 | | - Returns |
137 | | - ------- |
138 | | - int |
139 | | - Number of chunks along the dimension. |
140 | | - """ |
| 67 | + def chunks_per_dim(self, dim: int) -> int: |
| 68 | + """Get the number of chunks along a specific dimension.""" |
| 69 | + ... |
141 | 70 |
|
142 | 71 | @abstractmethod |
143 | | - def get_chunk_grid_shape(self, array_shape: tuple[int, ...]) -> tuple[int, ...]: |
144 | | - """ |
145 | | - Get the shape of the chunk grid (number of chunks along each dimension). |
146 | | -
|
147 | | - Parameters |
148 | | - ---------- |
149 | | - array_shape : tuple[int, ...] |
150 | | - Shape of the full array. |
151 | | -
|
152 | | - Returns |
153 | | - ------- |
154 | | - tuple[int, ...] |
155 | | - Number of chunks along each dimension. |
156 | | - """ |
| 72 | + def get_chunk_grid_shape(self) -> tuple[int, ...]: |
| 73 | + """Get the shape of the chunk grid (number of chunks along each dimension).""" |
| 74 | + ... |
157 | 75 |
|
158 | 76 |
|
159 | 77 | def _guess_chunks( |
|
0 commit comments