1#!/usr/bin/env python
2# coding=utf-8
3
4
5"""Tests for REFUGE"""
6
7import numpy
8import pytest
9
10from ...binseg.data.refuge import dataset
11from .utils import count_bw
12
13
14def test_protocol_consistency():
15
16 for protocol in ("optic-disc", "optic-cup"):
17
18 subset = dataset.subsets(protocol)
19 assert len(subset) == 3
20
21 assert "train" in subset
22 assert len(subset["train"]) == 400
23 for s in subset["train"]:
24 assert s.key.startswith("Training400")
25
26 assert "validation" in subset
27 assert len(subset["validation"]) == 400
28 for s in subset["validation"]:
29 assert s.key.startswith("REFUGE-Validation400")
30
31 assert "test" in subset
32 assert len(subset["test"]) == 400
33 for s in subset["test"]:
34 assert s.key.startswith("Test400")
35
36
37@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.refuge.datadir")
38@pytest.mark.slow
39def test_loading():
40 def _check_sample(
41 s, image_size, glaucoma_label, entries, bw_threshold_label
42 ):
43
44 data = s.data
45 assert isinstance(data, dict)
46 assert len(data) == entries
47
48 assert "data" in data
49 assert data["data"].size == image_size
50 assert data["data"].mode == "RGB"
51
52 assert "label" in data
53 assert data["label"].size == image_size
54 assert data["label"].mode == "1"
55 b, w = count_bw(data["label"])
56 assert (b + w) == numpy.prod(image_size), (
57 f"Counts of black + white ({b}+{w}) do not add up to total "
58 f"image size ({numpy.prod(image_size)}) at '{s.key}':label"
59 )
60 assert (w / b) < bw_threshold_label, (
61 f"The proportion between black and white pixels "
62 f"({w}/{b}={w/b:.3f}) is larger than the allowed threshold "
63 f"of {bw_threshold_label} at '{s.key}':label - this could "
64 f"indicate a loading problem!"
65 )
66
67 assert "mask" in data
68 assert data["mask"].size == image_size
69 assert data["mask"].mode == "1"
70
71 if glaucoma_label:
72 assert "glaucoma" in data
73
74 # to visualize images, uncomment the folowing code
75 # it should display an image with a faded background representing the
76 # original data, blended with green labels.
77 # from ..data.utils import overlayed_image
78 # display = overlayed_image(data["data"], data["label"])
79 # display.show()
80 # import ipdb; ipdb.set_trace()
81
82 return w / b
83
84 limit = None # use this to limit testing to first images only
85 subset = dataset.subsets("optic-disc")
86 proportions = [
87 _check_sample(s, (2124, 2056), True, 4, 0.029)
88 for s in subset["train"][:limit]
89 ]
90 # print(f"max label proportions = {max(proportions)}")
91 proportions = [
92 _check_sample(s, (1634, 1634), False, 3, 0.043)
93 for s in subset["validation"][:limit]
94 ]
95 # print(f"max label proportions = {max(proportions)}")
96 proportions = [
97 _check_sample(s, (1634, 1634), True, 4, 0.026)
98 for s in subset["test"][:limit]
99 ]
100 # print(f"max label proportions = {max(proportions)}")
101
102 subset = dataset.subsets("optic-cup")
103 proportions = [
104 _check_sample(s, (2124, 2056), True, 4, 0.018)
105 for s in subset["train"][:limit]
106 ]
107 # print(f"max label proportions = {max(proportions)}")
108 proportions = [
109 _check_sample(s, (1634, 1634), False, 3, 0.030)
110 for s in subset["validation"][:limit]
111 ]
112 # print(f"max label proportions = {max(proportions)}")
113 proportions = [
114 _check_sample(s, (1634, 1634), True, 4, 0.017)
115 for s in subset["test"][:limit]
116 ]
117 # print(f"max label proportions = {max(proportions)}")
118 del proportions # only to satisfy flake8
119
120
121@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.refuge.datadir")
122@pytest.mark.slow
123def test_check():
124 assert dataset.check() == 0