1#!/usr/bin/env python
2# coding=utf-8
3
4
5"""Tests for CHASE-DB1"""
6
7import numpy
8import pytest
9
10from ...binseg.data.chasedb1 import dataset
11from .utils import count_bw
12
13
14def test_protocol_consistency():
15
16 subset = dataset.subsets("first-annotator")
17 assert len(subset) == 2
18
19 assert "train" in subset
20 assert len(subset["train"]) == 8
21 for s in subset["train"]:
22 assert s.key.startswith("Image_")
23
24 assert "test" in subset
25 assert len(subset["test"]) == 20
26 for s in subset["test"]:
27 assert s.key.startswith("Image_")
28
29 subset = dataset.subsets("second-annotator")
30 assert len(subset) == 2
31
32 assert "train" in subset
33 assert len(subset["train"]) == 8
34 for s in subset["train"]:
35 assert s.key.startswith("Image_")
36
37 assert "test" in subset
38 assert len(subset["test"]) == 20
39 for s in subset["test"]:
40 assert s.key.startswith("Image_")
41
42
43@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.chasedb1.datadir")
44def test_loading():
45
46 image_size = (999, 960)
47
48 def _check_sample(s, bw_threshold_label, bw_threshold_mask):
49
50 data = s.data
51 assert isinstance(data, dict)
52 assert len(data) == 3
53
54 assert "data" in data
55 assert data["data"].size == image_size
56 assert data["data"].mode == "RGB"
57
58 assert "label" in data
59 assert data["label"].size == image_size
60 assert data["label"].mode == "1"
61 b, w = count_bw(data["label"])
62 assert (b + w) == numpy.prod(image_size), (
63 f"Counts of black + white ({b}+{w}) do not add up to total "
64 f"image size ({numpy.prod(image_size)}) at '{s.key}':label"
65 )
66 assert (w / b) < bw_threshold_label, (
67 f"The proportion between black and white pixels "
68 f"({w}/{b}={w/b:.2f}) is larger than the allowed threshold "
69 f"of {bw_threshold_label} at '{s.key}':label - this could "
70 f"indicate a loading problem!"
71 )
72
73 assert "mask" in data
74
75 assert data["mask"].size == image_size
76 assert data["mask"].mode == "1"
77 bm, wm = count_bw(data["mask"])
78 assert (bm + wm) == numpy.prod(image_size), (
79 f"Counts of black + white ({bm}+{wm}) do not add up to total "
80 f"image size ({numpy.prod(image_size)}) at '{s.key}':mask"
81 )
82 assert (wm / bm) > bw_threshold_mask, (
83 f"The proportion between black and white pixels in masks "
84 f"({wm}/{bm}={wm/bm:.2f}) is smaller than the allowed "
85 f"threshold of {bw_threshold_mask} at '{s.key}':label - "
86 f"this could indicate a loading problem!"
87 )
88 # print (f"{s.key}: {wm/bm} > {bw_threshold_mask}? {(wm/bm)>bw_threshold_mask}")
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.
93 # from ..data.utils import overlayed_image
94 # display = overlayed_image(data["data"], data["label"], data["mask"])
95 # display.show()
96 # import ipdb; ipdb.set_trace()
97
98 return w / b, wm / bm
99
100 limit = None # use this to limit testing to first images only
101 subset = dataset.subsets("first-annotator")
102 proportions = [
103 _check_sample(s, 0.08, 1.87) for s in subset["train"][:limit]
104 ]
105 # print(f"max label proportions = {max(proportions)}")
106 proportions = [_check_sample(s, 0.10, 1.87) for s in subset["test"][:limit]]
107 # print(f"max label proportions = {max(proportions)}")
108
109 subset = dataset.subsets("second-annotator")
110 proportions = [
111 _check_sample(s, 0.09, 1.87) for s in subset["train"][:limit]
112 ]
113 # print(f"max label proportions = {max(proportions)}")
114 proportions = [_check_sample(s, 0.09, 1.87) for s in subset["test"][:limit]]
115 # print(f"max label proportions = {max(proportions)}")
116 del proportions # only to satisfy flake8
117
118
119@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.chasedb1.datadir")
120def test_check():
121 assert dataset.check() == 0