.. 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/tmpvp2pns89/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 [[[[[32976 36658 53302 ... 44004 9249 46148] [ 4257 48837 18892 ... 9175 44719 33267] [35419 39371 18623 ... 51437 27655 15248] ... [ 340 45525 41305 ... 48493 56735 47093] [31189 58125 54675 ... 56708 53160 6108] [51935 28046 7625 ... 55028 27770 17934]] [[40460 25200 56710 ... 56674 30917 12070] [62833 50291 155 ... 59502 14338 35489] [48438 10268 1248 ... 8122 37248 13335] ... [43348 2281 14620 ... 26689 18384 37944] [60428 63480 43652 ... 35890 24259 13589] [62793 25274 47832 ... 25138 31694 22969]] [[ 4089 42835 55326 ... 1041 20145 49921] [ 386 14533 64314 ... 47300 58978 33820] [56054 26362 39650 ... 4077 14681 1224] ... [31652 62599 61643 ... 34883 424 29327] [ 4617 39006 56819 ... 55086 46030 45734] [27344 25199 2918 ... 4255 53035 30906]]] [[[ 3518 22515 20084 ... 21563 7866 28076] [27878 4037 920 ... 46342 57041 25660] [61514 54170 5096 ... 25310 27885 52304] ... [34554 15712 43742 ... 60993 57530 22945] [48021 42387 18022 ... 408 35745 63211] [16338 18880 47008 ... 38986 689 40101]] [[ 7741 52428 29683 ... 42446 8427 32346] [33019 43222 38432 ... 45304 3055 42351] [34228 27361 12757 ... 8701 50287 22885] ... [43464 7556 45591 ... 58825 19422 21789] [ 1853 22020 36423 ... 27672 38369 43710] [40075 37277 41197 ... 47157 18758 58000]] [[63342 62534 54718 ... 21065 2769 14759] [37961 31272 23644 ... 63789 1332 12714] [50373 32334 31655 ... 48957 37524 35449] ... [50873 48878 2644 ... 19154 17953 9981] [ 8067 26634 22069 ... 19766 42101 31757] [49769 53110 24292 ... 495 31054 55934]]]] [[[[ 9988 15859 17620 ... 21779 29344 46665] [ 1535 9555 59768 ... 14567 62437 44948] [18067 52124 3029 ... 31761 21367 60043] ... [16441 65264 31550 ... 41844 25104 2461] [ 7257 60915 1601 ... 4415 38568 22504] [ 8427 61362 18913 ... 36660 49615 58964]] [[37930 40660 23164 ... 33362 12011 35649] [21297 36925 40219 ... 4786 16832 32147] [14248 23418 41292 ... 14461 28889 21245] ... [49117 49523 62609 ... 9712 33304 27453] [51398 33610 43343 ... 53104 3040 57116] [61187 11384 1456 ... 39404 26637 792]] [[47651 46358 60250 ... 16706 46910 27353] [ 2814 39064 26483 ... 45508 3941 64783] [13157 36400 36981 ... 41519 35671 40952] ... [64059 42619 549 ... 55219 2500 51499] [27011 12742 56138 ... 17132 9393 28674] [15003 27201 53900 ... 4365 16520 21523]]] [[[60489 6115 52624 ... 51366 3771 15140] [61087 29233 8438 ... 4385 37984 17118] [ 6287 45478 12575 ... 6446 16795 42355] ... [37285 12384 35509 ... 59540 50180 32650] [27218 3562 16303 ... 29594 55635 574] [ 5264 28642 21039 ... 58733 61209 20712]] [[58418 30946 36554 ... 50347 20628 18758] [ 1586 36826 23362 ... 33108 20871 49832] [55557 55591 18250 ... 32254 2708 33089] ... [57064 15885 24077 ... 14640 37602 16439] [63968 6303 36138 ... 20539 28721 12537] [34333 41851 58104 ... 32092 48929 59004]] [[44789 41935 9445 ... 64837 10423 34184] [38403 46076 40120 ... 19073 11876 19787] [30355 13200 5830 ... 26749 41162 9384] ... [ 7520 41176 6659 ... 19425 65511 22173] [ 3467 51477 35354 ... 36809 36789 22794] [48859 19842 11774 ... 31386 704 64423]]]] [[[[44012 28889 4580 ... 64063 64915 14204] [ 4037 27536 26049 ... 16730 25301 43264] [ 3237 37421 16982 ... 30447 7664 14472] ... [38243 5557 3955 ... 10921 8733 45249] [ 1313 14487 9610 ... 30083 50453 3364] [33708 19771 2741 ... 34789 41862 36522]] [[32592 47229 42440 ... 28390 3219 23141] [33576 6926 50102 ... 59361 2183 64380] [23933 526 53280 ... 5347 11957 62193] ... [38814 28902 52371 ... 52874 27958 55523] [16238 60574 8261 ... 46985 27504 56489] [29854 51114 9931 ... 11003 58169 40349]] [[50051 43828 23922 ... 36915 60093 57192] [40080 30583 18179 ... 25077 35402 56048] [49649 59157 42815 ... 25573 17965 29694] ... [10633 39969 40644 ... 41914 6740 29181] [58845 18083 53577 ... 18284 62479 23173] [30574 16095 54784 ... 13429 7081 50255]]] [[[44343 11814 40215 ... 7167 8370 24427] [52617 16886 29847 ... 28168 29626 506] [53160 32286 57242 ... 39300 29123 31141] ... [13522 1649 55824 ... 44663 35280 29303] [13935 63308 47012 ... 33205 50661 15233] [58549 49199 46382 ... 5983 17630 36450]] [[54898 4964 6915 ... 31849 15034 24683] [37957 61374 41009 ... 17392 18699 17280] [15646 20904 44713 ... 47313 26236 64622] ... [56571 54054 33116 ... 45714 35385 53216] [17894 40496 43673 ... 19860 54911 27419] [40162 33689 4853 ... 56120 11865 40192]] [[20006 41688 1665 ... 3463 59411 29119] [59524 21099 62257 ... 3980 48315 65411] [43153 53473 2771 ... 39388 14783 7307] ... [22351 29219 58344 ... 23189 42297 45693] [63435 6272 63150 ... 6150 36619 14887] [ 221 47146 6435 ... 15659 38384 1210]]]] [[[[22997 5535 4572 ... 50914 41341 172] [59344 20815 57089 ... 3669 48099 1764] [26287 45737 55996 ... 61696 7028 37040] ... [63356 34126 1394 ... 56771 24882 14561] [42008 52883 20030 ... 60322 32884 41403] [16774 25882 45152 ... 65461 24917 57365]] [[29812 52668 39048 ... 5788 22445 26169] [56306 26151 14937 ... 20796 28750 19624] [21331 64385 12905 ... 12315 20730 51473] ... [52229 36583 34403 ... 31454 41934 4368] [33188 56418 47053 ... 25824 2742 9158] [64582 36195 35612 ... 15603 38343 25750]] [[ 3079 27859 25600 ... 6134 54284 62844] [42508 38945 4764 ... 55484 42671 19259] [60075 24776 43506 ... 61460 40820 48191] ... [34203 5826 3096 ... 2770 4860 31165] [22558 48655 4807 ... 23818 62095 16228] [17507 610 11344 ... 40194 26883 63930]]] [[[35784 26102 13508 ... 14221 61758 49410] [62495 15518 27551 ... 23648 10815 19099] [37660 14681 37724 ... 62781 25585 14462] ... [42602 58264 40289 ... 23527 51886 28044] [ 9721 18521 27178 ... 9418 29541 14041] [27820 14398 1294 ... 20237 2728 59700]] [[45826 32343 52571 ... 20268 20304 60165] [41906 45296 65334 ... 32400 18079 10748] [ 3828 41801 2817 ... 53006 6888 14350] ... [21875 35024 56089 ... 46973 28080 17190] [ 4588 1426 55003 ... 40946 3390 21060] [29873 24958 52276 ... 62188 10691 4482]] [[ 9979 55658 16573 ... 30846 31479 8990] [19199 9377 16759 ... 34299 18797 3509] [43939 54097 28450 ... 31708 25346 41079] ... [21631 20180 12931 ... 43312 3417 22423] [19910 36714 52218 ... 36057 10975 58108] [20496 26550 43232 ... 34192 19402 38306]]]] [[[[47769 32884 16461 ... 1860 18655 3741] [21628 28254 44957 ... 38500 54596 56378] [46364 60219 29804 ... 52669 16458 30648] ... [12910 58933 5377 ... 48811 49033 27957] [ 2757 60952 36178 ... 26118 59162 60229] [43412 1721 46141 ... 24440 35202 8145]] [[43456 23114 12941 ... 63535 28406 61292] [25655 50867 51955 ... 32365 34720 12569] [42154 62187 21756 ... 36386 36477 48261] ... [38770 43598 23575 ... 3791 58532 49672] [30071 18281 56033 ... 35950 47136 25478] [ 2766 12158 56400 ... 18265 61941 49786]] [[35495 22095 55388 ... 60142 56364 5520] [51136 4704 61646 ... 14082 41791 4496] [61704 11773 31468 ... 59515 62553 64106] ... [23863 27219 65176 ... 25299 18520 45223] [ 2196 27068 61458 ... 37020 29097 64083] [30194 23023 1364 ... 42272 64478 17847]]] [[[24357 61652 62907 ... 200 14270 52291] [41757 45239 61982 ... 58962 57266 59430] [31346 43475 46490 ... 13733 7319 45196] ... [ 9296 13685 20632 ... 35971 24768 14936] [60828 22230 3785 ... 6108 23068 44439] [42017 21037 3043 ... 27379 32519 37644]] [[17742 7777 40241 ... 57968 58900 57511] [36333 49966 57913 ... 64378 8982 17088] [14028 50954 62941 ... 15676 34701 1656] ... [36996 40127 23206 ... 15690 56108 26080] [50779 20663 50249 ... 42571 9040 10617] [37327 64421 61616 ... 27018 31538 15916]] [[28224 51537 59425 ... 16415 45853 14778] [ 3878 33750 47244 ... 47972 5823 26078] [18624 45653 17602 ... 32701 33691 41111] ... [18677 16153 44766 ... 49808 40619 8462] [20354 2649 18941 ... 46859 30992 54454] [37283 8040 51533 ... 25957 5205 54001]]]]] 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 `