Skip to content
Snippets Groups Projects
Commit 0614ae82 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI
Browse files

Merge branch 'refactoring_2016' into 'master'

makes the annotations method implementation mandatory

See merge request !56
parents 2e7ac534 570ea2a9
No related branches found
No related tags found
1 merge request!56makes the annotations method implementation mandatory
Pipeline #
...@@ -151,9 +151,6 @@ class BioDatabase(six.with_metaclass(abc.ABCMeta, bob.db.base.Database)): ...@@ -151,9 +151,6 @@ class BioDatabase(six.with_metaclass(abc.ABCMeta, bob.db.base.Database)):
return "%s(%s)" % (str(self.__class__), params) return "%s(%s)" % (str(self.__class__), params)
###########################################################################
# Helper functions that you might want to use in derived classes
###########################################################################
def replace_directories(self, replacements=None): def replace_directories(self, replacements=None):
"""This helper function replaces the ``original_directory`` and the ``annotation_directory`` of the database with the directories read from the given replacement file. """This helper function replaces the ``original_directory`` and the ``annotation_directory`` of the database with the directories read from the given replacement file.
...@@ -203,27 +200,9 @@ class BioDatabase(six.with_metaclass(abc.ABCMeta, bob.db.base.Database)): ...@@ -203,27 +200,9 @@ class BioDatabase(six.with_metaclass(abc.ABCMeta, bob.db.base.Database)):
except AttributeError: except AttributeError:
pass pass
def sort(self, files): ###########################################################################
"""sort(files) -> sorted # Helper functions that you might want to use in derived classes
###########################################################################
Returns a sorted version of the given list of File's (or other structures that define an 'id' data member).
The files will be sorted according to their id, and duplicate entries will be removed.
**Parameters:**
files : [:py:class:`bob.bio.base.database.BioFile`]
The list of files to be uniquified and sorted.
**Returns:**
sorted : [:py:class:`bob.bio.base.database.BioFile`]
The sorted list of files, with duplicate `BioFile.id`\s being removed.
"""
# sort files using their sort function
sorted_files = sorted(files)
# remove duplicates
return [f for i, f in enumerate(sorted_files) if not i or sorted_files[i - 1].id != f.id]
def uses_probe_file_sets(self, protocol=None): def uses_probe_file_sets(self, protocol=None):
"""Defines if, for the current protocol, the database uses several probe files to generate a score. """Defines if, for the current protocol, the database uses several probe files to generate a score.
Returns True if the given protocol specifies file sets for probes, instead of a single probe file. Returns True if the given protocol specifies file sets for probes, instead of a single probe file.
...@@ -258,31 +237,6 @@ class BioDatabase(six.with_metaclass(abc.ABCMeta, bob.db.base.Database)): ...@@ -258,31 +237,6 @@ class BioDatabase(six.with_metaclass(abc.ABCMeta, bob.db.base.Database)):
files_by_clients.append(client_files[client]) files_by_clients.append(client_files[client])
return files_by_clients return files_by_clients
def annotations(self, file):
"""
Returns the annotations for the given File object, if available.
It uses `bob.db.base.read_annotation_file` to load the annotations.
**Parameters:**
file : :py:class:`bob.bio.base.database.BioFile`
The file for which annotations should be returned.
**Returns:**
annots : dict or None
The annotations for the file, if available.
"""
if self.annotation_directory:
try:
from bob.db.base.annotations import read_annotation_file
annotation_path = os.path.join(self.annotation_directory, file.path + self.annotation_extension)
return read_annotation_file(annotation_path, self.annotation_type)
except ImportError as e:
raise NotImplementedError(str(e) + " Annotations are not read." % e)
return None
def file_names(self, files, directory, extension): def file_names(self, files, directory, extension):
"""file_names(files, directory, extension) -> paths """file_names(files, directory, extension) -> paths
...@@ -313,26 +267,6 @@ class BioDatabase(six.with_metaclass(abc.ABCMeta, bob.db.base.Database)): ...@@ -313,26 +267,6 @@ class BioDatabase(six.with_metaclass(abc.ABCMeta, bob.db.base.Database)):
# List of files, do not remove duplicates # List of files, do not remove duplicates
return [f.make_path(directory, extension) for f in files] return [f.make_path(directory, extension) for f in files]
def original_file_names(self, files):
"""original_file_names(files) -> paths
Returns the full path of the original data of the given File objects.
**Parameters:**
files : [:py:class:`bob.bio.base.database.BioFile`]
The list of file object to retrieve the original data file names for.
**Returns:**
paths : [str] or [[str]]
The paths extracted for the files, in the same order.
If this database provides file sets, a list of lists of file names is returned, one sub-list for each file set.
"""
assert self.original_directory is not None
assert self.original_extension is not None
return self.file_names(files, self.original_directory, self.original_extension)
################################################################# #################################################################
###### Methods to be overwritten by derived classes ############# ###### Methods to be overwritten by derived classes #############
################################################################# #################################################################
...@@ -356,23 +290,6 @@ class BioDatabase(six.with_metaclass(abc.ABCMeta, bob.db.base.Database)): ...@@ -356,23 +290,6 @@ class BioDatabase(six.with_metaclass(abc.ABCMeta, bob.db.base.Database)):
""" """
raise NotImplementedError("Please implement this function in derived classes") raise NotImplementedError("Please implement this function in derived classes")
def model_ids(self, groups='dev'):
"""model_ids(group = 'dev') -> ids
Returns a list of model ids for the given group, respecting the current protocol.
**Parameters:**
group : one of ``('dev', 'eval')``
The group to get the model ids for.
**Returns:**
ids : [int] or [str]
The list of (unique) model ids for models of the given group.
"""
return sorted(self.model_ids_with_protocol(groups=groups, protocol=self.protocol))
@abc.abstractmethod @abc.abstractmethod
def objects(self, groups=None, protocol=None, purposes=None, model_ids=None, **kwargs): def objects(self, groups=None, protocol=None, purposes=None, model_ids=None, **kwargs):
"""This function returns lists of File objects, which fulfill the given restrictions. """This function returns lists of File objects, which fulfill the given restrictions.
...@@ -401,31 +318,44 @@ class BioDatabase(six.with_metaclass(abc.ABCMeta, bob.db.base.Database)): ...@@ -401,31 +318,44 @@ class BioDatabase(six.with_metaclass(abc.ABCMeta, bob.db.base.Database)):
""" """
raise NotImplementedError("This function must be implemented in your derived class.") raise NotImplementedError("This function must be implemented in your derived class.")
@abc.abstractmethod
def annotations(self, file):
"""
Returns the annotations for the given File object, if available.
It uses `bob.db.base.read_annotation_file` to load the annotations.
**Parameters:**
file : :py:class:`bob.bio.base.database.BioFile`
The file for which annotations should be returned.
**Returns:**
annots : dict or None
The annotations for the file, if available.
"""
raise NotImplementedError("This function must be implemented in your derived class.")
################################################################# #################################################################
######### Methods to provide common functionality ############### ######### Methods to provide common functionality ###############
################################################################# #################################################################
def original_file_name(self, file): def model_ids(self, groups='dev'):
"""This function returns the original file name for the given File object. """model_ids(group = 'dev') -> ids
Keyword parameters: Returns a list of model ids for the given group, respecting the current protocol.
**Parameters:**
file : :py:class:`bob.bio.base.database.BioFile` or a derivative group : one of ``('dev', 'eval')``
The File objects for which the file name should be retrieved The group to get the model ids for.
Return value : str **Returns:**
The original file name for the given File object
ids : [int] or [str]
The list of (unique) model ids for models of the given group.
""" """
# check if directory is set return sorted(self.model_ids_with_protocol(groups=groups, protocol=self.protocol))
if not self.original_directory or not self.original_extension:
raise ValueError(
"The original_directory and/or the original_extension were not specified in the constructor.")
# extract file name
file_name = file.make_path(self.original_directory, self.original_extension)
if not self.check_existence or os.path.exists(file_name):
return file_name
raise ValueError("The file '%s' was not found. Please check the original directory '%s' and extension '%s'?" % (
file_name, self.original_directory, self.original_extension))
def all_files(self, groups=None): def all_files(self, groups=None):
"""all_files(groups=None) -> files """all_files(groups=None) -> files
......
...@@ -16,16 +16,16 @@ class DummyDatabase(ZTBioDatabase): ...@@ -16,16 +16,16 @@ class DummyDatabase(ZTBioDatabase):
models_depend_on_protocol=False models_depend_on_protocol=False
) )
import bob.db.atnt import bob.db.atnt
self.__db = bob.db.atnt.Database() self._db = bob.db.atnt.Database()
def _make_bio(self, files): def _make_bio(self, files):
return [BioFile(client_id=f.client_id, path=f.path, file_id=f.id) for f in files] return [BioFile(client_id=f.client_id, path=f.path, file_id=f.id) for f in files]
def model_ids_with_protocol(self, groups=None, protocol=None, **kwargs): def model_ids_with_protocol(self, groups=None, protocol=None, **kwargs):
return self.__db.model_ids(groups, protocol) return self._db.model_ids(groups, protocol)
def objects(self, groups=None, protocol=None, purposes=None, model_ids=None, **kwargs): def objects(self, groups=None, protocol=None, purposes=None, model_ids=None, **kwargs):
return self._make_bio(self.__db.objects(model_ids, groups, purposes, protocol, **kwargs)) return self._make_bio(self._db.objects(model_ids, groups, purposes, protocol, **kwargs))
def tobjects(self, groups=None, protocol=None, model_ids=None, **kwargs): def tobjects(self, groups=None, protocol=None, model_ids=None, **kwargs):
return [] return []
...@@ -34,7 +34,7 @@ class DummyDatabase(ZTBioDatabase): ...@@ -34,7 +34,7 @@ class DummyDatabase(ZTBioDatabase):
return [] return []
def tmodel_ids_with_protocol(self, protocol=None, groups=None, **kwargs): def tmodel_ids_with_protocol(self, protocol=None, groups=None, **kwargs):
return self.__db.model_ids(groups) return self._db.model_ids(groups)
def t_enroll_files(self, t_model_id, group='dev'): def t_enroll_files(self, t_model_id, group='dev'):
return self.enroll_files(t_model_id, group) return self.enroll_files(t_model_id, group)
...@@ -42,4 +42,8 @@ class DummyDatabase(ZTBioDatabase): ...@@ -42,4 +42,8 @@ class DummyDatabase(ZTBioDatabase):
def z_probe_files(self, group='dev'): def z_probe_files(self, group='dev'):
return self.probe_files(None, group) return self.probe_files(None, group)
def annotations(self, file):
return None
database = DummyDatabase() database = DummyDatabase()
...@@ -15,7 +15,7 @@ class DummyDatabase(ZTBioDatabase): ...@@ -15,7 +15,7 @@ class DummyDatabase(ZTBioDatabase):
models_depend_on_protocol=False models_depend_on_protocol=False
) )
import bob.db.atnt import bob.db.atnt
self.__db = bob.db.atnt.Database() self._db = bob.db.atnt.Database()
def uses_probe_file_sets(self): def uses_probe_file_sets(self):
return True return True
...@@ -31,10 +31,10 @@ class DummyDatabase(ZTBioDatabase): ...@@ -31,10 +31,10 @@ class DummyDatabase(ZTBioDatabase):
return file_sets return file_sets
def model_ids_with_protocol(self, groups=None, protocol=None, **kwargs): def model_ids_with_protocol(self, groups=None, protocol=None, **kwargs):
return self.__db.model_ids(groups, protocol) return self._db.model_ids(groups, protocol)
def objects(self, groups=None, protocol=None, purposes=None, model_ids=None, **kwargs): def objects(self, groups=None, protocol=None, purposes=None, model_ids=None, **kwargs):
return self._make_bio(self.__db.objects(model_ids, groups, purposes, protocol, **kwargs)) return self._make_bio(self._db.objects(model_ids, groups, purposes, protocol, **kwargs))
def tobjects(self, groups=None, protocol=None, model_ids=None, **kwargs): def tobjects(self, groups=None, protocol=None, model_ids=None, **kwargs):
return [] return []
...@@ -43,7 +43,7 @@ class DummyDatabase(ZTBioDatabase): ...@@ -43,7 +43,7 @@ class DummyDatabase(ZTBioDatabase):
return [] return []
def tmodel_ids_with_protocol(self, protocol=None, groups=None, **kwargs): def tmodel_ids_with_protocol(self, protocol=None, groups=None, **kwargs):
return self.__db.model_ids(groups) return self._db.model_ids(groups)
def t_enroll_files(self, t_model_id, group='dev'): def t_enroll_files(self, t_model_id, group='dev'):
return self.enroll_files(t_model_id, group) return self.enroll_files(t_model_id, group)
...@@ -54,4 +54,8 @@ class DummyDatabase(ZTBioDatabase): ...@@ -54,4 +54,8 @@ class DummyDatabase(ZTBioDatabase):
def z_probe_file_sets(self, group='dev'): def z_probe_file_sets(self, group='dev'):
return self.probe_file_sets(None, group) return self.probe_file_sets(None, group)
def annotations(self, file):
return None
database = DummyDatabase() database = DummyDatabase()
from bob.bio.base.preprocessor import Preprocessor from bob.bio.base.preprocessor import Preprocessor
class DummyPreprocessor (Preprocessor): class DummyPreprocessor (Preprocessor):
def __init__(self, return_none=False, **kwargs): def __init__(self, return_none=False, **kwargs):
Preprocessor.__init__(self) Preprocessor.__init__(self)
...@@ -11,4 +12,5 @@ class DummyPreprocessor (Preprocessor): ...@@ -11,4 +12,5 @@ class DummyPreprocessor (Preprocessor):
return None return None
return data return data
preprocessor = DummyPreprocessor() preprocessor = DummyPreprocessor()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment