Skip to content

Commit a4b4456

Browse files
committed
improve docstrings and add examples
1 parent 58f05fe commit a4b4456

File tree

2 files changed

+130
-34
lines changed

2 files changed

+130
-34
lines changed

src/zarr/api/synchronous.py

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ def create_array(
859859
Examples
860860
--------
861861
>>> import zarr
862-
>>> store = zarr.storage.MemoryStore(mode='w')
862+
>>> store = zarr.storage.MemoryStore()
863863
>>> arr = await zarr.create_array(
864864
>>> store=store,
865865
>>> shape=(100,100),
@@ -919,7 +919,7 @@ def from_array(
919919
920920
Parameters
921921
----------
922-
data : Array
922+
data : Array | array-like
923923
The array to copy.
924924
store : str or Store
925925
Store or path to directory in file system or name of zip file for the new array.
@@ -932,9 +932,11 @@ def from_array(
932932
at the root of the store.
933933
chunks : ChunkCoords or "auto" or "keep", optional
934934
Chunk shape of the array.
935-
If not specified, defaults to the chunk shape of the data array.
936-
- "auto": Automatically determine the chunk shape based on the array's shape and dtype.
937-
- "keep": Retain the chunk shape of the input array.
935+
Following values are supported:
936+
- "auto": Automatically determine the chunk shape based on the array's shape and dtype.
937+
- "keep": Retain the chunk shape of the data array if it is a zarr Array.
938+
- ChunkCoords: A tuple of integers representing the chunk shape.
939+
If not specified, defaults to "keep" if data is a zarr Array, otherwise "auto".
938940
shards : ChunkCoords, optional
939941
Shard shape of the array. The default value of ``None`` results in no sharding at all.
940942
filters : Iterable[Codec] or "auto" or "keep", optional
@@ -948,9 +950,11 @@ def from_array(
948950
For Zarr format 2, a "filter" can be any numcodecs codec; you should ensure that the
949951
the order if your filters is consistent with the behavior of each filter.
950952
951-
If no ``filters`` are provided, defaults to the filters of the data array.
953+
Following values are supported:
954+
- Iterable[Codec]: List of filters to apply to the array.
952955
- "auto": Automatically determine the filters based on the array's dtype.
953-
- "keep": Retain the filters of the input array.
956+
- "keep": Retain the filters of the data array if it is a zarr Array.
957+
If no ``filters`` are provided, defaults to "keep" if data is a zarr Array, otherwise "auto".
954958
compressors : Iterable[Codec] or "auto" or "keep", optional
955959
List of compressors to apply to the array. Compressors are applied in order, and after any
956960
filters are applied (if any are specified) and the data is serialized into bytes.
@@ -961,16 +965,21 @@ def from_array(
961965
For Zarr format 2, a "compressor" can be any numcodecs codec. Only a single compressor may
962966
be provided for Zarr format 2.
963967
964-
If no ``compressors`` are provided, defaults to the compressors of the data array.
968+
Following values are supported:
969+
- Iterable[Codec]: List of compressors to apply to the array.
965970
- "auto": Automatically determine the compressors based on the array's dtype.
966-
- "keep": Retain the compressors of the input array.
971+
- "keep": Retain the compressors of the input array if it is a zarr Array.
972+
If no ``compressors`` are provided, defaults to "keep" if data is a zarr Array, otherwise "auto".
967973
serializer : dict[str, JSON] | ArrayBytesCodec or "auto" or "keep", optional
968974
Array-to-bytes codec to use for encoding the array data.
969975
Zarr format 3 only. Zarr format 2 arrays use implicit array-to-bytes conversion.
970976
971-
If no ``serializer`` is provided, defaults to the serializer of the input array.
972-
- "auto": Automatically determine the serializer based on the array's dtype.
973-
- "keep": Retain the serializer of the input array.
977+
Following values are supported:
978+
- dict[str, JSON]: A dict representation of an ``ArrayBytesCodec``.
979+
- ArrayBytesCodec: An instance of ``ArrayBytesCodec``.
980+
- "auto": a default serializer will be used. These defaults can be changed by modifying the value of
981+
``array.v3_default_serializer`` in :mod:`zarr.core.config`.
982+
- "keep": Retain the serializer of the input array if it is a zarr Array.
974983
fill_value : Any, optional
975984
Fill value for the array.
976985
If not specified, defaults to the fill value of the data array.
@@ -1007,12 +1016,53 @@ def from_array(
10071016
10081017
Returns
10091018
-------
1010-
AsyncArray
1019+
Array
10111020
The array.
10121021
10131022
Examples
10141023
--------
1015-
#TODO
1024+
Create an array from an existing Array:
1025+
>>> import zarr
1026+
>>> store = zarr.storage.MemoryStore()
1027+
>>> store2 = zarr.storage.LocalStore('example.zarr')
1028+
>>> arr = zarr.create_array(
1029+
>>> store=store,
1030+
>>> shape=(100,100),
1031+
>>> chunks=(10,10),
1032+
>>> dtype='int32',
1033+
>>> fill_value=0)
1034+
>>> arr2 = zarr.from_array(arr, store=store2)
1035+
<Array file://example.zarr shape=(100, 100) dtype=int32>
1036+
1037+
Create an array from an existing NumPy array:
1038+
>>> import numpy as np
1039+
>>> arr3 = zarr.from_array(
1040+
>>> np.arange(10000, dtype='i4').reshape(100, 100),
1041+
>>> store=zarr.storage.MemoryStore(),
1042+
>>> )
1043+
<Array memory://125477403529984 shape=(100, 100) dtype=int32>
1044+
1045+
Create an array from any array-like object:
1046+
>>> arr4 = zarr.from_array(
1047+
>>> [[1, 2], [3, 4]],
1048+
>>> store= zarr.storage.MemoryStore(),
1049+
>>> )
1050+
<Array memory://125477392154368 shape=(2, 2) dtype=int64>
1051+
>>> arr4[...]
1052+
[[1 2]
1053+
[3 4]]
1054+
1055+
Create an array from an existing Array without copying the data:
1056+
>>> arr5 = zarr.from_array(
1057+
>>> arr4,
1058+
>>> store=zarr.storage.MemoryStore(),
1059+
>>> write_data=False,
1060+
>>> )
1061+
<Array memory://140678602965568 shape=(2, 2) dtype=int64>
1062+
>>> arr5[...]
1063+
[[0 0]
1064+
[0 0]]
1065+
10161066
"""
10171067
return Array(
10181068
sync(

src/zarr/core/array.py

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ async def open(
837837
Examples
838838
--------
839839
>>> import zarr
840-
>>> store = zarr.storage.MemoryStore(mode='w')
840+
>>> store = zarr.storage.MemoryStore()
841841
>>> async_arr = await AsyncArray.open(store) # doctest: +ELLIPSIS
842842
<AsyncArray memory://... shape=(100, 100) dtype=int32>
843843
"""
@@ -1269,7 +1269,7 @@ async def getitem(
12691269
Examples
12701270
--------
12711271
>>> import zarr
1272-
>>> store = zarr.storage.MemoryStore(mode='w')
1272+
>>> store = zarr.storage.MemoryStore()
12731273
>>> async_arr = await zarr.api.asynchronous.create_array(
12741274
... store=store,
12751275
... shape=(100,100),
@@ -3760,22 +3760,24 @@ async def from_array(
37603760
37613761
Parameters
37623762
----------
3763-
data : Array
3763+
data : Array | array-like
37643764
The array to copy.
37653765
store : str or Store
37663766
Store or path to directory in file system or name of zip file for the new array.
3767-
name : str or None, optional
3768-
The name of the array within the store. If ``name`` is ``None``, the array will be located
3769-
at the root of the store.
37703767
write_data : bool, default True
37713768
Whether to copy the data from the input array to the new array.
37723769
If ``write_data`` is ``False``, the new array will be created with the same metadata as the
37733770
input array, but without any data.
3771+
name : str or None, optional
3772+
The name of the array within the store. If ``name`` is ``None``, the array will be located
3773+
at the root of the store.
37743774
chunks : ChunkCoords or "auto" or "keep", optional
37753775
Chunk shape of the array.
3776-
If not specified, defaults to the chunk shape of the data array.
3777-
- "auto": Automatically determine the chunk shape based on the array's shape and dtype.
3778-
- "keep": Retain the chunk shape of the input array.
3776+
Following values are supported:
3777+
- "auto": Automatically determine the chunk shape based on the array's shape and dtype.
3778+
- "keep": Retain the chunk shape of the data array if it is a zarr Array.
3779+
- ChunkCoords: A tuple of integers representing the chunk shape.
3780+
If not specified, defaults to "keep" if data is a zarr Array, otherwise "auto".
37793781
shards : ChunkCoords, optional
37803782
Shard shape of the array. The default value of ``None`` results in no sharding at all.
37813783
filters : Iterable[Codec] or "auto" or "keep", optional
@@ -3789,9 +3791,11 @@ async def from_array(
37893791
For Zarr format 2, a "filter" can be any numcodecs codec; you should ensure that the
37903792
the order if your filters is consistent with the behavior of each filter.
37913793
3792-
If no ``filters`` are provided, defaults to the filters of the data array.
3794+
Following values are supported:
3795+
- Iterable[Codec]: List of filters to apply to the array.
37933796
- "auto": Automatically determine the filters based on the array's dtype.
3794-
- "keep": Retain the filters of the input array.
3797+
- "keep": Retain the filters of the data array if it is a zarr Array.
3798+
If no ``filters`` are provided, defaults to "keep" if data is a zarr Array, otherwise "auto".
37953799
compressors : Iterable[Codec] or "auto" or "keep", optional
37963800
List of compressors to apply to the array. Compressors are applied in order, and after any
37973801
filters are applied (if any are specified) and the data is serialized into bytes.
@@ -3802,16 +3806,21 @@ async def from_array(
38023806
For Zarr format 2, a "compressor" can be any numcodecs codec. Only a single compressor may
38033807
be provided for Zarr format 2.
38043808
3805-
If no ``compressors`` are provided, defaults to the compressors of the data array.
3809+
Following values are supported:
3810+
- Iterable[Codec]: List of compressors to apply to the array.
38063811
- "auto": Automatically determine the compressors based on the array's dtype.
3807-
- "keep": Retain the compressors of the input array.
3812+
- "keep": Retain the compressors of the input array if it is a zarr Array.
3813+
If no ``compressors`` are provided, defaults to "keep" if data is a zarr Array, otherwise "auto".
38083814
serializer : dict[str, JSON] | ArrayBytesCodec or "auto" or "keep", optional
38093815
Array-to-bytes codec to use for encoding the array data.
38103816
Zarr format 3 only. Zarr format 2 arrays use implicit array-to-bytes conversion.
38113817
3812-
If no ``serializer`` is provided, defaults to the serializer of the input array.
3813-
- "auto": Automatically determine the serializer based on the array's dtype.
3814-
- "keep": Retain the serializer of the input array.
3818+
Following values are supported:
3819+
- dict[str, JSON]: A dict representation of an ``ArrayBytesCodec``.
3820+
- ArrayBytesCodec: An instance of ``ArrayBytesCodec``.
3821+
- "auto": a default serializer will be used. These defaults can be changed by modifying the value of
3822+
``array.v3_default_serializer`` in :mod:`zarr.core.config`.
3823+
- "keep": Retain the serializer of the input array if it is a zarr Array.
38153824
fill_value : Any, optional
38163825
Fill value for the array.
38173826
If not specified, defaults to the fill value of the data array.
@@ -3853,9 +3862,47 @@ async def from_array(
38533862
38543863
Examples
38553864
--------
3856-
#TODO
3865+
Create an array from an existing Array:
3866+
>>> import zarr
3867+
>>> store = zarr.storage.MemoryStore()
3868+
>>> store2 = zarr.storage.LocalStore('example.zarr')
3869+
>>> arr = zarr.create_array(
3870+
>>> store=store,
3871+
>>> shape=(100,100),
3872+
>>> chunks=(10,10),
3873+
>>> dtype='int32',
3874+
>>> fill_value=0)
3875+
>>> arr2 = await zarr.api.asynchronous.from_array(arr, store=store2)
3876+
<AsyncArray file://example.zarr shape=(100, 100) dtype=int32>
3877+
3878+
Create an array from an existing NumPy array:
3879+
>>> arr3 = await zarr.api.asynchronous.from_array(
3880+
>>> np.arange(10000, dtype='i4').reshape(100, 100),
3881+
>>> store=zarr.storage.MemoryStore(),
3882+
>>> )
3883+
<AsyncArray memory://123286956732800 shape=(100, 100) dtype=int32>
3884+
3885+
Create an array from any array-like object:
3886+
>>> arr4 = await zarr.api.asynchronous.from_array(
3887+
>>> [[1, 2], [3, 4]],
3888+
>>> store=zarr.storage.MemoryStore(),
3889+
>>> )
3890+
<AsyncArray memory://123286959761024 shape=(2, 2) dtype=int64>
3891+
>>> await arr4.getitem(...)
3892+
array([[1, 2],
3893+
[3, 4]])
3894+
3895+
Create an array from an existing Array without copying the data:
3896+
>>> arr5 = await zarr.api.asynchronous.from_array(
3897+
>>> Array(arr4),
3898+
>>> store=zarr.storage.MemoryStore(),
3899+
>>> write_data=False,
3900+
>>> )
3901+
<AsyncArray memory://140678602965568 shape=(2, 2) dtype=int64>
3902+
>>> await arr5.getitem(...)
3903+
array([[0, 0],
3904+
[0, 0]])
38573905
"""
3858-
38593906
if isinstance(data, Array):
38603907
if chunks == "keep":
38613908
chunks = data.chunks
@@ -3900,7 +3947,6 @@ async def from_array(
39003947
serializer = "auto"
39013948
if not hasattr(data, "dtype") or not hasattr(data, "shape"):
39023949
data = np.array(data)
3903-
print(data.shape)
39043950
new_array = await create_array(
39053951
store,
39063952
name=name,
@@ -4064,7 +4110,7 @@ async def create_array(
40644110
Examples
40654111
--------
40664112
>>> import zarr
4067-
>>> store = zarr.storage.MemoryStore(mode='w')
4113+
>>> store = zarr.storage.MemoryStore()
40684114
>>> async_arr = await zarr.api.asynchronous.create_array(
40694115
>>> store=store,
40704116
>>> shape=(100,100),

0 commit comments

Comments
 (0)