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 DRHAGIS"""
7import numpy
8import pytest
10from ..data.drhagis import dataset
11from .utils import count_bw
14def test_protocol_consistency():
15 subset = dataset.subsets("default")
16 assert len(subset) == 2
18 assert "train" in subset
19 assert len(subset["train"]) == 19
20 for s in subset["train"]:
21 assert s.key.startswith("Fundus_Images")
23 assert "test" in subset
24 assert len(subset["test"]) == 20
25 for s in subset["test"]:
26 assert s.key.startswith("Fundus_Images")
29@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drhagis.datadir")
30def test_loading():
31 def _check_sample(s, bw_threshold_label, bw_threshold_mask):
33 data = s.data
34 assert isinstance(data, dict)
35 assert len(data) == 3
37 assert "data" in data
38 assert data["data"].mode == "RGB"
39 assert data["data"].size[0] >= 2816, (
40 f"Width ({data['data'].size[0]}) for {s.key} is smaller "
41 f"than 2816 pixels"
42 )
43 assert data["data"].size[1] >= 1880, (
44 f"Width ({data['data'].size[1]}) for {s.key} is smaller "
45 f"than 1880 pixels"
46 )
48 assert "label" in data
49 assert data["data"].size == data["label"].size
50 assert data["label"].mode == "1"
52 b, w = count_bw(data["label"])
53 assert (b + w) == numpy.prod(data["data"].size), (
54 f"Counts of black + white ({b}+{w}) do not add up to total "
55 f"image size ({numpy.prod(data['data'].size)}) at '{s.key}':label"
56 )
58 assert (w / b) < bw_threshold_label, (
59 f"The proportion between black and white pixels "
60 f"({w}/{b}={w/b:.3f}) is larger than the allowed threshold "
61 f"of {bw_threshold_label} at '{s.key}':label - this could "
62 f"indicate a loading problem!"
63 )
65 bm, wm = count_bw(data["mask"])
66 assert (bm + wm) == numpy.prod(data["data"].size), (
67 f"Counts of black + white ({bm}+{wm}) do not add up to total "
68 f"image size ({numpy.prod(data['data'].size)}) at '{s.key}':mask"
69 )
71 assert (wm / bm) > bw_threshold_mask, (
72 f"The proportion between black and white pixels in masks "
73 f"({wm}/{bm}={wm/bm:.2f}) is smaller than the allowed "
74 f"threshold of {bw_threshold_mask} at '{s.key}':label - "
75 f"this could indicate a loading problem!"
76 )
78 # to visualize images, uncomment the folowing code
79 # it should display an image with a faded background representing the
80 # original data, blended with green labels and blue area indicating the
81 # parts to be masked out.
82 # from ..data.utils import overlayed_image
83 # display = overlayed_image(data["data"], data["label"], data["mask"])
84 # display.show()
85 # import ipdb; ipdb.set_trace()
87 return w / b, wm / bm
89 limit = None # use this to limit testing to first images only
90 subset = dataset.subsets("default")
91 proportions = [_check_sample(s, 0.07, 0.8) for s in subset["train"][:limit]]
92 proportions = [_check_sample(s, 0.08, 0.8) for s in subset["test"][:limit]]
93 del proportions # only to satisfy flake8
96@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drhagis.datadir")
97def test_check():
98 assert dataset.check() == 0