@@ -5,32 +5,29 @@ the Zarr library in Python to efficiently manage and analyze multi-dimensional a
55
66To get started, you can create a simple Zarr array:
77
8- ``` python
8+ ``` python exec="true" session="quickstart"
9+ import shutil
10+ shutil.rmtree(' data' , ignore_errors = True )
11+ import numpy as np
12+
13+ np.random.seed(0 )
14+ ```
15+
16+ ``` python exec="true" session="quickstart" source="material-block" result="ansi"
917import zarr
1018import numpy as np
1119
1220# Create a 2D Zarr array
1321z = zarr.create_array(
14- store = " data/example-1.zarr" ,
15- shape = (100 , 100 ),
16- chunks = (10 , 10 ),
17- dtype = " f4"
22+ store = " data/example-1.zarr" ,
23+ shape = (100 , 100 ),
24+ chunks = (10 , 10 ),
25+ dtype = " f4"
1826)
1927
2028# Assign data to the array
2129z[:, :] = np.random.random((100 , 100 ))
22- z.info
23-
24- # Type : Array
25- # Zarr format : 3
26- # Data type : DataType.float32
27- # Shape : (100, 100)
28- # Chunk shape : (10, 10)
29- # Order : C
30- # Read-only : False
31- # Store type : LocalStore
32- # Codecs : [{'endian': <Endian.little: 'little'>}, {'level': 0, 'checksum': False}]
33- # No. bytes : 40000 (39.1K)
30+ print (z.info)
3431```
3532
3633Here, we created a 2D array of shape ` (100, 100) ` , chunked into blocks of
@@ -41,36 +38,38 @@ written to a `LocalStore` in the `data/example-1.zarr` directory.
4138
4239Zarr supports data compression and filters. For example, to use Blosc compression:
4340
44- ``` python
41+
42+ ``` python exec="true" session="quickstart" source="material-block" result="ansi"
43+
44+ # Create a 2D Zarr array with Blosc compression
4545z = zarr.create_array(
46- " data/example-3.zarr" ,
47- mode = " w" , shape = (100 , 100 ),
48- chunks = (10 , 10 ), dtype = " f4" ,
49- compressors = zarr.codecs.BloscCodec(cname = " zstd" , clevel = 3 , shuffle = zarr.codecs.BloscShuffle.shuffle)
46+ store = " data/example-2.zarr" ,
47+ shape = (100 , 100 ),
48+ chunks = (10 , 10 ),
49+ dtype = " f4" ,
50+ compressors = zarr.codecs.BloscCodec(
51+ cname = " zstd" ,
52+ clevel = 3 ,
53+ shuffle = zarr.codecs.BloscShuffle.shuffle
54+ )
5055)
56+
57+ # Assign data to the array
5158z[:, :] = np.random.random((100 , 100 ))
52- z.info
53- # Type : Array
54- # Zarr format : 3
55- # Data type : DataType.float32
56- # Shape : (100, 100)
57- # Chunk shape : (10, 10)
58- # Order : C
59- # Read-only : False
60- # Store type : LocalStore
61- # Codecs : [{'endian': <Endian.little: 'little'>}, {'level': 0, 'checksum': False}]
62- # No. bytes : 40000 (39.1K)
59+ print (z.info)
6360```
6461
65- This compresses the data using the Zstandard codec with shuffle enabled for better compression.
62+ This compresses the data using the Blosc codec with shuffle enabled for better compression.
63+
6664
6765### Hierarchical Groups
6866
6967Zarr allows you to create hierarchical groups, similar to directories:
7068
71- ``` python
69+ ``` python exec="true" session="quickstart" source="material-block" result="ansi"
70+
7271# Create nested groups and add arrays
73- root = zarr.group(" data/example-2 .zarr" )
72+ root = zarr.group(" data/example-3 .zarr" )
7473foo = root.create_group(name = " foo" )
7574bar = root.create_array(
7675 name = " bar" , shape = (100 , 10 ), chunks = (10 , 10 ), dtype = " f4"
@@ -82,11 +81,7 @@ bar[:, :] = np.random.random((100, 10))
8281spam[:] = np.arange(10 )
8382
8483# print the hierarchy
85- root.tree()
86- # /
87- # ├── bar (100, 10) float32
88- # └── foo
89- # └── spam (10,) int32
84+ print (root.tree())
9085```
9186
9287This creates a group with two datasets: ` foo ` and ` bar ` .
@@ -96,17 +91,17 @@ This creates a group with two datasets: `foo` and `bar`.
9691Zarr provides tools for creating a collection of arrays and groups with a single function call.
9792Suppose we want to copy existing groups and arrays into a new storage backend:
9893
99- ``` python
94+ ``` python exec="true" session="quickstart" source="material-block" result="ansi"
95+
10096# Create nested groups and add arrays
101- root = zarr.group(" data/example-3 .zarr" , attributes = {' name' : ' root' })
97+ root = zarr.group(" data/example-4 .zarr" , attributes = {' name' : ' root' })
10298foo = root.create_group(name = " foo" )
10399bar = root.create_array(
104100 name = " bar" , shape = (100 , 10 ), chunks = (10 , 10 ), dtype = " f4"
105101)
106102nodes = {' ' : root.metadata} | {k: v.metadata for k,v in root.members()}
107103print (nodes)
108- from zarr.storage import MemoryStore
109- new_nodes = dict (zarr.create_hierarchy(store = MemoryStore(), nodes = nodes))
104+ new_nodes = dict (zarr.create_hierarchy(store = zarr.storage.MemoryStore(), nodes = nodes))
110105new_root = new_nodes[' ' ]
111106assert new_root.attrs == root.attrs
112107```
@@ -124,6 +119,7 @@ using external libraries like [s3fs](https://s3fs.readthedocs.io) or
124119[ gcsfs] ( https://gcsfs.readthedocs.io ) :
125120
126121``` python
122+
127123import s3fs
128124
129125z = zarr.create_array(" s3://example-bucket/foo" , mode = " w" , shape = (100 , 100 ), chunks = (10 , 10 ), dtype = " f4" )
@@ -132,13 +128,13 @@ z[:, :] = np.random.random((100, 100))
132128
133129A single-file store can also be created using the ` zarr.storage.ZipStore ` :
134130
135- ``` python
131+ ``` python exec="true" session="quickstart" source="material-block"
132+
136133# Store the array in a ZIP file
137- store = zarr.storage.ZipStore(" data/example-3 .zip" , mode = ' w ' )
134+ store = zarr.storage.ZipStore(" data/example-5 .zip" , mode = " w " )
138135
139136z = zarr.create_array(
140137 store = store,
141- mode = " w" ,
142138 shape = (100 , 100 ),
143139 chunks = (10 , 10 ),
144140 dtype = " f4"
@@ -153,27 +149,15 @@ store.close()
153149
154150To open an existing array from a ZIP file:
155151
156- ``` python
152+ ``` python exec="true" session="quickstart" source="material-block" result="ansi"
153+
157154# Open the ZipStore in read-only mode
158- store = zarr.storage.ZipStore(" data/example-3 .zip" , read_only = True )
155+ store = zarr.storage.ZipStore(" data/example-5 .zip" , read_only = True )
159156
160157z = zarr.open_array(store, mode = ' r' )
161158
162159# read the data as a NumPy Array
163- z[:]
164- # array([[0.66734236, 0.15667458, 0.98720884, ..., 0.36229587, 0.67443246,
165- # 0.34315267],
166- # [0.65787303, 0.9544212 , 0.4830079 , ..., 0.33097172, 0.60423803,
167- # 0.45621237],
168- # [0.27632037, 0.9947008 , 0.42434934, ..., 0.94860053, 0.6226942 ,
169- # 0.6386924 ],
170- # ...,
171- # [0.12854576, 0.934397 , 0.19524333, ..., 0.11838563, 0.4967675 ,
172- # 0.43074256],
173- # [0.82029045, 0.4671437 , 0.8090906 , ..., 0.7814118 , 0.42650765,
174- # 0.95929915],
175- # [0.4335856 , 0.7565437 , 0.7828931 , ..., 0.48119593, 0.66220033,
176- # 0.6652362 ]], shape=(100, 100), dtype=float32)
160+ print (z[:])
177161```
178162
179163Read more about Zarr's storage options in the [ User Guide] ( user-guide/storage.md ) .
0 commit comments