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
4import importlib
5import logging
6import os
8import click
9import pkg_resources
11from bob.extension import rc
12from bob.extension.scripts.click_helper import AliasedGroup, verbosity_option
14logger = logging.getLogger(__name__)
17def _get_supported_datasets():
18 """Returns a list of supported dataset names"""
20 basedir = pkg_resources.resource_filename(__name__, "")
21 basedir = os.path.join(os.path.dirname(basedir), "data")
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
31def _get_installed_datasets():
32 """Returns a list of installed datasets as regular expressions
34 * group(0): the name of the key for the dataset directory
35 * group("name"): the short name for the dataset
37 """
39 import re
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)]
45@click.group(cls=AliasedGroup)
46def dataset():
47 """Commands for listing and verifying datasets"""
48 pass
51@dataset.command(
52 epilog="""Examples:
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"
61 Notice this setting **is** case-sensitive.
63 2. List all raw datasets supported (and configured):
65 $ bob binseg dataset list
67""",
68)
69@verbosity_option()
70def list(**kwargs):
71 """Lists all supported and configured datasets"""
73 supported = _get_supported_datasets()
74 installed = _get_installed_datasets()
75 installed = dict((k.group("name"), k.group(0)) for k in installed)
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)")
85@dataset.command(
86 epilog="""Examples:
88 1. Check if all files of the DRIVE dataset can be loaded:
90 $ bob binseg dataset check -vv drive
92 2. Check if all files of multiple installed datasets can be loaded:
94 $ bob binseg dataset check -vv drive stare
96 3. Check if all files of all installed datasets can be loaded:
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"""
118 to_check = _get_installed_datasets()
120 if dataset: # check only some
121 to_check = [k for k in to_check if k.group("name") in dataset]
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")