Skip to content

Commit 638ac3a

Browse files
committed
docs: clarify Zarr v3 codec pipeline roles
1 parent ab76998 commit 638ac3a

4 files changed

Lines changed: 52 additions & 2 deletions

File tree

‎changes/4159.doc.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clarify how Zarr format 3 codec pipeline roles map to `filters`, `serializer`, and `compressors` when creating arrays.

‎docs/user-guide/arrays.md‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,48 @@ arr_f = arr.with_config({"order": "F"})
195195
print(arr_f.config)
196196
```
197197

198+
## Zarr format 3 codec pipeline
199+
200+
Zarr format 3 stores a single ordered `codecs` pipeline in array metadata, but
201+
Zarr-Python's array creation functions expose that pipeline through three
202+
role-specific parameters:
203+
204+
- `filters`: [`zarr.abc.codec.ArrayArrayCodec`][] instances. These transform chunk
205+
arrays into chunk arrays before serialization.
206+
- `serializer`: one [`zarr.abc.codec.ArrayBytesCodec`][] instance. This transforms
207+
a chunk array into bytes. Every Zarr format 3 array needs exactly one
208+
array-to-bytes codec, either supplied explicitly or chosen by default.
209+
- `compressors`: [`zarr.abc.codec.BytesBytesCodec`][] instances. These transform
210+
bytes into bytes after serialization.
211+
212+
The `compressors` parameter is only for bytes-to-bytes codecs. If a codec is an
213+
`ArrayBytesCodec`, pass it with `serializer`, not `compressors`. For example, the
214+
built-in [`zarr.codecs.BytesCodec`][] can be supplied explicitly as the serializer:
215+
216+
```python exec="true" session="arrays" source="above" result="ansi"
217+
serializer = zarr.codecs.BytesCodec(endian="little")
218+
z_explicit_serializer = zarr.create_array(
219+
store="data/example-explicit-serializer.zarr",
220+
shape=(100,),
221+
chunks=(10,),
222+
dtype="int32",
223+
serializer=serializer,
224+
compressors=None,
225+
)
226+
print(z_explicit_serializer.serializer)
227+
print(f"Compressors: {z_explicit_serializer.compressors}")
228+
```
229+
230+
The same rule applies to third-party Zarr format 3 codecs: if the codec is
231+
documented as an `ArrayBytesCodec`, provide an instance as `serializer=...`.
232+
198233
## Compressors
199234

200235
A number of different compressors can be used with Zarr. Zarr includes Blosc,
201236
Zstandard and Gzip compressors. Additional compressors are available through
202237
a separate package called [NumCodecs](https://numcodecs.readthedocs.io/) which provides various
203238
compressor libraries including LZ4, Zlib, BZ2 and LZMA.
204-
Different compressors can be provided via the `compressors` keyword
239+
Different bytes-to-bytes compressors can be provided via the `compressors` keyword
205240
argument accepted by all array creation functions. For example:
206241

207242
```python exec="true" session="arrays" source="above" result="ansi"

‎src/zarr/api/asynchronous.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,10 @@ async def create(
10221022
10231023
The elements of ``codecs`` specify the transformation from array values to stored bytes.
10241024
Zarr format 3 only. Zarr format 2 arrays should use ``filters`` and ``compressor`` instead.
1025+
In order, a Zarr format 3 pipeline contains zero or more
1026+
[`zarr.abc.codec.ArrayArrayCodec`][] filters, exactly one
1027+
[`zarr.abc.codec.ArrayBytesCodec`][] serializer, and zero or more
1028+
[`zarr.abc.codec.BytesBytesCodec`][] compressors.
10251029
10261030
If no codecs are provided, default codecs will be used based on the data type of the array.
10271031
For most data types, the default codecs are the tuple ``(BytesCodec(), ZstdCodec())``;

‎src/zarr/api/synchronous.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,10 @@ def create(
760760
761761
The elements of ``codecs`` specify the transformation from array values to stored bytes.
762762
Zarr format 3 only. Zarr format 2 arrays should use ``filters`` and ``compressor`` instead.
763+
In order, a Zarr format 3 pipeline contains zero or more
764+
[`zarr.abc.codec.ArrayArrayCodec`][] filters, exactly one
765+
[`zarr.abc.codec.ArrayBytesCodec`][] serializer, and zero or more
766+
[`zarr.abc.codec.BytesBytesCodec`][] compressors.
763767
764768
If no codecs are provided, default codecs will be used based on the data type of the array.
765769
For most data types, the default codecs are the tuple ``(BytesCodec(), ZstdCodec())``;
@@ -892,7 +896,11 @@ def create_array(
892896
filters are applied (if any are specified) and the data is serialized into bytes.
893897
894898
For Zarr format 3, a "compressor" is a codec that takes a bytestream, and
895-
returns another bytestream. Multiple compressors may be provided for Zarr format 3.
899+
returns another bytestream. These values must be instances of
900+
[`zarr.abc.codec.BytesBytesCodec`][], or dict representations of
901+
[`zarr.abc.codec.BytesBytesCodec`][]. Multiple compressors may be provided
902+
for Zarr format 3. Codecs that take an array and return bytes are serializers
903+
and must be supplied with ``serializer`` instead.
896904
If no ``compressors`` are provided, a default set of compressors will be used.
897905
These defaults can be changed by modifying the value of ``array.v3_default_compressors``
898906
in [`zarr.config`][zarr.config].
@@ -906,6 +914,8 @@ def create_array(
906914
serializer : dict[str, JSON] | ArrayBytesCodec, optional
907915
Array-to-bytes codec to use for encoding the array data.
908916
Zarr format 3 only. Zarr format 2 arrays use implicit array-to-bytes conversion.
917+
Codecs that are instances of [`zarr.abc.codec.ArrayBytesCodec`][] must be
918+
supplied here, not with ``compressors``.
909919
If no ``serializer`` is provided, a default serializer will be used.
910920
These defaults can be changed by modifying the value of ``array.v3_default_serializer``
911921
in [`zarr.config`][zarr.config].

0 commit comments

Comments
 (0)