apply_threshold.py 1.77 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

36 37
import bob.core
logger = bob.core.log.setup("bob.measure")
38

39 40
from .eval_threshold import apthres

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

def main(user_input=None):

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  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]),
      version=pkg_resources.require('bob.measure')[0].version
      )

  args = docopt.docopt(
      __doc__ % completions,
      argv=argv,
      version=completions['version'],
      )

63
  # Sets-up logging
64 65
  verbosity = int(args['--verbose'])
  bob.core.log.set_verbosity_level(logger, verbosity)
66

67 68 69 70 71 72 73 74 75
  # handles threshold validation
  try:
    args['<threshold>'] = float(args['<threshold>'])
  except:
    raise docopt.DocoptExit("cannot convert %s into float for threshold" % \
        args['<threshold>'])

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

77
  apthres(neg, pos, args['<threshold>'])
André Anjos's avatar
André Anjos committed
78 79

  return 0