Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ziw-liu committed Feb 11, 2023
1 parent a4a83ae commit 6657abb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 47 deletions.
64 changes: 23 additions & 41 deletions examples/hcs_zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,55 @@
# and adds an extra well-position to an existing dataset.
# It can be run as a plain Python script, or as interactive cells in some IDEs.

import os

import numpy as np

from iohub.ngff import HCSZarr
from iohub.ngff import open_ome_zarr

import os
# %%
# Set storage path

store_path = f'{os.path.expanduser("~/")}hcs.zarr'

# %%
# Write 5D data to multiple wells
# Write 5D data to multiple wells.
# While the NGFF specification allows for arbitrary names,
# the ome-zarr-py library (thus the napari-ome-zarr plugin)
# only load positions and arrays with name '0'.

position_list = (
("A", "1", "0"),
("H", 10, 0),
("Control", "Blank", 0),
)

file_path = f'{os.path.expanduser("~/")}hcs.zarr'

with HCSZarr.open(
file_path, mode="a", channel_names=["DAPI", "GFP", "Brightfield"]
) as dataset:
for row, col, fov in position_list:
position = dataset.create_position(row, col, fov)
position["raw"] = np.random.randint(
0, np.iinfo(np.uint16).max, size=(5, 3, 2, 32, 32), dtype=np.uint16
)
# "raw" is the custom name for the array in the OME-Zarr file.
# The default name is "0".
# Dragging above hcs.zarr into napari-ome-zarr doesn't load the array,
# since the plugin expects the array name to default to "0" .
# The following zarr will open with each position shown as a tile:

file_path = f'{os.path.expanduser("~/")}hcs_default.zarr'

with HCSZarr.open(
file_path, mode="a", channel_names=["DAPI", "GFP", "Brightfield"]
with open_ome_zarr(
store_path,
layout="hcs",
mode="a",
channel_names=["DAPI", "GFP", "Brightfield"],
) as dataset:
# create and write to positions
for row, col, fov in position_list:
position = dataset.create_position(row, col, fov)
position["0"] = np.random.randint(
0, np.iinfo(np.uint16).max, size=(5, 3, 2, 32, 32), dtype=np.uint16
)

# Examine the shape, size, dtype, and chunks of the array at a position.
print(f'Array 0 @ Well A1, FOV 0:\n'
f'shape:{dataset["A"]["1"]["0"]["0"].shape}\n'
f'size:{dataset["A"]["1"]["0"]["0"].size}\n'
f'dtype:{dataset["A"]["1"]["0"]["0"].dtype}\n'
f'chunks:{dataset["A"]["1"]["0"]["0"].chunks}\n')
# print dataset summary
dataset.print_tree()

# %%
# Append a channel to all the positions

file_path = f'{os.path.expanduser("~/")}hcs_default.zarr'

with HCSZarr.open(file_path, mode="r+") as dataset:
with open_ome_zarr(store_path, mode="r+") as dataset:
for name, position in dataset.positions():
print(name)
print(f"Appending a channel to position: {name}")
position.append_channel("Segmentation", resize_arrays=True)
position['0'][:, 3] = np.random.randint(
position["0"][:, 3] = np.random.randint(
0, np.iinfo(np.uint16).max, size=(5, 2, 32, 32), dtype=np.uint16
)

print(f'Array 0 @ Well A1, FOV 0:\n'
f'shape:{dataset["A"]["1"]["0"]["0"].shape}\n'
f'size:{dataset["A"]["1"]["0"]["0"].size}\n'
f'dtype:{dataset["A"]["1"]["0"]["0"].dtype}\n'
f'chunks:{dataset["A"]["1"]["0"]["0"].chunks}\n')
dataset.print_tree()

# %%
# Try viewing the images with napari-ome-zarr
13 changes: 7 additions & 6 deletions examples/ome_zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import numpy as np

from iohub.ngff import OMEZarrFOV
from iohub.ngff import open_ome_zarr

# %%
# Write 5D data to a new Zarr store
Expand All @@ -17,16 +17,17 @@
0, np.iinfo(np.uint16).max, size=(5, 2, 3, 32, 32), dtype=np.uint16
)

with OMEZarrFOV.open(
"ome.zarr", mode="a", channel_names=["DAPI", "GFP"]
with open_ome_zarr(
"ome.zarr", layout="fov", mode="a", channel_names=["DAPI", "GFP"]
) as dataset:
dataset["0"] = tczyx

# %%
# Opening in read-only mode prevents writing

with OMEZarrFOV.open("ome.zarr", mode="r") as dataset:
with open_ome_zarr("ome.zarr", layout="auto", mode="r") as dataset:
img = dataset["0"]
print(img)
print(img.numpy())
try:
img[0, 0, 0, 0, 0] = 0
Expand All @@ -40,7 +41,7 @@
0, np.iinfo(np.uint16).max, size=(1, 2, 3, 32, 32), dtype=np.uint16
)

with OMEZarrFOV.open("ome.zarr", mode="r+") as dataset:
with open_ome_zarr("ome.zarr", layout="fov", mode="r+") as dataset:
img = dataset["0"]
print(img.shape)
img.append(new_1czyx, axis=0)
Expand All @@ -53,7 +54,7 @@
0, np.iinfo(np.uint16).max, size=(3, 32, 32), dtype=np.uint16
)

dataset = OMEZarrFOV.open("ome.zarr", mode="r+")
dataset = open_ome_zarr("ome.zarr", mode="r+")
dataset.append_channel("New", resize_arrays=True)
dataset["0"][0, 2] = new_zyx
dataset.close()
Expand Down

0 comments on commit 6657abb

Please sign in to comment.