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/tmpuugvsylz/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>
[[[[[37723 24589 12970 ...  9538 57196 35461]
    [13382 14120  6773 ... 30921 27913  2881]
    [31849  7656 45252 ... 38076 32808 51686]
    ...
    [ 3186 38459 18694 ...  6724 36944 43810]
    [17858 30086 27520 ... 57559 62267  1177]
    [36312 14065  2281 ... 24971 60859 56255]]

   [[39255 29040 61651 ... 45467 45251 27511]
    [28294 51517 64037 ... 30146  3718  9413]
    [43462 47483 14872 ... 61762 60776 48348]
    ...
    [45470 49832 23081 ... 58463 23109 37257]
    [38801 29771 35417 ... 57469   253 34043]
    [20752 13463 22774 ... 17725 16217 31979]]

   [[36622   674  8576 ...  2425 39659  3300]
    [47568 44055 12424 ... 60568 10757 10149]
    [39224 63426  8279 ...  3759 20676 20416]
    ...
    [ 7980 20638 23653 ... 29428 63799 23127]
    [11215  6644  5431 ... 48211 49556 24276]
    [11909 64089 43798 ...  8821 16543 39977]]]


  [[[ 2272  2003  6973 ... 47360 12108 19300]
    [46627 52275 48186 ... 19514 37053 41160]
    [33086 11603 60937 ... 21296  1644 15293]
    ...
    [13864  1949  1697 ... 64958  5605  3620]
    [14493 45046  2260 ... 41800 10613 23623]
    [53037 58233 37620 ... 44773 19356  3522]]

   [[50872  3938 35116 ... 31338 64050 26556]
    [11414 61760  3536 ...   188 52988 12307]
    [17683 10384 43291 ...  4908 11971 58068]
    ...
    [26467 50842 45666 ... 33723 32806 23565]
    [41117 63825 40627 ... 48444 39092 10277]
    [44787 43676 58634 ... 41985 16199 19578]]

   [[43822 42354 54753 ... 52387 53396 47130]
    [40780 30448 28166 ... 47937 31676 56395]
    [ 8131 37330 56938 ... 33617  3780 20166]
    ...
    [63165 42659 23915 ... 57935 28721 19461]
    [43461  8836 29057 ... 21724 51969 10852]
    [15485 17773 25905 ... 60247  3533 43084]]]]



 [[[[10555 15289 27407 ...  1380  5894 51823]
    [50861  5538 31542 ... 38016 61804 59281]
    [18383 15984 13340 ... 63589 55180  2002]
    ...
    [51038 61322 44790 ... 56127 16275 36220]
    [59213 42505 23661 ... 64401 35635  8616]
    [45718 63386 21867 ...  6814  8255  2756]]

   [[41096 36608 16941 ...  3499 55076 54295]
    [33278 49333 37659 ...  9510 64631 61327]
    [25262 30815 52613 ... 43354  5196 33246]
    ...
    [ 8766  8329 58380 ... 29173 36697 48009]
    [46561 43928 18931 ... 41111 21315  9750]
    [52983 39149 55318 ... 26655 28246 11710]]

   [[38654 45160  8359 ... 21171 56667 57921]
    [51821 35464 55598 ... 13409 53826  4307]
    [28560 45770 18352 ... 27221 59885 29513]
    ...
    [56182 50425 23867 ... 44007 11384 29583]
    [52828 16423 62180 ... 51247  2074 41846]
    [60605 18027 63852 ... 48351 32927  3475]]]


  [[[ 5128 35713 31699 ... 39849   571  1888]
    [34331 42636 12993 ... 24204 39272 62595]
    [60567 50458 39585 ... 53371 56557 60545]
    ...
    [36402 12748 19233 ... 48037 41250 16238]
    [ 9380  4023 28836 ... 55151 15092 37465]
    [57505 19985 33391 ... 41113 31265 27692]]

   [[26253 14438 15049 ... 60865 47164 54758]
    [ 8287 36371 41208 ... 50907 14108  6492]
    [44416 29084 46061 ...  2276 22867 54356]
    ...
    [30977  9686 53848 ...  7268  9068 13027]
    [55384 49551 63830 ... 25063 51458  5829]
    [30997 36081 38837 ...  1800 18982  8321]]

   [[ 8824 11402 62814 ... 53911 12029 47194]
    [40474 23249 42130 ...  2718 62999 54571]
    [32531 29017 57061 ...  1291 33337 27551]
    ...
    [64936 41846 43009 ... 42789 14833 19631]
    [16719  9408 12779 ... 11574 44910 13151]
    [26618  4888 15720 ... 62621 11173 19830]]]]



 [[[[ 8285   280 61857 ... 52669 28568 11842]
    [31262 23044  7595 ... 27001 63571 59590]
    [31798 11555 59032 ... 44896 17743 10012]
    ...
    [27864 10676 29735 ... 41140   633 54016]
    [22332 33263 59606 ... 46433 31612  9177]
    [25969 49971  6854 ...  8761 54396 36857]]

   [[34797   386 43748 ... 40724 10066 60290]
    [59735 16107 14558 ... 46431 52830 40754]
    [60122 36263 33437 ... 61140  8454  8537]
    ...
    [33571 57043 27488 ... 27294 14917 20540]
    [51718 40978 18092 ... 35300  8204 57954]
    [38785  2105 42032 ... 38586 29128 45790]]

   [[31380 26202 57410 ... 42533 58449 47305]
    [55355 31658 50941 ...   177 39473 41142]
    [32734 39964 31659 ... 18445 55312  8784]
    ...
    [58411 13321 18545 ... 53615 19285  6840]
    [55789  2796 46067 ...  6696 36374  8658]
    [19487  6069  8360 ... 18793 60881 43465]]]


  [[[61472 23216 38171 ... 63789 12153 20139]
    [12457 39276 26639 ...  2754  6668 41623]
    [54999 36992 11470 ... 62829 44908 52255]
    ...
    [61064  8443 30410 ... 13789 52212 37480]
    [21105 64296 63349 ... 47881 37891 10523]
    [64497 22772 56800 ... 48057 41385  9777]]

   [[38064 10394 42287 ...  5195   399  1047]
    [ 1461 43898 47861 ... 33923 14722  1588]
    [ 7380 63023 21335 ... 19614 24341 53132]
    ...
    [52642 21010 63876 ... 32979 44090 30744]
    [39835 10943 54107 ... 58709  9535 61210]
    [51529 59365 26628 ... 36640 32025 15639]]

   [[62116  2293  9918 ... 61557 29309 20992]
    [36038 51179 44091 ... 24564  3594 34674]
    [20910 15570 42385 ... 35483 40692  1986]
    ...
    [ 2177  8864 29697 ... 54997 54060 43962]
    [22885 49619 46566 ... 15136 18956 14969]
    [36404 29788 34658 ... 64903  3899  6629]]]]



 [[[[22922 16520 19333 ... 27307  8380  7603]
    [30782 18543 14352 ... 44168 49528 51731]
    [23252 36832 20076 ... 47314 52717 11747]
    ...
    [38833 20028 43855 ... 17168 23073 57308]
    [48695 45753 58299 ... 59921 35286 21457]
    [ 3784  3426 13706 ...  2816 21324 31153]]

   [[41976 63239 45800 ... 30328 38696 47210]
    [64769 52362  6635 ... 25056 27543  8795]
    [24908 55643 21789 ... 10452 25260 65525]
    ...
    [ 4076 40175 45980 ...  5541 30164 57923]
    [60162  7362  3901 ... 60536  2992 10923]
    [30791 26947  5709 ... 53568   254 55862]]

   [[ 4443 51650 44937 ... 61955  1836  1138]
    [15560  4679 50235 ... 28909 31282 46066]
    [49808 61640 33013 ... 61721 55792 30315]
    ...
    [25773 27091 17441 ... 62050 38740 57887]
    [ 4716 14483 34626 ... 54147 23940  8299]
    [16320 34819 43493 ... 15599  1032 11792]]]


  [[[44029 46633 10538 ... 58036 32117 38776]
    [53803 56394 51417 ... 18068  7584 50957]
    [25744 13892 64713 ... 59916 50330 62554]
    ...
    [40981 52075 31532 ... 62872 26813 21053]
    [10340 62111 37370 ...   145 60520  2788]
    [ 8974  4942 26244 ... 35837 62078 29246]]

   [[ 8568 43706 46143 ... 24567 45914 11309]
    [31024 53264 49179 ... 56334  7362  6082]
    [ 9743   935 47251 ... 35985 39947  5593]
    ...
    [65252  9113  2513 ... 60150 48128 65020]
    [21048 30610 46575 ... 29624 43440 41518]
    [14683 61171 19228 ...  4095 36780  5740]]

   [[55647 30684 43501 ... 22250 59913 24760]
    [31446 59463 18493 ... 56882 19300 55421]
    [22989 55859 60138 ... 32091 31675 63877]
    ...
    [33853 31821 26147 ... 25833 13405 45534]
    [15801 28436 45007 ... 41117 11101 19114]
    [ 9715 22621 61670 ... 42549 17594 31418]]]]



 [[[[54326  4810   821 ... 16893  8824 40718]
    [53668 34542 41412 ... 25280 55862 46233]
    [20483 57652 20405 ... 42753 63750 48702]
    ...
    [45540  4029 50458 ...  6066 12680 51396]
    [21501 17885  8876 ... 57463 46430  4559]
    [63780 27849  4377 ... 60248 14532 21444]]

   [[48581 26666 27196 ... 50264  4741 48493]
    [47683 47484 31670 ... 59665 26634  2198]
    [16038 29585 16921 ... 41997 12415 36751]
    ...
    [59384 45147 44809 ... 37968 11710 42689]
    [47406 27961 37623 ... 44663 26689 31672]
    [23860 50346 26991 ... 58207 27142 37586]]

   [[51946 22349 23286 ... 14711 57513 56725]
    [45614 30196 53315 ... 65505 44344  9821]
    [ 2476  6709 33067 ... 29661 35108 16390]
    ...
    [ 4078 29398 29635 ... 13180 19573 56092]
    [62661 59930 37294 ...  9259 39554 51868]
    [62213 61851 25617 ... 25080 50957  5478]]]


  [[[15745 17100  5229 ...  7496  3354 35970]
    [35633 62145  3943 ... 57594 20319 38442]
    [47165 55484  9064 ... 51472 20641  9800]
    ...
    [28454 33967 23686 ... 24550 64269  5760]
    [24088  8837 53808 ... 25073 36523  5822]
    [53177 41150  5684 ... 26166 45243 39542]]

   [[13469 57833 23351 ...  3264  7075 35629]
    [37975 54907  6009 ... 58639 58449 29159]
    [37676 51381 25029 ... 37807 60443 38728]
    ...
    [57315 51770 15183 ... 36264  2786  6703]
    [25334 32650  9544 ...  9525  7329 24221]
    [12235 46224  2506 ... 18382 48634 64927]]

   [[16132 46206  3115 ... 20074 27523  1646]
    [51074 36114 17776 ...  2940 21198 18404]
    [57990 13835 36824 ... 24422 62041 57778]
    ...
    [10786 30836 56903 ... 64425 15594 32557]
    [29082 47502 33710 ... 32704 54394 16648]
    [47557  1512  5340 ...  7897 61955  9424]]]]]
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()