Skip to content

Commit 9c845bf

Browse files
authored
Merge branch 'main' into docs/examples
2 parents 6ee16fe + 49db79a commit 9c845bf

File tree

19 files changed

+461
-231
lines changed

19 files changed

+461
-231
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,10 @@ jobs:
129129
pip install hatch
130130
- name: Set Up Hatch Env
131131
run: |
132-
hatch env create docs
133-
hatch env run -e docs list-env
132+
hatch run doctest:pip list
134133
- name: Run Tests
135134
run: |
136-
hatch env run --env docs check
135+
hatch run doctest:test
137136
138137
test-complete:
139138
name: Test complete

docs/user-guide/storage.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ print(group)
2525

2626
```python exec="true" session="storage" source="above" result="ansi"
2727
# Implicitly create a read-only FsspecStore
28+
# Note: requires s3fs to be installed
2829
group = zarr.open_group(
2930
store='s3://noaa-nwm-retro-v2-zarr-pds',
3031
mode='r',
@@ -59,6 +60,7 @@ print(group)
5960

6061
- an FSSpec URI string, indicating a [remote store](#remote-store) location:
6162
```python exec="true" session="storage" source="above" result="ansi"
63+
# Note: requires s3fs to be installed
6264
group = zarr.open_group(
6365
store='s3://noaa-nwm-retro-v2-zarr-pds',
6466
mode='r',
@@ -125,6 +127,7 @@ that implements the [AbstractFileSystem](https://filesystem-spec.readthedocs.io/
125127
API. `storage_options` can be used to configure the fsspec backend:
126128

127129
```python exec="true" session="storage" source="above" result="ansi"
130+
# Note: requires s3fs to be installed
128131
store = zarr.storage.FsspecStore.from_url(
129132
's3://noaa-nwm-retro-v2-zarr-pds',
130133
read_only=True,
@@ -138,6 +141,7 @@ The type of filesystem (e.g. S3, https, etc..) is inferred from the scheme of th
138141
In case a specific filesystem is needed, one can explicitly create it. For example to create a S3 filesystem:
139142

140143
```python exec="true" session="storage" source="above" result="ansi"
144+
# Note: requires s3fs to be installed
141145
import fsspec
142146
fs = fsspec.filesystem(
143147
's3', anon=True, asynchronous=True,

pyproject.toml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,18 @@ serve = "mkdocs serve --watch src"
255255
build = "mkdocs build"
256256
check = "mkdocs build --strict"
257257
readthedocs = "rm -rf $READTHEDOCS_OUTPUT/html && cp -r site $READTHEDOCS_OUTPUT/html"
258+
259+
[tool.hatch.envs.doctest]
260+
description = "Test environment for validating executable code blocks in documentation"
261+
features = ['test', 'remote'] # Include remote dependencies for s3fs
262+
dependencies = [
263+
"s3fs>=2023.10.0",
264+
"pytest",
265+
"pytest-examples",
266+
]
267+
268+
[tool.hatch.envs.doctest.scripts]
269+
test = "pytest tests/test_docs.py -v"
258270
list-env = "pip list"
259271

260272
[tool.ruff]
@@ -396,7 +408,8 @@ addopts = [
396408
]
397409
filterwarnings = [
398410
"error",
399-
"ignore:Unclosed client session <aiohttp.client.ClientSession.*:ResourceWarning"
411+
"ignore:Unclosed client session <aiohttp.client.ClientSession.*:ResourceWarning",
412+
"ignore:Numcodecs codecs are not in the Zarr version 3 specification.*:UserWarning"
400413
]
401414
markers = [
402415
"asyncio: mark test as asyncio test",

src/zarr/api/synchronous.py

Lines changed: 69 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -946,15 +946,17 @@ def create_array(
946946
947947
Examples
948948
--------
949-
>>> import zarr
950-
>>> store = zarr.storage.MemoryStore()
951-
>>> arr = await zarr.create_array(
952-
>>> store=store,
953-
>>> shape=(100,100),
954-
>>> chunks=(10,10),
955-
>>> dtype='i4',
956-
>>> fill_value=0)
957-
<Array memory://140349042942400 shape=(100, 100) dtype=int32>
949+
```python
950+
import zarr
951+
store = zarr.storage.MemoryStore()
952+
arr = zarr.create_array(
953+
store=store,
954+
shape=(100,100),
955+
chunks=(10,10),
956+
dtype='i4',
957+
fill_value=0)
958+
# <Array memory://... shape=(100, 100) dtype=int32>
959+
```
958960
"""
959961
return Array(
960962
sync(
@@ -1132,49 +1134,64 @@ def from_array(
11321134
11331135
Examples
11341136
--------
1135-
Create an array from an existing Array::
1136-
1137-
>>> import zarr
1138-
>>> store = zarr.storage.MemoryStore()
1139-
>>> store2 = zarr.storage.LocalStore('example.zarr')
1140-
>>> arr = zarr.create_array(
1141-
>>> store=store,
1142-
>>> shape=(100,100),
1143-
>>> chunks=(10,10),
1144-
>>> dtype='int32',
1145-
>>> fill_value=0)
1146-
>>> arr2 = zarr.from_array(store2, data=arr)
1147-
<Array file://example.zarr shape=(100, 100) dtype=int32>
1148-
1149-
Create an array from an existing NumPy array::
1150-
1151-
>>> import numpy as np
1152-
>>> arr3 = zarr.from_array(
1153-
zarr.storage.MemoryStore(),
1154-
>>> data=np.arange(10000, dtype='i4').reshape(100, 100),
1155-
>>> )
1156-
<Array memory://125477403529984 shape=(100, 100) dtype=int32>
1157-
1158-
Create an array from any array-like object::
1159-
1160-
>>> arr4 = zarr.from_array(
1161-
>>> zarr.storage.MemoryStore(),
1162-
>>> data=[[1, 2], [3, 4]],
1163-
>>> )
1164-
<Array memory://125477392154368 shape=(2, 2) dtype=int64>
1165-
>>> arr4[...]
1166-
array([[1, 2],[3, 4]])
1167-
1168-
Create an array from an existing Array without copying the data::
1169-
1170-
>>> arr5 = zarr.from_array(
1171-
>>> zarr.storage.MemoryStore(),
1172-
>>> data=arr4,
1173-
>>> write_data=False,
1174-
>>> )
1175-
<Array memory://140678602965568 shape=(2, 2) dtype=int64>
1176-
>>> arr5[...]
1177-
array([[0, 0],[0, 0]])
1137+
Create an array from an existing Array:
1138+
1139+
```python
1140+
import zarr
1141+
store = zarr.storage.MemoryStore()
1142+
store2 = zarr.storage.LocalStore('example_from_array.zarr')
1143+
arr = zarr.create_array(
1144+
store=store,
1145+
shape=(100,100),
1146+
chunks=(10,10),
1147+
dtype='int32',
1148+
fill_value=0)
1149+
arr2 = zarr.from_array(store2, data=arr, overwrite=True)
1150+
# <Array file://example_from_array.zarr shape=(100, 100) dtype=int32>
1151+
```
1152+
1153+
Create an array from an existing NumPy array:
1154+
1155+
```python
1156+
import zarr
1157+
import numpy as np
1158+
arr3 = zarr.from_array(
1159+
zarr.storage.MemoryStore(),
1160+
data=np.arange(10000, dtype='i4').reshape(100, 100),
1161+
)
1162+
# <Array memory://... shape=(100, 100) dtype=int32>
1163+
```
1164+
1165+
Create an array from any array-like object:
1166+
1167+
```python
1168+
import zarr
1169+
arr4 = zarr.from_array(
1170+
zarr.storage.MemoryStore(),
1171+
data=[[1, 2], [3, 4]],
1172+
)
1173+
# <Array memory://... shape=(2, 2) dtype=int64>
1174+
arr4[...]
1175+
# array([[1, 2],[3, 4]])
1176+
```
1177+
1178+
Create an array from an existing Array without copying the data:
1179+
1180+
```python
1181+
import zarr
1182+
arr4 = zarr.from_array(
1183+
zarr.storage.MemoryStore(),
1184+
data=[[1, 2], [3, 4]],
1185+
)
1186+
arr5 = zarr.from_array(
1187+
zarr.storage.MemoryStore(),
1188+
data=arr4,
1189+
write_data=False,
1190+
)
1191+
# <Array memory://... shape=(2, 2) dtype=int64>
1192+
arr5[...]
1193+
# array([[0, 0],[0, 0]])
1194+
```
11781195
"""
11791196
return Array(
11801197
sync(

src/zarr/codecs/numcodecs/_codecs.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33
44
These codecs were previously defined in [numcodecs][], and have now been moved to `zarr`.
55
6-
>>> import numpy as np
7-
>>> import zarr
8-
>>> import zarr.codecs.numcodecs as numcodecs
9-
>>>
10-
>>> array = zarr.create_array(
11-
... store="data.zarr",
12-
... shape=(1024, 1024),
13-
... chunks=(64, 64),
14-
... dtype="uint32",
15-
... filters=[numcodecs.Delta(dtype="uint32")],
16-
... compressors=[numcodecs.BZ2(level=5)])
17-
>>> array[:] = np.arange(np.prod(array.shape), dtype=array.dtype).reshape(*array.shape)
6+
```python
7+
import numpy as np
8+
import zarr
9+
import zarr.codecs.numcodecs as numcodecs
10+
11+
array = zarr.create_array(
12+
store="data_numcodecs.zarr",
13+
shape=(1024, 1024),
14+
chunks=(64, 64),
15+
dtype="uint32",
16+
filters=[numcodecs.Delta(dtype="uint32")],
17+
compressors=[numcodecs.BZ2(level=5)],
18+
overwrite=True)
19+
array[:] = np.arange(np.prod(array.shape), dtype=array.dtype).reshape(*array.shape)
20+
```
1821
1922
!!! note
2023
Please note that the codecs in [zarr.codecs.numcodecs][] are not part of the Zarr version

0 commit comments

Comments
 (0)