Skip to content

Commit

Permalink
Pass metadata (#464)
Browse files Browse the repository at this point in the history
* pass plate metadata through reconstruction if it exists

* simple test

* bug fix

* use zattrs.update

Co-authored-by: Ziwen Liu <[email protected]>

* add all metadata then remove `plate`

Co-authored-by: Ziwen Liu <[email protected]>

---------

Co-authored-by: Ziwen Liu <[email protected]>
  • Loading branch information
talonchandler and ziw-liu authored Dec 20, 2023
1 parent b9ccc93 commit 57f611e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
15 changes: 15 additions & 0 deletions recOrder/cli/apply_inverse_transfer_function.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import itertools
import warnings
from functools import partial
from pathlib import Path

Expand Down Expand Up @@ -34,6 +35,19 @@ def _check_background_consistency(background_shape, data_shape):


def get_reconstruction_output_metadata(position_path: Path, config_path: Path):
# Get non-OME-Zarr plate-level metadata if it's available
plate_metadata = {}
try:
input_plate = open_ome_zarr(
position_path.parent.parent.parent, mode="r"
)
plate_metadata = dict(input_plate.zattrs)
plate_metadata.pop("plate")
except RuntimeError:
warnings.warn(
"Position is not part of a plate...no plate metadata will be copied."
)

# Load the first position to infer dataset information
input_dataset = open_ome_zarr(str(position_path), mode="r")
T, _, Z, Y, X = input_dataset.data.shape
Expand Down Expand Up @@ -76,6 +90,7 @@ def get_reconstruction_output_metadata(position_path: Path, config_path: Path):
"scale": input_dataset.scale,
"channel_names": channel_names,
"dtype": np.float32,
"plate_metadata": plate_metadata,
}


Expand Down
5 changes: 5 additions & 0 deletions recOrder/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def create_empty_hcs_zarr(
scale: Tuple[float],
channel_names: list[str],
dtype: DTypeLike,
plate_metadata: dict = {},
) -> None:
"""If the plate does not exist, create an empty zarr plate.
Expand All @@ -36,13 +37,17 @@ def create_empty_hcs_zarr(
channel_names : list[str]
Channel names, will append if not present in metadata.
dtype : DTypeLike
plate_metadata : dict
"""

# Create plate
output_plate = open_ome_zarr(
str(store_path), layout="hcs", mode="a", channel_names=channel_names
)

# Pass metadata
output_plate.zattrs.update(plate_metadata)

# Create positions
for position_key in position_keys:
position_key_string = "/".join(position_key)
Expand Down
11 changes: 10 additions & 1 deletion recOrder/tests/util_tests/test_create_empty.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ def test_create_empty_hcs_zarr():
scale = (1, 1, 1, 0.5, 0.5)
channel_names = ["Channel1", "Channel2"]
dtype = np.uint16
plate_metadata = {"test": 2}

create_empty_hcs_zarr(
store_path, position_keys, shape, chunks, scale, channel_names, dtype
store_path,
position_keys,
shape,
chunks,
scale,
channel_names,
dtype,
plate_metadata,
)

# Verify existence of positions and channels
with open_ome_zarr(store_path, mode="r") as plate:
assert plate.zattrs["test"] == 2
for position_key in position_keys:
position = plate["/".join(position_key)]
assert isinstance(position, Position)
Expand Down

0 comments on commit 57f611e

Please sign in to comment.