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/tmptanjsjpp/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.ImageArray '/img' (5, 2, 3, 32, 32) uint16 read-only>
[[[[[57380 51115 41492 ... 42118 62594  6091]
    [26772 62839 59006 ... 19804 63151 44647]
    [14735  2493 35782 ... 60528 19365 34024]
    ...
    [ 7144 63825 19114 ... 42192 30157  9190]
    [49700 50772 39073 ... 57184 56739 15670]
    [26147 24482  3520 ... 64342 36546 22734]]

   [[55804 38343 40622 ... 63685 65146     9]
    [40379 20816 33059 ... 42396 27813 58288]
    [ 7750  3623 14323 ... 17538 10961 54892]
    ...
    [41279 18421 12338 ... 63934 17324 57434]
    [ 3892 45116 59479 ... 36398 51965  9737]
    [ 9448 36219 21234 ... 60273 43091 52934]]

   [[26733 48602 22609 ... 31091   712 35155]
    [20047 45143 42319 ... 18628 39696 43728]
    [18304 61022 41469 ... 64613 54071  9355]
    ...
    [59826 51923 43075 ... 28989 62778 63056]
    [55389 28663 24728 ... 59808 53030 24333]
    [ 2292 55774 42538 ... 23440 38675 60141]]]


  [[[12580 47578 55433 ... 34605 37507 15649]
    [63567 37132 63042 ... 21191  2866 22805]
    [49338 12166 53781 ... 54623 48899 22635]
    ...
    [17477 54674  5651 ... 28770 24863 12981]
    [25122   268 44863 ... 60309 40748 47909]
    [64589 19582 12195 ... 21501 29609  2098]]

   [[19659 50886 45709 ... 20397 43260  7461]
    [ 2769 39895 15243 ... 41669 34893 37367]
    [31381  4940 37641 ... 55036   705  6805]
    ...
    [39267 32729  8420 ... 16837 64053  4372]
    [42755 37173 48050 ...  1345 37541  8765]
    [56688 49883 38240 ... 28655 31019  6295]]

   [[51110 51772  2647 ... 26961 44358 15959]
    [32094  2047 43904 ...  2070  8591 18555]
    [41575 37426 11640 ... 45209 39088 11177]
    ...
    [39090 25231 32639 ... 55091 21917 24498]
    [38629 50835 58998 ...   511 50751 19353]
    [53906 55885 16042 ... 26034 16944 16776]]]]



 [[[[20983  6065  7418 ... 40914 39197 19228]
    [ 9675 37732  1678 ...  4607 35641 63688]
    [ 1091 44734 41098 ... 63124 32941 55488]
    ...
    [ 9500  3014 56992 ... 33738 40639 45110]
    [55413  1011 52422 ... 62102 43842 21109]
    [60006 13756 19171 ... 30434 23811 21009]]

   [[19634 44513 16327 ... 34654 10697  4088]
    [35127 33295 29366 ... 37201   884  3406]
    [57154 31855 11957 ... 49749 56909 21212]
    ...
    [44419 16978 24834 ... 36137 36139 31741]
    [55611 61941 50563 ... 43815 28465 61602]
    [10413 10367 21889 ... 18209  5786  1708]]

   [[ 8093   202  9359 ... 47898 48050 51138]
    [49046 35188 22981 ... 59490 11975  9188]
    [34827 49995 25426 ... 60926 33357 54996]
    ...
    [17133 27480 14502 ... 58816 15441 21408]
    [30627 43172  2008 ...  3198 23356 28586]
    [32968 39768 27720 ... 61996 26687 48779]]]


  [[[64403 42891   893 ... 25430 52448  7641]
    [64764 24384 54285 ... 42005 58954 34277]
    [51856 30697 54830 ... 23399 50060  2988]
    ...
    [37521  4229 31462 ...  8732 28135 63005]
    [31503 34319 49661 ... 24820 40766 58506]
    [17036 46359 22573 ... 42203 49814 19133]]

   [[17820 39437 47019 ... 45453 59489 42965]
    [54463 35606 38876 ... 63179 28515 50914]
    [46565 25471 48050 ... 36036 33903 20693]
    ...
    [37677 50936  6087 ... 41797 28933 21346]
    [39482  9158  4976 ...  4340 14574  2373]
    [25129 24663 53137 ... 39877  5566 14582]]

   [[15050 24834 55847 ...  3929 51076  9308]
    [23809 61994 33985 ... 15529 62011 59820]
    [37358 60806 53280 ... 36992 42674  5871]
    ...
    [31822 38783 14271 ... 37303 61317  7842]
    [10206 55618 36123 ... 21125  4759 55335]
    [65219  3337 11999 ...  4706  8851 45228]]]]



 [[[[27569 11097  8644 ... 45243 34878 29192]
    [ 1338 41706 58073 ...  6693 26148 28859]
    [51542  6338 64404 ... 55173 27815 32899]
    ...
    [ 8472 10244 29449 ... 31247 61916 24816]
    [41153 13370 38735 ... 29960 60557 44549]
    [60689  2723 41410 ... 18590 43792 62466]]

   [[43285  6706 30548 ... 59094 21265 32217]
    [16395 27463 49202 ... 60750 11548 33298]
    [26242 31629 41784 ... 49327 56143 63696]
    ...
    [62836 64969 45610 ... 29691  4528 10716]
    [37394  2608 30542 ...  7162 30015 28589]
    [25012 50949 42377 ...  6812 38040  1140]]

   [[19581 19924 29117 ...  4702 40897 49109]
    [37727 57354 29101 ... 49343 47582  4953]
    [54219 54941  2204 ...  3839 12938 23748]
    ...
    [32051   461 63147 ... 35401  9832 51058]
    [42637 13193  7392 ... 61819 14613 60571]
    [58803 12084 43033 ... 58083 53982 29774]]]


  [[[ 9418 50611 18885 ... 34095 20941 12387]
    [26086 61081 59588 ...  6929 64085 32512]
    [ 1895 24499 48899 ... 59473 57252  7833]
    ...
    [30057 42249 30724 ... 38975 55062 64522]
    [  872 38292  2706 ... 13937  1400  7629]
    [40937 60428 11880 ... 12045 18399 26557]]

   [[ 1966 40679  4633 ... 32999 54478 43237]
    [  812 39932 53905 ... 54902 60510 43271]
    [ 2214 61678 65344 ...  8567 15223 25497]
    ...
    [64152 57833 51134 ... 41857 16449 48514]
    [64659 39385 12932 ... 27996  5061 24824]
    [44150 22148 25272 ... 52896 11720 19713]]

   [[19385 14881 34605 ... 37864 18125 11592]
    [54339 59515 51103 ... 16688 64082 59728]
    [59073  8659 60252 ... 53545 10115  8771]
    ...
    [  736  1341 44384 ... 23410 65420 54079]
    [42903 55079  4335 ... 47738 32586 26068]
    [ 7589 37894 56939 ... 14528 14016 62410]]]]



 [[[[60792 10705 47025 ... 39495 22383 64653]
    [45175 62603 30656 ... 18225  9510 23965]
    [ 7522 61614 48221 ...    31 11572 27405]
    ...
    [34549 46684 58475 ... 50623 30444 27712]
    [24095  4739 48936 ...  9195 31523 53005]
    [34665 48221 59288 ... 50591 47738 43835]]

   [[49317 18540 13851 ... 34343 22744 27458]
    [19370 44523  8272 ... 48128  5415 16031]
    [46528 55764 48272 ...  6154  4794  4495]
    ...
    [45386  2290 28659 ... 59405  4197  1665]
    [ 5888 25630 37689 ... 23417 41582 25466]
    [62643 39759 20091 ... 16701 56029  7876]]

   [[44516 37825 10665 ... 30786 43116 43743]
    [14822 23663  9699 ... 13888 16818 37927]
    [65432 35329 51956 ... 19493 25074 22007]
    ...
    [15225 48779 14442 ... 63003 51334 16683]
    [11557 54414 28437 ...  4344 14160 30543]
    [ 8120 20967 57637 ... 46005 38603  9359]]]


  [[[22892 62889  8722 ... 16211  5106 19002]
    [22368  1162 61606 ... 16561 44458 48280]
    [32664 16543 43932 ... 15578 31704  7764]
    ...
    [34453 65149 63984 ... 50273  8940 16736]
    [27784 33310  7251 ... 50608   352 61031]
    [41408  3337 22190 ... 46515 19160 55981]]

   [[55644 61016 36925 ... 44172 29758 31851]
    [50641 30240 64579 ... 53613 31941 47766]
    [55436 58102 61282 ... 37746   908 11540]
    ...
    [48879 42968 60797 ... 58572 55596 45071]
    [64049 52081  8566 ... 35184 58774 44632]
    [45607  3367 34002 ... 60400  1888 30044]]

   [[64859 55868 17145 ...  9185 54870 43187]
    [ 5637  1409 51493 ... 51863  3416 54830]
    [ 7244 13275 38481 ... 17928 40083 63560]
    ...
    [16934 64521  8335 ... 39237 35649 58712]
    [59584 21386 39249 ... 22557 17625 56493]
    [17829 32744 47035 ... 28148 59932 19555]]]]



 [[[[62834 17664 14598 ... 32626 37919 59198]
    [45751 44544 33093 ... 44160 26474 12835]
    [ 7102  5962 14275 ... 60883 49860 25861]
    ...
    [48323  7394 55495 ... 31137 34486  9723]
    [26549 64938 34659 ... 50072 27722 59329]
    [ 4172 56630 57769 ... 60816 35085  1711]]

   [[58717 61730 58769 ... 13843  3710 10603]
    [26915 51822 47054 ... 39721 20910 10712]
    [43465 55520 39445 ... 37244 50737 12503]
    ...
    [38124  2046 30591 ... 51248 59597 28198]
    [24970 38755 50177 ... 65459 63950 17903]
    [44649 62370 52571 ... 26837  9796 26386]]

   [[24552 42397  2954 ... 24520   644  9125]
    [27684 11285 18268 ... 53481 17458 42861]
    [41453 58774 40312 ... 49860 34701 17455]
    ...
    [56755 50682 37159 ... 17188 60025 23221]
    [26208   606 35815 ... 18783 48037  2406]
    [ 4844  7953  5235 ... 60553 36231 48621]]]


  [[[56116 40954 33188 ... 43026 49876 28263]
    [ 4262 38101 63905 ... 55079  9632 30591]
    [53993 53916 29138 ... 45406  6322 22660]
    ...
    [28485 57070 29576 ... 52567 43193 22785]
    [53172 16563 63692 ... 13431  3891 55940]
    [22969 35422 35346 ... 52091 33559 65294]]

   [[24743 22810  5288 ... 28012 64237 27143]
    [ 3901 28958 30666 ... 22020  1723 21771]
    [61435 44278 26663 ... 37447 13768  8322]
    ...
    [34637 53094 27275 ... 16851 23040 48076]
    [24230  6012 46659 ... 48959 54607  1190]
    [37563 41825 43313 ... 57582 50493   539]]

   [[ 3859 12674 31723 ... 39427 55138  2551]
    [ 6980 61053 34186 ...  3849 44584 18085]
    [14951 32377 54290 ... 19101 47012  3119]
    ...
    [50371 50075 12419 ... 55962 61603 25809]
    [60234 31956 59638 ... 37271 46738 39343]
    [43241 49103 39509 ... 15601 33123 44724]]]]]
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()