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