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