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/tmpvz_p5l5k/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>
[[[[[ 5186 19747 15752 ... 10938 34438  2138]
    [39471 57729 24985 ... 30335 46497 32430]
    [ 7219 48628  6437 ... 23778  6914 13166]
    ...
    [26667 34415  6755 ...  9154 31374 14077]
    [52199 36585 53879 ... 13876 26527 38623]
    [35717 40959  1256 ... 65389 14858 39872]]

   [[32944 57140 33450 ... 63914 11061 11758]
    [19625  9277 33178 ...  5041 17889 49778]
    [51302   713 46521 ... 33092 27379 59172]
    ...
    [35072 32246 40404 ... 37701 63719  3653]
    [41335 47060  8566 ... 12502 17842 34058]
    [43097 39697 10227 ...   976 37421 51051]]

   [[55767 22267 50282 ... 31564 16323 55264]
    [50551 40308 64912 ... 57190 20954 54092]
    [33407 16546 51943 ... 21316 25941  5595]
    ...
    [46780 59486 35242 ... 16686 30974 10701]
    [28260  4021 30699 ... 31163 60332 22292]
    [50143 27158 29290 ... 60717 34252 15150]]]


  [[[16822 21300 63525 ... 32727 55616  5704]
    [41721  5631 17659 ... 53283 43385 13992]
    [16503 42469 50535 ... 55833 36975 33466]
    ...
    [49994 11938  9975 ... 29521 36577 25236]
    [60987  1839 39393 ...  9062 39603  2044]
    [   62  8558 28163 ...  9948 50044 56551]]

   [[21123 35207 32062 ... 23384 24571  5051]
    [44078  6846 21994 ... 25097  6142  7422]
    [37991 52282 47603 ... 35575 39885 25999]
    ...
    [36827 54110  5255 ...  7205 16994 24598]
    [12311 64206 17335 ... 39195 28482 33645]
    [15534 51811 27215 ... 48267 24383 24940]]

   [[65300  3430 11051 ...  3962 12928 42837]
    [48259 43976 24460 ... 56132 34241 10815]
    [42766 39439 44932 ... 50630 30857 45211]
    ...
    [ 9923 36783 64989 ... 53106 52035 63423]
    [63893 29752 11567 ... 53491 38310 51608]
    [63013  2594 50930 ... 27030  7091 12027]]]]



 [[[[23626 18753 36878 ... 44999 44717 11041]
    [30806  8567 61489 ... 18490 24803 33616]
    [43741  1772 54369 ... 28571 30484 38400]
    ...
    [ 3876 19560 16907 ... 43146 19017 37515]
    [ 3909 47593 53653 ... 10967 51403 41677]
    [52990 65315 44671 ... 56308 12823 21797]]

   [[10519 21671 29842 ... 64680 22607 15078]
    [61208 60532 37959 ... 14422 30499 11668]
    [21938 47800 24265 ... 35277 54981 35115]
    ...
    [50914 50532 21742 ...  6343 60997 21866]
    [ 4873 36683 55516 ...  6622 59865 47966]
    [58301 23465 49117 ... 58593 62068  3050]]

   [[47288 48340 53406 ... 63945 50288 21265]
    [50427 25239 16654 ... 25587 49553 39873]
    [22968 45128 10145 ... 34845 64628 21396]
    ...
    [13155 41505 12395 ... 15180 14815 14242]
    [ 6930 21234 14063 ... 10120 10927 53993]
    [22749 48280  6785 ...  8711 45709 43286]]]


  [[[49676 65531 21922 ... 30189 63863 22023]
    [54009 30671 18206 ... 36632 50949 49630]
    [22223  1296 64602 ... 45803 34872 34374]
    ...
    [50200 12591 15234 ... 20787  8977 26381]
    [62850 19375 12347 ... 19549 11021  6109]
    [53804 25089 22475 ...  4321 32712 60951]]

   [[27624  5448  8399 ... 56288 19839  5679]
    [59668 56135 62146 ... 51077 63452 27317]
    [20364 44773 62406 ... 11031 17637 32822]
    ...
    [63651 29491 21182 ... 24036 50149 36025]
    [63484 61590 23322 ... 29007 41873 31259]
    [41936 44535 47258 ... 53199 24688  2028]]

   [[44441 51874 29188 ... 56544 14441 29240]
    [10816 22996 28071 ... 10234  8246 48523]
    [17391 30084 58196 ...  6044 35676  1071]
    ...
    [ 1867  6521  6043 ...  7506  1917 19639]
    [19358 35833 46393 ... 15452  7940 47845]
    [50834 32641 16014 ...  6412 40053 54290]]]]



 [[[[31908  9296 47483 ... 17317 13231 31019]
    [26644  7249 33247 ... 39852  2368 62921]
    [ 5679 16264 63462 ... 57238 47553 31702]
    ...
    [20993 60459 57235 ... 40356 13478 43830]
    [46882 51281 43989 ...  7912  1229 39715]
    [28840  1835 31633 ... 29482 58519 14640]]

   [[43335 62506 21574 ... 59122 15056  7385]
    [45221 41407 57799 ... 54850    54 41612]
    [13604 37654 24565 ... 32666 16526 50661]
    ...
    [54275 56488 38591 ... 59975 51999 61105]
    [19166 25071 47566 ... 27708 45375 51324]
    [27752 39805 21575 ... 15165 63329   114]]

   [[14078 13602  3768 ...   955 61635 55717]
    [23461 45178 63505 ... 22483 35352 20436]
    [56414 35413 64256 ... 52880 50051  5647]
    ...
    [50351 49687 31507 ... 37120 41182 48466]
    [ 1186 55536 32137 ... 62219 37035  6869]
    [24898 26572  1191 ... 31802 34642  8573]]]


  [[[52768 30444 17675 ... 45512 62861 30213]
    [23990 11631 43841 ... 46769 30960 30882]
    [64840   237 23736 ... 52221 28489 60239]
    ...
    [59332 49815 54022 ... 63251  2673 41909]
    [42672 47159 62151 ... 28987 29691 29885]
    [52644  8196 33225 ... 28520 29959 17064]]

   [[13166  9635 27840 ... 26162 52123 48495]
    [58056 32403 37659 ... 26100 20351 24143]
    [18112 36539 65149 ... 61748 27366 50889]
    ...
    [33562 39205   109 ...  1829 18220 47414]
    [57495 26035 35876 ...  9296 46677  1689]
    [47605 54175 18941 ... 34930 51520  6617]]

   [[57275 45090 18091 ... 19142 15756 15153]
    [33324 57166 42362 ... 34846 64045 41889]
    [60465 48092 57445 ... 39913 59066  6674]
    ...
    [37776 19448 21487 ...  2781 56320 12893]
    [64534 24329 11104 ... 47218 62373 53421]
    [19071 57919 18160 ... 28220 55497 32316]]]]



 [[[[37301 31472 35260 ... 54691 10075  3759]
    [47146 55450 45071 ...  8290 59235 37554]
    [22006 18950 12072 ... 22522 27526   544]
    ...
    [42105 22099 52154 ... 49672 56858 62485]
    [47870 13268 15436 ... 17472  5046 12398]
    [41455  1663 26482 ... 35990 58117 28005]]

   [[ 6147 30748 64182 ... 40837 60652 34662]
    [13835 64984 31733 ... 32577 49209 43805]
    [50958 16329 56543 ... 31507 41775 16920]
    ...
    [44546 36052 41911 ... 46997  7097 45330]
    [26551 40941 29351 ... 41914  1774 30331]
    [42312   837  4408 ... 17382 17591   953]]

   [[11970 33425  3448 ... 33304 18951  5468]
    [51008  6032 20597 ... 27631 64688 21155]
    [11499   308 54452 ...  7633 44071 57519]
    ...
    [25872 30055 19648 ... 60901 35953  5721]
    [64324 26041 11609 ... 10636 33565 47757]
    [31818 17814 30132 ... 27169 62222 36587]]]


  [[[45356  3154 34204 ... 33270 31580 59739]
    [34290 44916 42791 ... 42908 20927 37872]
    [38433 29397 46994 ... 48530  2579 25847]
    ...
    [ 5242 24816 38506 ... 32001 21130  4287]
    [23349 65107 16022 ...  1635 56936  6982]
    [58020 34011  7903 ... 28644 49965 57722]]

   [[13682 42508 11448 ... 37159 22510 11332]
    [ 3156 20456 58148 ... 21725 46470 26853]
    [12841 39681 50530 ... 35605 54832 30765]
    ...
    [ 4662 39376  7778 ... 52888 62138 24314]
    [39381 31512 40891 ... 59604 63767 60828]
    [47263 43577 38697 ... 47337 45205 28234]]

   [[54345 63808 21980 ... 41029 25517 61896]
    [45049 39006 24089 ... 49449 27919 15703]
    [25806  4857 53509 ... 13738  5024 51174]
    ...
    [60093 22632 27177 ... 23309 62939 35213]
    [42684 64264 20073 ... 43601 12612 12076]
    [15172 65481 44181 ... 40619 34941 62467]]]]



 [[[[35551 56409 31827 ... 58351 58463 64734]
    [56340 17630 64400 ... 51665 20937  4177]
    [36684 46808 32523 ... 63743 31573 40003]
    ...
    [22636 20615 59250 ... 28279 47673  2068]
    [18485 18471 34759 ...  7858 16662 39544]
    [53983 53303 10083 ... 23720 60000 28432]]

   [[37805  7763 58366 ... 19217 19260 58011]
    [27766 55599 46769 ... 63871 28965  2032]
    [56866  6712 18448 ... 54715  4493 55011]
    ...
    [ 7126 34347 44261 ... 17528  3569 23086]
    [46481  2019 19159 ... 59371 42118 51115]
    [ 8012 56260 63004 ... 48127  2533  3454]]

   [[ 5887  6797 20288 ... 44853  5813 53047]
    [14105 33501 38104 ... 24359 44687  1199]
    [17810  8124 52002 ...  3616 15414 32481]
    ...
    [14634 26374 16130 ... 53780 35926 42107]
    [25555 41946  4362 ... 31215 47669  4318]
    [58123 42346  7374 ... 51001  1135 29380]]]


  [[[  449 13072 54085 ... 20857 13366 30686]
    [60831 64410 10178 ... 62078 60698 27240]
    [25948 49770 17283 ... 64310  5273 37470]
    ...
    [45919 30825 28972 ... 45233 40068  9447]
    [38679 61189 29979 ...  8808 61616 15013]
    [17911 41139 49735 ...  7275 39554 11752]]

   [[59865 37393 10874 ... 44521 51779 34386]
    [60616 27312 33925 ... 46795 13534 26608]
    [65088 51455 52224 ... 62881 48392 57791]
    ...
    [27034 58269 11115 ... 42945 56026 52969]
    [56221 29112 64780 ... 44690  2994 21791]
    [39503 26843 58534 ... 13781 10486  9752]]

   [[35563 51033 11811 ...  8665 24438 41828]
    [33745 54505  3105 ... 41110 50362 27635]
    [38089 20862 21625 ...   455 59255 37649]
    ...
    [54398 35361 37893 ... 28155 35879 45541]
    [51174  8948 54023 ... 45265  2196 62077]
    [53225 54773  7226 ... 33550 44145 24246]]]]]
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()