Skip to content

Commit

Permalink
enh: support .ome.tif files (no stacks supported yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed May 16, 2024
1 parent 20e12a8 commit 2922b56
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0.4.7
- enh: support .ome.tif files (no stacks supported yet)
0.4.6
- maintenance release
0.4.5
Expand Down
4 changes: 3 additions & 1 deletion impose/formats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .fmt_fl_zeiss import load_czi
from .fmt_bm_bmlab import load_h5
from .fmt_bf_generic import load_img
from .fmt_fl_ometif import load_ometif


def load(path):
Expand Down Expand Up @@ -72,7 +73,8 @@ def get_signature(path):

suffix_dict = {
".czi": load_czi,
".png": load_img,
".h5": load_h5,
".jpg": load_img,
".png": load_img,
".tif": load_ometif,
}
46 changes: 46 additions & 0 deletions impose/formats/fmt_fl_ometif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from collections import OrderedDict
from xml.dom.minidom import parseString

import numpy as np
import tifffile


def load_ometif(path):
"""Load .ome.tif files
Please see :func:`impose.formats.load` for more information.
"""
with tifffile.TiffFile(path) as tif:
if not tif.is_ome:
raise NotImplementedError(
f"Could not load ome_metadata from file {path}. Please ask "
f"for implementing support for your file format.")
ome_meta = parseString(tif.ome_metadata)
data = tif.asarray()

channels = OrderedDict()
name = ome_meta.getElementsByTagName("Image")[0].attributes["Name"].value
channels[name] = data

# Initial Metadata
px = ome_meta.getElementsByTagName("Pixels")[0]
xs = float(px.attributes["PhysicalSizeX"].value)
ys = float(px.attributes["PhysicalSizeY"].value)

# sanity checks
assert px.attributes["PhysicalSizeXUnit"].value == "µm"
assert px.attributes["PhysicalSizeYUnit"].value == "µm"

# Channel names
data_uuid = ome_meta.getElementsByTagName(
"UUID")[0].childNodes[0].data.split(":")[-1]

meta = {
"pixel size x": xs,
"pixel size y": ys,
"pixel size z": np.nan,
"shape": data.shape,
"signature": data_uuid,
}

return channels, meta
Binary file added tests/data/fmt_fl_ometif.zip
Binary file not shown.
8 changes: 0 additions & 8 deletions tests/test_fmt_czi.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,3 @@ def test_load_czi():
assert data["mCher"][10, 10, 0] == 1
assert data["mCher"][205, 227, 0] == 255
assert data["mCher"][113, 206, 0] == 123


if __name__ == "__main__":
# Run all tests
loc = locals()
for key in list(loc.keys()):
if key.startswith("test_") and hasattr(loc[key], "__call__"):
loc[key]()
34 changes: 34 additions & 0 deletions tests/test_fmt_fl_ometif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pathlib
import tempfile
import zipfile

import numpy as np

from impose import formats


data_path = pathlib.Path(__file__).parent / "data"


def get_ometif():
td = pathlib.Path(tempfile.mkdtemp(prefix="impose_ometif_"))
with zipfile.ZipFile(data_path / "fmt_fl_ometif.zip") as arc:
arc.extractall(path=td)
return list(td.glob("*.ome.tif"))[0]


def test_load_ometif():
path = get_ometif()
data, meta = formats.load(path)

# check metadata
assert np.allclose(meta["pixel size x"], 0.2136)
assert np.allclose(meta["pixel size y"], 0.2136)
assert np.isnan(meta["pixel size z"])
assert meta["shape"] == (512, 512)
assert meta["signature"] == "a704111f-8e8d-40b0-9f55-4d649ef49167"
chan = "Brillouin-2_MMStack_Default"
assert data[chan].shape == (512, 512)
assert data[chan][10, 10] == 246
assert data[chan][205, 227] == 217
assert data[chan][113, 206] == 280

0 comments on commit 2922b56

Please sign in to comment.