Skip to content

Commit 13d9ce0

Browse files
sanketverma1704pre-commit-ci[bot]jhamman
authored
Add docstrings for array module (#2281)
* Add docstrings for array module * style: pre-commit fixes * Add more docstrings * Remove docstrings for internal functions * Fix pre-commit * Minor fix * Precommit fix * Update src/zarr/core/array.py Co-authored-by: Joe Hamman <[email protected]> * Update src/zarr/core/array.py Co-authored-by: Joe Hamman <[email protected]> * Update src/zarr/core/array.py Co-authored-by: Joe Hamman <[email protected]> * Update src/zarr/core/array.py Co-authored-by: Joe Hamman <[email protected]> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Joe Hamman <[email protected]> Co-authored-by: Joe Hamman <[email protected]>
1 parent 498cb78 commit 13d9ce0

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

src/zarr/core/array.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,29 @@ def from_dict(
645645
store_path: StorePath,
646646
data: dict[str, JSON],
647647
) -> AsyncArray[ArrayV3Metadata] | AsyncArray[ArrayV2Metadata]:
648+
"""
649+
Create a Zarr array from a dictionary, with support for both Zarr v2 and v3 metadata.
650+
651+
Parameters
652+
----------
653+
store_path : StorePath
654+
The path within the store where the array should be created.
655+
656+
data : dict
657+
A dictionary representing the array data. This dictionary should include necessary metadata
658+
for the array, such as shape, dtype, and other attributes. The format of the metadata
659+
will determine whether a Zarr v2 or v3 array is created.
660+
661+
Returns
662+
-------
663+
AsyncArray[ArrayV3Metadata] or AsyncArray[ArrayV2Metadata]
664+
The created Zarr array, either using v2 or v3 metadata based on the provided data.
665+
666+
Raises
667+
------
668+
ValueError
669+
If the dictionary data is invalid or incompatible with either Zarr v2 or v3 array creation.
670+
"""
648671
metadata = parse_array_metadata(data)
649672
return cls(metadata=metadata, store_path=store_path)
650673

@@ -1040,6 +1063,9 @@ async def getitem(
10401063
return await self._get_selection(indexer, prototype=prototype)
10411064

10421065
async def _save_metadata(self, metadata: ArrayMetadata, ensure_parents: bool = False) -> None:
1066+
"""
1067+
Asynchronously save the array metadata.
1068+
"""
10431069
to_save = metadata.to_buffer_dict(default_buffer_prototype())
10441070
awaitables = [set_or_delete(self.store_path / key, value) for key, value in to_save.items()]
10451071

@@ -1118,6 +1144,39 @@ async def setitem(
11181144
value: npt.ArrayLike,
11191145
prototype: BufferPrototype | None = None,
11201146
) -> None:
1147+
"""
1148+
Asynchronously set values in the array using basic indexing.
1149+
1150+
Parameters
1151+
----------
1152+
selection : BasicSelection
1153+
The selection defining the region of the array to set.
1154+
1155+
value : numpy.typing.ArrayLike
1156+
The values to be written into the selected region of the array.
1157+
1158+
prototype : BufferPrototype or None, optional
1159+
A prototype buffer that defines the structure and properties of the array chunks being modified.
1160+
If None, the default buffer prototype is used. Default is None.
1161+
1162+
Returns
1163+
-------
1164+
None
1165+
This method does not return any value.
1166+
1167+
Raises
1168+
------
1169+
IndexError
1170+
If the selection is out of bounds for the array.
1171+
1172+
ValueError
1173+
If the values are not compatible with the array's dtype or shape.
1174+
1175+
Notes
1176+
-----
1177+
- This method is asynchronous and should be awaited.
1178+
- Supports basic indexing, where the selection is contiguous and does not involve advanced indexing.
1179+
"""
11211180
if prototype is None:
11221181
prototype = default_buffer_prototype()
11231182
indexer = BasicIndexer(
@@ -1128,6 +1187,32 @@ async def setitem(
11281187
return await self._set_selection(indexer, value, prototype=prototype)
11291188

11301189
async def resize(self, new_shape: ShapeLike, delete_outside_chunks: bool = True) -> None:
1190+
"""
1191+
Asynchronously resize the array to a new shape.
1192+
1193+
Parameters
1194+
----------
1195+
new_shape : ChunkCoords
1196+
The desired new shape of the array.
1197+
1198+
delete_outside_chunks : bool, optional
1199+
If True (default), chunks that fall outside the new shape will be deleted. If False,
1200+
the data in those chunks will be preserved.
1201+
1202+
Returns
1203+
-------
1204+
AsyncArray
1205+
The resized array.
1206+
1207+
Raises
1208+
------
1209+
ValueError
1210+
If the new shape is incompatible with the current array's chunking configuration.
1211+
1212+
Notes
1213+
-----
1214+
- This method is asynchronous and should be awaited.
1215+
"""
11311216
new_shape = parse_shapelike(new_shape)
11321217
assert len(new_shape) == len(self.metadata.shape)
11331218
new_metadata = self.metadata.update_shape(new_shape)
@@ -1210,6 +1295,31 @@ async def append(self, data: npt.ArrayLike, axis: int = 0) -> ChunkCoords:
12101295
return new_shape
12111296

12121297
async def update_attributes(self, new_attributes: dict[str, JSON]) -> Self:
1298+
"""
1299+
Asynchronously update the array's attributes.
1300+
1301+
Parameters
1302+
----------
1303+
new_attributes : dict of str to JSON
1304+
A dictionary of new attributes to update or add to the array. The keys represent attribute
1305+
names, and the values must be JSON-compatible.
1306+
1307+
Returns
1308+
-------
1309+
AsyncArray
1310+
The array with the updated attributes.
1311+
1312+
Raises
1313+
------
1314+
ValueError
1315+
If the attributes are invalid or incompatible with the array's metadata.
1316+
1317+
Notes
1318+
-----
1319+
- This method is asynchronous and should be awaited.
1320+
- The updated attributes will be merged with existing attributes, and any conflicts will be
1321+
overwritten by the new values.
1322+
"""
12131323
# metadata.attributes is "frozen" so we simply clear and update the dict
12141324
self.metadata.attributes.clear()
12151325
self.metadata.attributes.update(new_attributes)
@@ -1328,6 +1438,28 @@ def from_dict(
13281438
store_path: StorePath,
13291439
data: dict[str, JSON],
13301440
) -> Array:
1441+
"""
1442+
Create a Zarr array from a dictionary.
1443+
1444+
Parameters
1445+
----------
1446+
store_path : StorePath
1447+
The path within the store where the array should be created.
1448+
1449+
data : dict
1450+
A dictionary representing the array data. This dictionary should include necessary metadata
1451+
for the array, such as shape, dtype, fill value, and attributes.
1452+
1453+
Returns
1454+
-------
1455+
Array
1456+
The created Zarr array.
1457+
1458+
Raises
1459+
------
1460+
ValueError
1461+
If the dictionary data is invalid or missing required fields for array creation.
1462+
"""
13311463
async_array = AsyncArray.from_dict(store_path=store_path, data=data)
13321464
return cls(async_array)
13331465

@@ -2934,6 +3066,30 @@ def append(self, data: npt.ArrayLike, axis: int = 0) -> ChunkCoords:
29343066
return sync(self._async_array.append(data, axis=axis))
29353067

29363068
def update_attributes(self, new_attributes: dict[str, JSON]) -> Array:
3069+
"""
3070+
Update the array's attributes.
3071+
3072+
Parameters
3073+
----------
3074+
new_attributes : dict
3075+
A dictionary of new attributes to update or add to the array. The keys represent attribute
3076+
names, and the values must be JSON-compatible.
3077+
3078+
Returns
3079+
-------
3080+
Array
3081+
The array with the updated attributes.
3082+
3083+
Raises
3084+
------
3085+
ValueError
3086+
If the attributes are invalid or incompatible with the array's metadata.
3087+
3088+
Notes
3089+
-----
3090+
- The updated attributes will be merged with existing attributes, and any conflicts will be
3091+
overwritten by the new values.
3092+
"""
29373093
# TODO: remove this cast when type inference improves
29383094
new_array = sync(self._async_array.update_attributes(new_attributes))
29393095
# TODO: remove this cast when type inference improves

0 commit comments

Comments
 (0)