Commit c3cd137a authored by Amir MOHAMMADI's avatar Amir MOHAMMADI
Browse files

[finalize-scores] Backup scores, speed up help

parent 8dcd5aac
"""Finalizes the scores that are produced by
import click
import numpy
import logging
from bob.extension.scripts.click_helper import (
verbosity_option, log_parameters)
from bob.extension.scripts.click_helper import log_parameters
from bob.extension.scripts.click_helper import verbosity_option
logger = logging.getLogger(__name__)
@click.command(name='finalize-scores', epilog='''\b
$ bin/bob pad finalize_scores /path/to/scores-dev
$ bin/bob pad finalize_scores /path/to/scores-{dev,eval}
@click.argument('scores', type=click.Path(exists=True, dir_okay=False),
@click.option('-m', '--method', default='mean',
type=click.Choice(['mean', 'min', 'max']), show_default=True,
help='The method to use when finalizing the scores.')
@click.argument("scores", type=click.Path(exists=True, dir_okay=False), nargs=-1)
type=click.Choice(["mean", "min", "max"]),
help="The method to use when finalizing the scores.",
@click.option("--backup/--no-backup", default=True, help="Whether to backup scores.")
def finalize_scores(scores, method, **kwargs):
"""Finalizes the scores given by
def finalize_scores(scores, method, backup, verbose):
"""Finalizes the scores given by bob pad vanilla-pad
When using bob.pad.base, Algorithms can produce several score values for
each unique sample. You can use this script to average (or min/max) these
scores to have one final score per sample.
The conversion is done in-place. The order of scores will change.
The conversion is done in-place (original files will be backed up).
The order of scores will change.
import logging
import numpy
logger = logging.getLogger(__name__)
mean = {'mean': numpy.nanmean, 'max': numpy.nanmax, 'min': numpy.nanmin}[method]
mean = {"mean": numpy.nanmean, "max": numpy.nanmax, "min": numpy.nanmin}[method]
for path in scores:
new_lines = []
with open(path) as f:
old_lines = f.readlines()
if backup:
with open(f"{path}.bak", "w") as f:
for i, line in enumerate(old_lines):
uniq, s = line.strip().rsplit(maxsplit=1)
s = float(s)
......@@ -47,14 +63,13 @@ def finalize_scores(scores, method, **kwargs):
if uniq == last_line:
new_lines.append('{} {}\n'.format(
last_line, mean(last_scores)))
new_lines.append("{} {}\n".format(last_line, mean(last_scores)))
last_scores = [s]
last_line = uniq
else: # this else is for the for loop
new_lines.append('{} {}\n'.format(last_line, mean(last_scores)))
new_lines.append("{} {}\n".format(last_line, mean(last_scores)))
with open(path, 'w') as f:
with open(path, "w") as f:
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment