Skip to content

Commit cb2a205

Browse files
committed
Executable code blocks in data types
1 parent d8e4378 commit cb2a205

File tree

1 file changed

+88
-92
lines changed

1 file changed

+88
-92
lines changed

docs/user-guide/data_types.md

Lines changed: 88 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,20 @@ Version 2 of the Zarr format defined its data types relative to
4545
and added a few non-NumPy data types as well. With one exception ([structured data types](#structured-data-type)), the Zarr
4646
V2 JSON identifier for a data type is just the NumPy `str` attribute of that data type:
4747

48-
```python
49-
>>> import zarr
50-
>>> import numpy as np
51-
>>> import json
52-
>>>
53-
>>> store = {}
54-
>>> np_dtype = np.dtype('int64')
55-
>>> np_dtype.str
56-
'<i8'
57-
>>> z = zarr.create_array(store=store, shape=(1,), dtype=np_dtype, zarr_format=2)
58-
>>> dtype_meta = json.loads(store['.zarray'].to_bytes())["dtype"]
59-
>>> dtype_meta
60-
'<i8'
48+
```python exec="true" session="data_types" source="above" result="ansi"
49+
import zarr
50+
import numpy as np
51+
import json
52+
53+
store = {}
54+
np_dtype = np.dtype('int64')
55+
print(np_dtype.str)
56+
```
57+
58+
```python exec="true" session="data_types" source="above" result="ansi"
59+
z = zarr.create_array(store=store, shape=(1,), dtype=np_dtype, zarr_format=2)
60+
dtype_meta = json.loads(store['.zarray'].to_bytes())["dtype"]
61+
print(dtype_meta)
6162
```
6263

6364
!!! note
@@ -88,15 +89,16 @@ element is a Zarr V2 data type specification. This representation supports recur
8889

8990
For example:
9091

91-
```python
92-
>>> store = {}
93-
>>> np_dtype = np.dtype([('field_a', '>i2'), ('field_b', [('subfield_c', '>f4'), ('subfield_d', 'i2')])])
94-
>>> np_dtype.str
95-
'|V8'
96-
>>> z = zarr.create_array(store=store, shape=(1,), dtype=np_dtype, zarr_format=2)
97-
>>> dtype_meta = json.loads(store['.zarray'].to_bytes())["dtype"]
98-
>>> dtype_meta
99-
[['field_a', '>i2'], ['field_b', [['subfield_c', '>f4'], ['subfield_d', '<i2']]]]
92+
```python exec="true" session="data_types" source="above" result="ansi"
93+
store = {}
94+
np_dtype = np.dtype([('field_a', '>i2'), ('field_b', [('subfield_c', '>f4'), ('subfield_d', 'i2')])])
95+
print(np_dtype.str)
96+
```
97+
98+
```python exec="true" session="data_types" source="above" result="ansi"
99+
z = zarr.create_array(store=store, shape=(1,), dtype=np_dtype, zarr_format=2)
100+
dtype_meta = json.loads(store['.zarray'].to_bytes())["dtype"]
101+
print(dtype_meta)
100102
```
101103

102104
### Object Data Type
@@ -169,7 +171,7 @@ We do this with an abstract Zarr data type class: [ZDType][zarr.dtype.ZDType]
169171
which provides Zarr V2 and Zarr V3 compatibility routines for "native" data types.
170172

171173
In this context, a "native" data type is a Python class, typically defined in another library, that
172-
models an array's data type. For example, `np.dtypes.UInt8DType` is a native data type defined in NumPy.
174+
models an array's data type. For example, [`numpy.dtypes.UInt8DType`][] is a native data type defined in NumPy.
173175
Zarr Python wraps the NumPy `uint8` with a [ZDType][zarr.dtype.ZDType] instance called
174176
[UInt8][zarr.dtype.UInt8].
175177

@@ -233,31 +235,31 @@ This section will demonstrates the basic usage of Zarr data types.
233235

234236
Create a `ZDType` from a native data type:
235237

236-
```python
237-
>>> from zarr.core.dtype import Int8
238-
>>> import numpy as np
239-
>>> int8 = Int8.from_native_dtype(np.dtype('int8'))
238+
```python exec="true" session="data_types" source="above"
239+
from zarr.core.dtype import Int8
240+
import numpy as np
241+
int8 = Int8.from_native_dtype(np.dtype('int8'))
240242
```
241243

242244
Convert back to a native data type:
243245

244-
```python
245-
>>> native_dtype = int8.to_native_dtype()
246-
>>> assert native_dtype == np.dtype('int8')
246+
```python exec="true" session="data_types" source="above"
247+
native_dtype = int8.to_native_dtype()
248+
assert native_dtype == np.dtype('int8')
247249
```
248250

249251
Get the default scalar value for the data type:
250252

251-
```python
252-
>>> default_value = int8.default_scalar()
253-
>>> assert default_value == np.int8(0)
253+
```python exec="true" session="data_types" source="above"
254+
default_value = int8.default_scalar()
255+
assert default_value == np.int8(0)
254256
```
255257

256258
Serialize to JSON for Zarr V2:
257259

258-
```python
259-
>>> json_v2 = int8.to_json(zarr_format=2)
260-
>>> json_v2
260+
```python exec="true" session="data_types" source="above" result="ansi"
261+
json_v2 = int8.to_json(zarr_format=2)
262+
print(json_v2)
261263
{'name': '|i1', 'object_codec_id': None}
262264
```
263265

@@ -272,25 +274,23 @@ Serialize to JSON for Zarr V2:
272274

273275
And for V3:
274276

275-
```python
276-
>>> json_v3 = int8.to_json(zarr_format=3)
277-
>>> json_v3
278-
'int8'
277+
```python exec="true" session="data_types" source="above" result="ansi"
278+
json_v3 = int8.to_json(zarr_format=3)
279+
print(json_v3)
279280
```
280281

281282
Serialize a scalar value to JSON:
282283

283-
```python
284-
>>> json_value = int8.to_json_scalar(42, zarr_format=3)
285-
>>> json_value
286-
42
284+
```python exec="true" session="data_types" source="above" result="ansi"
285+
json_value = int8.to_json_scalar(42, zarr_format=3)
286+
print(json_value)
287287
```
288288

289289
Deserialize a scalar value from JSON:
290290

291-
```python
292-
>>> scalar_value = int8.from_json_scalar(42, zarr_format=3)
293-
>>> assert scalar_value == np.int8(42)
291+
```python exec="true" session="data_types" source="above"
292+
scalar_value = int8.from_json_scalar(42, zarr_format=3)
293+
assert scalar_value == np.int8(42)
294294
```
295295

296296
### Adding New Data Types
@@ -312,34 +312,31 @@ Python project directory.
312312
Although Zarr Python uses a different data type model from NumPy, you can still define a Zarr array
313313
with a NumPy data type object:
314314

315-
```python
316-
>>> from zarr import create_array
317-
>>> import numpy as np
318-
>>> a = create_array({}, shape=(10,), dtype=np.dtype('int'))
319-
>>> a
320-
<Array memory:... shape=(10,) dtype=int64>
315+
```python exec="true" session="data_types" source="above" result="ansi"
316+
from zarr import create_array
317+
import numpy as np
318+
a = create_array({}, shape=(10,), dtype=np.dtype('int'))
319+
print(a)
321320
```
322321

323322
Or a string representation of a NumPy data type:
324323

325-
```python
326-
>>> a = create_array({}, shape=(10,), dtype='<i8')
327-
>>> a
328-
<Array memory:... shape=(10,) dtype=int64>
324+
```python exec="true" session="data_types" source="above" result="ansi"
325+
a = create_array({}, shape=(10,), dtype='<i8')
326+
print(a)
329327
```
330328

331329
The `Array` object presents itself like a NumPy array, including exposing a NumPy
332330
data type as its `dtype` attribute:
333331

334-
```python
335-
>>> type(a.dtype)
336-
<class 'numpy.dtypes.Int64DType'>
332+
```python exec="true" session="data_types" source="above" result="ansi"
333+
print(type(a.dtype))
337334
```
338335

339336
But if we inspect the metadata for the array, we can see the Zarr data type object:
340337

341338
```python
342-
>>> type(a.metadata.data_type)
339+
type(a.metadata.data_type)
343340
<class 'zarr.core.dtype.npy.int.Int64'>
344341
```
345342

@@ -353,16 +350,17 @@ For simple data types like `int`, the solution could be extremely simple: just
353350
maintain a lookup table that maps a NumPy data type to the Zarr data type equivalent. But not all
354351
data types are so simple. Consider this case:
355352

356-
```python
357-
>>> from zarr import create_array
358-
>>> import warnings
359-
>>> import numpy as np
360-
>>> warnings.simplefilter("ignore", category=FutureWarning)
361-
>>> a = create_array({}, shape=(10,), dtype=[('a', 'f8'), ('b', 'i8')])
362-
>>> a.dtype # this is the NumPy data type
363-
dtype([('a', '<f8'), ('b', '<i8')])
364-
>>> a.metadata.data_type # this is the Zarr data type
365-
Structured(fields=(('a', Float64(endianness='little')), ('b', Int64(endianness='little'))))
353+
```python exec="true" session="data_types" source="above"
354+
from zarr import create_array
355+
import warnings
356+
import numpy as np
357+
warnings.simplefilter("ignore", category=FutureWarning)
358+
a = create_array({}, shape=(10,), dtype=[('a', 'f8'), ('b', 'i8')])
359+
print(a.dtype) # this is the NumPy data type
360+
```
361+
362+
```python exec="true" session="data_types" source="above"
363+
print(a.metadata.data_type) # this is the Zarr data type
366364
```
367365

368366
In this example, we created a
@@ -394,38 +392,36 @@ handles a range of input types:
394392

395393
- NumPy data types:
396394

397-
```python
398-
>>> import numpy as np
399-
>>> from zarr.dtype import parse_dtype
400-
>>> my_dtype = np.dtype('>M8[10s]')
401-
>>> parse_dtype(my_dtype, zarr_format=2)
402-
DateTime64(endianness='big', scale_factor=10, unit='s')
395+
```python exec="true" session="data_types" source="above" result="ansi"
396+
import numpy as np
397+
from zarr.dtype import parse_dtype
398+
my_dtype = np.dtype('>M8[10s]')
399+
print(parse_dtype(my_dtype, zarr_format=2))
403400
```
404401

405402
- NumPy data type-compatible strings:
406403

407-
```python
408-
>>> dtype_str = '>M8[10s]'
409-
>>> parse_dtype(dtype_str, zarr_format=2)
410-
DateTime64(endianness='big', scale_factor=10, unit='s')
404+
```python exec="true" session="data_types" source="above" result="ansi"
405+
dtype_str = '>M8[10s]'
406+
print(parse_dtype(dtype_str, zarr_format=2))
411407
```
412408

413409
- `ZDType` instances:
414410

415-
```python
416-
>>> from zarr.dtype import DateTime64
417-
>>> zdt = DateTime64(endianness='big', scale_factor=10, unit='s')
418-
>>> parse_dtype(zdt, zarr_format=2) # Use a ZDType (this is a no-op)
419-
DateTime64(endianness='big', scale_factor=10, unit='s')
411+
```python exec="true" session="data_types" source="above" result="ansi"
412+
from zarr.dtype import DateTime64
413+
zdt = DateTime64(endianness='big', scale_factor=10, unit='s')
414+
print(parse_dtype(zdt, zarr_format=2)) # Use a ZDType (this is a no-op)
420415
```
421416

422417
- Python dictionaries (requires `zarr_format=3`). These dictionaries must be consistent with the
423418
`JSON` form of the data type:
424419

425-
```python
426-
>>> dt_dict = {"name": "numpy.datetime64", "configuration": {"unit": "s", "scale_factor": 10}}
427-
>>> parse_dtype(dt_dict, zarr_format=3)
428-
DateTime64(endianness='little', scale_factor=10, unit='s')
429-
>>> parse_dtype(dt_dict, zarr_format=3).to_json(zarr_format=3)
430-
{'name': 'numpy.datetime64', 'configuration': {'unit': 's', 'scale_factor': 10}}
420+
```python exec="true" session="data_types" source="above" result="ansi"
421+
dt_dict = {"name": "numpy.datetime64", "configuration": {"unit": "s", "scale_factor": 10}}
422+
print(parse_dtype(dt_dict, zarr_format=3))
423+
```
424+
425+
```python exec="true" session="data_types" source="above" result="ansi"
426+
print(parse_dtype(dt_dict, zarr_format=3).to_json(zarr_format=3))
431427
```

0 commit comments

Comments
 (0)