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/tmpy08eraec/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>
[[[[[28285 12037 24638 ... 45099 61833 35604]
    [44965 16634 40587 ... 58127  4615 45675]
    [13729  1667 42632 ... 43391 12009 60166]
    ...
    [61975  2389 20209 ...   485  9140 65075]
    [31313 63974 35118 ... 36316  6090 54056]
    [  106 20106 10573 ... 38565 60127 30231]]

   [[44560  5976 43660 ... 46164 65295 41833]
    [62181 13664 21738 ... 63012 17012  9882]
    [48625 48334 60524 ... 26549 50692  7140]
    ...
    [ 9978 20047 38895 ... 13427 16847 21891]
    [56182  5574 29718 ... 51783 56573 18431]
    [ 5134 31723 58074 ... 18613 13056 59401]]

   [[56536  9089 56527 ... 60064 35624 32870]
    [60972 13938 51028 ... 11113 42397  4160]
    [29309 15651 49562 ... 21845  5775 41679]
    ...
    [ 3465  2592  4885 ... 63002  2232 36642]
    [36156 18022 34758 ... 19918 20473 43654]
    [12486 26920 22267 ... 45766 44739 64928]]]


  [[[65361 33909 39389 ... 21246 24906  1480]
    [ 7360 49540  7034 ... 17122 42554 43517]
    [40888 24177 54247 ...  6325 55525 21337]
    ...
    [22951 38918 23565 ...  8085 15730  1121]
    [ 3715 21586 47762 ... 35115 27604  3222]
    [19939 14803 44491 ... 57056  3188 23647]]

   [[ 8526 38106 40122 ... 54767 12841 57988]
    [56894  6704 17084 ... 61252 12799 49153]
    [ 2383 34815 62845 ...  7533 37577 56339]
    ...
    [ 1382 23801 33180 ... 57259 18442 35084]
    [59649 61320 51005 ...  7743 39201   987]
    [49553  1737  8013 ...  5126 24618 51507]]

   [[28267 28675 31016 ... 38851 13194 50150]
    [13567  8017  6773 ... 33789 15472   494]
    [27958  5516 51835 ... 59261 30230  8960]
    ...
    [24303 15647 31387 ... 36713 27719 42635]
    [46802 43913 45765 ... 14812 59548 18499]
    [24362 15023 12873 ... 32886 64274 35672]]]]



 [[[[26513  9351   269 ... 34872 13511   793]
    [ 4444 43769 47827 ... 33583  6789 25392]
    [24182 36615 60813 ...  8502 33192 57739]
    ...
    [42418  2062  6217 ... 18434 10279 57425]
    [39324 49139 50783 ... 16619  4037  8337]
    [35407 59284  6907 ...   874 27203 45698]]

   [[18760  8823 36944 ... 30462  9178   102]
    [58198  1139 26031 ... 58482 33586 28716]
    [14444 27741 64717 ... 32525  5227 32686]
    ...
    [32298 42032 64238 ... 45565 57136  3652]
    [19040  5217 49850 ... 24026 39175 25412]
    [62438 37863  1039 ... 32605 17877 27006]]

   [[60679 59134 63073 ... 55497 61190 49925]
    [57010  8744 12876 ... 46840 57565 44652]
    [39628 50324  3474 ...  7399 53094 62065]
    ...
    [22195 60641 48306 ... 61391    73 38609]
    [ 6398 48944 41913 ... 16579  1277 28382]
    [ 4775 35443 27673 ... 39353  2770 10320]]]


  [[[56062 62290 56681 ...  1548 47679 15512]
    [57933  7241 32166 ... 10045  7640 44195]
    [13177 51184  9249 ... 12250 56607 44698]
    ...
    [28825 17510 13360 ... 30216 10996 21924]
    [61441  1585  8276 ...   901 58355 10265]
    [47502 54782 61578 ... 61601 48068 34319]]

   [[32870 14733 42845 ... 18888 38443 12621]
    [42956 29874 26210 ... 29075 10609 26789]
    [61782 21681 14151 ... 27658 26530 44141]
    ...
    [21348 20444  6113 ... 17306 51960 38042]
    [35661  8073 14089 ... 61204 56852 48288]
    [60628 46459  8154 ... 11312 28766 21882]]

   [[46191 61961 50229 ... 64664 50074 32462]
    [12586 34062 51317 ... 31556 57956 51388]
    [52780 57889 45128 ... 25382 22518 33551]
    ...
    [ 3202 41665 61119 ... 39932  1434 63416]
    [50703 38996 15982 ... 54247 12595 57058]
    [45883 45332 33952 ...  4653 53129 33860]]]]



 [[[[ 5365 22647 20223 ... 19301 30652 22719]
    [58028 60284 23523 ... 58639 63689 51624]
    [ 6527 16683 25805 ... 52448 18976 36153]
    ...
    [64683 54883 20426 ... 27153 64760 30131]
    [49929 53717 50427 ... 35937 61721 41215]
    [ 7321   321 11361 ... 17717 47481 58581]]

   [[63942 23386 50157 ... 21589 41261 54044]
    [29528 48149 23685 ...  1849 31547 48801]
    [58831  4965 25378 ... 53418 61620  6047]
    ...
    [54349 46662 59037 ... 13040 30124 35444]
    [14524  9229 34287 ... 11979 26286 37669]
    [45463 11532 23110 ... 27926 58276 63862]]

   [[ 8259 34054 42091 ... 59939 22108 28357]
    [22919  8495  8111 ... 21731 44806 23627]
    [41104 46285 22176 ... 23332 46020 56150]
    ...
    [60955 34498 30173 ... 20432 60705 35455]
    [ 1158 35118   934 ... 14557 42827 39377]
    [63673 50010 64525 ... 58882 16245 35669]]]


  [[[57373 61340  3834 ... 37405 28593 15011]
    [41965 53401 24863 ... 48638 53573 51409]
    [41317 18954 13023 ... 32148  3854 18622]
    ...
    [52930 58276  6863 ... 47509 36142  8597]
    [40494 38035 13319 ... 24806  7239  3978]
    [50573 22139 40801 ...  9121 52273 32557]]

   [[27526 33268 16097 ... 23588 11749 65141]
    [65021 65522   643 ... 51380 30582 54839]
    [48353 60818 23536 ... 48006 20412 39983]
    ...
    [29976 49256 30945 ...  4413 12935 21060]
    [57939 62936 55800 ...  3686 51510  4443]
    [43504 29686  2353 ... 36313 23487 55875]]

   [[18407 22483 51549 ...  8389 39242 62652]
    [ 4931 32580 18140 ... 44124 23284 28699]
    [35344 43538 12997 ... 17939 30509 61756]
    ...
    [59264 19021 61413 ... 13397 10689  6870]
    [15443 32003 49796 ... 13055 49573 43151]
    [60124 20278 42605 ... 43301 56690 23048]]]]



 [[[[34654 19708 24990 ... 30796 42060 38779]
    [18659 49788 45125 ... 47483 12023 62005]
    [37217 14947 61255 ... 49280 48307 25849]
    ...
    [ 6877 48627 65096 ... 36841  4318 26083]
    [  494 38522 57173 ... 44629 61741 29445]
    [42512 12001 50625 ... 16784  5311 65169]]

   [[47764  6526 51244 ... 37817 10289 49852]
    [41979 21696 24181 ... 31521  2191 11652]
    [47875 11987 19827 ... 26285 33953 28755]
    ...
    [39229 58963  4322 ... 48978  3629 19439]
    [56876 46006 41256 ... 49086 12364 36268]
    [29866 48154 28783 ... 28534 45597 44058]]

   [[ 5423 20678 51198 ... 47313 54167 42765]
    [21001 58829 59996 ... 46372   234 25158]
    [42631 50304 48988 ...  3726 41291 19933]
    ...
    [ 4131 41511 11200 ... 12188 50277 36078]
    [38885 16283 18452 ... 64546  1348 62994]
    [34632 28454 21934 ... 20248 17263  2903]]]


  [[[44601 43542 19043 ... 51941 57088 25581]
    [25463 12812 47984 ... 53970  6488 64433]
    [42421 32272 48267 ... 15416 51642  5321]
    ...
    [ 5320 62346 41481 ... 47659 42427 19494]
    [ 2605 14965 17399 ... 35119 52050 26102]
    [27239 34392  2546 ... 47285 13416 17684]]

   [[64785 17897 16933 ... 45326 30491 57498]
    [15939 33809 47576 ... 26371 28047 41762]
    [ 5517 53410 23402 ... 20341 52763 14771]
    ...
    [46693 30918  5594 ...    94 34366 55455]
    [47333 58812 21291 ... 53953 61635 63142]
    [29421 59931 51433 ... 55193 38102 10224]]

   [[23997 19474 62104 ... 24551 22939 18882]
    [ 5466 15638 42000 ... 12895  4675 34397]
    [20243  9487 60469 ...  6392 36819  4528]
    ...
    [ 4214 22889 57027 ... 25710 27732  2852]
    [10624 55633  5415 ... 43546 36404 30560]
    [61515 26730 11907 ... 42224 32294 50080]]]]



 [[[[61718 59696 34990 ... 52980 32028 24276]
    [43558 39438  2319 ... 13857 41257 37432]
    [24063 36362 57171 ... 60121  6003 39305]
    ...
    [20039 61431 60345 ... 46905 16772 62607]
    [26070   226 60827 ... 17715 56405  9297]
    [44476 34226 33769 ...  9108 64721 17629]]

   [[26647  5261 30623 ...  4252  9213 55429]
    [ 5802 48325 56572 ... 63079 45248 27390]
    [31269 16881 16184 ... 34792  4744 57513]
    ...
    [62098 45642 50025 ... 46778 26536 51020]
    [62266 51327 29947 ... 64171 51408 38248]
    [30198 19294 53449 ... 34606 53311 40450]]

   [[ 9536 39511   158 ... 10062 37576 23411]
    [63130 42131 59349 ... 12608 20651   299]
    [15880 54354 57100 ...  4970 54097 33006]
    ...
    [56242  9346 45285 ... 60565 28486 65313]
    [24193  7913 51157 ... 25623 65531 48838]
    [ 1171 21026 29817 ... 45778 53878 57962]]]


  [[[48033 58116 21934 ... 62471 38211 63901]
    [49603 32412 65073 ... 44564  1690 58131]
    [18328 61552 19295 ...  6399 26140 18197]
    ...
    [47886 49065 50312 ... 35072 41850 36716]
    [37374 42607 56850 ...  2501 11059 46609]
    [53220 34554 25701 ...  7000 44384 45167]]

   [[56716   786 10447 ...   731 36484 26817]
    [53088 65363 12028 ... 11489 15898  7727]
    [10782 40654 29837 ... 58510 13064 59227]
    ...
    [35198 53110 30710 ... 49228 49250 17224]
    [63160 34400 19590 ...  8366  1907 50410]
    [ 7429  4488  7822 ... 24504 16198  7126]]

   [[33251 47891 40838 ... 34445 17814 63867]
    [63964 55284 22523 ...  6743 15040 30164]
    [ 4325 63536   371 ... 32779 54563  2829]
    ...
    [26273 30724 14105 ... 36983 23578 24699]
    [31502 29670 35265 ...  9115 13201 36738]
    [ 3772  2719 22899 ... 23864 45929 48066]]]]]
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()