From dec5fa812e0fb623f74fb0a9afc946d3b44688fa Mon Sep 17 00:00:00 2001
From: Amir MOHAMMADI <amir.mohammadi@idiap.ch>
Date: Fri, 18 May 2018 16:12:46 +0200
Subject: [PATCH] Write parameters in a temporary config file to enable chain
 loading

---
 bob/bio/base/baseline/Baseline.py   |  2 +-
 bob/bio/base/script/baseline.py     | 53 ++++++++++++++++++++---------
 bob/bio/base/test/test_baselines.py |  6 ++--
 3 files changed, 40 insertions(+), 21 deletions(-)

diff --git a/bob/bio/base/baseline/Baseline.py b/bob/bio/base/baseline/Baseline.py
index 40459cca..36912797 100644
--- a/bob/bio/base/baseline/Baseline.py
+++ b/bob/bio/base/baseline/Baseline.py
@@ -34,7 +34,7 @@ def get_available_databases():
       available_databases[database]["groups"] = []
       # Searching for database groups
       try:
-        groups = list(database_entry_point.groups())
+        groups = list(database_entry_point.groups()) or ["dev"]
         for g in ["dev", "eval"]:
           available_databases[database]["groups"] += [g] if g in groups else []
       except Exception:
diff --git a/bob/bio/base/script/baseline.py b/bob/bio/base/script/baseline.py
index 74c21944..59a8e93b 100644
--- a/bob/bio/base/script/baseline.py
+++ b/bob/bio/base/script/baseline.py
@@ -8,11 +8,14 @@ A script to run biometric recognition baselines
 
 
 from .. import load_resource
-import os
 from .verify import main as verify
 from ..baseline import get_available_databases, search_preprocessor
 from bob.extension.scripts.click_helper import verbosity_option
 import click
+import tempfile
+import logging
+
+logger = logging.getLogger("bob.bio.base")
 
 
 @click.command(context_settings={'ignore_unknown_options': True,
@@ -45,29 +48,45 @@ def baseline(ctx, baseline, database):
     Hint: pass `--temp-directory <dir>` to set the directory for temporary files
 
     Hint: pass `--result-directory <dir>` to set the directory for resulting score files
- 
+
     """
     # Triggering training for each baseline/database
     loaded_baseline = load_resource(
         baseline, 'baseline', package_prefix="bob.bio.")
 
-    # this is the default sub-directory that is used
-    sub_directory = os.path.join(database, baseline)
-
     # find the compatible preprocessor for this database
     database_data = get_available_databases()[database]
     db = search_preprocessor(database, loaded_baseline.preprocessors.keys())
     preprocessor = loaded_baseline.preprocessors[db]
 
-    # call verify with all parameters
-    parameters = [
-        '-p', preprocessor,
-        '-e', loaded_baseline.extractor,
-        '-d', database,
-        '-a', loaded_baseline.algorithm,
-        '--sub-directory', sub_directory
-    ] + ['-v'] * ctx.meta['verbosity']
-
-    parameters += ['--groups'] + database_data["groups"]
-
-    verify(parameters + ctx.args)
+    logger.debug('Database groups are %s', database_data["groups"])
+
+    # call verify with newly generated config file. We will create a new config
+    # file to allow people to use chain-loading and further modify the
+    # baselines. See: https://gitlab.idiap.ch/bob/bob.bio.video/issues/12
+    config = '''
+preprocessor = '{preprocessor}'
+extractor = '{extractor}'
+algorithm = '{algorithm}'
+database = '{database}'
+sub_directory = '{sub_directory}'
+groups = ['{groups}']
+verbose = {verbose}
+'''.format(
+        preprocessor=preprocessor,
+        extractor=loaded_baseline.extractor,
+        algorithm=loaded_baseline.algorithm,
+        database=database,
+        sub_directory=baseline,
+        groups="', '".join(database_data["groups"]),
+        verbose=ctx.meta['verbosity'],
+    )
+
+    with tempfile.NamedTemporaryFile(mode='w+t', prefix='{}_'.format(baseline),
+                                     suffix='.py', delete=False, dir='.') as f:
+        f.write(config)
+        f.flush()
+        f.seek(0)
+        verify([f.name] + ctx.args)
+        click.echo("You may want to delete `{}' after the experiments are "
+                   "finished running.".format(f.name))
diff --git a/bob/bio/base/test/test_baselines.py b/bob/bio/base/test/test_baselines.py
index 27dd5749..595505b8 100644
--- a/bob/bio/base/test/test_baselines.py
+++ b/bob/bio/base/test/test_baselines.py
@@ -10,11 +10,11 @@ def test_baselines():
         runner = CliRunner()
         result = runner.invoke(baseline, args=('dummy', 'dummy', '-T', tmp_dir, '-R', tmp_dir))
         assertion_error_message = (
-              'Command exited with this output: `{}\' \n'
+              'Command exited with this output and exception: `{}\' \n `{}\' \n'
               'If the output is empty, you can run this script locally to see '
               'what is wrong:\n'
-              'bin/bob bio baseline  -d dummy -a dummy -o /tmp/temp_annotations'
-              ''.format(result.output))
+              'bin/bob bio baseline dummy dummy -T /tmp/baseline -R /tmp/baseline'
+              ''.format(result.output, result.exception))
         assert result.exit_code == 0, assertion_error_message
 
     finally:
-- 
GitLab