Note
Go to the end to download the full example code.
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/tmpgfd_d9v1/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>
[[[[[11652 18669 39322 ... 55256 13016 13435]
[47630 62432 38040 ... 42967 23741 21135]
[10130 54346 46581 ... 42528 2198 36029]
...
[12870 43672 6333 ... 48167 46754 10879]
[38190 20324 35358 ... 39919 12344 52764]
[42028 56668 25237 ... 56233 20166 18038]]
[[12821 222 27107 ... 1627 26417 23446]
[22679 15313 8070 ... 4396 46447 30823]
[ 5909 58243 12319 ... 37412 5743 44847]
...
[22020 27013 23176 ... 47664 14420 31835]
[32782 40287 57249 ... 520 3621 30806]
[44768 26659 41237 ... 40029 52052 38922]]
[[43016 53134 54321 ... 47769 63780 35025]
[ 785 57473 10685 ... 29251 11275 36392]
[ 8457 50433 55445 ... 8753 56973 34776]
...
[ 7107 55823 31691 ... 7689 63785 10422]
[ 6461 59364 600 ... 1425 50310 15582]
[35938 31086 25800 ... 4507 19193 40101]]]
[[[ 5087 40935 48355 ... 53228 63295 49566]
[61721 31119 3232 ... 20226 64550 10882]
[45025 38301 64950 ... 153 254 44098]
...
[28166 44446 23327 ... 28907 55900 11511]
[12824 28590 44833 ... 34315 47588 51140]
[63102 21596 63436 ... 23589 24892 37380]]
[[62518 49297 52902 ... 56610 37500 13946]
[63926 10847 403 ... 17127 49531 55872]
[13695 8469 58224 ... 1998 58053 51825]
...
[53935 10807 10783 ... 4012 49761 24851]
[49040 17517 38046 ... 62624 18892 25923]
[29013 30203 52181 ... 51211 36515 62953]]
[[62451 27978 47071 ... 17970 44569 47235]
[33366 42914 43253 ... 54164 65304 55065]
[23282 62387 29231 ... 15665 15316 53817]
...
[11812 764 53661 ... 54760 34326 32335]
[16949 44991 41690 ... 47561 63354 45592]
[53448 25180 40040 ... 32806 21840 25612]]]]
[[[[43964 25575 21994 ... 32081 49471 22023]
[33658 19030 29111 ... 25292 27696 35863]
[15688 16089 5895 ... 22883 16882 19512]
...
[37701 2348 44578 ... 5757 48752 39378]
[49126 11096 53763 ... 36590 28304 18617]
[30865 11782 37290 ... 29115 550 64352]]
[[57701 40992 40900 ... 53237 21455 43424]
[25885 10947 46793 ... 17438 34109 46769]
[29667 58169 48310 ... 18798 3896 23745]
...
[ 6337 48829 47488 ... 41361 23094 48913]
[55985 16968 19355 ... 53832 45453 7603]
[30289 45848 33272 ... 10415 26596 43249]]
[[17645 49719 29660 ... 54470 40339 26972]
[59011 43555 50275 ... 23625 48014 195]
[30358 47921 59952 ... 48614 31733 47782]
...
[26839 23008 36750 ... 15074 50767 804]
[57379 43440 42383 ... 13993 48732 59187]
[49670 59691 28089 ... 21326 21638 37655]]]
[[[ 3152 54756 50996 ... 18403 65069 10069]
[40301 16226 13436 ... 36941 51955 13960]
[30237 55745 29518 ... 40311 49625 21450]
...
[49489 26427 42418 ... 12087 48163 33383]
[35853 7475 4668 ... 4489 38727 10804]
[17963 62632 40614 ... 43838 63454 13697]]
[[64287 22676 21518 ... 10436 62804 55278]
[57790 17380 40521 ... 18938 58531 65130]
[11084 10383 57550 ... 5184 39254 19447]
...
[65204 23624 13786 ... 24364 52036 13900]
[11634 61351 45870 ... 39303 50672 55597]
[24367 64795 65190 ... 4521 49177 18158]]
[[ 2825 2277 63459 ... 59603 21082 41617]
[ 1684 58 47729 ... 2746 10769 25141]
[15167 58949 64141 ... 29666 45912 40115]
...
[21180 13999 3949 ... 21009 12573 28828]
[ 8959 11827 59569 ... 63186 4218 39261]
[27635 32472 35690 ... 50695 62884 50802]]]]
[[[[37285 23616 45719 ... 39274 28685 43869]
[34152 63406 11216 ... 18233 16438 56521]
[48347 33767 22288 ... 6046 50245 11897]
...
[51996 55051 12375 ... 19660 60110 17243]
[12577 629 58083 ... 54131 25390 53239]
[11720 63522 4404 ... 25434 47603 20148]]
[[61628 8917 42582 ... 42939 47992 47899]
[15802 4016 13231 ... 24327 54947 27049]
[ 1597 39134 46591 ... 38188 19653 23277]
...
[ 6842 21641 24549 ... 29562 65523 2537]
[46132 6122 1067 ... 52793 30076 15528]
[24953 27111 25012 ... 42234 19983 4419]]
[[15359 65514 54180 ... 22914 48116 64656]
[52641 6758 558 ... 54821 9216 6367]
[25087 44464 57237 ... 58979 65157 26160]
...
[13561 45237 65239 ... 13742 27683 29194]
[19797 2552 50560 ... 11551 31954 30299]
[19892 23445 33798 ... 24117 41511 4545]]]
[[[42470 43751 18713 ... 57803 27496 41998]
[57070 44726 37100 ... 27254 48913 34696]
[58860 53598 45051 ... 27901 61349 51195]
...
[12144 22966 64987 ... 61362 57193 3325]
[33652 15223 56676 ... 28225 64092 14792]
[56516 31540 47344 ... 57694 56978 38593]]
[[10371 57217 22280 ... 22305 21624 29547]
[36596 65386 37893 ... 6951 7209 60724]
[11378 50075 35877 ... 36254 64876 16393]
...
[26002 48339 34677 ... 22426 54512 13904]
[50018 37351 65398 ... 8708 38445 1596]
[18488 53780 41851 ... 61030 61218 10694]]
[[44078 52315 60784 ... 46311 64548 34033]
[42205 45039 43624 ... 10711 49111 45214]
[ 5512 28342 56831 ... 43025 28529 11564]
...
[64363 26995 27908 ... 60705 62770 64605]
[51268 5170 1636 ... 9135 63657 40803]
[ 1249 57021 18990 ... 61828 62070 44514]]]]
[[[[ 7495 52823 50568 ... 54147 2391 53892]
[52935 44437 7209 ... 7331 29583 39208]
[58887 813 32180 ... 18963 24599 61950]
...
[14714 44069 39483 ... 22198 16868 9128]
[56013 59939 30024 ... 36277 44718 29242]
[13622 17571 51181 ... 34150 50347 6321]]
[[55641 23428 5952 ... 18640 33961 9764]
[62131 54581 12541 ... 15028 38432 27745]
[53618 61625 26835 ... 16371 64060 60173]
...
[30796 31914 58945 ... 51589 39950 62109]
[40255 61921 62409 ... 42845 40926 14370]
[48242 35054 41140 ... 36964 24111 30521]]
[[54899 64359 44355 ... 21098 31185 38943]
[20745 22498 27096 ... 50767 40679 53483]
[49083 13201 11383 ... 46166 51947 10753]
...
[31775 44411 58360 ... 41505 52054 2275]
[20995 17582 31691 ... 52708 6643 34060]
[63015 6689 6159 ... 21852 45814 52726]]]
[[[47912 55682 11306 ... 15943 5228 41565]
[56968 3683 18856 ... 2947 25319 24974]
[ 4767 8601 55917 ... 37867 15349 42230]
...
[49236 39787 48534 ... 49123 34513 48254]
[25390 10165 26 ... 33208 48748 61647]
[30947 5259 50529 ... 34397 29155 16213]]
[[21487 53430 31168 ... 60382 31720 23428]
[24999 62875 3837 ... 62099 53049 4277]
[21679 37139 57968 ... 42437 7432 21912]
...
[ 1957 62602 11851 ... 63146 3476 35333]
[ 3173 21116 50098 ... 5742 55338 7981]
[38785 52677 32608 ... 50266 6075 39089]]
[[13321 2229 14002 ... 62731 28597 21589]
[18183 40202 55265 ... 6387 44171 17345]
[45459 57738 63835 ... 774 7910 7004]
...
[ 9199 11828 36201 ... 48071 42388 50375]
[10995 13129 12931 ... 48844 31826 37428]
[30672 8121 58982 ... 48620 16933 52107]]]]
[[[[54577 57522 55308 ... 3022 20721 32090]
[62928 60405 25013 ... 48808 60041 8707]
[20915 18785 47207 ... 894 1753 53115]
...
[44273 9772 10484 ... 13762 7459 53443]
[21186 40819 7459 ... 23824 59409 98]
[17373 145 47379 ... 63657 34854 51139]]
[[17389 50052 6148 ... 7945 25921 6669]
[56890 31360 61417 ... 15604 3053 11139]
[57814 26794 34002 ... 23683 18146 7404]
...
[58012 24824 34476 ... 32993 13052 1590]
[ 7123 31180 27385 ... 33137 25371 22439]
[21587 63536 9996 ... 18369 17624 10439]]
[[47983 25160 37985 ... 31837 13324 59792]
[51896 6158 44343 ... 19544 23641 23760]
[17552 13360 7455 ... 18649 55369 46941]
...
[49231 59875 21168 ... 37255 65237 50195]
[45841 32095 65044 ... 49817 11777 31663]
[13172 26325 27283 ... 48103 2261 34057]]]
[[[48359 64649 61172 ... 53366 31488 54638]
[61007 23758 49798 ... 42614 10694 8492]
[19986 46311 15317 ... 32341 12190 12014]
...
[46075 57542 36750 ... 10235 52257 5149]
[45793 18884 27780 ... 14711 38212 63462]
[44464 38334 39186 ... 24477 31520 19835]]
[[50399 45853 56349 ... 13400 48912 35755]
[40686 30380 23283 ... 11510 13587 59253]
[51466 20117 10411 ... 63394 34620 57093]
...
[41309 42482 18392 ... 1802 59283 54909]
[56437 12084 1481 ... 54752 53209 18234]
[13913 49176 56617 ... 6775 19223 34615]]
[[13686 47458 44632 ... 19901 30344 33915]
[39372 61729 4129 ... 41007 35246 22912]
[30750 63938 44180 ... 34142 30585 11118]
...
[47160 5704 27838 ... 870 19615 50064]
[45201 53179 36876 ... 2017 39488 64433]
[ 4329 7345 2354 ... 48346 45352 52675]]]]]
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()