diff --git a/bob/bio/base/test/test_algorithms.py b/bob/bio/base/test/test_algorithms.py index 9d7f0bb1b1799639fb095b0da35e3557db5b42d9..ffb872a5cfe061e746218415a73096751abc9bb0 100644 --- a/bob/bio/base/test/test_algorithms.py +++ b/bob/bio/base/test/test_algorithms.py @@ -64,7 +64,7 @@ def _gmm_stats(self, feature_file, count = 50, minimum = 0, maximum = 1): def test_pca(): temp_file = bob.io.base.test_utils.temporary_filename() # load PCA from configuration - pca1 = bob.bio.base.load_resource("pca", "algorithm") + pca1 = bob.bio.base.load_resource("pca", "algorithm", preferred_package = 'bob.bio.base') assert isinstance(pca1, bob.bio.base.algorithm.PCA) assert isinstance(pca1, bob.bio.base.algorithm.Algorithm) assert pca1.performs_projection @@ -134,10 +134,10 @@ def test_pca(): def test_lda(): temp_file = bob.io.base.test_utils.temporary_filename() # assure that the configurations are loadable - lda1 = bob.bio.base.load_resource("lda", "algorithm") + lda1 = bob.bio.base.load_resource("lda", "algorithm", preferred_package = 'bob.bio.base') assert isinstance(lda1, bob.bio.base.algorithm.LDA) assert isinstance(lda1, bob.bio.base.algorithm.Algorithm) - lda2 = bob.bio.base.load_resource("pca+lda", "algorithm") + lda2 = bob.bio.base.load_resource("pca+lda", "algorithm", preferred_package = 'bob.bio.base') assert isinstance(lda2, bob.bio.base.algorithm.LDA) assert isinstance(lda2, bob.bio.base.algorithm.Algorithm) @@ -209,7 +209,7 @@ def test_lda(): def test_bic(): temp_file = bob.io.base.test_utils.temporary_filename() # assure that the configurations are loadable - bic1 = bob.bio.base.load_resource("bic", "algorithm") + bic1 = bob.bio.base.load_resource("bic", "algorithm", preferred_package = 'bob.bio.base') assert isinstance(bic1, bob.bio.base.algorithm.BIC) assert isinstance(bic1, bob.bio.base.algorithm.Algorithm) @@ -278,10 +278,10 @@ def test_bic(): def test_plda(): temp_file = bob.io.base.test_utils.temporary_filename() # assure that the configurations are loadable - plda1 = bob.bio.base.load_resource("plda", "algorithm") + plda1 = bob.bio.base.load_resource("plda", "algorithm", preferred_package = 'bob.bio.base') assert isinstance(plda1, bob.bio.base.algorithm.PLDA) assert isinstance(plda1, bob.bio.base.algorithm.Algorithm) - plda2 = bob.bio.base.load_resource("pca+plda", "algorithm") + plda2 = bob.bio.base.load_resource("pca+plda", "algorithm", preferred_package = 'bob.bio.base') assert isinstance(plda2, bob.bio.base.algorithm.PLDA) assert isinstance(plda2, bob.bio.base.algorithm.Algorithm) diff --git a/bob/bio/base/test/test_extractor.py b/bob/bio/base/test/test_extractor.py index 488997354c75bd57033dd3beb8677b1f7f31b59c..ec3eed8f85e6ce39ccab61ccad0d328c6f45d700 100644 --- a/bob/bio/base/test/test_extractor.py +++ b/bob/bio/base/test/test_extractor.py @@ -7,7 +7,7 @@ from . import utils def test_linearize(): # load extractor - extractor = bob.bio.base.load_resource("linearize", "extractor") + extractor = bob.bio.base.load_resource("linearize", "extractor", preferred_package = 'bob.bio.base') # generate input data = utils.random_training_set((10,10), 1)[0] diff --git a/bob/bio/base/test/test_scripts.py b/bob/bio/base/test/test_scripts.py index 127f2e6e243acabb6c2d54c9611a1d5c595234d5..5d002d4b27cacde07b18e088c5fb6ab735e67fad 100644 --- a/bob/bio/base/test/test_scripts.py +++ b/bob/bio/base/test/test_scripts.py @@ -93,7 +93,8 @@ def test_verify_resources(): '--zt-norm', '-vs', 'test_resource', '--temp-directory', test_dir, - '--result-directory', test_dir + '--result-directory', test_dir, + '--preferred-package', 'bob.bio.base' ] print (bob.bio.base.tools.command_line(parameters)) @@ -138,7 +139,8 @@ def test_verify_parallel(): '-g', 'bob.bio.base.grid.Grid(grid_type = "local", number_of_parallel_processes = 2, scheduler_sleep_time = 0.1)', '-G', test_database, '--run-local-scheduler', '--stop-on-failure', '-D', 'success', - '--import', 'bob.io.image' + '--import', 'bob.io.image', + '--preferred-package', 'bob.bio.base' ] print (bob.bio.base.tools.command_line(parameters)) @@ -158,7 +160,8 @@ def test_verify_compressed(): '-vs', 'test_compressed', '--temp-directory', test_dir, '--result-directory', test_dir, - '--write-compressed-score-files' + '--write-compressed-score-files', + '--preferred-package', 'bob.bio.base' ] print (bob.bio.base.tools.command_line(parameters)) @@ -178,7 +181,8 @@ def test_verify_calibrate(): '-vs', 'test_calibrate', '--temp-directory', test_dir, '--result-directory', test_dir, - '--calibrate-scores' + '--calibrate-scores', + '--preferred-package', 'bob.bio.base' ] print (bob.bio.base.tools.command_line(parameters)) @@ -197,7 +201,8 @@ def test_verify_fileset(): '--zt-norm', '-vs', 'test_fileset', '--temp-directory', test_dir, - '--result-directory', test_dir + '--result-directory', test_dir, + '--preferred-package', 'bob.bio.base' ] print (bob.bio.base.tools.command_line(parameters)) @@ -221,7 +226,8 @@ def test_verify_filelist(): '--zt-norm', '-vs', 'test_filelist', '--temp-directory', test_dir, - '--result-directory', test_dir + '--result-directory', test_dir, + '--preferred-package', 'bob.bio.base' ] print (bob.bio.base.tools.command_line(parameters)) @@ -308,6 +314,7 @@ def test_grid_search(): '-R', test_dir, '-v', '--', '--dry-run', + '--preferred-package', 'bob.bio.base' ] from bob.bio.base.script.grid_search import main with utils.Quiet(): @@ -331,6 +338,7 @@ def test_grid_search(): '-g', 'grid', '-v', '--', '--dry-run', + '--preferred-package', 'bob.bio.base' ] with utils.Quiet(): main(parameters) @@ -352,6 +360,7 @@ def test_grid_search(): '-l', '4', '-L', '-1', '-v', '--', '--imports', 'bob.io.image', '--dry-run', + '--preferred-package', 'bob.bio.base' ] with utils.Quiet(): main(parameters) diff --git a/bob/bio/base/tools/command_line.py b/bob/bio/base/tools/command_line.py index 6a991baace8061ac64da3651614d65e7b504f1f8..495f5865e96be8e43e5712b3e216aade1f9d2862 100644 --- a/bob/bio/base/tools/command_line.py +++ b/bob/bio/base/tools/command_line.py @@ -56,10 +56,12 @@ def command_line_parser(description=__doc__, exclude_resources_from=[]): help = 'Biometric recognition; registered algorithms are: %s' % utils.resource_keys('algorithm', exclude_resources_from)) config_group.add_argument('-g', '--grid', metavar = 'x', nargs = '+', help = 'Configuration for the grid setup; if not specified, the commands are executed sequentially on the local machine.') - config_group.add_argument('--imports', metavar = 'LIB', nargs = '+', default = ['bob.bio.base'], + config_group.add_argument('-I', '--imports', metavar = 'LIB', nargs = '+', default = ['bob.bio.base'], help = 'If one of your configuration files is an actual command, please specify the lists of required libraries (imports) to execute this command') + config_group.add_argument('-W', '--preferred-package', metavar = 'LIB', + help = 'If resources with identical names are defined in several packages, prefer the one from the given package') config_group.add_argument('-s', '--sub-directory', metavar = 'DIR', required = True, - help = 'The sub-directory where the files of the current experiment should be stored. Please specify a directory name with a name describing your experiment.') + help = 'The sub-directory where the files of the current experiment should be stored. Please specify a directory name with a name describing your experiment') config_group.add_argument('--groups', metavar = 'GROUP', nargs = '+', default = ['dev'], help = "The groups (i.e., 'dev', 'eval') for which the models and scores should be generated; by default, only the 'dev' group is evaluated") config_group.add_argument('-P', '--protocol', metavar='PROTOCOL', @@ -208,12 +210,12 @@ def initialize(parsers, command_line_parameters = None, skips = []): args.timer = ('real', 'system', 'user') # load configuration resources - args.database = utils.load_resource(' '.join(args.database), 'database', imports = args.imports) - args.preprocessor = utils.load_resource(' '.join(args.preprocessor), 'preprocessor', imports = args.imports) - args.extractor = utils.load_resource(' '.join(args.extractor), 'extractor', imports = args.imports) - args.algorithm = utils.load_resource(' '.join(args.algorithm), 'algorithm', imports = args.imports) + args.database = utils.load_resource(' '.join(args.database), 'database', imports = args.imports, preferred_package = args.preferred_package) + args.preprocessor = utils.load_resource(' '.join(args.preprocessor), 'preprocessor', imports = args.imports, preferred_package = args.preferred_package) + args.extractor = utils.load_resource(' '.join(args.extractor), 'extractor', imports = args.imports, preferred_package = args.preferred_package) + args.algorithm = utils.load_resource(' '.join(args.algorithm), 'algorithm', imports = args.imports, preferred_package = args.preferred_package) if args.grid is not None: - args.grid = utils.load_resource(' '.join(args.grid), 'grid', imports = args.imports) + args.grid = utils.load_resource(' '.join(args.grid), 'grid', imports = args.imports, preferred_package = args.preferred_package) # set base directories if args.temp_directory is None: diff --git a/bob/bio/base/utils/resources.py b/bob/bio/base/utils/resources.py index 01c5fa988c42f31be8188ecdc53a87cb7f230462..504ac8f14911de180bb668a4ddd826c4b2bb7ecf 100644 --- a/bob/bio/base/utils/resources.py +++ b/bob/bio/base/utils/resources.py @@ -80,8 +80,8 @@ def _get_entry_points(keyword, strip = []): return [entry_point for entry_point in pkg_resources.iter_entry_points('bob.bio.' + keyword) if not entry_point.name.startswith(tuple(strip))] -def load_resource(resource, keyword, imports = ['bob.bio.base'], preferred_extension = None): - """load_resource(resource, keyword, imports = ['bob.bio.base'], preferred_extension = None) -> resource +def load_resource(resource, keyword, imports = ['bob.bio.base'], preferred_package = None): + """load_resource(resource, keyword, imports = ['bob.bio.base'], preferred_package = None) -> resource Loads the given resource that is registered with the given keyword. The resource can be: @@ -101,8 +101,8 @@ def load_resource(resource, keyword, imports = ['bob.bio.base'], preferred_exten imports : [str] A list of strings defining which modules to import, when constructing new objects (option 3). - preferred_extension : str or ``None`` - When several resources with the same name are found in different extension (in different ``bob.bio`` packages), this specifies the preferred extension to load the resource from. + preferred_package : str or ``None`` + When several resources with the same name are found in different extension (in different ``bob.bio`` packages), this specifies the preferred package to load the resource from. If not specified, the extension that is **not** ``bob.bio.base`` is selected. **Returns:** @@ -129,9 +129,9 @@ def load_resource(resource, keyword, imports = ['bob.bio.base'], preferred_exten # Now: check if there are only two entry points, and one is from the bob.bio.base, then use the other one index = -1 - if preferred_extension: + if preferred_package: for i,p in enumerate(entry_points): - if p.dist.project_name == preferred_extension: index = i + if p.dist.project_name == preferred_package: index = i if index == -1: if len(entry_points) == 2: