load.py 2.06 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
#!/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')

13

14
15
16
17
18
19
20
21
22
23
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
24
25
    filename: :py:class:`str`:
        The name of the file containing the scores.
26
27
28
29
30
31
32
33
34
35
36
37
38

    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]
39
        scores = columns[:, 1]
40
    except Exception:
41
42
43
44
        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))
45
        raise
46
47
    positives = neg_pos == 1
    return (scores[~positives], scores[positives])
48

49

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def split_files(filenames):
    """split_files

    Parse a list of files using :py:func:`split`

    Parameters
    ----------

    filenames :
        :any:`list`: A list of file paths

    Returns
    -------
    :any:`list`: A list of tuples, where each tuple contains the
    ``negative`` and ``positive`` scores for one probe of the database. Both
    ``negatives`` and ``positives`` can be either an 1D
    :py:class:`numpy.ndarray` of type ``float``, or ``None``.
    """
    if filenames is None:
        return None
    res = []
    for file_path in filenames:
        try:
            res.append(split(file_path))
        except:
            raise
    return res