.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/run_single_fov_ome_zarr.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_run_single_fov_ome_zarr.py: Single-FOV OME-Zarr =================== This script shows how to create a single-FOV, single-scale OME-Zarr dataset, read data in read-only mode, append an extra time point to an existing dataset, and adding a new channel to an existing dataset. .. GENERATED FROM PYTHON SOURCE LINES 12-19 .. code-block:: Python import os from tempfile import TemporaryDirectory import numpy as np from iohub.ngff import open_ome_zarr .. GENERATED FROM PYTHON SOURCE LINES 20-21 Set storage path .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: Python tmp_dir = TemporaryDirectory() store_path = os.path.join(tmp_dir.name, "ome.zarr") print("Zarr store path", store_path) .. rst-class:: sphx-glr-script-out .. code-block:: none Zarr store path /tmp/tmpz9z6wu7r/ome.zarr .. GENERATED FROM PYTHON SOURCE LINES 26-27 Write 5D data to a new Zarr store .. GENERATED FROM PYTHON SOURCE LINES 27-37 .. code-block:: Python tczyx = np.random.randint( 0, np.iinfo(np.uint16).max, size=(5, 2, 3, 32, 32), dtype=np.uint16 ) with open_ome_zarr( store_path, layout="fov", mode="a", channel_names=["DAPI", "GFP"] ) as dataset: dataset["img"] = tczyx .. GENERATED FROM PYTHON SOURCE LINES 38-39 Opening in read-only mode prevents writing .. GENERATED FROM PYTHON SOURCE LINES 39-49 .. code-block:: Python with open_ome_zarr(store_path, layout="auto", mode="r") as dataset: img = dataset["img"] print(img) print(img.numpy()) try: img[0, 0, 0, 0, 0] = 0 except Exception as e: print(f"Writing was rejected: {e}") .. rst-class:: sphx-glr-script-out .. code-block:: none [[[[[47054 47814 54729 ... 27141 13709 51087] [10476 46138 49201 ... 16195 12624 2863] [35786 48266 29223 ... 32973 40856 49124] ... [34800 57887 29335 ... 37837 58226 39299] [50553 52735 3039 ... 9430 65304 22744] [17164 21919 500 ... 1262 4261 187]] [[52575 22444 17900 ... 60507 13595 35130] [51122 35665 41892 ... 25947 22689 43287] [ 5251 6566 50925 ... 22673 35394 51806] ... [25629 6693 22777 ... 52534 51735 43934] [23489 21141 5816 ... 46339 4465 23561] [23262 35172 62244 ... 3657 12626 22353]] [[47174 30872 52547 ... 34228 26102 20001] [34383 12651 41335 ... 41270 6151 5741] [15003 29564 61286 ... 52876 45236 10876] ... [ 8203 30865 48915 ... 39002 9585 57752] [47688 52099 14315 ... 34825 46764 31358] [48315 65003 21278 ... 5744 38039 50909]]] [[[15331 42847 10142 ... 38825 37616 49862] [16611 10006 23455 ... 2782 25854 18095] [65494 7317 5117 ... 8741 24992 37576] ... [ 7348 13698 3532 ... 1519 21104 22126] [26364 11691 57014 ... 42973 13576 57508] [23611 48587 41093 ... 61804 8210 34647]] [[51192 35059 3030 ... 22878 61149 10422] [39942 57901 25747 ... 33754 1375 33958] [ 1317 65253 32109 ... 25787 46322 24220] ... [11688 34083 32894 ... 35138 2435 9988] [30959 4046 45743 ... 36021 55442 47219] [55524 46287 45887 ... 27905 49884 25010]] [[42446 1551 14149 ... 32685 31527 5411] [30932 6069 39996 ... 26959 59497 19634] [ 2578 32312 36035 ... 40442 2079 63056] ... [62764 23292 26532 ... 24405 7940 42900] [10919 3258 39703 ... 59996 2105 54400] [60541 50923 63772 ... 37459 9288 8699]]]] [[[[54757 10473 47982 ... 31866 22562 4418] [33221 2258 1473 ... 47783 3042 45514] [46935 6778 33957 ... 59668 50196 32063] ... [56288 44090 17063 ... 10970 38929 8425] [47874 39154 63846 ... 28137 4512 51173] [50659 26300 25197 ... 21074 48888 9527]] [[61455 58545 39701 ... 4760 18597 8847] [ 2332 645 64645 ... 52588 34990 23256] [25314 46996 40965 ... 52676 11173 24909] ... [58395 60317 17251 ... 31469 22017 51377] [12183 20166 59867 ... 21378 12312 31434] [57489 62485 60879 ... 65025 46576 9576]] [[ 4682 1004 33960 ... 64760 62360 30845] [22920 21777 14619 ... 6167 20348 35426] [45015 12239 62290 ... 10093 32727 39785] ... [46036 47093 28535 ... 60171 30640 57832] [35974 23319 6260 ... 40176 33566 27620] [ 5896 41177 27992 ... 51442 51966 28845]]] [[[26255 59227 54985 ... 45677 52522 46832] [20774 930 19078 ... 61644 28827 32060] [ 25 7183 53488 ... 21203 26071 26558] ... [43235 64172 59963 ... 12525 51227 44413] [19467 12697 41491 ... 46286 41977 6488] [19333 50457 21569 ... 61006 3902 36679]] [[57925 30425 23055 ... 38396 29088 38851] [37688 36769 9404 ... 64532 63301 35034] [45804 43352 56784 ... 40787 45154 48754] ... [ 3916 15267 2625 ... 57923 10160 38690] [54744 1527 49212 ... 44194 58617 51774] [53290 50781 20392 ... 58114 63049 12944]] [[13021 41164 8741 ... 21691 27490 822] [ 6400 18093 37552 ... 11684 42598 59711] [34923 24205 61486 ... 63469 27554 21906] ... [33603 45506 55192 ... 12389 34833 51259] [13122 58132 57906 ... 5129 36450 40847] [28369 42006 4664 ... 61112 27678 1210]]]] [[[[16523 45309 28040 ... 42110 52020 59961] [12701 50456 43927 ... 18344 56973 313] [29474 27762 30602 ... 41211 13653 5355] ... [ 1286 1487 11532 ... 27518 1317 27478] [26530 34901 34170 ... 22116 17823 9209] [10672 57098 48881 ... 24176 65238 37718]] [[21962 47473 11160 ... 38808 48749 63647] [ 1571 49008 33734 ... 6423 11910 13428] [ 1207 56288 50467 ... 11883 49436 61726] ... [ 2183 55413 9413 ... 483 36128 56285] [22083 57142 18151 ... 171 15322 47029] [29881 18112 18356 ... 47287 35404 49810]] [[21027 62151 50107 ... 10524 2204 5165] [55559 29362 55087 ... 64103 30862 26109] [50028 53991 36320 ... 43902 8777 9818] ... [ 8071 21959 63555 ... 39389 39882 47897] [65332 49550 31913 ... 27886 25528 46671] [16670 54439 12425 ... 58686 19254 24777]]] [[[ 622 48177 1677 ... 6068 29923 40813] [27340 57435 12671 ... 23557 32370 47407] [41510 27266 29697 ... 39819 5916 12412] ... [46763 50794 25043 ... 21978 10882 24710] [53905 57855 53888 ... 2006 3486 27597] [59795 8423 60335 ... 38228 270 13883]] [[13585 9517 54399 ... 22203 36562 10880] [22291 36241 13380 ... 23869 58189 52572] [13537 51946 32916 ... 19986 42488 28796] ... [12733 5833 56080 ... 16643 18359 59711] [63783 3148 16199 ... 46608 742 15152] [57339 14684 59741 ... 31365 63862 42166]] [[41834 31104 9270 ... 52607 51318 42669] [20214 4412 62896 ... 1075 6005 60455] [14082 3841 23745 ... 49161 15433 44956] ... [60293 62609 9481 ... 2412 52055 48038] [19757 20387 30020 ... 65421 23527 52214] [55909 64161 62898 ... 61463 34923 42083]]]] [[[[13880 24802 48691 ... 61665 56663 21095] [40648 42516 12741 ... 45092 16724 10160] [30564 28010 5262 ... 25978 17111 10809] ... [19783 48562 33241 ... 11513 4157 56871] [50591 27865 43442 ... 22233 58040 55300] [29639 38805 19695 ... 48934 43376 34665]] [[10900 31577 34861 ... 53114 19183 42674] [ 5833 34905 60481 ... 13593 21889 10417] [28001 10984 63439 ... 28502 57570 8795] ... [61569 17924 34610 ... 45324 26093 35233] [ 3102 16672 59831 ... 33782 28311 4520] [59147 28376 21219 ... 34869 51726 34242]] [[31411 59485 41003 ... 62190 11757 58829] [35737 3728 54400 ... 19765 29229 18848] [30134 38735 63018 ... 30650 16663 57314] ... [44772 53519 1200 ... 35239 35524 7433] [ 3360 48839 24280 ... 12326 40891 30169] [36765 26661 1903 ... 12088 33420 43663]]] [[[ 1587 47474 41805 ... 54438 9411 58176] [ 7572 45401 51091 ... 56788 6690 31893] [14151 30179 2815 ... 35171 31833 28708] ... [30668 25761 56262 ... 19791 2230 19533] [ 3184 52126 44084 ... 53165 46753 2064] [32407 20167 32476 ... 15426 5067 28544]] [[48872 47084 51406 ... 45222 37542 28656] [16244 30305 59336 ... 64696 51609 839] [18836 17722 36602 ... 41195 50009 1782] ... [61069 62167 48986 ... 45170 53987 26371] [ 9340 30350 42685 ... 39640 29881 31249] [51146 36659 52261 ... 59848 32785 9753]] [[16019 24365 28945 ... 61281 35240 57691] [22359 7049 49849 ... 29698 45994 50701] [40974 918 34724 ... 13855 12916 31848] ... [31499 23213 16729 ... 35523 1580 63209] [24471 20450 65183 ... 57665 44886 29818] [10103 14273 60583 ... 39751 50386 33663]]]] [[[[ 5057 52498 53591 ... 44223 37264 16971] [50667 523 48685 ... 5097 7315 56887] [36247 43452 12836 ... 55779 64461 22200] ... [35464 47221 16522 ... 8937 20211 18487] [50691 50130 52414 ... 34480 12310 46896] [11095 56819 56127 ... 37074 5147 59392]] [[54197 34171 35674 ... 33970 22888 62857] [37687 32142 5365 ... 6708 24606 41887] [30960 5324 58960 ... 2772 58022 40847] ... [ 575 33060 12890 ... 52110 45441 32164] [60893 4801 22986 ... 38781 18632 44010] [14636 65100 60838 ... 55673 48315 41034]] [[49765 41378 47880 ... 23240 37545 20417] [41659 686 27561 ... 47828 35047 58072] [11896 40262 29921 ... 32841 26186 27491] ... [45467 59742 58933 ... 6787 2384 40100] [49809 27347 46297 ... 19784 22379 57778] [58471 166 56607 ... 17701 63259 41214]]] [[[ 8700 38257 41757 ... 23100 19746 59098] [18967 31986 30296 ... 63758 28370 38518] [52223 5522 63173 ... 40774 21870 16782] ... [38094 55997 45708 ... 17587 33148 22345] [ 8297 21230 10813 ... 10161 42919 7250] [31540 61156 22622 ... 7408 25554 24593]] [[ 5279 12963 43532 ... 51401 62344 20780] [ 1331 33004 50595 ... 16836 40034 13326] [ 4343 61597 17474 ... 5415 5527 30897] ... [40826 59947 30666 ... 46931 7857 64647] [54563 58918 62092 ... 5554 55195 20631] [61071 40118 41251 ... 48551 6557 42201]] [[48348 3606 11073 ... 51634 13637 52358] [15044 39066 43422 ... 45371 24635 5499] [51470 1582 43651 ... 9723 7750 25562] ... [61136 49802 51883 ... 31206 19703 64111] [ 669 47279 19418 ... 25993 65263 40413] [52854 48138 64060 ... 3424 45644 41180]]]]] Writing was rejected: store was opened in read-only mode and does not support writing .. GENERATED FROM PYTHON SOURCE LINES 50-51 Append a new timepoint to an existing dataset .. GENERATED FROM PYTHON SOURCE LINES 51-62 .. code-block:: Python new_1czyx = np.random.randint( 0, np.iinfo(np.uint16).max, size=(1, 2, 3, 32, 32), dtype=np.uint16 ) with open_ome_zarr(store_path, layout="fov", mode="r+") as dataset: img = dataset["img"] print(img.shape) img.append(new_1czyx, axis=0) print(img.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none (5, 2, 3, 32, 32) (6, 2, 3, 32, 32) .. GENERATED FROM PYTHON SOURCE LINES 63-64 Modify channels .. GENERATED FROM PYTHON SOURCE LINES 64-96 .. code-block:: Python # Open the dataset used above dataset = open_ome_zarr(store_path, mode="r+") dataset.print_tree() # Append a new channel and write a Z-stack new_zyx = np.random.randint( 0, np.iinfo(np.uint16).max, size=(3, 32, 32), dtype=np.uint16 ) dataset.append_channel("New", resize_arrays=True) dataset["img"][0, 2] = new_zyx print(dataset.channel_names) dataset.print_tree() # Rename the new channel dataset.rename_channel("New", "Renamed") print(dataset.channel_names) # Write new data to the channel new_tzyx = np.random.randint( 0, np.iinfo(np.uint16).max, size=(6, 3, 32, 32), dtype=np.uint16 ) c_idx = dataset.get_channel_index("Renamed") dataset["img"][:, c_idx] = new_tzyx # Which is equivalent to: if False: # remove this line dataset.update_channel("Renamed", target="img", data=new_tzyx) # Close the dataset dataset.close() .. rst-class:: sphx-glr-script-out .. code-block:: none / └── img (6, 2, 3, 32, 32) uint16 ['DAPI', 'GFP', 'New'] / └── img (6, 3, 3, 32, 32) uint16 ['DAPI', 'GFP', 'Renamed'] .. GENERATED FROM PYTHON SOURCE LINES 97-98 Try viewing the images with napari-ome-zarr .. GENERATED FROM PYTHON SOURCE LINES 100-101 Clean up .. GENERATED FROM PYTHON SOURCE LINES 101-101 .. code-block:: Python tmp_dir.cleanup() .. _sphx_glr_download_auto_examples_run_single_fov_ome_zarr.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: run_single_fov_ome_zarr.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: run_single_fov_ome_zarr.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: run_single_fov_ome_zarr.zip `