Skip to content

Commit

Permalink
simplify as well with bioio
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Oct 23, 2024
1 parent 14ad61b commit 84fa2a6
Showing 1 changed file with 10 additions and 28 deletions.
38 changes: 10 additions & 28 deletions src/depiction/persistence/format_ome_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from pathlib import Path
from typing import Any

import ome_types
import tifffile
import xarray
from bioio import BioImage
from bioio.writers import OmeTiffWriter
from bioio_base.types import PhysicalPixelSizes

Expand Down Expand Up @@ -34,34 +33,17 @@ def write(cls, image: xarray.DataArray, path: Path) -> None:

@classmethod
def read(cls, path: Path) -> xarray.DataArray:
file = tifffile.TiffFile(path)
if not file.is_ome:
raise ValueError("File is not an OME-TIFF file")

metadata_xml = file.ome_metadata
ome_data = ome_types.from_xml(metadata_xml)

if len(ome_data.images) != 1:
raise ValueError("Only single image OME-TIFF files are supported")

image_data = ome_data.images[0]
np_array = file.asarray(squeeze=False).T[0]
array = (
xarray.DataArray(
np_array,
dims=[char.lower() for char in image_data.pixels.dimension_order.value],
coords={"c": [channel.name for channel in image_data.pixels.channels]},
)
.squeeze(["z", "t"])
.transpose("c", "y", "x")
image = BioImage(path)
data = xarray.DataArray(
image.data,
dims=[d.lower() for d in image.dims.order],
coords={"c": image.channel_names},
)

array.attrs["pixel_size"] = PixelSize(
size_x=image_data.pixels.physical_size_x,
size_y=image_data.pixels.physical_size_y,
unit="micrometer",
data = data.squeeze(["t", "z"])
data.attrs["pixel_size"] = PixelSize(
size_x=image.physical_pixel_sizes.X, size_y=image.physical_pixel_sizes.Y, unit="micrometer"
)
return array
return data

@staticmethod
def _get_image_resolution_metadata(pixel_size: PixelSize) -> dict[str, Any]:
Expand Down

0 comments on commit 84fa2a6

Please sign in to comment.