load.py 1.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Mon 23 May 2011 16:23:05 CEST

"""A set of utilities to load score files with different formats.
"""

import logging
import numpy

LOGGER = logging.getLogger('bob.measure')

def split(filename):
    """split(filename) -> negatives, positives

    Loads the scores from the given file and splits them into positive
    and negative arrays. The file must be a two columns file where the first
    column contains -1 or 1 (for negative or positive respectively) and the
    second the corresponding scores.

    Parameters
    ----------
Theophile GENTILHOMME's avatar
Theophile GENTILHOMME committed
23 24
    filename: :py:class:`str`:
        The name of the file containing the scores.
25 26 27 28 29 30 31 32 33 34 35 36 37

    Returns
    -------
    negatives: 1D :py:class:`numpy.ndarray` of type float
        This array contains the list of negative scores

    positives: 1D :py:class:`numpy.ndarray` of type float
        This array contains the list of positive scores

    """
    try:
        columns = numpy.loadtxt(filename)
        neg_pos = columns[:, 0]
38
        scores = columns[:, 1]
39 40 41 42 43 44 45 46
    except:
        LOGGER.error('''Cannot read {}. This file must be a two columns file with
                   the first column containing -1 or 1 (i.e. negative or
                   positive) and the second the scores
                     (float).'''.format(filename))
        return None, None
    return (scores[numpy.where(neg_pos == -1)],
            scores[numpy.where(neg_pos == 1)])