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/tmp1gr4g142/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/tmp1gr4g142/ome.zarr/img shape=(5, 2, 3, 32, 32) dtype=uint16>
[[[[[ 3988 51001 58689 ...  2356 57980 39932]
    [10590 59783  9649 ... 49454  4420 45472]
    [18987 64809 15785 ...  9941 62343 16271]
    ...
    [10519 42034 11193 ... 22609 41047 13387]
    [50379 22250 50290 ... 29988 33650 20729]
    [ 3762 30562 54686 ... 52204 40806  7655]]

   [[36914 23558 55399 ... 26600 45148  7110]
    [27165  4735 17079 ... 61113 16215 52791]
    [ 7841 36178 27164 ... 42935 11590 25568]
    ...
    [17925 44234 61910 ... 31440 36976 51374]
    [29359 37008 49175 ...  9030 51459 51696]
    [57002 64675 63758 ... 31445  5760 31722]]

   [[55488 41752 62908 ... 60358 50273 61428]
    [54613 37407 28289 ...  6952 51235 14577]
    [26107  9715 17763 ...  7589 54121 18217]
    ...
    [47985 17439 59398 ... 51073  8426 15068]
    [36650 13395 41809 ... 41959 32699  4046]
    [53159  1761 32645 ... 18024 26978 55883]]]


  [[[45911 11757 54521 ... 21526 23908 62468]
    [64717  1648 57580 ... 42128 34051 53930]
    [37910 60936 29512 ... 21556 50978 43168]
    ...
    [29202 60170 55971 ... 39890 62235  8657]
    [ 5200  3871 64407 ... 22604 57740 63118]
    [43309 37595 59220 ... 17911  4003 27943]]

   [[42531 20134 42275 ... 11913 29107 19408]
    [44105 55186 60245 ...  2439 65506 20831]
    [20265 37385 30248 ...  9206 33047  2935]
    ...
    [ 5265 64721 63994 ... 23456 24131 15555]
    [ 2066 56287 21476 ... 48490  7611 43316]
    [58837 56063 16221 ... 18849  7502 56351]]

   [[24766 30936 23396 ... 29595 36809 35582]
    [43560  8316 19454 ... 33190 12710 59466]
    [14805 35406 14502 ... 27548 29413  4602]
    ...
    [ 1492 64350 59764 ...  2170 21579    81]
    [58160 36519 48090 ... 57890 44345 44755]
    [46330 15285 53998 ... 61972 58870 30329]]]]



 [[[[55290 13442 18279 ...  6854 12498 46685]
    [13761 57047 33059 ... 45104 48656 50711]
    [25319 52128 58387 ... 53957 38058 41570]
    ...
    [56865 44220 60830 ... 15535  1597  8145]
    [31624 46813 64838 ... 39414 11945 56833]
    [22240 19933 24499 ... 43451 46179 53691]]

   [[53444 56549 48379 ... 41836  8987  6574]
    [46460 52298 37578 ... 32673 28731 35540]
    [47688  4063 31886 ... 64971 26437 17108]
    ...
    [54367  7609 58605 ... 21609 27669 10148]
    [58837 49783   712 ... 35487 57072 27021]
    [45545 15713 45594 ... 34289 28058 42354]]

   [[52135 47974 55496 ... 52080  4585 15090]
    [37145 22160 35301 ... 29518 10496 32357]
    [20182 24233 10271 ... 58413 31443  7570]
    ...
    [ 3784 59165 43936 ... 36874  8900 53023]
    [26956 48429 22154 ... 52949 15121 57448]
    [56795 23896 14250 ...  9408 51761 62525]]]


  [[[54606 55755 28599 ... 55734 14673 10232]
    [32057 17402 16785 ...  6107 12150 37767]
    [60588 61978 59433 ... 11269 33671 24886]
    ...
    [42154 15715 51017 ...  5306 14217 45452]
    [23804 52849 34588 ... 62442 50327 47626]
    [53295  4048 24848 ...  6786 32242 48859]]

   [[59720 42746  5847 ... 43095  4819 38796]
    [ 6713 38447 16365 ... 47867 13461  5773]
    [20322 50526 33333 ... 33376 38823   801]
    ...
    [31937 23122  6533 ... 20278 39673 50828]
    [28821  5876 54005 ... 16988 44332 26867]
    [18793 27423 20580 ... 35376 60871 14884]]

   [[50898 51613  5332 ... 27431 54574 55306]
    [56298 16517 49535 ... 10824 11875 50641]
    [ 8420 32221 18847 ... 42047 16551 48778]
    ...
    [35867  5020 37140 ... 46181 19648 49445]
    [13876 47486 23257 ...  9295 34538 44183]
    [32012 62823 36481 ... 34217 49577 64158]]]]



 [[[[64305 64398 44150 ... 17564  2074  1819]
    [30973   681 23364 ... 49025 47496 18858]
    [21314 48459 64807 ... 14524 55494 48823]
    ...
    [22710 15999 26884 ... 36068 53229 40949]
    [47704 52931 14574 ... 55333 19736   984]
    [44082 47937 36484 ... 16927 55483 52108]]

   [[64499 15544 45618 ... 17538 27927 22356]
    [57735 57423 17730 ... 61549   800 57414]
    [22297 25293  1736 ... 26610 10488 46076]
    ...
    [20291 56079 14661 ... 46721 56227 60697]
    [18399 59476 61268 ... 31657 56020 41644]
    [23878 36245 41073 ... 27887 51274 15954]]

   [[36168 16424 22482 ... 39543 41633 11541]
    [35291 13914 32260 ... 57748 15704 18943]
    [34446 54386 36700 ... 37193 52006 19316]
    ...
    [52447 30505  5017 ... 13667 38455 38804]
    [31923 36727 38896 ... 46777 51259 51816]
    [48284 63211 12062 ... 27631 37802 54536]]]


  [[[ 5689 42353 27764 ... 50359 53500 33579]
    [40963 22169 24917 ... 47041 64094 30615]
    [13329 59221 39168 ...  5293  6736 23195]
    ...
    [54574 33992 47238 ... 48563 26814 13008]
    [12794  2596 44612 ... 25460 28252 37631]
    [61951 29780  9990 ... 29514 24212 26846]]

   [[60944   994 14721 ... 51338 42161  6858]
    [ 2910 42658 31889 ... 30871 57472 42485]
    [50449 60525 45948 ... 31216 51620 51212]
    ...
    [54992 41428 47903 ... 50985 14171 42826]
    [45154 39000 64943 ... 24367 19199 10308]
    [ 8531 39250  5679 ...  4250 44763  2439]]

   [[12336 61802 24533 ... 57915 14393 59224]
    [ 3921 18572 27085 ... 34707 12863 60313]
    [36982 31320 28710 ...  7988 37391 52515]
    ...
    [ 2899 38369  8151 ... 17897 60322 45438]
    [58813 65443 12392 ... 39615 12188 35265]
    [59365 49381  7625 ...  3797 57261 14115]]]]



 [[[[65444 31418 61581 ... 32127 35405  1966]
    [33936 61250 28848 ... 25016 11758 62464]
    [44068 13402  3118 ... 17265  1606 26738]
    ...
    [19513 36116 32992 ... 64321  8004  3267]
    [58021 62271  7715 ... 43868 60461   110]
    [41426 32960 24139 ... 57728 46951 21567]]

   [[40615 64179 54980 ... 40856 18292 20994]
    [48834 25687   672 ... 26012 21885 48719]
    [59784 47735 39256 ... 53382 37353 54280]
    ...
    [16940 41435 58255 ... 31866 21173  5046]
    [43521 38356 16474 ...  7392 37381 33783]
    [45246 37447 13147 ... 48101  9306 40234]]

   [[15875 23325 17158 ... 58334 34684 36604]
    [  480 20488 38458 ... 32498 59613 50530]
    [ 6101 17974  2017 ... 52146   673 46610]
    ...
    [27854 23215 60939 ... 27195 63211  1555]
    [ 3921 59522  3470 ... 12363 37475 41844]
    [32310 58081 37859 ... 58771  2204  7652]]]


  [[[47764  2798 43795 ... 48037 21291 25725]
    [58417 23116 40150 ...   784 64005 45380]
    [17750 15562  1308 ... 31628 29889 24559]
    ...
    [12875 62874 41801 ... 65343 44638  9329]
    [ 9062 32567 52306 ...  1765 38190 52463]
    [53542 30766 54996 ... 39118 43665 36845]]

   [[63506 26375 61870 ... 25961 36568 29292]
    [ 7844 36949 35866 ...  6843 31661 29734]
    [52984  6523  6424 ... 37455 39421  6318]
    ...
    [43406 32165 49141 ... 27306 34234  7869]
    [27629 20372 51814 ... 13210 38900   704]
    [26785  7555 61784 ... 54234 49127 25358]]

   [[56193  4268 18366 ...  2375 64171 24704]
    [40127 53019 43988 ...  3736  4933 21586]
    [ 3870 34991 21580 ... 21828  8141 57012]
    ...
    [60329 55444 34662 ... 16567 60813 48194]
    [63892 43194 37299 ... 65164 22212 64402]
    [45476 40042 22706 ...  3283 46888 15681]]]]



 [[[[56830 60496 15222 ... 33279 47284 47196]
    [45881 21252 43497 ... 15081 10186 51382]
    [11136 12599 64092 ... 19757  3745 42098]
    ...
    [35208 50809 22137 ... 27274  4773 34235]
    [23523 50402 16673 ... 33039 13777 36248]
    [44211 30102 60187 ... 52585 47026 11650]]

   [[18579 17662 22213 ... 19752 14624  1106]
    [10705  2588 53105 ... 35826 31711 61152]
    [17374 34491 40534 ... 55936   811 61947]
    ...
    [53639 58683 14608 ... 59684  9763 49955]
    [55950 34512 10411 ... 57171  6640 58240]
    [36889 31364 57711 ... 44423 44921 51918]]

   [[42688  6565  6490 ... 57140 17373 25195]
    [29791  2510  3604 ... 27709 10782  7840]
    [64933 10634  8619 ... 24572 44575  8670]
    ...
    [28893  6535 16602 ... 34280 15706 18938]
    [ 6883 43717 58216 ... 34979 42044 12922]
    [65145 12910  5514 ... 13544 24266 62597]]]


  [[[27737 50652 42614 ...   219 45717 16199]
    [21335 51358 42500 ... 19163 47804 50036]
    [31291 56815 42353 ... 25409 37589 16417]
    ...
    [21659 11566 39521 ...  6493 32982 36108]
    [35240 47502 63422 ... 10518 46810  3048]
    [57863 31728 53764 ... 22506 63946 63005]]

   [[57582 60200  7176 ... 56933 49525 49328]
    [14912  8065 19983 ... 21585  7777 25609]
    [22345  4780  2294 ... 35645 22656 53066]
    ...
    [ 8607  7802 25293 ... 42937 11779 54979]
    [55148 46453  7761 ... 32170  8585 40706]
    [61798 13726 34683 ... 21736 55977 52456]]

   [[63325 13520 35178 ... 38133   694 25131]
    [ 5687 55597   855 ...   349 57839 15670]
    [42578 37231 21653 ... 55923 35031 49617]
    ...
    [41513 20661 19199 ... 58355 16140 52155]
    [58315 45609 19763 ... 38703 51095 41391]
    [42477 63096  6614 ... 29245  5450 12113]]]]]
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()