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_stare.py: 100%

57 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 STARE""" 

6 

7import os 

8 

9import numpy 

10 

11# special trick for CI builds 

12from . import mock_dataset 

13from .utils import count_bw 

14 

15datadir, dataset = mock_dataset() 

16 

17 

18def test_protocol_consistency(): 

19 

20 subset = dataset.subsets("ah") 

21 assert len(subset) == 2 

22 

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

27 

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

32 

33 subset = dataset.subsets("vk") 

34 assert len(subset) == 2 

35 

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

40 

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

45 

46 

47def test_loading(): 

48 

49 image_size = (700, 605) 

50 

51 def _check_sample(s, bw_threshold_label, bw_threshold_mask): 

52 

53 data = s.data 

54 assert isinstance(data, dict) 

55 assert len(data) == 3 

56 

57 assert "data" in data 

58 assert data["data"].size == image_size 

59 assert data["data"].mode == "RGB" 

60 

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 ) 

75 

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}") 

91 

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

99 

100 return w / b 

101 

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)}") 

110 

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 

117 

118 

119def test_check(): 

120 assert dataset.check() == 0