Skip to content

Commit

Permalink
Custom removal of macro/label in OME metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsitsi committed Sep 18, 2024
1 parent a1b8852 commit 57718b6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
7 changes: 7 additions & 0 deletions tiledb/bioimg/converters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
iter_levels_meta,
iter_pixel_depths_meta,
open_bioimg,
remove_ome_image_metadata,
resolve_path,
validate_ingestion,
)
Expand Down Expand Up @@ -526,6 +527,12 @@ def to_tiledb(

if not exclude_metadata:
original_metadata = reader.original_metadata
else:
if ome_xml := reader.original_metadata.get("ome_metadata"):
pruned_metadata = remove_ome_image_metadata(ome_xml)
original_metadata = (
{"ome_metadata": pruned_metadata} if pruned_metadata else {}
)

with rw_group:
rw_group.w_group.meta.update(
Expand Down
7 changes: 6 additions & 1 deletion tiledb/bioimg/converters/ome_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
from tiledb.highlevel import _get_ctx

from .. import ATTR_NAME, EXPORT_TILE_SIZE, WHITE_RGBA
from ..helpers import get_decimal_from_rgba, get_logger_wrapper, get_rgba, iter_color
from ..helpers import (
get_decimal_from_rgba,
get_logger_wrapper,
get_rgba,
iter_color,
)
from .axes import Axes
from .base import ImageConverter, ImageReader, ImageWriter
from .io import as_array
Expand Down
27 changes: 27 additions & 0 deletions tiledb/bioimg/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
import sys
import xml.etree.ElementTree as ET
from pathlib import Path
from typing import (
Any,
Expand All @@ -14,6 +15,7 @@
Optional,
Sequence,
Tuple,
Union,
)
from urllib.parse import urlparse

Expand Down Expand Up @@ -456,3 +458,28 @@ def merge_ned_ranges(
merged_ranges_per_axis = [merge_ranges(ranges) for ranges in ranges_per_axis]

return tuple(merged_ranges_per_axis)


def remove_ome_image_metadata(xml_string: str) -> Union[str, Any]:

if not xml_string.lstrip().startswith("<OME") or not xml_string:
return None

# Parse the XML string
root = ET.fromstring(xml_string)

# Extract the namespace from the root element's tag
namespace = root.tag.split("}")[0].strip("{") # Extract namespace
ns = {"ome": namespace}

# Find all images
images = root.findall("ome:Image", ns)

# Iterate over images and remove those with Name 'macro' or 'label'
for image in images:
name = image.attrib.get("Name")
if name in ["macro", "label"]:
root.remove(image)

# Return the modified XML as a string
return ET.tostring(root, encoding="unicode")

0 comments on commit 57718b6

Please sign in to comment.