Skip to content

Commit e607105

Browse files
committed
distinguish between keep and auto for from_array arguments
1 parent 93ed8d6 commit e607105

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

src/zarr/api/synchronous.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -899,11 +899,11 @@ def from_array(
899899
store: str | StoreLike,
900900
*,
901901
name: str | None = None,
902-
chunks: ChunkCoords | Literal["auto"] = "auto",
902+
chunks: Literal["auto", "keep"] | ChunkCoords = "keep",
903903
shards: ShardsLike | None = None,
904-
filters: FiltersLike = "auto",
905-
compressors: CompressorsLike = "auto",
906-
serializer: SerializerLike = "auto",
904+
filters: FiltersLike | Literal["keep"] = "keep",
905+
compressors: CompressorsLike | Literal["keep"] = "keep",
906+
serializer: SerializerLike | Literal["keep"] = "keep",
907907
fill_value: Any | None = None,
908908
order: MemoryOrder | None = None,
909909
zarr_format: ZarrFormat | None = 3,
@@ -925,12 +925,14 @@ def from_array(
925925
name : str or None, optional
926926
The name of the array within the store. If ``name`` is ``None``, the array will be located
927927
at the root of the store.
928-
chunks : ChunkCoords, optional
928+
chunks : ChunkCoords or "auto" or "keep", optional
929929
Chunk shape of the array.
930930
If not specified, defaults to the chunk shape of the data array.
931+
- "auto": Automatically determine the chunk shape based on the array's shape and dtype.
932+
- "keep": Retain the chunk shape of the input array.
931933
shards : ChunkCoords, optional
932934
Shard shape of the array. The default value of ``None`` results in no sharding at all.
933-
filters : Iterable[Codec], optional
935+
filters : Iterable[Codec] or "auto" or "keep", optional
934936
Iterable of filters to apply to each chunk of the array, in order, before serializing that
935937
chunk to bytes.
936938
@@ -942,7 +944,9 @@ def from_array(
942944
the order if your filters is consistent with the behavior of each filter.
943945
944946
If no ``filters`` are provided, defaults to the filters of the data array.
945-
compressors : Iterable[Codec], optional
947+
- "auto": Automatically determine the filters based on the array's dtype.
948+
- "keep": Retain the filters of the input array.
949+
compressors : Iterable[Codec] or "auto" or "keep", optional
946950
List of compressors to apply to the array. Compressors are applied in order, and after any
947951
filters are applied (if any are specified) and the data is serialized into bytes.
948952
@@ -953,11 +957,15 @@ def from_array(
953957
be provided for Zarr format 2.
954958
955959
If no ``compressors`` are provided, defaults to the compressors of the data array.
956-
serializer : dict[str, JSON] | ArrayBytesCodec, optional
960+
- "auto": Automatically determine the compressors based on the array's dtype.
961+
- "keep": Retain the compressors of the input array.
962+
serializer : dict[str, JSON] | ArrayBytesCodec or "auto" or "keep", optional
957963
Array-to-bytes codec to use for encoding the array data.
958964
Zarr format 3 only. Zarr format 2 arrays use implicit array-to-bytes conversion.
959965
960966
If no ``serializer`` is provided, defaults to the serializer of the input array.
967+
- "auto": Automatically determine the serializer based on the array's dtype.
968+
- "keep": Retain the serializer of the input array.
961969
fill_value : Any, optional
962970
Fill value for the array.
963971
If not specified, defaults to the fill value of the data array.

src/zarr/core/array.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3740,11 +3740,11 @@ async def from_array(
37403740
store: str | StoreLike,
37413741
*,
37423742
name: str | None = None,
3743-
chunks: ChunkCoords | Literal["auto"] = "auto",
3743+
chunks: Literal["auto", "keep"] | ChunkCoords = "keep",
37443744
shards: ShardsLike | None = None,
3745-
filters: FiltersLike = "auto",
3746-
compressors: CompressorsLike = "auto",
3747-
serializer: SerializerLike = "auto",
3745+
filters: FiltersLike | Literal["keep"] = "keep",
3746+
compressors: CompressorsLike | Literal["keep"] = "keep",
3747+
serializer: SerializerLike | Literal["keep"] = "keep",
37483748
fill_value: Any | None = None,
37493749
order: MemoryOrder | None = None,
37503750
zarr_format: ZarrFormat | None = 3,
@@ -3766,12 +3766,14 @@ async def from_array(
37663766
name : str or None, optional
37673767
The name of the array within the store. If ``name`` is ``None``, the array will be located
37683768
at the root of the store.
3769-
chunks : ChunkCoords, optional
3769+
chunks : ChunkCoords or "auto" or "keep", optional
37703770
Chunk shape of the array.
37713771
If not specified, defaults to the chunk shape of the data array.
3772+
- "auto": Automatically determine the chunk shape based on the array's shape and dtype.
3773+
- "keep": Retain the chunk shape of the input array.
37723774
shards : ChunkCoords, optional
37733775
Shard shape of the array. The default value of ``None`` results in no sharding at all.
3774-
filters : Iterable[Codec], optional
3776+
filters : Iterable[Codec] or "auto" or "keep", optional
37753777
Iterable of filters to apply to each chunk of the array, in order, before serializing that
37763778
chunk to bytes.
37773779
@@ -3783,7 +3785,9 @@ async def from_array(
37833785
the order if your filters is consistent with the behavior of each filter.
37843786
37853787
If no ``filters`` are provided, defaults to the filters of the data array.
3786-
compressors : Iterable[Codec], optional
3788+
- "auto": Automatically determine the filters based on the array's dtype.
3789+
- "keep": Retain the filters of the input array.
3790+
compressors : Iterable[Codec] or "auto" or "keep", optional
37873791
List of compressors to apply to the array. Compressors are applied in order, and after any
37883792
filters are applied (if any are specified) and the data is serialized into bytes.
37893793
@@ -3794,11 +3798,15 @@ async def from_array(
37943798
be provided for Zarr format 2.
37953799
37963800
If no ``compressors`` are provided, defaults to the compressors of the data array.
3797-
serializer : dict[str, JSON] | ArrayBytesCodec, optional
3801+
- "auto": Automatically determine the compressors based on the array's dtype.
3802+
- "keep": Retain the compressors of the input array.
3803+
serializer : dict[str, JSON] | ArrayBytesCodec or "auto" or "keep", optional
37983804
Array-to-bytes codec to use for encoding the array data.
37993805
Zarr format 3 only. Zarr format 2 arrays use implicit array-to-bytes conversion.
38003806
38013807
If no ``serializer`` is provided, defaults to the serializer of the input array.
3808+
- "auto": Automatically determine the serializer based on the array's dtype.
3809+
- "keep": Retain the serializer of the input array.
38023810
fill_value : Any, optional
38033811
Fill value for the array.
38043812
If not specified, defaults to the fill value of the data array.
@@ -3843,23 +3851,31 @@ async def from_array(
38433851
#TODO
38443852
"""
38453853

3846-
# fill missing arguments with metadata of data Array
3847-
if chunks == "auto":
3854+
if chunks == "keep":
38483855
chunks = data.chunks
3849-
if filters is None:
3850-
filters = data.filters
3851-
if compressors is None:
3852-
compressors = data.compressors
3856+
if zarr_format is None:
3857+
zarr_format = data.metadata.zarr_format
3858+
if filters == "keep":
3859+
if zarr_format == data.metadata.zarr_format:
3860+
filters = data.filters
3861+
else:
3862+
filters = "auto"
3863+
if compressors == "keep":
3864+
if zarr_format == data.metadata.zarr_format:
3865+
compressors = data.compressors
3866+
else:
3867+
compressors = "auto"
3868+
if serializer == "keep":
3869+
if zarr_format == 3:
3870+
serializer = cast(SerializerLike, data.serializer)
3871+
else:
3872+
serializer = "auto"
38533873
if fill_value is None:
38543874
fill_value = data.fill_value
38553875
if order is None:
38563876
order = data.order
3857-
if zarr_format is None:
3858-
zarr_format = data.metadata.zarr_format
3859-
if zarr_format == 3 and serializer == "auto":
3860-
serializer = cast(SerializerLike, data.serializer)
38613877
if chunk_key_encoding is None and zarr_format == data.metadata.zarr_format:
3862-
if data.metadata.zarr_format == 2:
3878+
if zarr_format == 2:
38633879
chunk_key_encoding = {"name": "v2", "separator": data.metadata.dimension_separator}
38643880
else:
38653881
chunk_key_encoding = data.metadata.chunk_key_encoding

0 commit comments

Comments
 (0)