Note
Go to the end to download the full example code.
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.
import os
from tempfile import TemporaryDirectory
import numpy as np
from iohub.ngff import open_ome_zarr
Set storage path
tmp_dir = TemporaryDirectory()
store_path = os.path.join(tmp_dir.name, "ome.zarr")
print("Zarr store path", store_path)
Zarr store path /tmp/tmpqt9cyszp/ome.zarr
Write 5D data to a new Zarr store
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
Opening in read-only mode prevents writing
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}")
<iohub.ngff.nodes.ImageArray '/img' (5, 2, 3, 32, 32) uint16 read-only>
[[[[[32609 47156 52893 ... 53240 29413 57275]
[ 683 2977 63520 ... 26140 2328 28940]
[13580 52755 43316 ... 55265 55438 30574]
...
[ 1684 15703 38613 ... 50029 1701 52261]
[27598 3079 14413 ... 8805 23079 29961]
[ 9371 55302 16193 ... 16308 45470 21636]]
[[ 5042 15073 18727 ... 4499 9932 6536]
[ 9338 3589 29847 ... 11742 15741 29744]
[10884 32832 13022 ... 6260 12848 63120]
...
[63966 34899 58757 ... 44109 47265 61125]
[50514 2339 29591 ... 55553 48818 40766]
[48881 24974 20145 ... 14287 45106 19862]]
[[48648 31661 29488 ... 33189 9205 23130]
[ 8559 36933 13989 ... 40562 37620 59678]
[ 2414 23567 8806 ... 46855 46843 54431]
...
[65254 38700 919 ... 21304 10293 47605]
[64033 51552 21424 ... 24866 17324 6673]
[61156 11400 40081 ... 58309 63617 38319]]]
[[[ 7099 42048 18450 ... 38573 43152 55758]
[60678 45306 55897 ... 45121 43862 13917]
[57150 51619 11965 ... 35256 44132 19616]
...
[17689 12227 10220 ... 14902 3382 38004]
[65047 5976 56385 ... 62054 44760 33863]
[35862 9099 30095 ... 9466 6665 1376]]
[[ 1322 56890 8755 ... 43087 26233 58514]
[51587 20874 2985 ... 56502 35675 24997]
[31010 61443 20238 ... 17816 63313 47387]
...
[ 4444 40792 56410 ... 45147 18807 31089]
[13983 35273 43352 ... 30104 51747 48475]
[52597 48495 18641 ... 12458 31056 12920]]
[[65144 22328 7259 ... 5164 49334 64943]
[ 2027 41883 62953 ... 52883 42883 14716]
[61763 5568 44012 ... 27593 14170 19530]
...
[12655 20152 58863 ... 64226 55839 15957]
[17130 57371 18832 ... 36618 46144 38844]
[40901 47806 52722 ... 5435 29154 29883]]]]
[[[[55676 52700 27645 ... 30316 192 16157]
[52298 32978 33846 ... 11900 12420 53942]
[53774 22616 35729 ... 33797 25437 36817]
...
[25275 46133 51349 ... 2052 63579 14872]
[46831 12937 28130 ... 48269 12546 63158]
[24187 24070 57042 ... 33087 8800 43421]]
[[59647 25472 33558 ... 55650 17252 20950]
[35123 32830 54902 ... 13926 2307 996]
[53697 18525 12100 ... 44603 42719 58787]
...
[46032 19476 29412 ... 27711 11121 1767]
[45361 50813 1180 ... 21250 47230 32364]
[54851 57239 31432 ... 42228 65411 48972]]
[[22652 55963 363 ... 54280 27342 18923]
[13140 13239 10864 ... 8336 17147 2031]
[ 427 875 86 ... 4752 34472 6804]
...
[ 9798 27095 24880 ... 49527 25210 28435]
[13496 32211 41883 ... 59403 54849 60182]
[48652 46539 30104 ... 24606 11563 44732]]]
[[[ 840 13136 54829 ... 51285 8788 44304]
[51079 16345 65486 ... 48531 22723 10845]
[55962 16323 30678 ... 29875 1958 46651]
...
[26451 3216 29439 ... 39635 50330 41735]
[27694 20539 38078 ... 37115 48473 44797]
[53934 2719 27465 ... 50588 50969 1444]]
[[10320 23251 45388 ... 7658 44929 35306]
[21949 61436 60873 ... 61249 43142 31084]
[38860 8512 34690 ... 11168 8016 17722]
...
[43510 3986 20646 ... 23106 55272 12325]
[24048 59125 35089 ... 33794 38338 38418]
[51563 61302 44385 ... 50583 13151 41138]]
[[53932 51260 32862 ... 43762 4683 55675]
[31992 58623 31263 ... 4802 13643 21112]
[44786 59318 8656 ... 12509 46172 47339]
...
[ 1821 54087 29317 ... 4599 52433 22516]
[34139 12347 49406 ... 64166 27698 10809]
[ 8074 45248 16559 ... 38270 43484 59283]]]]
[[[[37964 827 11410 ... 4111 21711 17336]
[64551 41764 50497 ... 34620 27203 45756]
[56109 58377 10246 ... 36644 47501 45830]
...
[ 9534 56868 57460 ... 8345 3944 10431]
[50110 21837 4298 ... 34998 22489 20849]
[47590 59996 16424 ... 25170 18965 45442]]
[[52958 13384 18870 ... 34467 60588 40440]
[41084 25977 23614 ... 42970 10757 64028]
[56391 51446 23880 ... 41315 61373 35444]
...
[39368 27670 65165 ... 32815 32075 62221]
[30088 58492 16284 ... 58806 15427 9313]
[56799 22362 40978 ... 58690 27254 50911]]
[[37103 33233 35675 ... 56117 45826 52631]
[32856 47847 32623 ... 52899 54005 34109]
[41557 32761 7193 ... 8802 6216 57017]
...
[39513 8988 49007 ... 55875 28493 56219]
[11975 28308 17457 ... 17044 33235 15434]
[56990 51560 750 ... 17295 44339 8421]]]
[[[46369 41180 59900 ... 2461 6054 22783]
[60607 14290 5024 ... 26257 48273 51123]
[25157 24447 19232 ... 27021 44117 6139]
...
[26785 4642 56025 ... 39037 3215 46174]
[28678 45771 1063 ... 10853 44952 12431]
[32441 11137 5172 ... 32077 44999 18534]]
[[53419 50667 15047 ... 25112 38959 23766]
[19833 64586 20068 ... 14267 55252 23330]
[41993 16273 57012 ... 13586 58400 4837]
...
[17466 19276 65129 ... 14833 61935 64238]
[59114 27013 13399 ... 32963 64838 105]
[28534 21717 56402 ... 38623 27059 37340]]
[[20651 36391 19256 ... 22614 25923 21052]
[45985 22880 47848 ... 1218 41536 56185]
[31664 5816 11331 ... 38566 24193 49434]
...
[50242 49942 60627 ... 11596 7542 43738]
[62463 33540 6506 ... 47890 49762 63688]
[30854 12400 6875 ... 52633 12154 35487]]]]
[[[[ 3897 63011 60293 ... 62790 40186 48797]
[41049 25392 11902 ... 1409 52513 18365]
[ 9160 8087 58361 ... 23809 46011 49588]
...
[46643 32908 17691 ... 35427 59574 27865]
[23078 14144 21905 ... 45118 31646 63643]
[22082 39575 4084 ... 63323 5354 21388]]
[[58350 36928 7669 ... 48997 14083 49251]
[35409 7076 4497 ... 4043 39244 29768]
[25459 55823 62002 ... 51187 17954 7597]
...
[59797 46503 40767 ... 21273 19095 62267]
[36718 8505 46457 ... 60087 28277 34172]
[54492 14382 40705 ... 58807 1880 18827]]
[[57764 60129 22875 ... 50267 2775 63087]
[ 4344 3463 9270 ... 59024 6112 12727]
[19505 44681 5446 ... 3276 54384 19922]
...
[26269 63347 42907 ... 53648 30654 29523]
[20668 18521 58456 ... 6754 64496 54174]
[58999 43216 6797 ... 12674 13662 55891]]]
[[[55884 53527 14407 ... 27649 22521 28833]
[17640 54770 40326 ... 18265 30566 2980]
[ 5168 16580 11093 ... 38029 11539 35165]
...
[32517 16529 14358 ... 49234 23824 44152]
[27040 12454 63914 ... 44380 19864 27066]
[28553 61228 31208 ... 12376 59462 54619]]
[[ 223 31879 12727 ... 42625 38768 11223]
[36863 63164 24156 ... 33265 52678 12772]
[38266 36642 57948 ... 5024 38956 47047]
...
[ 6870 13468 60946 ... 2249 54516 60312]
[39552 9655 12039 ... 4490 40639 55536]
[30091 21101 55417 ... 37627 10851 32620]]
[[55649 5851 38061 ... 15945 9666 59746]
[31293 15313 32475 ... 9938 18656 60780]
[49688 19886 13547 ... 34449 25682 59543]
...
[29185 52529 62767 ... 52225 20682 25749]
[37987 53881 27058 ... 40916 45824 1936]
[63161 33747 58286 ... 28152 22941 20061]]]]
[[[[43339 65092 2752 ... 30191 44410 16129]
[17026 46407 28798 ... 47717 8785 56253]
[ 2949 44406 50979 ... 38856 11325 64353]
...
[64666 44156 58708 ... 23178 49962 6479]
[62156 28018 21744 ... 21720 14863 41695]
[26028 35163 21654 ... 64511 10694 57755]]
[[27449 31446 22180 ... 11046 29152 22919]
[11912 59649 47051 ... 5888 43448 61340]
[21547 7896 44112 ... 15543 57062 11787]
...
[44364 7138 29583 ... 62040 27281 49810]
[12562 26396 18295 ... 11160 57340 15964]
[33587 25507 11451 ... 34072 54478 49022]]
[[ 2623 25611 55058 ... 62415 55718 40274]
[34641 9756 23071 ... 32934 2913 5342]
[33008 22257 64685 ... 28230 57098 33802]
...
[33614 5035 56245 ... 21065 6558 34424]
[34024 64370 9385 ... 46256 2019 28560]
[ 6303 9043 47852 ... 2909 44177 61909]]]
[[[57217 61176 34479 ... 46742 24612 65436]
[53276 34941 57141 ... 48030 41883 44161]
[54119 56807 25985 ... 13406 32413 53989]
...
[59227 1697 18266 ... 27281 25869 32250]
[37876 63723 46463 ... 20002 30438 49718]
[17506 25793 42860 ... 33158 6269 49273]]
[[38045 31857 49104 ... 38284 5140 31800]
[47760 30613 15209 ... 49544 27390 39756]
[64192 59499 36206 ... 3486 40869 13287]
...
[37297 41387 58482 ... 37566 50905 9988]
[15125 32394 20314 ... 39298 4956 19826]
[12581 42963 53146 ... 24859 65215 1402]]
[[53638 30985 60766 ... 28748 44140 35345]
[55154 50612 46079 ... 13347 22212 44693]
[ 5190 23393 2403 ... 4229 21506 22827]
...
[34996 8810 63848 ... 62052 56788 63737]
[39883 2059 57676 ... 25810 9560 26593]
[26808 51652 43644 ... 20983 52719 62632]]]]]
Writing was rejected: object is read-only
Append a new timepoint to an existing dataset
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)
(5, 2, 3, 32, 32)
(6, 2, 3, 32, 32)
Modify channels
# 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()
/
└── img (6, 2, 3, 32, 32) uint16
['DAPI', 'GFP', 'New']
/
└── img (6, 3, 3, 32, 32) uint16
['DAPI', 'GFP', 'Renamed']
Try viewing the images with napari-ome-zarr
Clean up
tmp_dir.cleanup()