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_jsrt.py: 52%

46 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 Japanese Society of Radiological Technology""" 

6 

7import numpy 

8import pytest 

9 

10from ...binseg.data.jsrt import dataset 

11from .utils import count_bw 

12 

13 

14def test_protocol_consistency(): 

15 

16 subset = dataset.subsets("default") 

17 assert len(subset) == 3 

18 

19 assert "train" in subset 

20 assert len(subset["train"]) == 172 

21 for s in subset["train"]: 

22 assert s.key.startswith("All247images") 

23 

24 assert "validation" in subset 

25 assert len(subset["validation"]) == 25 

26 for s in subset["validation"]: 

27 assert s.key.startswith("All247images") 

28 

29 assert "test" in subset 

30 assert len(subset["test"]) == 50 

31 for s in subset["test"]: 

32 assert s.key.startswith("All247images") 

33 

34 

35@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.jsrt.datadir") 

36def test_loading(): 

37 

38 image_size = (1024, 1024) 

39 

40 def _check_sample(s, bw_threshold_label): 

41 

42 data = s.data 

43 assert isinstance(data, dict) 

44 assert len(data) == 2 

45 

46 assert "data" in data 

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

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

49 

50 assert "label" in data 

51 assert data["label"].size == image_size 

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

53 

54 b, w = count_bw(data["label"]) 

55 assert (b + w) == numpy.prod(image_size), ( 

56 f"Counts of black + white ({b}+{w}) do not add up to total " 

57 f"image size ({numpy.prod(image_size)}) at '{s.key}':label" 

58 ) 

59 assert (w / b) < bw_threshold_label, ( 

60 f"The proportion between black and white pixels " 

61 f"({w}/{b}={w/b:.3f}) is larger than the allowed threshold " 

62 f"of {bw_threshold_label} at '{s.key}':label - this could " 

63 f"indicate a loading problem!" 

64 ) 

65 

66 # to visualize images, uncomment the folowing code it should display an 

67 # image with a faded background representing the original data, blended 

68 # with green labels. 

69 # from ..data.utils import overlayed_image 

70 # display = overlayed_image(data["data"], data["label"]) 

71 # display.show() 

72 # import ipdb; ipdb.set_trace() 

73 

74 return w / b 

75 

76 limit = None # use this to limit testing to first images only 

77 subset = dataset.subsets("default") 

78 proportions = [_check_sample(s, 0.85) for s in subset["train"][:limit]] 

79 proportions = [_check_sample(s, 0.85) for s in subset["validation"][:limit]] 

80 proportions = [_check_sample(s, 0.85) for s in subset["test"][:limit]] 

81 del proportions # only to satisfy flake8 

82 

83 

84@pytest.mark.skip_if_rc_var_not_set("bob.ip.binseg.jsrt.datadir") 

85def test_check(): 

86 assert dataset.check() == 0