Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
2# coding=utf-8
5"""Tests for STARE"""
7import os
9import numpy
11# special trick for CI builds
12from . import mock_dataset
13from .utils import count_bw
15datadir, dataset = mock_dataset()
18def test_protocol_consistency():
20 subset = dataset.subsets("ah")
21 assert len(subset) == 2
23 assert "train" in subset
24 assert len(subset["train"]) == 10
25 for s in subset["train"]:
26 assert s.key.startswith(os.path.join("stare-images", "im0"))
28 assert "test" in subset
29 assert len(subset["test"]) == 10
30 for s in subset["test"]:
31 assert s.key.startswith(os.path.join("stare-images", "im0"))
33 subset = dataset.subsets("vk")
34 assert len(subset) == 2
36 assert "train" in subset
37 assert len(subset["train"]) == 10
38 for s in subset["train"]:
39 assert s.key.startswith(os.path.join("stare-images", "im0"))
41 assert "test" in subset
42 assert len(subset["test"]) == 10
43 for s in subset["test"]:
44 assert s.key.startswith(os.path.join("stare-images", "im0"))
47def test_loading():
49 image_size = (700, 605)
51 def _check_sample(s, bw_threshold_label, bw_threshold_mask):
53 data = s.data
54 assert isinstance(data, dict)
55 assert len(data) == 3
57 assert "data" in data
58 assert data["data"].size == image_size
59 assert data["data"].mode == "RGB"
61 assert "label" in data
62 assert data["label"].size == image_size
63 assert data["label"].mode == "1"
64 b, w = count_bw(data["label"])
65 assert (b + w) == numpy.prod(image_size), (
66 f"Counts of black + white ({b}+{w}) do not add up to total "
67 f"image size ({numpy.prod(image_size)}) at '{s.key}':label"
68 )
69 assert (w / b) < bw_threshold_label, (
70 f"The proportion between black and white pixels "
71 f"({w}/{b}={w/b:.2f}) is larger than the allowed threshold "
72 f"of {bw_threshold_label} at '{s.key}':label - this could "
73 f"indicate a loading problem!"
74 )
76 assert "mask" in data
77 assert data["mask"].size == image_size
78 assert data["mask"].mode == "1"
79 bm, wm = count_bw(data["mask"])
80 assert (bm + wm) == numpy.prod(image_size), (
81 f"Counts of black + white ({bm}+{wm}) do not add up to total "
82 f"image size ({numpy.prod(image_size)}) at '{s.key}':mask"
83 )
84 assert (wm / bm) > bw_threshold_mask, (
85 f"The proportion between black and white pixels in masks "
86 f"({wm}/{bm}={wm/bm:.2f}) is smaller than the allowed "
87 f"threshold of {bw_threshold_mask} at '{s.key}':label - "
88 f"this could indicate a loading problem!"
89 )
90 # print (f"{s.key}: {wm/bm} > {bw_threshold_mask}? {(wm/bm)>bw_threshold_mask}")
92 # to visualize images, uncomment the folowing code
93 # it should display an image with a faded background representing the
94 # original data, blended with green labels.
95 # from ..data.utils import overlayed_image
96 # display = overlayed_image(data["data"], data["label"], data["mask"])
97 # display.show()
98 # import ipdb; ipdb.set_trace()
100 return w / b
102 limit = None # use this to limit testing to first images only
103 subset = dataset.subsets("ah")
104 proportions = [
105 _check_sample(s, 0.10, 2.67) for s in subset["train"][:limit]
106 ]
107 # print(f"max label proportions = {max(proportions)}")
108 proportions = [_check_sample(s, 0.12, 2.70) for s in subset["test"][:limit]]
109 # print(f"max label proportions = {max(proportions)}")
111 subset = dataset.subsets("vk")
112 # proportions = [_check_sample(s, 0.19, 2.67) for s in subset["train"][:limit]]
113 # print(f"max label proportions = {max(proportions)}")
114 # proportions = [_check_sample(s, 0.18, 2.70) for s in subset["test"][:limit]]
115 # print(f"max label proportions = {max(proportions)}")
116 del proportions # only to satisfy flake8
119def test_check():
120 assert dataset.check() == 0