Commit 2c96ca74 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI

Fixes #8 Algorithm.read_toscore_object should not exist

parent 5d9460a9
Pipeline #26925 passed with stage
in 5 minutes and 54 seconds
......@@ -85,7 +85,9 @@ class Algorithm(object):
**Parameters:**
toscore : object
The object to compute the score for.
The object to compute the score for. This will be the output of
extractor if performs_projection is False, otherwise this will be the
output of project method of the algorithm.
**Returns:**
......@@ -159,27 +161,6 @@ class Algorithm(object):
"""
return utils.load(feature_file)
def read_toscore_object(self, toscore_object_file):
"""read_toscore_object(toscore_object_file) -> toscore_object
Reads the toscore_object feature from a file.
By default, the toscore_object feature is identical to the projected feature.
Hence, this base class implementation simply calls :py:meth:`read_feature`.
If your algorithm requires different behavior, please overwrite this function.
**Parameters:**
toscore_object_file : str or :py:class:`bob.io.base.HDF5File`
The file open for reading, or the file name to read from.
**Returns:**
toscore_object : object
The toscore_object that was read from file.
"""
return self.read_feature(toscore_object_file)
def train_projector(self, training_features, projector_file):
"""This function can be overwritten to train the feature projector.
If you do this, please also register the function by calling this base class constructor
......
......@@ -6,12 +6,12 @@ from bob.bio.base.algorithm import LDA
class PadLDA(LDA):
"""Wrapper for bob.bio.base.algorithm.LDA,
Here, LDA is used in a PAD context. This means that the feature
will be projected on a single dimension subspace, which acts as a score
For more details, you may want to have a look at
For more details, you may want to have a look at
`bob.learn.linear Documentation`_
.. _bob.learn.linear Documentation:
......@@ -58,26 +58,5 @@ class PadLDA(LDA):
**kwargs
)
def read_toscore_object(self, toscore_object_file):
"""Reads the toscore_object feature from a file.
By default, the toscore_object feature is identical to the projected feature.
Hence, this base class implementation simply calls :py:meth:`bob.pad.base.algorithm.Algorithm.read_feature`.
If your algorithm requires different behavior, please overwrite this function.
Parameters
----------
toscore_object_file : str or :py:class:`bob.io.base.HDF5File`
The file open for reading, or the file name to read from.
Returns
-------
object:
The toscore_object that was read from file.
"""
return self.read_feature(toscore_object_file)
def score(self, toscore):
return [toscore[0]]
......@@ -202,6 +202,7 @@ def execute(args):
elif args.sub_task == 'compute-scores':
tools.compute_scores(
args.algorithm,
args.extractor,
groups=[args.group],
allow_missing_files=args.allow_missing_files,
force=args.force,
......
......@@ -23,7 +23,7 @@ from .FileSelector import FileSelector
from bob.bio.base import utils
def _compute_scores(algorithm, toscore_objects, allow_missing_files):
def _compute_scores(algorithm, extractor, toscore_objects, allow_missing_files):
"""Compute scores for the given list of objects using provided algorithm.
"""
# the scores to be computed
......@@ -37,7 +37,10 @@ def _compute_scores(algorithm, toscore_objects, allow_missing_files):
scores.insert(i, [numpy.nan])
continue
# read toscore
toscore = algorithm.read_toscore_object(toscore_element)
if algorithm.performs_projection:
toscore = algorithm.read_feature(toscore_element)
else:
toscore = extractor.read_feature(toscore_element)
# compute score
if isinstance(toscore, list) or isinstance(toscore[0], numpy.ndarray):
scores.insert(i, algorithm.score_for_multiple_projections(toscore))
......@@ -120,7 +123,7 @@ def _save_scores(score_file, scores, toscore_objects, write_compressed=False):
_close_written(score_file, f, write_compressed)
def _scores_all(algorithm, group, force, allow_missing_files=False, write_compressed=False):
def _scores_all(algorithm, extractor, group, force, allow_missing_files=False, write_compressed=False):
"""Computes scores for all (real, attack) files in a given group using the provided algorithm."""
# the file selector object
fs = FileSelector.instance()
......@@ -148,7 +151,7 @@ def _scores_all(algorithm, group, force, allow_missing_files=False, write_compre
# get the attack files
current_files = fs.get_paths(current_objects, 'projected' if algorithm.performs_projection else 'extracted')
# compute scores for the list of File objects
cur_scores = _compute_scores(algorithm, current_files, allow_missing_files)
cur_scores = _compute_scores(algorithm, extractor, current_files, allow_missing_files)
total_scores += cur_scores
# Save scores to text file
_save_scores(score_file, cur_scores, current_objects, write_compressed)
......@@ -164,7 +167,7 @@ def _scores_all(algorithm, group, force, allow_missing_files=False, write_compre
current_toscore_objects[0]+current_toscore_objects[1], write_compressed)
def compute_scores(algorithm, force=False, groups=['dev', 'eval'], allow_missing_files=False, write_compressed=False):
def compute_scores(algorithm, extractor, force=False, groups=['dev', 'eval'], allow_missing_files=False, write_compressed=False):
"""Computes the scores for the given groups.
This function computes all scores for the experiment and writes them to score files.
......@@ -175,6 +178,8 @@ def compute_scores(algorithm, force=False, groups=['dev', 'eval'], allow_missing
algorithm : py:class:`bob.bio.base.algorithm.Algorithm` or derived
The algorithm, used for enrolling model and writing them to file.
extractor : py:class:`bob.bio.base.extractor.Extractor` or derived
force : bool
If given, files are regenerated, even if they already exist.
......@@ -192,4 +197,4 @@ def compute_scores(algorithm, force=False, groups=['dev', 'eval'], allow_missing
algorithm.load_projector(fs.projector_file)
for group in groups:
_scores_all(algorithm, group, force, allow_missing_files, write_compressed)
_scores_all(algorithm, extractor, group, force, allow_missing_files, write_compressed)
Markdown is supported
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