-
-
Notifications
You must be signed in to change notification settings - Fork 258
/
transpose.py
101 lines (78 loc) · 3.44 KB
/
transpose.py
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
from __future__ import annotations
from typing import TYPE_CHECKING, Dict, Iterable, Union, cast
from dataclasses import dataclass, replace
from zarr.v3.common import JSON, ArraySpec, ChunkCoordsLike, parse_named_configuration
if TYPE_CHECKING:
from zarr.v3.config import RuntimeConfiguration
from typing import TYPE_CHECKING, Optional, Tuple
from typing_extensions import Self
import numpy as np
from zarr.v3.abc.codec import ArrayArrayCodec
from zarr.v3.codecs.registry import register_codec
def parse_transpose_order(data: Union[JSON, Iterable[int]]) -> Tuple[int, ...]:
if not isinstance(data, Iterable):
raise TypeError(f"Expected an iterable. Got {data} instead.")
if not all(isinstance(a, int) for a in data):
raise TypeError(f"Expected an iterable of integers. Got {data} instead.")
return tuple(cast(Iterable[int], data))
@dataclass(frozen=True)
class TransposeCodec(ArrayArrayCodec):
is_fixed_size = True
order: Tuple[int, ...]
def __init__(self, *, order: ChunkCoordsLike) -> None:
order_parsed = parse_transpose_order(order)
object.__setattr__(self, "order", order_parsed)
@classmethod
def from_dict(cls, data: Dict[str, JSON]) -> Self:
_, configuration_parsed = parse_named_configuration(data, "transpose")
return cls(**configuration_parsed) # type: ignore[arg-type]
def to_dict(self) -> Dict[str, JSON]:
return {"name": "transpose", "configuration": {"order": list(self.order)}}
def evolve(self, array_spec: ArraySpec) -> Self:
if len(self.order) != array_spec.ndim:
raise ValueError(
"The `order` tuple needs have as many entries as "
+ f"there are dimensions in the array. Got {self.order}."
)
if len(self.order) != len(set(self.order)):
raise ValueError(
f"There must not be duplicates in the `order` tuple. Got {self.order}."
)
if not all(0 <= x < array_spec.ndim for x in self.order):
raise ValueError(
"All entries in the `order` tuple must be between 0 and "
+ f"the number of dimensions in the array. Got {self.order}."
)
order = tuple(self.order)
if order != self.order:
return replace(self, order=order)
return self
def resolve_metadata(self, chunk_spec: ArraySpec) -> ArraySpec:
from zarr.v3.common import ArraySpec
return ArraySpec(
shape=tuple(chunk_spec.shape[self.order[i]] for i in range(chunk_spec.ndim)),
dtype=chunk_spec.dtype,
fill_value=chunk_spec.fill_value,
)
async def decode(
self,
chunk_array: np.ndarray,
chunk_spec: ArraySpec,
_runtime_configuration: RuntimeConfiguration,
) -> np.ndarray:
inverse_order = [0] * chunk_spec.ndim
for x, i in enumerate(self.order):
inverse_order[x] = i
chunk_array = chunk_array.transpose(inverse_order)
return chunk_array
async def encode(
self,
chunk_array: np.ndarray,
chunk_spec: ArraySpec,
_runtime_configuration: RuntimeConfiguration,
) -> Optional[np.ndarray]:
chunk_array = chunk_array.transpose(self.order)
return chunk_array
def compute_encoded_size(self, input_byte_length: int, _chunk_spec: ArraySpec) -> int:
return input_byte_length
register_codec("transpose", TransposeCodec)