Hide keyboard shortcuts

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 

3 

4 

5"""Tests for Japanese Society of Radiological Technology""" 

6 

7import numpy 

8import pytest 

9 

10from ..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("JSRT") 

23 

24 assert "validation" in subset 

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

26 for s in subset["validation"]: 

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

28 

29 assert "test" in subset 

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

31 for s in subset["test"]: 

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

33 

34 

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

36def test_loading(): 

37 

38 image_size = (2048, 2048) 

39 label_size = (1024, 1024) 

40 

41 def _check_sample(s, bw_threshold_label): 

42 

43 data = s.data 

44 assert isinstance(data, dict) 

45 assert len(data) == 2 

46 

47 assert "data" in data 

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

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

50 

51 assert "label" in data 

52 assert data["label"].size == label_size 

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

54 

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

56 assert (b + w) == numpy.prod(label_size), ( 

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

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

59 ) 

60 assert (w / b) < bw_threshold_label, ( 

61 f"The proportion between black and white pixels " 

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

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

64 f"indicate a loading problem!" 

65 ) 

66 

67 return w / b 

68 

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

70 subset = dataset.subsets("default") 

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

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

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

74 del proportions # only to satisfy flake8 

75 

76 

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

78def test_check(): 

79 assert dataset.check() == 0