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/tmp9h403ep4/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>
[[[[[56834 25706 17977 ... 15509 38128 51163]
[47505 61388 47601 ... 23020 58563 9884]
[33805 4802 63209 ... 28456 40442 54418]
...
[61000 46085 44823 ... 23171 11170 16288]
[63327 52627 6889 ... 56317 65052 19992]
[26964 63882 50455 ... 8460 9442 39998]]
[[37406 34107 5246 ... 36274 19636 50403]
[46485 3794 28220 ... 34839 59705 31672]
[24550 45606 44893 ... 10798 57365 46112]
...
[51341 24257 15774 ... 36552 16158 15534]
[24561 41951 19060 ... 52887 45869 40542]
[37336 63541 30575 ... 40391 13453 29516]]
[[28580 26636 39865 ... 41374 59852 45536]
[35098 40207 27427 ... 62515 35158 62429]
[ 4337 26485 12119 ... 36974 26382 54207]
...
[ 9922 40112 61112 ... 10861 38693 12835]
[57065 14989 38006 ... 38296 55193 49018]
[44020 10716 51273 ... 8475 11996 48436]]]
[[[19694 56755 17056 ... 48650 1567 7621]
[29339 64385 4566 ... 38582 33117 31107]
[60398 4262 27802 ... 13320 58062 63219]
...
[31385 8558 16598 ... 9488 31393 62168]
[ 6372 15460 29703 ... 28972 61850 18906]
[32051 63444 19945 ... 8351 37603 32792]]
[[47414 534 45053 ... 15380 47595 3296]
[ 9748 1770 25186 ... 52954 26143 48634]
[37055 19818 3795 ... 17795 1797 37896]
...
[45604 45980 2477 ... 56359 43289 25182]
[ 197 35021 63793 ... 20165 50776 20582]
[ 9246 62138 47689 ... 26337 29818 24976]]
[[45259 48399 26895 ... 42763 54911 5063]
[ 9165 43206 29471 ... 55659 52947 9725]
[59460 59601 32065 ... 44993 21219 40258]
...
[57479 23304 3065 ... 29979 64658 50450]
[ 8342 59403 14844 ... 53980 46206 879]
[29696 36742 62090 ... 34156 53994 60373]]]]
[[[[11576 5554 27661 ... 4033 43548 50865]
[ 1301 31763 45559 ... 42734 6266 6838]
[ 1110 41975 52799 ... 48252 57749 25722]
...
[60477 7108 50236 ... 34553 2750 29524]
[44713 36026 43325 ... 6199 773 45638]
[29734 24190 38218 ... 2038 36891 28116]]
[[ 8536 993 37206 ... 39585 37955 59093]
[39482 20906 20808 ... 18341 4396 41811]
[60897 45512 18074 ... 59208 53502 47754]
...
[37572 9856 26740 ... 54249 4609 25258]
[62327 11445 32176 ... 36359 59959 44590]
[44722 54532 5706 ... 34969 62987 11458]]
[[65180 11502 42449 ... 16724 10844 36648]
[39338 44836 40346 ... 6478 49164 64052]
[60149 43156 43673 ... 40993 33453 34685]
...
[15547 49768 38164 ... 55718 8058 21541]
[63204 40473 51062 ... 57198 10957 24106]
[ 8430 64462 10418 ... 16021 60359 35132]]]
[[[56858 57190 2474 ... 20655 57238 55098]
[55632 17773 21012 ... 59360 40241 35290]
[64065 30682 35349 ... 13320 20184 24620]
...
[47734 53618 22385 ... 64543 35177 42377]
[46821 61197 21908 ... 13133 37826 12078]
[ 1310 32759 35004 ... 4773 37886 19944]]
[[25376 12245 52542 ... 27598 140 45092]
[20721 53324 64890 ... 55432 55838 39110]
[ 7790 52882 23031 ... 4383 26747 32012]
...
[46931 35274 59083 ... 54294 22987 41442]
[ 9225 21506 27834 ... 54962 62138 34824]
[62725 58204 48682 ... 8537 30175 58664]]
[[26307 26601 7756 ... 20971 30784 189]
[54006 51944 14836 ... 41754 5522 49704]
[12635 26148 61065 ... 32049 8080 34927]
...
[17872 55937 30118 ... 55517 64659 62768]
[47208 12105 33798 ... 36501 8448 56528]
[58303 38549 24404 ... 21579 63834 34103]]]]
[[[[40702 56839 51896 ... 36185 32235 53727]
[38544 19133 6537 ... 12811 6162 8192]
[35311 40664 13111 ... 24965 33623 3527]
...
[31979 3105 1047 ... 35759 4391 43975]
[ 2445 33621 38944 ... 43043 54674 4510]
[27522 13776 54918 ... 65406 29886 44198]]
[[32716 5432 7247 ... 4943 2079 51046]
[30313 26380 21096 ... 49139 17428 42484]
[40755 6686 52002 ... 60111 8779 42437]
...
[62280 4656 54750 ... 46534 16225 276]
[41582 23665 48298 ... 5418 20839 6568]
[18268 21280 65022 ... 25955 62532 46511]]
[[40420 53812 214 ... 53453 44934 58791]
[43861 57882 33760 ... 55097 36923 27000]
[28487 27608 14773 ... 50708 15052 34616]
...
[65108 28619 13509 ... 21716 11546 41828]
[ 7924 45278 24918 ... 63520 34230 30462]
[51833 51920 33978 ... 49205 5313 15688]]]
[[[16439 43465 9046 ... 40747 15254 59680]
[15570 4087 27881 ... 38550 41686 6580]
[53387 44830 4232 ... 16975 20191 28989]
...
[36965 45115 40343 ... 59373 35122 3331]
[63722 57083 11118 ... 26464 39028 55481]
[48610 40211 26401 ... 53812 31084 20795]]
[[20806 35697 45868 ... 28807 17671 33608]
[15023 5685 59726 ... 31900 32450 57958]
[13092 43868 6111 ... 15569 16305 57600]
...
[59660 30709 27381 ... 34028 22477 6377]
[19890 41013 31819 ... 61495 18918 40407]
[ 7832 35214 51644 ... 37403 2920 22883]]
[[15907 8425 59261 ... 49695 11511 17955]
[59621 51079 6568 ... 64408 15351 31991]
[21510 55464 60044 ... 26454 65331 55211]
...
[53053 30454 26180 ... 6150 26874 35123]
[ 4819 20491 52954 ... 41580 20726 27911]
[59554 8155 14973 ... 18049 48156 19205]]]]
[[[[64401 45002 12614 ... 41665 16266 26158]
[16176 4035 1526 ... 25148 50061 41346]
[ 7570 56503 58644 ... 33469 9898 58207]
...
[53517 25075 32203 ... 41746 47916 63565]
[46166 4806 16616 ... 54141 61185 47884]
[16751 21256 9624 ... 61939 28996 61293]]
[[48495 61675 9070 ... 40805 59905 39971]
[43963 30430 20646 ... 6392 39524 29226]
[44267 41958 10303 ... 3 41229 48873]
...
[43150 50651 5181 ... 60442 19765 31080]
[12388 37158 41367 ... 54884 26638 10195]
[46162 51798 37313 ... 55395 56996 45613]]
[[32649 18152 65524 ... 40346 31885 60199]
[14547 32216 11702 ... 52682 1734 46737]
[18209 48997 24905 ... 21085 40151 47066]
...
[ 2237 12844 24520 ... 18244 60260 36405]
[26552 21423 53303 ... 37495 63094 54211]
[43721 58926 26736 ... 43842 59024 29652]]]
[[[41939 21599 64036 ... 52262 62595 55358]
[14694 34555 54046 ... 45487 48542 38881]
[28393 38320 56084 ... 14652 2529 37725]
...
[14199 37681 36429 ... 16879 11443 14416]
[23522 52342 6160 ... 13447 1772 25396]
[63397 63688 6128 ... 49013 49638 31436]]
[[33680 44915 53415 ... 61905 62545 52439]
[35113 24852 39173 ... 12771 24446 8514]
[29481 19574 12277 ... 50678 43973 48692]
...
[52132 65328 32630 ... 64255 64006 39688]
[17469 62558 14439 ... 46319 24656 35300]
[ 1635 2929 11625 ... 34081 10664 61048]]
[[41838 23411 1471 ... 62341 27464 16619]
[ 2742 31726 15391 ... 18039 54948 60371]
[ 2788 35021 1374 ... 12475 41964 4288]
...
[37153 41427 56275 ... 27307 19728 48960]
[30942 54314 9484 ... 17409 1555 49737]
[36316 62037 8477 ... 41299 61912 24044]]]]
[[[[18698 48415 21172 ... 60073 32003 46744]
[27549 7079 57198 ... 63801 54474 12844]
[ 3024 38057 40529 ... 18153 10907 57319]
...
[ 9796 11375 11645 ... 5147 4414 45889]
[58220 38118 42686 ... 20585 5433 46942]
[23602 52186 1772 ... 27140 26587 53366]]
[[55028 52046 1821 ... 62112 55564 48534]
[ 3777 38110 50704 ... 28613 26804 50581]
[46207 34522 62901 ... 46039 35191 35858]
...
[20038 29195 18380 ... 40478 3387 20129]
[ 2553 36092 42979 ... 36662 11575 14385]
[39199 27217 3897 ... 59352 60016 65492]]
[[41151 42407 3744 ... 27510 236 26557]
[54581 4005 30508 ... 58896 17622 1319]
[40814 64913 31470 ... 29653 61101 50728]
...
[16421 20018 53130 ... 42544 18132 26769]
[40470 7245 59672 ... 53482 19622 51422]
[61640 30155 8703 ... 46521 16590 39149]]]
[[[ 4872 60826 43998 ... 4845 15472 36920]
[47363 12956 18168 ... 47975 21244 20264]
[63491 50593 16616 ... 61338 17088 64377]
...
[55612 20777 60891 ... 17559 14713 43818]
[30406 4017 6340 ... 56593 61879 8157]
[45360 40931 2225 ... 10111 50439 55134]]
[[53869 4741 14449 ... 32682 10296 9481]
[40812 12071 60678 ... 51735 57241 1550]
[46982 62092 10375 ... 60260 20586 45359]
...
[54608 22329 61303 ... 55117 21304 44728]
[41869 57473 7530 ... 21369 29712 52482]
[44011 45702 28991 ... 29704 39069 9143]]
[[54766 42198 34618 ... 25911 7718 59737]
[37438 41758 23555 ... 42445 12825 46035]
[33262 65070 13579 ... 6765 62502 23374]
...
[46625 58559 39720 ... 62252 483 20191]
[64119 33598 4415 ... 39667 52561 37055]
[51268 49042 7729 ... 19010 9101 7611]]]]]
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()