Skip to content

Commit

Permalink
Fix doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
dstansby committed Dec 12, 2024
1 parent 29f2335 commit b311a4e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
34 changes: 18 additions & 16 deletions zarr/convenience.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

_builtin_open = open # builtin open is later shadowed by a local open function

__doctest_requires__ = {("*"): ["numpy>=2.2"]}


def _check_and_update_path(store: BaseStore, path):
if getattr(store, "_store_version", 2) > 2 and not path:
Expand Down Expand Up @@ -174,13 +176,13 @@ def save_array(store: StoreLike, arr, *, zarr_version=None, path=None, **kwargs)
>>> arr = np.arange(10000)
>>> zarr.save_array('data/example.zarr', arr)
>>> zarr.load('data/example.zarr')
array([ 0, 1, 2, ..., 9997, 9998, 9999])
array([ 0, 1, 2, ..., 9997, 9998, 9999], shape=(10000,))
Save an array to a single file (uses a :class:`ZipStore`)::
>>> zarr.save_array('data/example.zip', arr)
>>> zarr.load('data/example.zip')
array([ 0, 1, 2, ..., 9997, 9998, 9999])
array([ 0, 1, 2, ..., 9997, 9998, 9999], shape=(10000,))
"""
may_need_closing = _might_close(store)
Expand Down Expand Up @@ -234,9 +236,9 @@ def save_group(store: StoreLike, *args, zarr_version=None, path=None, **kwargs):
>>> loader
<LazyLoader: arr_0, arr_1>
>>> loader['arr_0']
array([ 0, 1, 2, ..., 9997, 9998, 9999])
array([ 0, 1, 2, ..., 9997, 9998, 9999], shape=(10000,))
>>> loader['arr_1']
array([10000, 9999, 9998, ..., 3, 2, 1])
array([10000, 9999, 9998, ..., 3, 2, 1], shape=(10000,))
Save several arrays using named keyword arguments::
Expand All @@ -245,9 +247,9 @@ def save_group(store: StoreLike, *args, zarr_version=None, path=None, **kwargs):
>>> loader
<LazyLoader: bar, foo>
>>> loader['foo']
array([ 0, 1, 2, ..., 9997, 9998, 9999])
array([ 0, 1, 2, ..., 9997, 9998, 9999], shape=(10000,))
>>> loader['bar']
array([10000, 9999, 9998, ..., 3, 2, 1])
array([10000, 9999, 9998, ..., 3, 2, 1], shape=(10000,))
Store several arrays in a single zip file (uses a :class:`ZipStore`)::
Expand All @@ -256,9 +258,9 @@ def save_group(store: StoreLike, *args, zarr_version=None, path=None, **kwargs):
>>> loader
<LazyLoader: bar, foo>
>>> loader['foo']
array([ 0, 1, 2, ..., 9997, 9998, 9999])
array([ 0, 1, 2, ..., 9997, 9998, 9999], shape=(10000,))
>>> loader['bar']
array([10000, 9999, 9998, ..., 3, 2, 1])
array([10000, 9999, 9998, ..., 3, 2, 1], shape=(10000,))
Notes
-----
Expand Down Expand Up @@ -316,13 +318,13 @@ def save(store: StoreLike, *args, zarr_version=None, path=None, **kwargs):
>>> arr = np.arange(10000)
>>> zarr.save('data/example.zarr', arr)
>>> zarr.load('data/example.zarr')
array([ 0, 1, 2, ..., 9997, 9998, 9999])
array([ 0, 1, 2, ..., 9997, 9998, 9999], shape=(10000,))
Save an array to a Zip file (uses a :class:`ZipStore`)::
>>> zarr.save('data/example.zip', arr)
>>> zarr.load('data/example.zip')
array([ 0, 1, 2, ..., 9997, 9998, 9999])
array([ 0, 1, 2, ..., 9997, 9998, 9999], shape=(10000,))
Save several arrays to a directory on the file system (uses a
:class:`DirectoryStore` and stores arrays in a group)::
Expand All @@ -336,9 +338,9 @@ def save(store: StoreLike, *args, zarr_version=None, path=None, **kwargs):
>>> loader
<LazyLoader: arr_0, arr_1>
>>> loader['arr_0']
array([ 0, 1, 2, ..., 9997, 9998, 9999])
array([ 0, 1, 2, ..., 9997, 9998, 9999], shape=(10000,))
>>> loader['arr_1']
array([10000, 9999, 9998, ..., 3, 2, 1])
array([10000, 9999, 9998, ..., 3, 2, 1], shape=(10000,))
Save several arrays using named keyword arguments::
Expand All @@ -347,9 +349,9 @@ def save(store: StoreLike, *args, zarr_version=None, path=None, **kwargs):
>>> loader
<LazyLoader: bar, foo>
>>> loader['foo']
array([ 0, 1, 2, ..., 9997, 9998, 9999])
array([ 0, 1, 2, ..., 9997, 9998, 9999], shape=(10000,))
>>> loader['bar']
array([10000, 9999, 9998, ..., 3, 2, 1])
array([10000, 9999, 9998, ..., 3, 2, 1], shape=(10000,))
Store several arrays in a single zip file (uses a :class:`ZipStore`)::
Expand All @@ -358,9 +360,9 @@ def save(store: StoreLike, *args, zarr_version=None, path=None, **kwargs):
>>> loader
<LazyLoader: bar, foo>
>>> loader['foo']
array([ 0, 1, 2, ..., 9997, 9998, 9999])
array([ 0, 1, 2, ..., 9997, 9998, 9999], shape=(10000,))
>>> loader['bar']
array([10000, 9999, 9998, ..., 3, 2, 1])
array([10000, 9999, 9998, ..., 3, 2, 1], shape=(10000,))
See Also
--------
Expand Down
17 changes: 9 additions & 8 deletions zarr/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
)

__all__ = ["Array"]
__doctest_requires__ = {("*"): ["numpy>=2.2"]}


# noinspection PyUnresolvedReferences
Expand Down Expand Up @@ -2793,33 +2794,33 @@ def view(
>>> a = zarr.array(data, chunks=1000, filters=filters)
>>> a[:]
array(['female', 'male', 'female', ..., 'male', 'male', 'female'],
dtype='<U6')
shape=(10000,), dtype='<U6')
>>> v = a.view(dtype='u1', filters=[])
>>> v.is_view
True
>>> v[:]
array([1, 2, 1, ..., 2, 2, 1], dtype=uint8)
array([1, 2, 1, ..., 2, 2, 1], shape=(10000,), dtype=uint8)
Views can be used to modify data:
>>> x = v[:]
>>> x.sort()
>>> v[:] = x
>>> v[:]
array([1, 1, 1, ..., 2, 2, 2], dtype=uint8)
array([1, 1, 1, ..., 2, 2, 2], shape=(10000,), dtype=uint8)
>>> a[:]
array(['female', 'female', 'female', ..., 'male', 'male', 'male'],
dtype='<U6')
shape=(10000,), dtype='<U6')
View as a different dtype with the same item size:
>>> data = np.random.randint(0, 2, size=10000, dtype='u1')
>>> a = zarr.array(data, chunks=1000)
>>> a[:]
array([0, 0, 1, ..., 1, 0, 0], dtype=uint8)
array([0, 0, 1, ..., 1, 0, 0], shape=(10000,), dtype=uint8)
>>> v = a.view(dtype=bool)
>>> v[:]
array([False, False, True, ..., True, False, False])
array([False, False, True, ..., True, False, False], shape=(10000,))
>>> np.all(a[:].view(dtype=bool) == v[:])
np.True_
Expand All @@ -2841,10 +2842,10 @@ def view(
>>> a = zarr.full(10000, chunks=1000, fill_value=-1, dtype='i1')
>>> a[:]
array([-1, -1, -1, ..., -1, -1, -1], dtype=int8)
array([-1, -1, -1, ..., -1, -1, -1], shape=(10000,), dtype=int8)
>>> v = a.view(fill_value=42)
>>> v[:]
array([42, 42, 42, ..., 42, 42, 42], dtype=int8)
array([42, 42, 42, ..., 42, 42, 42], shape=(10000,), dtype=int8)
Note that resizing or appending to views is not permitted:
Expand Down

0 comments on commit b311a4e

Please sign in to comment.