|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "95d84769", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Customizing the pyramid\n", |
| 9 | + "(advanced:pyramid)=\n", |
| 10 | + "\n", |
| 11 | + "\n", |
| 12 | + "Multi-resolution pyramids are an integral part of ome-zarr image data\n", |
| 13 | + "and enable fast rendering of large images.\n", |
| 14 | + "The entrypoints to writing ome-zarr images in ome-zarr-py ({py:func}`ome_zarr.writer.write_image` and {py:func}`ome_zarr.writer.write_labels`)\n", |
| 15 | + "build these pyramids under the hood as delayed dask arrays based on the settings for the scaling functions and scale factors.\n", |
| 16 | + "\n", |
| 17 | + "In this example, the downsampling will be applied in all spatial dimensions *except the z dimension*, which will be left at a scale factor of 1.\n", |
| 18 | + "To apply equal or custom downsampling factors along all spatial dimensions, pass the scale factors as a list of dicts (see [below](#advanced:custom-downsampling-values))." |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": 16, |
| 24 | + "id": "642955b1", |
| 25 | + "metadata": {}, |
| 26 | + "outputs": [ |
| 27 | + { |
| 28 | + "data": { |
| 29 | + "text/plain": [ |
| 30 | + "[]" |
| 31 | + ] |
| 32 | + }, |
| 33 | + "execution_count": 16, |
| 34 | + "metadata": {}, |
| 35 | + "output_type": "execute_result" |
| 36 | + } |
| 37 | + ], |
| 38 | + "source": [ |
| 39 | + "import numpy as np\n", |
| 40 | + "\n", |
| 41 | + "from ome_zarr.writer import write_image\n", |
| 42 | + "\n", |
| 43 | + "scale_factors = [2, 4, 8]\n", |
| 44 | + "rng = np.random.default_rng(0)\n", |
| 45 | + "data = rng.poisson(lam=10, size=(64, 64, 64)).astype(np.uint8)\n", |
| 46 | + "\n", |
| 47 | + "write_image(\n", |
| 48 | + " data,\n", |
| 49 | + " \"test_ngff_image.ome.zarr\",\n", |
| 50 | + " axes=\"zyx\",\n", |
| 51 | + " scale_factors=scale_factors,\n", |
| 52 | + " )" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "markdown", |
| 57 | + "id": "cec0fc73", |
| 58 | + "metadata": {}, |
| 59 | + "source": [ |
| 60 | + "## Custom downsampling values\n", |
| 61 | + "(advanced:custom-downsampling-values)=\n", |
| 62 | + "\n", |
| 63 | + "To specify custom downsampling values, pass a list of dictionaries with the keys being the names of the axes to the writer function like in the following example.\n", |
| 64 | + "This will apply equal downsampling factors along all present axes (`zyx` in this case):" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "code", |
| 69 | + "execution_count": 17, |
| 70 | + "id": "60dcd278", |
| 71 | + "metadata": {}, |
| 72 | + "outputs": [ |
| 73 | + { |
| 74 | + "data": { |
| 75 | + "text/plain": [ |
| 76 | + "[]" |
| 77 | + ] |
| 78 | + }, |
| 79 | + "execution_count": 17, |
| 80 | + "metadata": {}, |
| 81 | + "output_type": "execute_result" |
| 82 | + } |
| 83 | + ], |
| 84 | + "source": [ |
| 85 | + "scale_factors = [\n", |
| 86 | + " {\"z\": 2,\"x\": 2, \"y\": 2},\n", |
| 87 | + " {\"z\": 4,\"x\": 4, \"y\": 4},\n", |
| 88 | + " {\"z\": 8,\"x\": 8, \"y\": 8},\n", |
| 89 | + "]\n", |
| 90 | + "\n", |
| 91 | + "write_image(\n", |
| 92 | + " data,\n", |
| 93 | + " \"test_ngff_image_custom_scale.ome.zarr\",\n", |
| 94 | + " axes=\"zyx\",\n", |
| 95 | + " scale_factors=scale_factors,\n", |
| 96 | + " )" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "markdown", |
| 101 | + "id": "6b276be7", |
| 102 | + "metadata": {}, |
| 103 | + "source": [ |
| 104 | + "## Custom downsampling functions\n", |
| 105 | + "\n", |
| 106 | + "ome-zarr-py provides multiple methods for downsampling, which can be found in the {py:class}`ome_zarr.scale.Methods` class:" |
| 107 | + ] |
| 108 | + }, |
| 109 | + { |
| 110 | + "cell_type": "code", |
| 111 | + "execution_count": 18, |
| 112 | + "id": "877ab60d", |
| 113 | + "metadata": {}, |
| 114 | + "outputs": [ |
| 115 | + { |
| 116 | + "name": "stdout", |
| 117 | + "output_type": "stream", |
| 118 | + "text": [ |
| 119 | + "['resize', 'nearest', 'local_mean', 'zoom']\n" |
| 120 | + ] |
| 121 | + } |
| 122 | + ], |
| 123 | + "source": [ |
| 124 | + "from ome_zarr.scale import Methods\n", |
| 125 | + "\n", |
| 126 | + "print([m.value for m in Methods])" |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "cell_type": "markdown", |
| 131 | + "id": "9a086164", |
| 132 | + "metadata": {}, |
| 133 | + "source": [ |
| 134 | + "You can use one of these functions for downsampling by passing the method name as a string to the writer function, e.g. `method=\"local_mean\"` or `method=\"resize\"`, i.e.:" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "code", |
| 139 | + "execution_count": 19, |
| 140 | + "id": "859ca482", |
| 141 | + "metadata": {}, |
| 142 | + "outputs": [ |
| 143 | + { |
| 144 | + "data": { |
| 145 | + "text/plain": [ |
| 146 | + "[]" |
| 147 | + ] |
| 148 | + }, |
| 149 | + "execution_count": 19, |
| 150 | + "metadata": {}, |
| 151 | + "output_type": "execute_result" |
| 152 | + } |
| 153 | + ], |
| 154 | + "source": [ |
| 155 | + "write_image(\n", |
| 156 | + " data,\n", |
| 157 | + " \"test_ngff_image_custom_method.ome.zarr\",\n", |
| 158 | + " axes=\"zyx\",\n", |
| 159 | + " scale_factors=scale_factors,\n", |
| 160 | + " method=\"nearest\"\n", |
| 161 | + ")\n" |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "markdown", |
| 166 | + "id": "893a1b3b", |
| 167 | + "metadata": {}, |
| 168 | + "source": [ |
| 169 | + "```{warning}\n", |
| 170 | + "\n", |
| 171 | + "The choice of the correct downsampling function is typically of secondary importance,\n", |
| 172 | + "*unless* your data specifically requires a certain method.\n", |
| 173 | + "\n", |
| 174 | + "For instance, when writing categorical data (i.e., segmentations or generally labels),\n", |
| 175 | + "you will want to use a method that preserves the label values, such as {py:func}`ome_zarr.scale.Methods.NEAREST`.\n", |
| 176 | + "\n", |
| 177 | + "See also section on [writing labels](basic:labels)\n", |
| 178 | + "\n", |
| 179 | + "```" |
| 180 | + ] |
| 181 | + }, |
| 182 | + { |
| 183 | + "cell_type": "markdown", |
| 184 | + "id": "fbbeecf3", |
| 185 | + "metadata": {}, |
| 186 | + "source": [] |
| 187 | + } |
| 188 | + ], |
| 189 | + "metadata": { |
| 190 | + "kernelspec": { |
| 191 | + "display_name": "ngff-spec", |
| 192 | + "language": "python", |
| 193 | + "name": "python3" |
| 194 | + }, |
| 195 | + "language_info": { |
| 196 | + "codemirror_mode": { |
| 197 | + "name": "ipython", |
| 198 | + "version": 3 |
| 199 | + }, |
| 200 | + "file_extension": ".py", |
| 201 | + "mimetype": "text/x-python", |
| 202 | + "name": "python", |
| 203 | + "nbconvert_exporter": "python", |
| 204 | + "pygments_lexer": "ipython3", |
| 205 | + "version": "3.12.12" |
| 206 | + } |
| 207 | + }, |
| 208 | + "nbformat": 4, |
| 209 | + "nbformat_minor": 5 |
| 210 | +} |
0 commit comments