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 

4import importlib 

5import logging 

6import os 

7 

8import click 

9import pkg_resources 

10 

11from bob.extension import rc 

12from bob.extension.scripts.click_helper import AliasedGroup, verbosity_option 

13 

14logger = logging.getLogger(__name__) 

15 

16 

17def _get_supported_datasets(): 

18 """Returns a list of supported dataset names""" 

19 

20 basedir = pkg_resources.resource_filename(__name__, "") 

21 basedir = os.path.join(os.path.dirname(basedir), "data") 

22 

23 retval = [] 

24 for k in os.listdir(basedir): 

25 candidate = os.path.join(basedir, k) 

26 if os.path.isdir(candidate) and "__init__.py" in os.listdir(candidate): 

27 retval.append(k) 

28 return retval 

29 

30 

31def _get_installed_datasets(): 

32 """Returns a list of installed datasets as regular expressions 

33 

34 * group(0): the name of the key for the dataset directory 

35 * group("name"): the short name for the dataset 

36 

37 """ 

38 

39 import re 

40 

41 dataset_re = re.compile(r"^bob\.ip\.binseg\.(?P<name>[^\.]+)\.datadir$") 

42 return [dataset_re.match(k) for k in rc.keys() if dataset_re.match(k)] 

43 

44 

45@click.group(cls=AliasedGroup) 

46def dataset(): 

47 """Commands for listing and verifying datasets""" 

48 pass 

49 

50 

51@dataset.command( 

52 epilog="""Examples: 

53 

54\b 

55 1. To install a dataset, set up its data directory ("datadir"). For 

56 example, to setup access to DRIVE files you downloaded locally at 

57 the directory "/path/to/drive/files", do the following: 

58\b 

59 $ bob config set "bob.ip.binseg.drive.datadir" "/path/to/drive/files" 

60 

61 Notice this setting **is** case-sensitive. 

62 

63 2. List all raw datasets supported (and configured): 

64 

65 $ bob binseg dataset list 

66 

67""", 

68) 

69@verbosity_option() 

70def list(**kwargs): 

71 """Lists all supported and configured datasets""" 

72 

73 supported = _get_supported_datasets() 

74 installed = _get_installed_datasets() 

75 installed = dict((k.group("name"), k.group(0)) for k in installed) 

76 

77 click.echo("Supported datasets:") 

78 for k in supported: 

79 if k in installed: 

80 click.echo(f'- {k}: {installed[k]} = "{rc.get(installed[k])}"') 

81 else: 

82 click.echo(f"* {k}: bob.ip.binseg.{k}.datadir (not set)") 

83 

84 

85@dataset.command( 

86 epilog="""Examples: 

87 

88 1. Check if all files of the DRIVE dataset can be loaded: 

89 

90 $ bob binseg dataset check -vv drive 

91 

92 2. Check if all files of multiple installed datasets can be loaded: 

93 

94 $ bob binseg dataset check -vv drive stare 

95 

96 3. Check if all files of all installed datasets can be loaded: 

97 

98 $ bob binseg dataset check 

99""", 

100) 

101@click.argument( 

102 "dataset", 

103 nargs=-1, 

104) 

105@click.option( 

106 "--limit", 

107 "-l", 

108 help="Limit check to the first N samples in each dataset, making the " 

109 "check sensibly faster. Set it to zero to check everything.", 

110 required=True, 

111 type=click.IntRange(0), 

112 default=0, 

113) 

114@verbosity_option() 

115def check(dataset, limit, **kwargs): 

116 """Checks file access on one or more datasets""" 

117 

118 to_check = _get_installed_datasets() 

119 

120 if dataset: # check only some 

121 to_check = [k for k in to_check if k.group("name") in dataset] 

122 

123 if not to_check: 

124 click.echo("No configured datasets matching specifications") 

125 click.echo( 

126 "Try bob binseg dataset list --help to get help in " 

127 "configuring a dataset" 

128 ) 

129 else: 

130 errors = 0 

131 for k in to_check: 

132 click.echo(f"Checking \"{k.group('name')}\" dataset...") 

133 module = importlib.import_module( 

134 f"...data.{k.group('name')}", __name__ 

135 ) 

136 errors += module.dataset.check(limit) 

137 if not errors: 

138 click.echo("No errors reported")