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/tmppq1sw4x5/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/tmppq1sw4x5/ome.zarr/img shape=(5, 2, 3, 32, 32) dtype=uint16>
[[[[[16100 7873 28238 ... 29235 40461 33146]
[15093 19675 1572 ... 23394 57899 47477]
[ 9089 16355 12624 ... 59289 3130 6427]
...
[58233 40267 15620 ... 28510 3090 1334]
[55791 21646 52760 ... 7733 49471 35453]
[41537 16771 47111 ... 3065 62085 11164]]
[[ 5871 52278 43053 ... 2008 23373 21894]
[31168 35522 14416 ... 56160 58668 49502]
[15330 39217 45091 ... 33466 23112 56170]
...
[47427 56880 19485 ... 46227 24989 51236]
[38296 10843 65235 ... 1500 16430 35481]
[31899 23739 10755 ... 15951 8869 30745]]
[[61792 9770 27955 ... 43440 37697 28549]
[39888 64233 40933 ... 55006 42970 40427]
[65168 21890 49491 ... 39031 49833 7783]
...
[17920 18625 42469 ... 19524 24297 21383]
[64284 24733 10279 ... 36275 42414 40256]
[16555 18640 39702 ... 38015 31323 13844]]]
[[[12281 47222 36489 ... 18966 46558 35403]
[58401 4605 62107 ... 46067 25635 22669]
[24980 51705 51854 ... 49143 59216 12285]
...
[ 948 19721 40686 ... 21254 43442 20232]
[31462 43658 1414 ... 429 64391 26694]
[15910 29452 63035 ... 546 20986 46404]]
[[41340 20694 36136 ... 30745 49204 4057]
[ 1892 63820 38338 ... 63649 1190 64630]
[ 3168 63611 61138 ... 31026 18973 55444]
...
[ 3974 7661 27791 ... 30468 64681 10796]
[51061 31140 44352 ... 18027 32253 10640]
[23817 7221 30072 ... 60204 57198 63585]]
[[10820 715 46542 ... 27727 23163 30258]
[ 4330 2851 54484 ... 29177 18736 32968]
[38783 19849 54755 ... 38496 29432 25532]
...
[32122 11555 63031 ... 50106 64743 26796]
[57937 33031 15768 ... 59848 43465 55739]
[38933 57625 19747 ... 47393 32748 14469]]]]
[[[[18487 49059 26059 ... 54414 36676 16185]
[28156 9177 20010 ... 14263 4067 40957]
[18171 14337 18676 ... 44741 29937 26840]
...
[ 7024 41730 50891 ... 1170 6530 43038]
[63648 41965 51990 ... 59665 12133 3549]
[58634 28496 18283 ... 17848 59320 20248]]
[[ 2691 61316 39215 ... 32285 11182 18509]
[62285 63179 31432 ... 57552 43526 57948]
[ 6238 795 34217 ... 40551 45346 6715]
...
[ 5513 60734 49128 ... 52435 14803 54541]
[ 5834 22214 26298 ... 51405 1926 14950]
[61078 16593 29574 ... 61357 49213 10419]]
[[60407 17291 5048 ... 65197 14858 17052]
[ 5305 44050 61671 ... 7770 5422 28329]
[ 8999 5468 40162 ... 9282 37331 25358]
...
[27427 21405 29769 ... 10677 53602 64312]
[60230 14510 50617 ... 23727 60055 51122]
[65286 47670 33149 ... 31605 13275 39242]]]
[[[31234 24020 5583 ... 35252 35992 5956]
[64868 9009 14938 ... 18216 16492 17742]
[62346 56090 28702 ... 50308 20003 42411]
...
[32609 27421 31151 ... 17510 23097 50626]
[33957 62470 54275 ... 31761 58165 15612]
[58589 58344 35028 ... 34524 3103 32239]]
[[64255 28498 33885 ... 43048 12406 53471]
[59516 6178 15910 ... 63887 12885 61288]
[ 283 64497 50580 ... 34689 29689 2219]
...
[38907 55014 41209 ... 9196 22878 32452]
[25278 13661 27660 ... 59322 18245 9652]
[64290 62537 54932 ... 42711 27047 60685]]
[[47635 33523 11249 ... 31639 6836 50453]
[21941 13508 2447 ... 31853 8769 23345]
[48238 30958 23308 ... 53726 61465 16309]
...
[32728 62955 2293 ... 3481 24751 39301]
[51081 5576 45044 ... 46331 33886 3585]
[39885 43166 19823 ... 41627 16060 53239]]]]
[[[[26564 1299 32339 ... 64037 63175 55178]
[61187 50457 44072 ... 11779 26396 41730]
[32156 46602 29002 ... 21467 15728 15658]
...
[ 7380 49375 40408 ... 20825 27996 60464]
[64101 32408 30590 ... 10291 31540 55239]
[34717 1002 12203 ... 22432 49627 30971]]
[[ 292 62480 3294 ... 8065 33168 18701]
[57797 50762 55582 ... 60969 10946 28811]
[26625 20536 51484 ... 46726 29487 47597]
...
[20301 36685 41325 ... 22352 38059 31878]
[12658 43820 61582 ... 19319 49489 47163]
[19587 4205 49201 ... 42488 54989 23018]]
[[30439 26476 23868 ... 3095 3227 30623]
[24181 48233 22712 ... 37739 30881 32732]
[ 1101 45402 63020 ... 37117 3303 8194]
...
[17903 39655 5586 ... 33548 14157 45512]
[14222 48069 22848 ... 4744 7139 30901]
[27590 306 35473 ... 31937 9048 1066]]]
[[[39915 36591 59229 ... 1126 9050 35683]
[57052 30970 19852 ... 20702 28880 60027]
[64137 26550 4376 ... 10259 15359 35183]
...
[46504 106 38111 ... 55831 43105 56580]
[ 1315 13328 6773 ... 22581 32337 10217]
[10289 34448 2631 ... 1920 13597 65502]]
[[ 67 61452 62462 ... 48401 25464 23528]
[12358 33399 61307 ... 34900 24659 9862]
[15424 41373 11641 ... 30231 31007 4999]
...
[57741 46219 26875 ... 28317 661 45610]
[ 7477 51168 37972 ... 17157 45232 54949]
[42948 21825 16591 ... 15742 9256 9319]]
[[25287 22217 45290 ... 57491 9811 33373]
[18889 18041 30133 ... 10597 3564 63141]
[57397 3598 59443 ... 2357 37713 63869]
...
[41447 57196 35618 ... 45432 59007 36022]
[45873 50650 15493 ... 25285 40560 26855]
[54129 55437 18617 ... 38608 47376 9754]]]]
[[[[17400 55288 60049 ... 38674 11337 65335]
[40932 31706 15605 ... 36506 59323 20909]
[14534 63557 6394 ... 42854 57951 46471]
...
[23092 16272 5376 ... 25516 50437 5695]
[39260 26530 51636 ... 37172 27300 60041]
[60557 63052 43450 ... 9646 27287 14702]]
[[30833 30947 1959 ... 36271 32417 37922]
[49034 61257 31589 ... 16040 16861 32627]
[62936 3018 64633 ... 3581 34753 10316]
...
[13776 575 15004 ... 28014 53283 56744]
[40804 25604 38202 ... 64278 26627 53720]
[50768 45595 49421 ... 53258 19294 23948]]
[[34638 61762 16189 ... 22250 50532 46115]
[25527 23399 25353 ... 15761 50635 64131]
[ 4065 14109 6373 ... 12779 16394 38881]
...
[ 5799 44799 56266 ... 64510 6663 22153]
[34260 13367 51256 ... 49734 5679 47908]
[13015 14360 40065 ... 10886 21586 25458]]]
[[[61940 52910 5956 ... 40346 433 23597]
[49181 14329 23648 ... 1051 21149 20566]
[ 8714 15620 60587 ... 18678 11990 44571]
...
[25749 17183 7225 ... 9425 38530 27024]
[ 9449 36223 64495 ... 49412 22363 20342]
[13796 40799 32322 ... 35571 24943 3162]]
[[27287 47572 45515 ... 52291 29296 31145]
[ 4773 58627 53386 ... 41055 26563 20528]
[22508 30804 16286 ... 8589 6900 22618]
...
[53924 50963 48560 ... 62410 52743 29052]
[57183 61430 28999 ... 4784 34466 21218]
[10130 6630 55324 ... 45074 47869 7650]]
[[ 6923 38950 52891 ... 440 61767 63289]
[ 3652 3029 14224 ... 44779 46314 3340]
[23238 29332 50538 ... 41984 44875 48368]
...
[47628 36744 607 ... 63723 30224 10365]
[15237 7270 24036 ... 50677 44765 32687]
[24647 14930 56324 ... 54902 39029 19644]]]]
[[[[62184 41345 15019 ... 24769 62375 12516]
[37425 27045 7096 ... 65106 40874 8558]
[58277 36285 54714 ... 53503 8353 17713]
...
[24910 60240 58385 ... 2862 58736 44758]
[31467 41644 21883 ... 59118 10090 15292]
[19585 45688 48823 ... 57239 53722 55324]]
[[11877 5118 63929 ... 43548 60255 21450]
[47701 65068 4716 ... 23696 36733 43830]
[51294 11111 52161 ... 47551 18782 20776]
...
[32810 65244 52665 ... 36822 21229 61278]
[48531 24383 38235 ... 57170 44747 39818]
[ 378 32198 10323 ... 58374 17950 7866]]
[[53630 5321 26546 ... 62974 41909 60621]
[47528 37153 24747 ... 58975 30635 16832]
[ 8613 11946 36936 ... 454 35031 24423]
...
[23865 47053 12381 ... 56000 58315 53864]
[48507 23973 32107 ... 30019 28576 17152]
[17234 50764 33329 ... 34561 4887 31909]]]
[[[11246 65235 46194 ... 22372 47051 9201]
[ 1732 43176 4578 ... 47137 46711 56832]
[43120 1389 36606 ... 1694 26677 55320]
...
[34908 18261 33394 ... 64192 10569 4312]
[63118 49117 49523 ... 20439 60663 13889]
[24329 21705 753 ... 1196 844 32596]]
[[ 4991 3804 21326 ... 59691 45544 41935]
[10970 21988 38036 ... 33869 9504 17144]
[ 5662 8461 28178 ... 23805 49886 38202]
...
[39784 27187 23235 ... 40596 1685 51126]
[14786 27368 42704 ... 7932 51776 62830]
[35635 20857 30768 ... 16853 45293 3317]]
[[10143 11842 41868 ... 33672 59360 38303]
[25799 35440 56288 ... 6805 2738 25783]
[19426 61809 42000 ... 20829 4888 48889]
...
[18024 52257 11441 ... 53667 19989 2900]
[41964 48912 11864 ... 3820 14048 47479]
[47675 61372 36009 ... 59063 47550 50156]]]]]
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()