raiseIOError("Score file '%s' does not exist."%filename)
ifnottarfile.is_tarfile(filename):
returnopen(filename,mode)
# open the tar file for reading
tar=tarfile.open(filename,'r')
# get the first file in the tar file
tar_info=tar.next()
whiletar_infoisnotNoneandnottar_info.isfile():
tar_info=tar.next()
# check that one file was found in the archive
iftar_infoisNone:
raiseIOError(
"The given file is a .tar file, but it does not contain any file.")
# open the file for reading
returntar.extractfile(tar_info)
deffour_column(filename):
"""Loads a score set from a single file and yield its lines
Loads a score set from a single file and yield its lines (to avoid loading
the score file at once into memory). This function verifies that all fields
are correctly placed and contain valid fields. The score file must contain
the following information in each line:
.. code-block:: text
claimed_id real_id test_label score
Parameters:
filename (:py:class:`str`, ``file-like``): The file object that will be
opened with :py:func:`open_file` containing the scores.
Yields:
str: The claimed identity -- the client name of the model that was used in
the comparison
str: The real identity -- the client name of the probe that was used in the
comparison
str: A label of the probe -- usually the probe file name, or the probe id
float: The result of the comparison of the model and the probe
"""
return_iterate_score_file(filename)
defsplit_four_column(filename):
"""Loads a score set from a single file and splits the scores
Loads a score set from a single file and splits the scores between negatives
and positives. The score file has to respect the 4 column format as defined
in the method :py:func:`four_column`.
This method avoids loading and allocating memory for the strings present in
the file. We only keep the scores.
Parameters:
filename (:py:class:`str`, ``file-like``): The file object that will be
opened with :py:func:`open_file` containing the scores.
Returns:
array: negatives, 1D float array containing the list of scores, for which
the ``claimed_id`` and the ``real_id`` are different
(see :py:func:`four_column`)
array: positives, 1D float array containing the list of scores, for which
the ``claimed_id`` and the ``real_id`` are identical
(see :py:func:`four_column`)
"""
score_lines=four_column(filename)
return_split_scores(score_lines,1)
defcmc_four_column(filename):
"""Loads scores to compute CMC curves from a file in four column format.
The four column file needs to be in the same format as described in
:py:func:`four_column`, and the ``test_label`` (column 3) has to contain the
test/probe file name or a probe id.
This function returns a list of tuples. For each probe file, the tuple
consists of a list of negative scores and a list of positive scores.
Usually, the list of positive scores should contain only one element, but
more are allowed. The result of this function can directly be passed to,
e.g., the :py:func:`bob.measure.cmc` function.
Parameters:
filename (:py:class:`str`, ``file-like``): The file object that will be
opened with :py:func:`open_file` containing the scores.
Returns:
:py:class:`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``.
"""
score_lines=four_column(filename)
return_split_cmc_scores(score_lines,1)
deffive_column(filename):
"""Loads a score set from a single file and yield its lines
Loads a score set from a single file and yield its lines (to avoid loading
the score file at once into memory). This function verifies that all fields
are correctly placed and contain valid fields. The score file must contain
the following information in each line:
.. code-block:: text
claimed_id model_label real_id test_label score
Parameters:
filename (:py:class:`str`, ``file-like``): The file object that will be
opened with :py:func:`open_file` containing the scores.
Yields:
str: The claimed identity -- the client name of the model that was used in
the comparison
str: A label for the model -- usually the model file name, or the model id
str: The real identity -- the client name of the probe that was used in the
comparison
str: A label of the probe -- usually the probe file name, or the probe id
float: The result of the comparison of the model and the probe
"""
return_iterate_score_file(filename)
defsplit_five_column(filename):
"""Loads a score set from a single file and splits the scores
Loads a score set from a single file in five column format and splits the
scores between negatives and positives. The score file has to respect the 5
column format as defined in the method :py:func:`five_column`.
This method avoids loading and allocating memory for the strings present in
the file. We only keep the scores.
Parameters:
filename (:py:class:`str`, ``file-like``): The file object that will be
opened with :py:func:`open_file` containing the scores.
Returns:
array: negatives, 1D float array containing the list of scores, for which
the ``claimed_id`` and the ``real_id`` are different
(see :py:func:`four_column`)
array: positives, 1D float array containing the list of scores, for which
the ``claimed_id`` and the ``real_id`` are identical
(see :py:func:`four_column`)
"""
score_lines=four_column(filename)
return_split_scores(score_lines,2)
defcmc_five_column(filename):
"""Loads scores to compute CMC curves from a file in five column format.
The five column file needs to be in the same format as described in
:py:func:`five_column`, and the ``test_label`` (column 4) has to contain the
test/probe file name or a probe id.
This function returns a list of tuples. For each probe file, the tuple
consists of a list of negative scores and a list of positive scores.
Usually, the list of positive scores should contain only one element, but
more are allowed. The result of this function can directly be passed to,
e.g., the :py:func:`bob.measure.cmc` function.
Parameters:
filename (:py:class:`str`, ``file-like``): The file object that will be
opened with :py:func:`open_file` containing the scores.
Returns:
:py:class:`list`: A list of tuples, where each tuple contains the
``negative`` and ``positive`` scores for one probe of the database.
"""
score_lines=four_column(filename)
return_split_cmc_scores(score_lines,2)
defscores(filename,ncolumns=None):
"""scores(filename, ncolumns=None) -> tuple
Loads the scores from the given score file and yield its lines.
Depending on the score file format, four or five elements are yielded, see :py:func:`bob.measure.load.four_column` and :py:func:`bob.measure.load.five_column` for details.
Parameters:
filename: :py:class:`str`, ``file-like``:
The file object that will be opened with :py:func:`open_file` containing the scores.
ncolumns: any
ignored
Yields:
tuple:
see :py:func:`bob.measure.load.four_column` or :py:func:`bob.measure.load.five_column`
Loads the scores from the given score file and splits them into positives and negatives.
Depending on the score file format, it calls see :py:func:`bob.measure.load.split_four_column` and `:py:func:`bob.measure.load.split_five_column` for details.
Parameters:
filename: :py:class:`str`, ``file-like``:
The file object that will be opened with :py:func:`open_file` containing the scores.
ncolumns: int or ``None``
If specified to be ``4`` or ``5``, the score file will be assumed to be in the given format.
If not specified, the score file format will be estimated automatically
Returns:
negatives: 1D :py:class:`numpy.ndarray` of type float
This array contains the list of scores, for which the ``claimed_id`` and the ``real_id`` are different (see :py:func:`four_column`)
positives: 1D :py:class:`numpy.ndarray` of type float
This array contains the list of scores, for which the ``claimed_id`` and the ``real_id`` are identical (see :py:func:`four_column`)
Depending on the score file format, it calls see :py:func:`bob.measure.load.cmc_four_column` and `:py:func:`bob.measure.load.cmc_five_column` for details.
Parameters:
filename (:py:class:`str` or ``file-like``): The file object that will be
opened with :py:func:`open_file` containing the scores.
ncolumns: (:py:class:`int`, Optional): If specified to be ``4`` or ``5``,
the score file will be assumed to be in the given format. If not
specified, the score file format will be estimated automatically
Returns:
:py:class:`list`: [(neg,pos)] A list of tuples, where each tuple contains the
``negative`` and ``positive`` scores for one probe of the database.