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/tmpixwlc52l/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}")
<Array file:///tmp/tmpixwlc52l/ome.zarr/img shape=(5, 2, 3, 32, 32) dtype=uint16>
[[[[[26141 60059 18318 ... 54326 48439 46905]
[52605 954 22439 ... 27867 8732 49961]
[35169 2243 5463 ... 55839 21713 37315]
...
[ 3010 44367 3876 ... 45003 63858 40904]
[15926 37348 54175 ... 41448 52649 916]
[ 6021 39901 26256 ... 24297 25504 26922]]
[[43883 56217 1779 ... 5391 8834 231]
[64875 25209 23963 ... 43163 8777 26308]
[14119 34485 26506 ... 2412 38537 2146]
...
[30677 27873 19412 ... 12247 58647 37671]
[42207 52684 12631 ... 24657 13172 63997]
[51355 28016 19501 ... 57608 43821 58046]]
[[ 2214 4788 40570 ... 47656 2513 6929]
[ 2246 50244 63054 ... 17829 13900 58654]
[49180 8435 21280 ... 2327 14406 5735]
...
[64201 58044 62759 ... 14815 18354 5495]
[26466 34234 42883 ... 20106 11999 2937]
[28418 49693 9361 ... 60273 6888 17844]]]
[[[ 8672 51538 5036 ... 18356 51308 47761]
[64107 21365 39877 ... 33519 32561 61787]
[ 9523 40825 37182 ... 49193 22882 15177]
...
[58932 10064 45240 ... 37732 49311 17295]
[56918 49923 362 ... 65235 28533 50994]
[47399 49623 45444 ... 50045 60607 63065]]
[[56542 17656 49587 ... 40539 52924 24234]
[37192 27754 24060 ... 20197 12721 49700]
[20735 62532 9353 ... 46250 35554 17390]
...
[11243 13455 49863 ... 44459 53872 65470]
[20951 31226 58485 ... 6119 62765 57235]
[21452 72 5640 ... 29806 65487 17053]]
[[16251 58728 27254 ... 58068 51810 23070]
[ 3364 46697 48790 ... 40293 37965 20925]
[57817 52144 59165 ... 20019 9147 38058]
...
[23579 8776 26583 ... 15636 3518 4021]
[ 2160 52471 63803 ... 31472 46621 24353]
[12550 38489 40583 ... 3735 29433 16771]]]]
[[[[ 3919 39653 16528 ... 711 10696 20287]
[34972 26718 59113 ... 3309 48489 28634]
[64727 31739 43758 ... 62122 17035 40438]
...
[19937 18256 32154 ... 12004 57299 28868]
[12859 55336 53776 ... 55102 11643 15851]
[ 2777 45170 31364 ... 34421 38339 29033]]
[[ 6315 17983 46256 ... 49983 8957 50227]
[26406 14700 1042 ... 9916 33469 334]
[35129 33239 826 ... 41726 60467 32157]
...
[30062 32631 44091 ... 11648 56696 5983]
[57543 2671 65180 ... 22345 38054 60530]
[43228 408 54113 ... 21430 24719 62739]]
[[19495 26698 60140 ... 37682 59251 28436]
[64182 20091 25224 ... 3543 48232 10927]
[35276 31260 2145 ... 22879 28670 52441]
...
[29211 60068 27421 ... 46776 45415 11980]
[35662 37841 60969 ... 40845 38976 63046]
[44026 3129 14702 ... 65115 6487 39924]]]
[[[59418 227 30784 ... 43249 46548 59007]
[ 5747 38558 4764 ... 33819 12074 9472]
[ 7829 34898 31490 ... 38954 60247 7002]
...
[ 7179 33579 27848 ... 49762 8725 55788]
[34406 59681 26014 ... 51820 28433 20975]
[14225 31853 39837 ... 853 45342 17000]]
[[26837 29025 24165 ... 48969 64833 9979]
[23731 60878 62827 ... 12733 134 7201]
[56997 60337 43643 ... 9158 58205 6023]
...
[12649 55568 4134 ... 65081 11236 3810]
[34225 33326 64863 ... 52723 51981 14909]
[ 8210 1896 22216 ... 25101 63472 47550]]
[[25142 28313 18793 ... 43166 38288 60432]
[44003 44313 1591 ... 59020 60371 32123]
[ 6749 2064 59436 ... 14299 44269 55989]
...
[24552 63297 55324 ... 4673 35896 20102]
[37732 32683 4706 ... 31970 52797 39665]
[39833 20653 38055 ... 48484 34907 55829]]]]
[[[[57935 22027 13625 ... 12707 14109 39260]
[29827 20236 35013 ... 51729 26517 50317]
[20829 4436 10480 ... 41991 23396 5463]
...
[42948 19399 27054 ... 43167 14009 18828]
[61873 309 38840 ... 52499 24887 15554]
[64555 36523 41808 ... 12547 49214 56821]]
[[10132 29714 2279 ... 39443 50124 13563]
[45983 41540 57747 ... 49110 59381 30366]
[ 6755 16845 19996 ... 51537 62265 29964]
...
[59640 48846 53789 ... 48600 42439 13298]
[21773 36131 53110 ... 27327 6245 59643]
[37954 63000 14599 ... 28704 23737 8996]]
[[ 3579 62198 36314 ... 34020 50950 59972]
[18223 44551 43834 ... 25455 22856 59917]
[41983 6264 41701 ... 43155 30133 6127]
...
[34123 331 9012 ... 55077 4902 50348]
[11333 7388 8575 ... 38969 48807 36750]
[58926 42581 52913 ... 65283 36699 42211]]]
[[[ 6148 52828 50505 ... 61177 2448 36259]
[35805 11332 1856 ... 45357 28965 11045]
[52247 22380 25605 ... 38653 54732 51036]
...
[35566 22172 49509 ... 36590 37285 29440]
[36819 10575 3932 ... 52206 29701 61423]
[32418 52649 16580 ... 14348 13093 20866]]
[[41212 10172 44928 ... 8594 30375 22066]
[ 4622 44371 23761 ... 1231 53587 248]
[ 1437 39318 58971 ... 11628 45104 48242]
...
[ 7920 11553 34985 ... 29140 30779 55512]
[ 9806 40772 18264 ... 17701 773 17618]
[42677 5455 33986 ... 56623 36433 15054]]
[[28150 16447 28686 ... 37305 58542 43550]
[37441 11660 47648 ... 59236 39456 37031]
[57504 43388 32999 ... 64099 49791 63915]
...
[23161 26381 1970 ... 43776 18808 48688]
[23826 15511 46704 ... 40918 63071 8045]
[59643 58274 21407 ... 6275 51149 56459]]]]
[[[[25192 61762 26884 ... 48453 32323 8338]
[ 6432 62276 16831 ... 28321 28558 41643]
[ 8209 46220 56751 ... 26970 21221 56474]
...
[62709 3192 13483 ... 30919 49194 15185]
[ 5245 24502 3491 ... 4041 55805 38781]
[63179 13167 9094 ... 56157 33617 38232]]
[[63511 15766 20576 ... 2818 5848 35840]
[ 773 61132 37298 ... 62744 56541 49601]
[22310 24806 22861 ... 65359 56497 50909]
...
[59545 26456 46343 ... 61164 53965 7772]
[61033 33577 50156 ... 36500 47602 46803]
[ 4574 29303 51804 ... 57292 53868 39928]]
[[35801 41749 60168 ... 8027 49191 12714]
[24022 56001 61322 ... 57453 49637 40499]
[18150 53351 50596 ... 16894 52448 2613]
...
[43435 45390 49010 ... 48651 47656 49629]
[45504 44202 47858 ... 24147 16871 18388]
[55512 50443 5839 ... 39680 63658 17079]]]
[[[18010 56253 42640 ... 925 48141 47749]
[51638 64857 39530 ... 29585 5773 16210]
[63356 32720 60990 ... 4104 58952 45698]
...
[10965 9657 11828 ... 27379 40714 35454]
[28034 42936 46141 ... 33689 5032 51551]
[18777 21922 27818 ... 245 27367 26241]]
[[37084 2929 30736 ... 15814 35745 26017]
[50615 6101 51560 ... 46988 24083 62980]
[21270 20666 7406 ... 35132 14509 9613]
...
[55937 19387 60204 ... 53074 21060 54477]
[43658 37483 9488 ... 46729 573 45195]
[26809 30206 50670 ... 48300 64339 60037]]
[[57648 20198 38599 ... 28868 43389 20703]
[45439 46106 23856 ... 19610 54360 47218]
[ 7142 59718 41463 ... 5133 23570 21030]
...
[27385 45679 13115 ... 16961 3029 41439]
[25241 2489 22111 ... 10765 58776 65283]
[17595 10626 54530 ... 37956 34883 33840]]]]
[[[[59096 51334 15991 ... 62160 54670 39797]
[51298 14701 9528 ... 39064 35940 4697]
[ 9755 12511 65201 ... 35014 57673 33885]
...
[ 7012 50793 52559 ... 1088 31161 57663]
[15547 22254 29170 ... 31112 15677 58048]
[57396 27302 57240 ... 18113 3804 38690]]
[[17683 52751 13722 ... 44649 49718 24996]
[62187 1038 49107 ... 1659 60667 58641]
[23932 26454 46310 ... 50307 37731 22877]
...
[35659 41010 26136 ... 61254 19896 59008]
[11167 51653 17204 ... 24551 58211 12255]
[40344 15843 29301 ... 40316 57367 6086]]
[[57542 62923 43258 ... 23713 41835 22193]
[16956 33186 28258 ... 49974 59002 13942]
[35240 40816 478 ... 53346 22872 29882]
...
[24575 55360 62160 ... 62978 6537 5254]
[41748 62491 27634 ... 32997 61740 29899]
[56257 35557 9375 ... 39178 24496 37359]]]
[[[61330 42995 25156 ... 59115 52560 45324]
[16493 46495 14309 ... 19540 34863 20818]
[65532 62636 42958 ... 16869 9502 15437]
...
[ 320 41127 42935 ... 15725 12054 32404]
[58726 36215 49980 ... 1037 39946 56770]
[ 7283 14496 51869 ... 1051 48747 48247]]
[[34891 53223 58930 ... 32674 17406 41608]
[56968 35421 47857 ... 10567 49867 41306]
[47642 14526 63863 ... 58104 12466 1374]
...
[64647 30575 25996 ... 34679 24146 27614]
[13031 33413 32986 ... 12063 49869 7581]
[45432 49561 21826 ... 59659 22963 40495]]
[[17909 45966 46593 ... 12058 3311 7787]
[51636 14969 17648 ... 22209 1494 37534]
[25903 28837 44738 ... 26106 60488 49030]
...
[20181 28429 50364 ... 42003 61593 10341]
[40834 45640 24754 ... 8728 41334 61152]
[21853 57741 12298 ... 47415 35682 17268]]]]]
Writing was rejected: store was opened in read-only mode and does not support writing
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()