Skip to content

Commit 14c45cd

Browse files
committed
pr feedback 1
1 parent 947f20e commit 14c45cd

File tree

7 files changed

+210
-22
lines changed

7 files changed

+210
-22
lines changed

src/zarr/api/asynchronous.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
ChunkCoords,
1919
MemoryOrder,
2020
ZarrFormat,
21-
_default_zarr_version,
21+
_default_zarr_format,
2222
_warn_order_kwarg,
2323
_warn_write_empty_chunks_kwarg,
2424
parse_dtype,
@@ -413,7 +413,7 @@ async def save_array(
413413
"""
414414
zarr_format = (
415415
_handle_zarr_version_or_format(zarr_version=zarr_version, zarr_format=zarr_format)
416-
or _default_zarr_version()
416+
or _default_zarr_format()
417417
)
418418
if not isinstance(arr, NDArrayLike):
419419
raise TypeError("arr argument must be numpy or other NDArrayLike array")
@@ -473,7 +473,7 @@ async def save_group(
473473
zarr_version=zarr_version,
474474
zarr_format=zarr_format,
475475
)
476-
or _default_zarr_version()
476+
or _default_zarr_format()
477477
)
478478

479479
for arg in args:
@@ -653,7 +653,7 @@ async def group(
653653
try:
654654
return await AsyncGroup.open(store=store_path, zarr_format=zarr_format)
655655
except (KeyError, FileNotFoundError):
656-
_zarr_format = zarr_format or _default_zarr_version()
656+
_zarr_format = zarr_format or _default_zarr_format()
657657
return await AsyncGroup.from_store(
658658
store=store_path,
659659
zarr_format=_zarr_format,
@@ -684,20 +684,22 @@ async def create_group(
684684
creating the group.
685685
zarr_format : {2, 3, None}, optional
686686
The zarr format to use when saving.
687+
If no ``zarr_format`` is provided, the default format will be used.
688+
This default can be changed by modifying the value of ``default_zarr_format``
689+
in :mod:`zarr.core.config`.
687690
storage_options : dict
688691
If using an fsspec URL to create the store, these will be passed to
689692
the backend implementation. Ignored otherwise.
690693
691694
Returns
692695
-------
693-
g : group
696+
AsyncGroup
694697
The new group.
695698
"""
696699

697700
if zarr_format is None:
698-
zarr_format = _default_zarr_version()
701+
zarr_format = _default_zarr_format()
699702

700-
# TODO: fix this when modes make sense. It should be `w` for overwriting, `w-` otherwise
701703
mode: Literal["a"] = "a"
702704

703705
store_path = await make_store_path(store, path=path, mode=mode, storage_options=storage_options)
@@ -812,7 +814,7 @@ async def open_group(
812814
pass
813815
if mode in _CREATE_MODES:
814816
overwrite = _infer_overwrite(mode)
815-
_zarr_format = zarr_format or _default_zarr_version()
817+
_zarr_format = zarr_format or _default_zarr_format()
816818
return await AsyncGroup.from_store(
817819
store_path,
818820
zarr_format=_zarr_format,
@@ -970,7 +972,7 @@ async def create(
970972
"""
971973
zarr_format = (
972974
_handle_zarr_version_or_format(zarr_version=zarr_version, zarr_format=zarr_format)
973-
or _default_zarr_version()
975+
or _default_zarr_format()
974976
)
975977

976978
if zarr_format == 2:
@@ -1243,7 +1245,7 @@ async def open_array(
12431245
except FileNotFoundError:
12441246
if not store_path.read_only and mode in _CREATE_MODES:
12451247
overwrite = _infer_overwrite(mode)
1246-
_zarr_format = zarr_format or _default_zarr_version()
1248+
_zarr_format = zarr_format or _default_zarr_format()
12471249
return await create(
12481250
store=store_path,
12491251
zarr_format=_zarr_format,

src/zarr/api/synchronous.py

Lines changed: 190 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,23 @@
1818

1919
from zarr.abc.codec import Codec
2020
from zarr.api.asynchronous import ArrayLike, PathLike
21+
from zarr.core.array import (
22+
ArrayBytesCodecParam,
23+
CompressorsParam,
24+
FiltersParam,
25+
ShardsParam,
26+
)
2127
from zarr.core.array_spec import ArrayConfig, ArrayConfigParams
2228
from zarr.core.buffer import NDArrayLike
23-
from zarr.core.chunk_key_encodings import ChunkKeyEncoding
24-
from zarr.core.common import JSON, AccessModeLiteral, ChunkCoords, MemoryOrder, ZarrFormat
29+
from zarr.core.chunk_key_encodings import ChunkKeyEncoding, ChunkKeyEncodingParams
30+
from zarr.core.common import (
31+
JSON,
32+
AccessModeLiteral,
33+
ChunkCoords,
34+
MemoryOrder,
35+
ShapeLike,
36+
ZarrFormat,
37+
)
2538
from zarr.storage import StoreLike
2639

2740
__all__ = [
@@ -534,6 +547,31 @@ def create_group(
534547
attributes: dict[str, Any] | None = None,
535548
storage_options: dict[str, Any] | None = None,
536549
) -> Group:
550+
"""Create a group.
551+
552+
Parameters
553+
----------
554+
store : Store or str
555+
Store or path to directory in file system.
556+
path : str, optional
557+
Group path within store.
558+
overwrite : bool, optional
559+
If True, pre-existing data at ``path`` will be deleted before
560+
creating the group.
561+
zarr_format : {2, 3, None}, optional
562+
The zarr format to use when saving.
563+
If no ``zarr_format`` is provided, the default format will be used.
564+
This default can be changed by modifying the value of ``default_zarr_format``
565+
in :mod:`zarr.core.config`.
566+
storage_options : dict
567+
If using an fsspec URL to create the store, these will be passed to
568+
the backend implementation. Ignored otherwise.
569+
570+
Returns
571+
-------
572+
Group
573+
The new group.
574+
"""
537575
return Group(
538576
sync(
539577
async_api.create_group(
@@ -700,8 +738,156 @@ def create(
700738
)
701739

702740

703-
def create_array(*args: Any, **kwargs: Any) -> Array:
704-
return Array(sync(zarr.core.array.create_array(*args, **kwargs)))
741+
def create_array(
742+
store: str | StoreLike,
743+
*,
744+
name: str | None = None,
745+
shape: ShapeLike,
746+
dtype: npt.DTypeLike,
747+
chunks: ChunkCoords | Literal["auto"] = "auto",
748+
shards: ShardsParam | None = None,
749+
filters: FiltersParam | None = "auto",
750+
compressors: CompressorsParam = "auto",
751+
array_bytes_codec: ArrayBytesCodecParam = "auto",
752+
fill_value: Any | None = None,
753+
order: MemoryOrder | None = None,
754+
zarr_format: ZarrFormat | None = 3,
755+
attributes: dict[str, JSON] | None = None,
756+
chunk_key_encoding: ChunkKeyEncoding | ChunkKeyEncodingParams | None = None,
757+
dimension_names: Iterable[str] | None = None,
758+
storage_options: dict[str, Any] | None = None,
759+
overwrite: bool = False,
760+
config: ArrayConfig | ArrayConfigParams | None = None,
761+
) -> Array:
762+
"""Create an array.
763+
764+
Parameters
765+
----------
766+
store : str or Store
767+
Store or path to directory in file system or name of zip file.
768+
name : str or None, optional
769+
The name of the array within the store. If ``name`` is ``None``, the array will be located
770+
at the root of the store.
771+
shape : ChunkCoords
772+
Shape of the array.
773+
dtype : npt.DTypeLike
774+
Data type of the array.
775+
chunks : ChunkCoords, optional
776+
Chunk shape of the array.
777+
If not specified, default are guessed based on the shape and dtype.
778+
shards : ChunkCoords, optional
779+
Shard shape of the array. The default value of ``None`` results in no sharding at all.
780+
filters : Iterable[Codec], optional
781+
Iterable of filters to apply to each chunk of the array, in order, before serializing that
782+
chunk to bytes.
783+
784+
For Zarr v3, a "filter" is a codec that takes an array and returns an array,
785+
and these values must be instances of ``ArrayArrayCodec``, or dict representations
786+
of ``ArrayArrayCodec``.
787+
If ``filters`` and ``compressors`` are not specified, then the default codecs for
788+
Zarr v3 will be used.
789+
These defaults can be changed by modifying the value of ``array.v3_default_codecs``
790+
in :mod:`zarr.core.config`.
791+
Use ``None`` to omit default filters.
792+
793+
For Zarr v2, a "filter" can be any numcodecs codec; you should ensure that the
794+
the order if your filters is consistent with the behavior of each filter.
795+
If no ``filters`` are provided, a default set of filters will be used.
796+
These defaults can be changed by modifying the value of ``array.v2_default_filters``
797+
in :mod:`zarr.core.config`.
798+
Use ``None`` to omit default filters.
799+
compressors : Iterable[Codec], optional
800+
List of compressors to apply to the array. Compressors are applied in order, and after any
801+
filters are applied (if any are specified).
802+
803+
For Zarr v3, a "compressor" is a codec that takes a bytestrea, and
804+
returns another bytestream. Multiple compressors my be provided for Zarr v3.
805+
If ``filters`` and ``compressors`` are not specified, then the default codecs for
806+
Zarr v3 will be used.
807+
These defaults can be changed by modifying the value of ``array.v3_default_codecs``
808+
in :mod:`zarr.core.config`.
809+
Use ``None`` to omit default compressors.
810+
811+
For Zarr v2, a "compressor" can be any numcodecs codec. Only a single compressor may
812+
be provided for Zarr v2.
813+
If no ``compressors`` are provided, a default compressor will be used.
814+
These defaults can be changed by modifying the value of ``array.v2_default_compressor``
815+
in :mod:`zarr.core.config`.
816+
Use ``None`` to omit the default compressor.
817+
array_bytes_codec : dict[str, JSON] | ArrayBytesCodec, optional
818+
Array-to-bytes codec to use for encoding the array data.
819+
Zarr v3 only. Zarr v2 arrays use implicit array-to-bytes conversion.
820+
If no ``array_bytes_codec`` is provided, the `zarr.codecs.BytesCodec` codec will be used.
821+
fill_value : Any, optional
822+
Fill value for the array.
823+
order : {"C", "F"}, optional
824+
The memory of the array (default is "C").
825+
For Zarr v2, this parameter sets the memory order of the array.
826+
For Zarr v3, this parameter is deprecated, because memory order
827+
is a runtime parameter for Zarr v3 arrays. The recommended way to specify the memory
828+
order for Zarr v3 arrays is via the ``config`` parameter, e.g. ``{'config': 'C'}``.
829+
If no ``order`` is provided, a default order will be used.
830+
This default can be changed by modifying the value of ``array.order`` in :mod:`zarr.core.config`.
831+
zarr_format : {2, 3}, optional
832+
The zarr format to use when saving.
833+
attributes : dict, optional
834+
Attributes for the array.
835+
chunk_key_encoding : ChunkKeyEncoding, optional
836+
A specification of how the chunk keys are represented in storage.
837+
For Zarr v3, the default is ``{"name": "default", "separator": "/"}}``.
838+
For Zarr v2, the default is ``{"name": "v2", "separator": "."}}``.
839+
dimension_names : Iterable[str], optional
840+
The names of the dimensions (default is None).
841+
Zarr v3 only. Zarr v2 arrays should not use this parameter.
842+
storage_options : dict, optional
843+
If using an fsspec URL to create the store, these will be passed to the backend implementation.
844+
Ignored otherwise.
845+
overwrite : bool, default False
846+
Whether to overwrite an array with the same name in the store, if one exists.
847+
config : ArrayConfig or ArrayConfigParams, optional
848+
Runtime configuration for the array.
849+
850+
Returns
851+
-------
852+
Array
853+
The array.
854+
855+
Examples
856+
--------
857+
>>> import zarr
858+
>>> store = zarr.storage.MemoryStore(mode='w')
859+
>>> arr = await zarr.create_array(
860+
>>> store=store,
861+
>>> shape=(100,100),
862+
>>> chunks=(10,10),
863+
>>> dtype='i4',
864+
>>> fill_value=0)
865+
<Array memory://140349042942400 shape=(100, 100) dtype=int32>
866+
"""
867+
return Array(
868+
sync(
869+
zarr.core.array.create_array(
870+
store=store,
871+
name=name,
872+
shape=shape,
873+
dtype=dtype,
874+
chunks=chunks,
875+
shards=shards,
876+
filters=filters,
877+
compressors=compressors,
878+
array_bytes_codec=array_bytes_codec,
879+
fill_value=fill_value,
880+
order=order,
881+
zarr_format=zarr_format,
882+
attributes=attributes,
883+
chunk_key_encoding=chunk_key_encoding,
884+
dimension_names=dimension_names,
885+
storage_options=storage_options,
886+
overwrite=overwrite,
887+
config=config,
888+
)
889+
)
890+
)
705891

706892

707893
# TODO: add type annotations for kwargs

src/zarr/core/array.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
MemoryOrder,
5454
ShapeLike,
5555
ZarrFormat,
56-
_default_zarr_version,
56+
_default_zarr_format,
5757
_warn_order_kwarg,
5858
concurrent_map,
5959
parse_dtype,
@@ -3765,7 +3765,7 @@ async def create_array(
37653765
37663766
Returns
37673767
-------
3768-
z : array
3768+
AsyncArray
37693769
The array.
37703770
37713771
Examples
@@ -3782,7 +3782,7 @@ async def create_array(
37823782
"""
37833783

37843784
if zarr_format is None:
3785-
zarr_format = _default_zarr_version()
3785+
zarr_format = _default_zarr_format()
37863786

37873787
from zarr.codecs.sharding import ShardingCodec, ShardingCodecIndexLocation
37883788

src/zarr/core/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,6 @@ def _warn_order_kwarg() -> None:
200200
warnings.warn(msg, RuntimeWarning, stacklevel=2)
201201

202202

203-
def _default_zarr_version() -> ZarrFormat:
203+
def _default_zarr_format() -> ZarrFormat:
204204
"""Return the default zarr_version"""
205-
return cast(ZarrFormat, int(zarr_config.get("default_zarr_version", 3)))
205+
return cast(ZarrFormat, int(zarr_config.get("default_zarr_format", 3)))

src/zarr/core/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def reset(self) -> None:
6262
"zarr",
6363
defaults=[
6464
{
65-
"default_zarr_version": 3,
65+
"default_zarr_format": 3,
6666
"array": {
6767
"order": "C",
6868
"write_empty_chunks": False,

tests/test_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_config_defaults_set() -> None:
5050
# regression test for available defaults
5151
assert config.defaults == [
5252
{
53-
"default_zarr_version": 3,
53+
"default_zarr_format": 3,
5454
"array": {
5555
"order": "C",
5656
"write_empty_chunks": False,

tests/test_metadata/test_consolidated.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ async def test_consolidated_metadata_v2(self):
523523
async def test_use_consolidated_false(
524524
self, memory_store: zarr.storage.MemoryStore, zarr_format: ZarrFormat
525525
) -> None:
526-
with zarr.config.set(default_zarr_version=zarr_format):
526+
with zarr.config.set(default_zarr_format=zarr_format):
527527
g = await group(store=memory_store, attributes={"foo": "bar"})
528528
await g.create_group(name="a")
529529

0 commit comments

Comments
 (0)