apply_threshold.py 1.78 KB
Newer Older
André Anjos's avatar
André Anjos committed
1
2
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Wed 28 Sep 2016 17:55:17 CEST


"""Applies a threshold to score file and reports error rates

Usage: %(prog)s [-v...] [options] <threshold> <scores>
       %(prog)s --help
       %(prog)s --version


Arguments:
  <threshold>  The threshold value to apply (float)
  <scores>     Path to the file containing the scores where to apply the
               threshold and calculate error rates

Options:
  -h, --help                       Shows this help message and exits
  -V, --version                    Prints the version and exits
  -v, --verbose                    Increases the output verbosity level
André Anjos's avatar
André Anjos committed
22
23
24
25


Examples:

26
27
28
  Applies the threshold of 0.5 to the scores file in scores.txt and reports:

     $ %(prog)s 0.5 scores.txt
André Anjos's avatar
André Anjos committed
29
30
31

"""

32

33
34
import os
import sys
35

Amir Mohammadi's avatar
lint    
Amir Mohammadi committed
36
from .eval_threshold import apthres
37
38
import bob.core
logger = bob.core.log.setup("bob.measure")
39

André Anjos's avatar
André Anjos committed
40
41
42

def main(user_input=None):

43
44
45
46
47
48
49
50
51
52
  if user_input is not None:
    argv = user_input
  else:
    argv = sys.argv[1:]

  import docopt
  import pkg_resources

  completions = dict(
      prog=os.path.basename(sys.argv[0]),
Amir Mohammadi's avatar
lint    
Amir Mohammadi committed
53
      version=pkg_resources.require('bob.measure')[0].version)
54
55
56
57

  args = docopt.docopt(
      __doc__ % completions,
      argv=argv,
Amir Mohammadi's avatar
lint    
Amir Mohammadi committed
58
      version=completions['version'],)
59

60
  # Sets-up logging
61
62
  verbosity = int(args['--verbose'])
  bob.core.log.set_verbosity_level(logger, verbosity)
63

64
65
66
  # handles threshold validation
  try:
    args['<threshold>'] = float(args['<threshold>'])
Amir Mohammadi's avatar
lint    
Amir Mohammadi committed
67
68
69
  except Exception:
    raise docopt.DocoptExit("cannot convert %s into float for threshold" %
                            args['<threshold>'])
70
71
72

  from ..load import load_score, get_negatives_positives
  neg, pos = get_negatives_positives(load_score(args['<scores>']))
André Anjos's avatar
André Anjos committed
73

74
  apthres(neg, pos, args['<threshold>'])
André Anjos's avatar
André Anjos committed
75
76

  return 0