-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtest_tiffreader.py
98 lines (87 loc) · 3.51 KB
/
test_tiffreader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Test TIFFWSIReader."""
from typing import Callable
import pytest
from defusedxml import ElementTree
from tiatoolbox.wsicore import wsireader
def test_ome_missing_instrument_ref(
monkeypatch: pytest.MonkeyPatch,
remote_sample: Callable,
) -> None:
"""Test that an OME-TIFF can be read without instrument reference."""
sample = remote_sample("ome-brightfield-small-level-0")
wsi = wsireader.TIFFWSIReader(sample)
page = wsi.tiff.pages[0]
description = page.description
tree = ElementTree.fromstring(description)
namespaces = {
"ome": "http://www.openmicroscopy.org/Schemas/OME/2016-06",
}
images = tree.findall("ome:Image", namespaces)
for image in images:
instruments = image.findall("ome:InstrumentRef", namespaces)
for instrument in instruments:
image.remove(instrument)
new_description = ElementTree.tostring(tree, encoding="unicode")
monkeypatch.setattr(page, "description", new_description)
monkeypatch.setattr(wsi, "_m_info", None)
assert wsi.info.objective_power is None
def test_ome_missing_physicalsize(
monkeypatch: pytest.MonkeyPatch,
remote_sample: Callable,
) -> None:
"""Test that an OME-TIFF can be read without physical size."""
sample = remote_sample("ome-brightfield-small-level-0")
wsi = wsireader.TIFFWSIReader(sample)
page = wsi.tiff.pages[0]
description = page.description
tree = ElementTree.fromstring(description)
namespaces = {
"ome": "http://www.openmicroscopy.org/Schemas/OME/2016-06",
}
images = tree.findall("ome:Image", namespaces)
for image in images:
pixels = image.find("ome:Pixels", namespaces)
del pixels.attrib["PhysicalSizeX"]
del pixels.attrib["PhysicalSizeY"]
new_description = ElementTree.tostring(tree, encoding="unicode")
monkeypatch.setattr(page, "description", new_description)
monkeypatch.setattr(wsi, "_m_info", None)
assert wsi.info.mpp is None
def test_ome_missing_physicalsizey(
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
remote_sample: Callable,
) -> None:
"""Test that an OME-TIFF can be read without physical size."""
sample = remote_sample("ome-brightfield-small-level-0")
wsi = wsireader.TIFFWSIReader(sample)
page = wsi.tiff.pages[0]
description = page.description
tree = ElementTree.fromstring(description)
namespaces = {
"ome": "http://www.openmicroscopy.org/Schemas/OME/2016-06",
}
images = tree.findall("ome:Image", namespaces)
for image in images:
pixels = image.find("ome:Pixels", namespaces)
del pixels.attrib["PhysicalSizeY"]
new_description = ElementTree.tostring(tree, encoding="unicode")
monkeypatch.setattr(page, "description", new_description)
monkeypatch.setattr(wsi, "_m_info", None)
assert pytest.approx(wsi.info.mpp, abs=0.1) == 0.5
assert "Only one MPP value found. Using it for both X and Y" in caplog.text
def test_tiffreader_non_tiled_metadata(
monkeypatch: pytest.MonkeyPatch,
remote_sample: Callable,
) -> None:
"""Test that fetching metadata for non-tiled TIFF works."""
sample = remote_sample("ome-brightfield-small-level-0")
wsi = wsireader.TIFFWSIReader(sample)
monkeypatch.setattr(wsi.tiff, "is_ome", False)
monkeypatch.setattr(
wsi.tiff.pages[0].__class__,
"is_tiled",
property(lambda _: False), # skipcq: PYL-W0612
)
monkeypatch.setattr(wsi, "_m_info", None)
assert pytest.approx(wsi.info.mpp, abs=0.1) == 0.5