|
| 1 | +.. ipython:: |
| 2 | + :suppress: |
| 3 | + |
| 4 | + In [999]: rm -r data/ |
| 5 | + |
| 6 | + In [999]: import numpy as np |
| 7 | + |
| 8 | + In [999]: np.random.seed(0) |
| 9 | + |
| 10 | +Quickstart |
| 11 | +========== |
| 12 | + |
| 13 | +Welcome to the Zarr-Python Quickstart guide! This page will help you get up and running with |
| 14 | +the Zarr library in Python to efficiently manage and analyze multi-dimensional arrays. |
| 15 | + |
| 16 | +Zarr is a powerful library for storage of n-dimensional arrays, supporting chunking, |
| 17 | +compression, and various backends, making it a versatile choice for scientific and |
| 18 | +large-scale data. |
| 19 | + |
| 20 | +Installation |
| 21 | +------------ |
| 22 | + |
| 23 | +Zarr requires Python 3.10 or higher. You can install it via `pip`: |
| 24 | + |
| 25 | +.. code-block:: bash |
| 26 | +
|
| 27 | + pip install zarr |
| 28 | +
|
| 29 | +or `conda`: |
| 30 | + |
| 31 | +.. code-block:: bash |
| 32 | +
|
| 33 | + conda install --channel conda-forge zarr |
| 34 | +
|
| 35 | +Creating an Array |
| 36 | +----------------- |
| 37 | + |
| 38 | +To get started, you can create a simple Zarr array: |
| 39 | + |
| 40 | +.. ipython:: python |
| 41 | +
|
| 42 | + import zarr |
| 43 | + import numpy as np |
| 44 | +
|
| 45 | + # Create a 2D Zarr array |
| 46 | + z = zarr.zeros( |
| 47 | + store="data/example-1.zarr", |
| 48 | + shape=(100, 100), |
| 49 | + chunks=(10, 10), |
| 50 | + dtype="f4" |
| 51 | + ) |
| 52 | +
|
| 53 | + # Assign data to the array |
| 54 | + z[:, :] = np.random.random((100, 100)) |
| 55 | + z.info |
| 56 | +
|
| 57 | +Here, we created a 2D array of shape ``(100, 100)``, chunked into blocks of |
| 58 | +``(10, 10)``, and filled it with random floating-point data. This array was |
| 59 | +written to a ``LocalStore`` in the ``data/example-1.zarr`` directory. |
| 60 | + |
| 61 | +Compression and Filters |
| 62 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 63 | + |
| 64 | +Zarr supports data compression and filters. For example, to use Blosc compression: |
| 65 | + |
| 66 | +.. ipython:: python |
| 67 | +
|
| 68 | + from numcodecs import Blosc |
| 69 | +
|
| 70 | + z = zarr.open( |
| 71 | + "data/example-3.zarr", |
| 72 | + mode="w", shape=(100, 100), |
| 73 | + chunks=(10, 10), dtype="f4", |
| 74 | + compressor=Blosc(cname="zstd", clevel=3, shuffle=Blosc.SHUFFLE), |
| 75 | + zarr_format=2 |
| 76 | + ) |
| 77 | + z[:, :] = np.random.random((100, 100)) |
| 78 | +
|
| 79 | + z.info |
| 80 | +
|
| 81 | +This compresses the data using the Zstandard codec with shuffle enabled for better compression. |
| 82 | + |
| 83 | +Hierarchical Groups |
| 84 | +------------------- |
| 85 | + |
| 86 | +Zarr allows you to create hierarchical groups, similar to directories: |
| 87 | + |
| 88 | +.. ipython:: python |
| 89 | +
|
| 90 | + # Create nested groups and add arrays |
| 91 | + root = zarr.group("data/example-2.zarr") |
| 92 | + foo = root.create_group(name="foo") |
| 93 | + bar = root.create_array( |
| 94 | + name="bar", shape=(100, 10), chunks=(10, 10) |
| 95 | + ) |
| 96 | + spam = foo.create_array(name="spam", shape=(10,), dtype="i4") |
| 97 | +
|
| 98 | + # Assign values |
| 99 | + bar[:, :] = np.random.random((100, 10)) |
| 100 | + spam[:] = np.arange(10) |
| 101 | +
|
| 102 | + # print the hierarchy |
| 103 | + root.tree() |
| 104 | +
|
| 105 | +This creates a group with two datasets: ``foo`` and ``bar``. |
| 106 | + |
| 107 | +Persistent Storage |
| 108 | +------------------ |
| 109 | + |
| 110 | +Zarr supports persistent storage to disk or cloud-compatible backends. While examples above |
| 111 | +utilized a :class:`zarr.storage.LocalStore`, a number of other storage options are available, |
| 112 | +including the :class:`zarr.storage.ZipStore` and :class:`zarr.storage.FsspecStore`. |
| 113 | + |
| 114 | +.. ipython:: python |
| 115 | +
|
| 116 | + # Store the array in a ZIP file |
| 117 | + store = zarr.storage.ZipStore("data/example-3.zip", mode='w') |
| 118 | +
|
| 119 | + z = zarr.open( |
| 120 | + store=store, |
| 121 | + mode="w", |
| 122 | + shape=(100, 100), |
| 123 | + chunks=(10, 10), |
| 124 | + dtype="f4" |
| 125 | + ) |
| 126 | +
|
| 127 | + # write to the array |
| 128 | + z[:, :] = np.random.random((100, 100)) |
| 129 | +
|
| 130 | + # the ZipStore must be explicitly closed |
| 131 | + store.close() |
| 132 | +
|
| 133 | +To open an existing array: |
| 134 | + |
| 135 | +.. ipython:: python |
| 136 | +
|
| 137 | + # Open the ZipStore in read-only mode |
| 138 | + store = zarr.storage.ZipStore("data/example-3.zip", read_only=True) |
| 139 | +
|
| 140 | + z = zarr.open(store, mode='r') |
| 141 | +
|
| 142 | + # read the data as a NumPy Array |
| 143 | + z[:] |
| 144 | +
|
| 145 | +Cloud Storage Backends |
| 146 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 147 | + |
| 148 | +Zarr integrates seamlessly with cloud storage such as Amazon S3 and Google Cloud Storage |
| 149 | +using external libraries like `s3fs <https://s3fs.readthedocs.io>`_ or |
| 150 | +`gcsfs <https://gcsfs.readthedocs.io>`_. |
| 151 | + |
| 152 | +For example, to use S3: |
| 153 | + |
| 154 | +.. ipython:: python |
| 155 | + :verbatim: |
| 156 | +
|
| 157 | + import s3fs |
| 158 | +
|
| 159 | + z = zarr.open("s3://example-bucket/foo", mode="w", shape=(100, 100), chunks=(10, 10)) |
| 160 | + z[:, :] = np.random.random((100, 100)) |
| 161 | +
|
| 162 | +Read more about Zarr's :ref:`tutorial_storage` options in the User Guide. |
| 163 | + |
| 164 | +Next Steps |
| 165 | +---------- |
| 166 | + |
| 167 | +Now that you're familiar with the basics, explore the following resources: |
| 168 | + |
| 169 | +- `User Guide <user-guide>`_ |
| 170 | +- `API Reference <api>`_ |
0 commit comments