From 49a629009892a9fe50085e19ba031a2992140bfa Mon Sep 17 00:00:00 2001 From: Tiago Freitas Pereira Date: Wed, 8 Nov 2017 12:26:49 +0100 Subject: [PATCH] Using config file loading mechanism from bob.extension Removed global variable results Using config file loading mechanism from bob.extension Used the bob.extension config file loading Removing an awesome feature implemented in 2 lines of code Integrated with bob.extension merge 58 [sphinx] Added some documentation about chain loading Fixed import --- bob/bio/base/script/collect_results.py | 27 ++++++++++------- bob/bio/base/tools/command_line.py | 3 +- bob/bio/base/utils/resources.py | 42 ++------------------------ doc/experiments.rst | 14 ++++++++- 4 files changed, 32 insertions(+), 54 deletions(-) diff --git a/bob/bio/base/script/collect_results.py b/bob/bio/base/script/collect_results.py index 3b6182a..6e1cc06 100644 --- a/bob/bio/base/script/collect_results.py +++ b/bob/bio/base/script/collect_results.py @@ -184,28 +184,32 @@ def add_results(args, nonorm, ztnorm = None): else: r.ztnorm(dev_file) - global results - results.append(r) + return r def recurse(args, path): """Recurse the directory structure and collect all results that are stored in the desired file names.""" dir_list = os.listdir(path) + results = [] # check if the score directories are included in the current path if args.nonorm in dir_list or args.nonorm == '.': if args.ztnorm in dir_list or args.ztnorm == '.': - add_results(args, os.path.join(path, args.nonorm), os.path.join(path, args.ztnorm)) + return results + [add_results(args, os.path.join(path, args.nonorm), os.path.join(path, args.ztnorm))] else: - add_results(args, os.path.join(path, args.nonorm)) + return results + [add_results(args, os.path.join(path, args.nonorm))] for e in dir_list: real_path = os.path.join(path, e) if os.path.isdir(real_path): - recurse(args, real_path) + r = recurse(args, real_path) + if r is not None: + results += r + return results + -def table(): +def table(results): """Generates a table containing all results in a nice format.""" A = " "*2 + 'dev nonorm'+ " "*5 + 'dev ztnorm' + " "*6 + 'eval nonorm' + " "*4 + 'eval ztnorm' + " "*12 + 'directory\n' A += "-"*100+"\n" @@ -219,12 +223,13 @@ def main(command_line_parameters = None): """Iterates through the desired directory and collects all result files.""" args = command_line_arguments(command_line_parameters) - global results results = [] # collect results directories = glob.glob(args.directory) for directory in directories: - recurse(args, directory) + r = recurse(args, directory) + if r is not None: + results += r # sort results if desired if args.sort: @@ -233,10 +238,10 @@ def main(command_line_parameters = None): # print the results if args.self_test: - table() + table(results) elif args.output: f = open(args.output, "w") - f.writelines(table()) + f.writelines(table(results)) f.close() else: - print (table()) + print (table(results)) diff --git a/bob/bio/base/tools/command_line.py b/bob/bio/base/tools/command_line.py index da73105..91af40e 100644 --- a/bob/bio/base/tools/command_line.py +++ b/bob/bio/base/tools/command_line.py @@ -241,7 +241,6 @@ def take_from_config_or_command_line(args, config, keyword, default, required=Tr preferred_package=args.preferred_package)) elif config is not None and hasattr(config, keyword): - val = getattr(config, keyword) if isinstance(val, str) and is_resource: val = utils.load_resource(val, keyword, imports=args.imports, package_prefix=args.package_prefix, @@ -295,7 +294,7 @@ def parse_config_file(parsers, args, args_dictionary, keywords, skips): take_from_config_or_command_line(args, config, "sub_directory", parser.get_default("sub_directory"), is_resource=False) - + skip_keywords = tuple(['skip_' + k.replace('-', '_') for k in skips]) for keyword in keywords + skip_keywords + ('execute_only',): diff --git a/bob/bio/base/utils/resources.py b/bob/bio/base/utils/resources.py index 1b4264d..e2c5816 100644 --- a/bob/bio/base/utils/resources.py +++ b/bob/bio/base/utils/resources.py @@ -8,7 +8,7 @@ from __future__ import print_function import imp import os import pkg_resources - +import bob.extension.config import sys if sys.version_info[0] == 2: from string import letters as ascii_letters @@ -45,45 +45,7 @@ def _collect_config(paths): ''' - def _attach_resources(src, dst): - for k in dir(src): - setattr(dst, k, getattr(src, k)) - - import random - - name = "".join(random.sample(ascii_letters, 10)) - retval = imp.new_module(name) - - for path in paths: - # execute the module code on the context of previously import modules - for ep in pkg_resources.iter_entry_points('bob.bio.config'): - if ep.name == path: - tmp = ep.load() # loads the pointed module - _attach_resources(tmp, retval) - break - else: - - # if you get to this point, then it is not a resource, maybe it is a module? - try: - tmp = __import__(path, retval.__dict__, retval.__dict__, ['*']) - _attach_resources(tmp, retval) - continue - except ImportError: - # module does not exist, ignore it - pass - except Exception as e: - raise IOError("The configuration module '%s' could not be loaded: %s" % (path, e)) - - # if you get to this point, then its not a resource nor a loadable module, is - # it on the file system? - if not os.path.exists(path): - raise IOError("The configuration file, resource or module '%s' could not be found, loaded or imported" % path) - - name = "".join(random.sample(ascii_letters, 10)) - tmp = imp.load_source(name, path) - _attach_resources(tmp, retval) - - return retval + return bob.extension.config.load(paths, entry_point_group="bob.bio.config") def read_config_file(filenames, keyword = None): diff --git a/doc/experiments.rst b/doc/experiments.rst index 1bf5059..701a1b6 100644 --- a/doc/experiments.rst +++ b/doc/experiments.rst @@ -94,6 +94,18 @@ Running the experiment is then as simple as: .. note:: To be able to run exactly the command line from above, it requires to have :ref:`bob.bio.face ` installed. + +.. note:: + Chain loading is possible through configuration files, i.e., variables of each + config is available during evaluation of the preceding config file. + + This allows us to spread our experiment setup in several configuration files and have a call similar to this:: + + $ verify.py config_1.py config_2.py config_n.py + + For more information see *Chain Loading* in :ref:`bob.extension.config`. + + Before running an experiment, it is recommended to add set the variable ``dry_run = True``, so that it will only print, which steps would be executed, without actually executing them, and make sure that everything works as expected. The final result of the experiment will be one (or more) score file(s). @@ -102,7 +114,7 @@ By default, you can find them in a sub-directory the ``result`` directory, but y .. note:: At Idiap_, the default result directory differs, see ``verify.py --help`` for your directory. - + .. _bob.bio.base.command_line: -- GitLab