Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metadata view #16

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ install_requires =
magicgui
qtpy
iohub
pandas
openpyxl

python_requires = >=3.10
include_package_data = True
Expand Down
42 changes: 40 additions & 2 deletions src/napari_iohub/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
open_ome_zarr,
)
from pydantic.color import Color
import pandas as pd

if TYPE_CHECKING:
from _typeshed import StrOrBytesPath
Expand Down Expand Up @@ -246,7 +247,22 @@ def plate_to_layers(
plate: Plate,
row_range: tuple[int, int] = None,
col_range: tuple[int, int] = None,
metadata_df: pd.DataFrame = None,
meta_list: list = None,
):
"""
Convert a Plate object to a list of layers for visualization in napari.

Args:
plate (Plate): The Plate object to convert.
row_range (tuple[int, int], optional): The range of rows to include. Defaults to None.
col_range (tuple[int, int], optional): The range of columns to include. Defaults to None.
metadata_df (pd.DataFrame, optional): The metadata DataFrame. Defaults to None.
meta_list (list, optional): The list of metadata. Defaults to None.

Returns:
list: A list of layers for visualization in napari.
"""
plate_arrays = []
rows = plate.metadata.rows
if row_range:
Expand All @@ -265,6 +281,7 @@ def plate_to_layers(
well_path = f"{row_name}/{col_name}"
if well_path in well_paths:
well = plate[row_name][col_name]
# Stack the well images by position
layers_kwargs, ch_axis, arrays = stack_well_by_position(well)
row_arrays.append([a[0] for a in arrays])
height, width = arrays[0][0].shape[-2:]
Expand All @@ -274,51 +291,72 @@ def plate_to_layers(
height * (i + 1),
width * (j + 1),
]
# Calculate the bounding box extents for each well
for k in range(len(boxes)):
boxes[k].append(box_extents[k] - 0.5)
# Add the well path and position to the properties dictionary
well_id = well_path + "/" + next(well.positions())[0]
meta_value = ""

# FIXME: Figure out parsing of metadata values from supplied keys.
# meta_value = metadata_df.loc[
# metadata_df["Well ID"] == row_name + col_name, meta_list
# ].values[0]
# meta_value = "\n".join(meta_value)

properties["fov"].append(
well_path + "/" + next(well.positions())[0]
well_id + "/" + next(well.positions())[0] + meta_value
)
else:
row_arrays.append(None)
plate_arrays.append(row_arrays)

# Get the shape and dtype of the first non-empty block
first_blocks = next(a for a in plate_arrays[0] if a is not None)
fill_args = [(b.shape, b.dtype) for b in first_blocks]

plate_levels = []
for level, _ in enumerate(first_blocks):
plate_level = []
for r in plate_arrays:
row_level = []
for c in r:
if c is None:
# Create an empty block with the same shape and dtype
arr = da.zeros(
shape=fill_args[level][0], dtype=fill_args[level][1]
)
else:
arr = c[level]
row_level.append(arr)
plate_level.append(row_level)
# Block the row levels to create the plate level
plate_levels.append(da.block(plate_level))

# Create layers from the plate levels
layers = layers_from_arrays(
layers_kwargs,
ch_axis,
plate_levels,
mode="stitch",
layer_type="image",
)

# Add a plate map layer with bounding boxes and properties
layers.append(
[
make_bbox(boxes),
{
"face_color": "transparent",
"edge_color": "black",
"properties": properties,
"text": {"string": "fov", "color": "orange"},
"text": {"string": "fov", "color": "white"},
"name": "Plate Map",
},
"shapes",
]
)

return layers


Expand Down
Loading
Loading