Coverage for /scratch/builds/bob/bob.ip.binseg/miniconda/conda-bld/bob.ip.binseg_1673966692152/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_p/lib/python3.10/site-packages/bob/ip/common/test/test_drhagis.py: 45%

44 statements  

« prev     ^ index     » next       coverage.py v7.0.5, created at 2023-01-17 15:03 +0000

1#!/usr/bin/env python 

2# coding=utf-8 

3 

4 

5"""Tests for DRHAGIS""" 

6 

7import numpy 

8import pytest 

9 

10from ...binseg.data.drhagis import dataset 

11from .utils import count_bw 

12 

13 

14def test_protocol_consistency(): 

15 subset = dataset.subsets("default") 

16 assert len(subset) == 2 

17 

18 assert "train" in subset 

19 assert len(subset["train"]) == 19 

20 for s in subset["train"]: 

21 assert s.key.startswith("Fundus_Images") 

22 

23 assert "test" in subset 

24 assert len(subset["test"]) == 20 

25 for s in subset["test"]: 

26 assert s.key.startswith("Fundus_Images") 

27 

28 

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): 

32 

33 data = s.data 

34 assert isinstance(data, dict) 

35 assert len(data) == 3 

36 

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 ) 

47 

48 assert "label" in data 

49 assert data["data"].size == data["label"].size 

50 assert data["label"].mode == "1" 

51 

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 ) 

57 

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 ) 

64 

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 ) 

70 

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 ) 

77 

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() 

86 

87 return w / b, wm / bm 

88 

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 

94 

95 

96@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.drhagis.datadir") 

97def test_check(): 

98 assert dataset.check() == 0