diff --git a/bob/bio/base/script/evaluate.py b/bob/bio/base/script/evaluate.py index 259579513eddab80ad888334329ba0e9873bf8e1..5c49eae6418deefcc39810a0d81d06ad792d222c 100644 --- a/bob/bio/base/script/evaluate.py +++ b/bob/bio/base/script/evaluate.py @@ -43,7 +43,7 @@ def command_line_arguments(command_line_parameters): parser.add_argument('-s', '--directory', default = '.', help = "A directory, where to find the --dev-files and the --eval-files") parser.add_argument('-c', '--criterion', choices = ('EER', 'HTER', 'FAR'), help = "If given, the threshold of the development set will be computed with this criterion.") - parser.add_argument('-f', '--far-value', type=float, default=0.1, help = "The FAR value in %% for which to evaluate (only for --criterion FAR)") + parser.add_argument('-f', '--far-value', type=float, default=0.001, help = "The FAR value for which to evaluate (only for --criterion FAR)") parser.add_argument('-x', '--cllr', action = 'store_true', help = "If given, Cllr and minCllr will be computed.") parser.add_argument('-m', '--mindcf', action = 'store_true', help = "If given, minDCF will be computed.") parser.add_argument('--cost', default=0.99, help='Cost for FAR in minDCF') @@ -58,6 +58,7 @@ def command_line_arguments(command_line_parameters): parser.add_argument('-C', '--cmc', help = "If given, CMC curves will be plotted into the given pdf file.") parser.add_argument('-E', '--epc', help = "If given, EPC curves will be plotted into the given pdf file. For this plot --eval-files is mandatory.") parser.add_argument('-M', '--min-far-value', type=float, default=1e-4, help = "Select the minimum FAR value used in ROC plots; should be a power of 10.") + parser.add_argument('-L', '--far-line-at', type=float, help = "If given, draw a veritcal line at this FAR value in the ROC plots.") parser.add_argument('--parser', default = '4column', choices = ('4column', '5column'), help="The style of the resulting score files. The default fits to the usual output of score files.") # add verbose option @@ -107,32 +108,35 @@ def _plot_roc(frrs, colors, labels, title, fontsize=18, position=None, farfrrs=N # plot FAR and CAR for each algorithm for i in range(len(frrs)): pyplot.semilogx([f for f in frrs[i][0]], [1. - f for f in frrs[i][1]], color=colors[i], lw=2, ms=10, mew=1.5, label=labels[i]) - if farfrrs is not None: - pyplot.plot(farfrrs[i][0]*100, (1-farfrrs[i][1])*100, 'o', color=colors[i], markeredgecolor='black') + if isinstance(farfrrs, list): + pyplot.plot(farfrrs[i][0], (1.-farfrrs[i][1]), 'o', color=colors[i], markeredgecolor='black') + # plot vertical bar, if desired if farfrrs is not None: - pyplot.plot([x[0]*100 for x in farfrrs], [(1-x[1])*100 for x in farfrrs], '--', color='black', linewidth=1.5) + if isinstance(farfrrs, float): + pyplot.plot([farfrrs,farfrrs],[0.,1.], "--", color='black') + else: + pyplot.plot([x[0] for x in farfrrs], [(1.-x[1]) for x in farfrrs], '--', color='black', linewidth=1.5) - # finalize plot - if farfrrs is None: - pyplot.plot([0.1,0.1],[0,100], "--", color='black') + # compute and apply tick marks min_far = frrs[0][0][0] ticks = [min_far] - while ticks[-1] < 1: ticks.append(ticks[-1] * 10) - pyplot.axis([min_far,1,0,1]) + while ticks[-1] < 1.: ticks.append(ticks[-1] * 10.) + pyplot.axis([min_far,1.,0.,1.]) pyplot.xticks(ticks) - pyplot.xlabel('FMR (%)') - pyplot.ylabel('1 - FNMR (%)') + + # set label, legend and title + pyplot.xlabel('FMR') + pyplot.ylabel('1 - FNMR') pyplot.grid(True, color=(0.6,0.6,0.6)) pyplot.legend(loc=position, prop = {'size':fontsize}) pyplot.title(title) - figure.set_tight_layout(True) return figure def _plot_det(dets, colors, labels, title, fontsize=18, position=None): - if position is None: position = 1 + if position is None: position = 'upper right' # open new page for current plot figure = pyplot.figure(figsize=(8.2,8)) @@ -144,43 +148,43 @@ def _plot_det(dets, colors, labels, title, fontsize=18, position=None): det_list = [0.0002, 0.001, 0.005, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 0.7, 0.9, 0.95] ticks = [bob.measure.ppndf(d) for d in det_list] labels = [("%.5f" % d).rstrip('0').rstrip('.') for d in det_list] - pyplot.xticks(ticks, labels) + pyplot.xticks(ticks, [l if i % 2 else "" for i,l in enumerate(labels)]) pyplot.yticks(ticks, labels) pyplot.axis((ticks[0], ticks[-1], ticks[0], ticks[-1])) - pyplot.xlabel('FMR (%)') - pyplot.ylabel('FNMR (%)') + pyplot.xlabel('FMR') + pyplot.ylabel('FNMR') pyplot.legend(loc=position, prop = {'size':fontsize}) pyplot.title(title) - figure.set_tight_layout(True) return figure + def _plot_cmc(cmcs, colors, labels, title, fontsize=18, position=None): - if position is None: position = 4 + if position is None: position = 'lower right' # open new page for current plot figure = pyplot.figure() - max_x = 0 - # plot the DET curves + max_R = 0 + # plot the CMC curves for i in range(len(cmcs)): - x = bob.measure.plot.cmc(cmcs[i], figure=figure, color=colors[i], lw=2, ms=10, mew=1.5, label=labels[i]) - max_x = max(x, max_x) + probs = bob.measure.cmc(cmcs[i]) + R = len(probs) + pyplot.semilogx(range(1, R+1), probs, figure=figure, color=colors[i], lw=2, ms=10, mew=1.5, label=labels[i]) + max_R = max(R, max_R) # change axes accordingly ticks = [int(t) for t in pyplot.xticks()[0]] pyplot.xlabel('Rank') - pyplot.ylabel('Probability (%)') + pyplot.ylabel('Probability') pyplot.xticks(ticks, [str(t) for t in ticks]) - pyplot.axis([0, max_x, 0, 100]) + pyplot.axis([0, max_R, 0., 1.]) pyplot.legend(loc=position, prop = {'size':fontsize}) pyplot.title(title) - figure.set_tight_layout(True) return figure - def _plot_epc(scores_dev, scores_eval, colors, labels, title, fontsize=18, position=None): if position is None: position = 'upper center' # open new page for current plot @@ -188,18 +192,18 @@ def _plot_epc(scores_dev, scores_eval, colors, labels, title, fontsize=18, posit # plot the DET curves for i in range(len(scores_dev)): - bob.measure.plot.epc(scores_dev[i][0], scores_dev[i][1], scores_eval[i][0], scores_eval[i][1], 100, color=colors[i], label=labels[i], lw=2) + x,y = bob.measure.epc(scores_dev[i][0], scores_dev[i][1], scores_eval[i][0], scores_eval[i][1], 100) + pyplot.plot(x, y, color=colors[i], label=labels[i], lw=2) # change axes accordingly pyplot.xlabel('alpha') - pyplot.ylabel('HTER (%)') + pyplot.ylabel('HTER') pyplot.title(title) pyplot.grid(True) pyplot.legend(loc=position, prop = {'size':fontsize}) pyplot.title(title) pyplot.xlim([-0.01, 1.01]) - pyplot.ylim([0, 51]) - figure.set_tight_layout(True) + pyplot.ylim([0., 0.51]) return figure @@ -319,18 +323,21 @@ def main(command_line_parameters=None): # create a multi-page PDF for the ROC curve pdf = PdfPages(args.roc) # create a separate figure for dev and eval - pdf.savefig(_plot_roc(frrs_dev, colors, args.legends, args.title[0] if args.title is not None else "ROC for development set", args.legend_font_size, args.legend_position), bbox_inches='tight') + pdf.savefig(_plot_roc(frrs_dev, colors, args.legends, args.title[0] if args.title is not None else "ROC for development set", args.legend_font_size, args.legend_position, args.far_line_at), bbox_inches='tight') del frrs_dev if args.eval_files: - farfrrs = [] - for i, scores in enumerate(scores_eval): - threshold = bob.measure.far_threshold(scores_dev[i][0], scores_dev[i][1], float(args.far_value) / 100) - farfrrs.append(bob.measure.farfrr(scores_eval[i][0], scores_eval[i][1], threshold)) + if args.far_line_at is not None: + farfrrs = [] + for i in range(len(scores_dev)): + threshold = bob.measure.far_threshold(scores_dev[i][0], scores_dev[i][1], args.far_line_at) + farfrrs.append(bob.measure.farfrr(scores_eval[i][0], scores_eval[i][1], threshold)) + else: + farfrrs = None pdf.savefig(_plot_roc(frrs_eval, colors, args.legends, args.title[1] if args.title is not None else "ROC for evaluation set", args.legend_font_size, args.legend_position, farfrrs), bbox_inches='tight') del frrs_eval pdf.close() except RuntimeError as e: - raise RuntimeError("During plotting of ROC curves, the following exception occured:\n%s\nUsually this happens when the label contains characters that LaTeX cannot parse." % e) + raise RuntimeError("During plotting of ROC curves, the following exception occured:\n%s" % e) if args.det: logger.info("Computing DET curves on the development " + ("and on the evaluation set" if args.eval_files else "set")) @@ -340,7 +347,7 @@ def main(command_line_parameters=None): logger.info("Plotting DET curves to file '%s'", args.det) try: - # create a multi-page PDF for the ROC curve + # create a multi-page PDF for the DET curve pdf = PdfPages(args.det) # create a separate figure for dev and eval pdf.savefig(_plot_det(dets_dev, colors, args.legends, args.title[0] if args.title is not None else "DET for development set", args.legend_font_size, args.legend_position), bbox_inches='tight') @@ -350,7 +357,7 @@ def main(command_line_parameters=None): del dets_eval pdf.close() except RuntimeError as e: - raise RuntimeError("During plotting of ROC curves, the following exception occured:\n%s\nUsually this happens when the label contains characters that LaTeX cannot parse." % e) + raise RuntimeError("During plotting of DET curves, the following exception occured:\n%s" % e) if args.epc: @@ -360,12 +367,12 @@ def main(command_line_parameters=None): raise ValueError("To plot the EPC curve the evaluation scores are necessary. Please, set it with the --eval-files option.") try: - # create a multi-page PDF for the ROC curve + # create a multi-page PDF for the EPC curve pdf = PdfPages(args.epc) pdf.savefig(_plot_epc(scores_dev, scores_eval, colors, args.legends, args.title if args.title is not None else "" , args.legend_font_size, args.legend_position), bbox_inches='tight') pdf.close() except RuntimeError as e: - raise RuntimeError("During plotting of EPC curves, the following exception occured:\n%s\nUsually this happens when the label contains characters that LaTeX cannot parse." % e) + raise RuntimeError("During plotting of EPC curves, the following exception occured:\n%s" % e) @@ -383,9 +390,9 @@ def main(command_line_parameters=None): # create a multi-page PDF for the ROC curve pdf = PdfPages(args.cmc) # create a separate figure for dev and eval - pdf.savefig(_plot_cmc(cmcs_dev, colors, args.legends, args.title[0] if args.title is not None else "CMC curve for development set", args.legend_font_size, args.legend_position)) + pdf.savefig(_plot_cmc(cmcs_dev, colors, args.legends, args.title[0] if args.title is not None else "CMC curve for development set", args.legend_font_size, args.legend_position), bbox_inches='tight') if args.eval_files: - pdf.savefig(_plot_cmc(cmcs_eval, colors, args.legends, args.title[1] if args.title is not None else "CMC curve for evaluation set", args.legend_font_size, args.legend_position)) + pdf.savefig(_plot_cmc(cmcs_eval, colors, args.legends, args.title[1] if args.title is not None else "CMC curve for evaluation set", args.legend_font_size, args.legend_position), bbox_inches='tight') pdf.close() except RuntimeError as e: raise RuntimeError("During plotting of ROC curves, the following exception occured:\n%s\nUsually this happens when the label contains characters that LaTeX cannot parse." % e) diff --git a/bob/bio/base/test/test_scripts.py b/bob/bio/base/test/test_scripts.py index dc5b5b5ec6d87a98722557caee80a0b08bcb3c9b..214ebc8997648b9dd2995db9f5f7ba90959d86ee 100644 --- a/bob/bio/base/test/test_scripts.py +++ b/bob/bio/base/test/test_scripts.py @@ -466,7 +466,7 @@ def test_evaluate(): # tests our 'evaluate' script using the reference files test_dir = tempfile.mkdtemp(prefix='bobtest_') reference_files = ('scores-nonorm-dev', 'scores-ztnorm-dev') - plots = [os.path.join(test_dir, '%s.pdf')%f for f in ['roc', 'cmc', 'det']] + plots = [os.path.join(test_dir, '%s.pdf')%f for f in ['roc', 'cmc', 'det', 'epc']] parameters = [ '--dev-files', reference_files[0], reference_files[1], '--eval-files', reference_files[0], reference_files[1], @@ -474,21 +474,27 @@ def test_evaluate(): '--legends', 'no norm', 'ZT norm', '--criterion', 'HTER', '--roc', plots[0], - '--det', plots[1], - '--cmc', plots[2], + '--cmc', plots[1], + '--det', plots[2], + '--epc', plots[3], '--rr', '--thresholds', '5000', '0', '--min-far-value', '1e-6', + '--far-line-at', '1e-5', '-v', ] # execute the script from bob.bio.base.script.evaluate import main - main(parameters) - for i in range(3): - assert os.path.exists(plots[i]) - os.remove(plots[i]) - os.rmdir(test_dir) + try: + main(parameters) + for i in range(4): + assert os.path.exists(plots[i]) + os.remove(plots[i]) + finally: + if os.path.exists(test_dir): + shutil.rmtree(test_dir) + def test_resources():