dvpio.read.image.read_metadata

Contents

dvpio.read.image.read_metadata#

dvpio.read.image.read_metadata(path, image_type, parse_metadata=True)#

Parse relevant microscopy metadata of dvp-io supported image file

Currently only supports czi files and openslide-compatible files.

Parameters:
  • path (str) – Path to image file

  • reader_type – One of the supported image data types (czi, openslide)

  • parse_metadata (bool (default: True)) – Whether to extract relevant metadata or return the raw metadata as json-style dictionary

Return type:

dict[str, Any]

Returns:

Metadata as dictionary

If parse_metadata is true, returns a dict with the following keys and associated values

  • image_type: str | None

    Name of original type: czi for Carl Zeiss, vendor name for openslide

  • objective_nominal_magnification: float | None

    Nominal magnification of objective, not considering additional optical setups

  • mpp_x: float | None

    Resolution in meters per pixel in x-dimension

  • mpp_x: float | None

    Resolution in meters per pixel in y-dimension

  • mpp_x: float | None

    Resolution in meters per pixel in z-dimension

  • channel_id: list[int] | None

    List of indices of microscopy channels

  • channel_names: list[str] | None

    List of channel names

Example

import spatialdata as sd
from dvpio.read.image import read_czi, parse_metadata

img_path = "./data/kabatnik2023_20211129_C1.czi"

# Initialize spatialdata
sdata = sd.SpatialData()

# Assign image
sdata.images["image"] = read_czi(img_path)

# Get controlled attributes from metadata
image_metadata = parse_metadata(img_path, image_type="czi", parse_metadata=True)
image_metadata
> {
    'channel_id': [0],
    'channel_names': ['TL Brightfield'],
    'image_type': 'czi',
    'mpp_x': 2.1999999999999998e-07,
    'mpp_y': 2.1999999999999998e-07,
    'mpp_z': 1.5e-06,
    'objective_nominal_magnification': 20.0
}

# Get the full metadata document
image_metadata = parse_metadata(img_path, image_type="czi", parse_metadata=False)
> {
'ImageDocument':
    {'Metadata':
        ...
        }
    ...
    }
...
}

# Assign it to spatialdata.SpatialData.attrs slot for future reference
# It is recommended to use the same name as the image
sdata.attrs["metadata"] = {
    "image": image_metadata
}

# Write
# sdata.write("/path/to/sdata.zarr")