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/tmp9t82c9to/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/tmp9t82c9to/ome.zarr/img shape=(5, 2, 3, 32, 32) dtype=uint16>
[[[[[40945  4963 63523 ... 58384 41713 59315]
    [ 5165 52488  1620 ...  5339 20212 32536]
    [52769 42833 30440 ...  4938 60499 65201]
    ...
    [21235 48609   924 ...  9710  6491 34427]
    [38798 18982 53790 ... 36545 30411 39307]
    [23743 56270 18809 ...  5373 40351 58645]]

   [[10551  1701 48617 ... 29513  4540 34496]
    [ 2071 58778 34168 ... 47554 39690 15810]
    [17128 50122 58852 ... 27967 65049 63012]
    ...
    [48278 11060 23642 ...  4480  9122 18128]
    [14841 44672 42059 ... 12359   279 29326]
    [ 7377 56822  7173 ... 46957  1694 13798]]

   [[24185 43110 24460 ... 58506 34552 21493]
    [46098 36169 18126 ... 40112  7246 31113]
    [30959 63249 64571 ... 56429  7868 19416]
    ...
    [33236 61394 58206 ... 17370  8077 27219]
    [33443   810 21170 ...  6442 46025 54730]
    [ 6008 43089 13042 ... 17309 53290 30455]]]


  [[[52578 10034 60948 ... 17457 20374 11992]
    [43923  3429 64967 ... 36325 22147 13711]
    [  109  2625 60655 ...  5978 16224 22846]
    ...
    [36317  4133 19080 ... 23604 32488 20802]
    [11316 44028 29196 ... 18004 41188 42671]
    [15374 19104 27451 ... 32787 37040 35639]]

   [[47103  4430 59393 ... 33776 56597 54132]
    [54885 24511 51841 ... 37100 22486  4249]
    [22201  7841  6763 ... 42119 18246  3654]
    ...
    [64996 34303 40382 ... 57792 12220 63531]
    [55539 53267 16685 ... 35724 45259 52180]
    [16095 48746 32351 ... 42538 29059 61129]]

   [[32838 64516  6500 ... 21374 43934  5348]
    [28369 15356 37555 ... 10011 20838 34161]
    [59142 12202 10121 ... 52307  9835 25359]
    ...
    [58995 14038 65308 ...  8815 26885 30389]
    [49287 47388 10279 ... 29054 55302 34549]
    [14134 33263 13849 ... 53672 54251  3544]]]]



 [[[[25073 20039 35336 ... 28361 58861 15531]
    [30330 16435 57716 ... 49067 54827 32396]
    [40913 17818 20382 ... 26453 15618 38474]
    ...
    [33201 58208 48821 ... 34290 34692 49529]
    [49537 58117 16326 ... 27673 12322  1220]
    [45910 39278 59674 ... 10422 53394 50853]]

   [[12016 48390 28131 ... 52604  1113 60754]
    [47472 16190 16605 ... 51828 64023 11695]
    [61139 41523  9696 ... 57833  4197 24980]
    ...
    [16652 54573 23756 ... 63108 45825 17533]
    [ 6564 10577 17228 ... 54528  9038 61938]
    [24443  2672 45737 ... 20652  5316 49189]]

   [[ 7666 23474 39918 ... 22595 33571 31216]
    [21306 53955 31051 ... 29617 61081 58587]
    [55272  6068 49354 ... 49914 60112 61819]
    ...
    [16750 11758 40838 ... 29614 28158 14467]
    [36697 28658  3079 ...  7374 48116 41346]
    [ 7001 25601 49322 ...  2456 61440 21323]]]


  [[[10196 55513 10647 ... 16934 28457 32333]
    [28166 53428 47433 ... 48303 26658 56628]
    [ 1765 55661 40573 ... 11736 32986 52751]
    ...
    [63258 34626 65304 ... 25186 56423  3232]
    [43097 20091 25029 ...  9465 37983 47143]
    [47054  3061 20297 ... 51177 55366  6705]]

   [[36943 16078 22418 ...  5082 16705 49417]
    [ 2049 13866 38210 ... 34548 48404 62456]
    [16968 60313 37998 ... 33137  7600 55598]
    ...
    [55322 50978  6005 ...   305 54991  4953]
    [ 4295 47606 26470 ...  7601 46205  1058]
    [41979 27082 21637 ... 17617 58631 39574]]

   [[54657 42056 50121 ... 59057 14705 56445]
    [49264 14571 31630 ... 47619 17037  4707]
    [35416  6846 61930 ...  4553 58591 47784]
    ...
    [42797  8075  2379 ... 64892 22957 33160]
    [41038 40321 39468 ... 36371 47715 23530]
    [    1 30989 62365 ... 10393 23500  4408]]]]



 [[[[28795 41781 57684 ...  9828 48564 29501]
    [ 2781 17690 27847 ... 49513 51775 55567]
    [12780 10571 12314 ... 23126 44538 58538]
    ...
    [ 2553 43912   872 ... 59257 11919 42767]
    [36750 58651 12399 ... 15631  6445 11926]
    [63999 38564 19131 ... 21791 42276 35357]]

   [[47932 63952  9028 ... 55091 55087 12987]
    [32120  6147 31183 ...   991   129 39387]
    [20799 44731 21892 ... 29791 10360 41332]
    ...
    [31854 44739 11971 ... 19277 55320 65062]
    [63200 21909 12003 ... 14980 48456 32013]
    [55220  4543 57724 ... 50613  1850 62778]]

   [[61653 63804  7360 ... 61086 41267 11428]
    [38986 38684  8049 ... 47266 28770 40560]
    [41889 54705  7296 ... 63473 32555  9228]
    ...
    [62678 59613  4405 ... 54617 51781 24218]
    [31197 58352 54541 ... 63341 62737 13761]
    [ 6694 17016 61080 ... 64204 62472 20643]]]


  [[[48646 52080 39730 ... 32259  5452 26285]
    [28665 19996 13321 ... 60628 32315 52579]
    [25730 32003 27440 ... 26598 19915 11935]
    ...
    [64501 61150 48718 ... 29083 24602 46073]
    [38201 11622  1097 ... 20909 62416 37010]
    [32385 19523  4911 ... 48250  1810 38677]]

   [[47428 58822 52409 ... 31837 19044 46034]
    [ 5913 31407 26315 ... 54471 16376 16778]
    [60269 57308 23342 ...  1563 26721  6583]
    ...
    [32316 62176 50364 ... 32647 17257  1313]
    [40353 24163 12002 ... 42528 36703 61637]
    [50720  2711 59821 ...  5438 34668 45517]]

   [[59494  3352 26075 ... 12561  4559 35390]
    [10996 12928 40117 ... 40071 43255 46233]
    [50124 46122 29759 ...  5778  5786 44753]
    ...
    [31384 48555 25438 ...  4162 60179 33211]
    [ 3796 55447 42662 ... 21977 48853 55970]
    [29840 46320 17824 ... 58848 13589 36194]]]]



 [[[[37782 36610 46033 ... 57904 15220  7477]
    [63832 32704  7391 ... 10265 33185 50573]
    [20695 30072 20027 ... 58890 52776 10418]
    ...
    [64366 45850 30376 ... 57310 37064 27402]
    [13573 33028 45760 ... 19914 61844 45600]
    [ 9324 44404 64781 ... 63808 38477 20446]]

   [[63785   119  8402 ... 34739 17159  7743]
    [20264 56653 29107 ... 38791 29876  2427]
    [ 6070 60796 30769 ... 58362 34943 51485]
    ...
    [26925 34503 33183 ... 55688 15558 49756]
    [28098  5752  1088 ... 24877 35961  7099]
    [37453 38201 54532 ... 40668 45001 19067]]

   [[49709 53523 17015 ... 37843 38894 37460]
    [53372 58272 46933 ... 63155 52336 47516]
    [25316  5808 19648 ... 17747 43524  5137]
    ...
    [44819  5610 63226 ... 61944   236 20852]
    [58509 49565  2632 ... 44334  1189  9817]
    [41127 62977 59998 ... 60705  1575 23534]]]


  [[[   79  3558 32757 ... 57542 40138 64624]
    [ 3411 36369  3970 ... 28946  2108  9221]
    [14682 58116 50615 ...  2601 19852 61155]
    ...
    [ 5445  6890 52952 ... 37457 29901 49781]
    [56115 57073 10410 ... 39165 36483 32148]
    [58054 22485 15423 ...  6777 57129 55347]]

   [[26542  3311  1924 ... 10229 51399 32286]
    [64825 55520 55271 ... 47449 15882 30072]
    [22871  6385 23364 ...  8404 43179 37181]
    ...
    [49241 22637  2227 ... 62106 33979   666]
    [22922 26136 56584 ...  3095 29213 38180]
    [35914  1820 22928 ... 31423 33299 14491]]

   [[60130 51244 32530 ... 23759 13445 53541]
    [49882 13623 22236 ... 19590 22713  2914]
    [64221  5183 33301 ... 38571 16551 44512]
    ...
    [ 6278 48243  2343 ...  6768 22797 54730]
    [31039 41784 18714 ... 60296 47471   915]
    [ 7082 24165 42782 ... 35168  1232 35638]]]]



 [[[[20494 37188 65456 ... 25012 52388  5427]
    [52400 53616 22503 ... 34600 16442 51933]
    [40602 53397 59398 ...  7845 51431 60765]
    ...
    [53180 35140 21740 ... 39870 42590 59961]
    [ 7132 27409   475 ... 43002 43914 29649]
    [39207  5805 34447 ... 50919 42659 53091]]

   [[57934 37882 56082 ... 38065 38238 63315]
    [65466 60519 17759 ... 26399 20779 36632]
    [49592 48033 48010 ... 57154 61212 38446]
    ...
    [17070 20163  8324 ... 19887 30016 33740]
    [49229 16962 46249 ... 10403 44138 21317]
    [54789 32867 38247 ... 23521 11025 39674]]

   [[50805 37727 29362 ... 30717 63247 58308]
    [ 7040 52750 29486 ... 14496  4582 16857]
    [41094 25712 32710 ... 13798 21704 29114]
    ...
    [11680 63770 26797 ... 43843 60058 22007]
    [40208 13653 55416 ... 28263 26183 58262]
    [16365 56974 58152 ... 26297 38869 54328]]]


  [[[60012  8509 45586 ... 18332  5128 34896]
    [ 2882 48173 27399 ... 64299 18373 63945]
    [ 4611 18600 35337 ... 24293 17929 34014]
    ...
    [ 4346 26639  1328 ...  5849 59225 63349]
    [29008 40566 29189 ... 59770  3782 24860]
    [65239 53487 13392 ...  3166  1636 27674]]

   [[62834 43632 11326 ... 55720 28638   465]
    [11253 14965 18472 ... 63000  4548 35679]
    [ 9502 21452 48947 ... 49584 19340 37030]
    ...
    [25345  4003 52585 ... 59180 20173 58545]
    [65084 21615 58527 ... 22992 13689 34634]
    [19621 36183 11689 ... 14990 56252 46129]]

   [[61886 36463  3092 ... 46050 40604   540]
    [49909  6614  5092 ... 41574 53495 10045]
    [64082 63110 63733 ... 15108 59912 49800]
    ...
    [22650 53937 34828 ... 57433 33216 40770]
    [15117 33380 62221 ... 43962 33484 13411]
    [55441  1139 63140 ... 52546 31219 35685]]]]]
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()