From 38e2773c230cb20cf999648866dd04e4927466ae Mon Sep 17 00:00:00 2001
From: lukas leufen <l.leufen@fz-juelich.de>
Date: Thu, 25 Jun 2020 15:45:33 +0200
Subject: [PATCH] split experiment path and name setup, replace parser inside
 ExpSetup by experiment_date parameter and add therefore args handling to all
 run scripts

---
 .gitlab-ci.yml                              |  2 +-
 run.py                                      |  5 +--
 run_hourly.py                               |  6 ++--
 run_zam347.py                               |  5 +--
 src/configuration/__init__.py               |  3 +-
 src/configuration/path_config.py            | 38 ++++++++++++++-------
 src/run.py                                  |  7 +---
 src/run_modules/experiment_setup.py         | 37 ++++++--------------
 test/test_configuration/test_path_config.py | 33 ++++++++++--------
 test/test_modules/test_experiment_setup.py  | 14 ++------
 10 files changed, 70 insertions(+), 80 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index dec6a0fb..32d8f138 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -198,4 +198,4 @@ pages:
       - public/badges/
       - public/coverage/
       - public/test/
-      - public/webpage/
+      - public/docs/
diff --git a/run.py b/run.py
index 572e59e2..2395064c 100644
--- a/run.py
+++ b/run.py
@@ -13,10 +13,11 @@ from src.run_modules.training import Training
 
 
 def main(parser_args):
+    experiment_date = parser_args.experiment_date
     with RunEnvironment():
-        ExperimentSetup(parser_args, stations=['DEBW107', 'DEBY081', 'DEBW013', 'DEBW076', 'DEBW087', 'DEBW001'],
+        ExperimentSetup(stations=['DEBW107', 'DEBY081', 'DEBW013', 'DEBW076', 'DEBW087', 'DEBW001'],
                         station_type='background', trainable=False, create_new_model=True, window_history_size=6,
-                        create_new_bootstraps=False)
+                        experiment_date=experiment_date, create_new_bootstraps=False)
         PreProcessing()
 
         PartitionCheck()
diff --git a/run_hourly.py b/run_hourly.py
index 559bf1a1..df326640 100644
--- a/run_hourly.py
+++ b/run_hourly.py
@@ -12,9 +12,11 @@ from src.run_modules.training import Training
 
 
 def main(parser_args):
+    experiment_date = parser_args.experiment_date
     with RunEnvironment():
-        ExperimentSetup(parser_args, stations=['DEBW107', 'DEBY081', 'DEBW013', 'DEBW076', 'DEBW087', 'DEBW001'],
-                        station_type='background', trainable=True, sampling="hourly", window_history_size=48)
+        ExperimentSetup(stations=['DEBW107', 'DEBY081', 'DEBW013', 'DEBW076', 'DEBW087', 'DEBW001'],
+                        station_type='background', trainable=True, sampling="hourly", window_history_size=48,
+                        experiment_date=experiment_date)
         PreProcessing()
 
         ModelSetup()
diff --git a/run_zam347.py b/run_zam347.py
index d95067bb..69b3cd6f 100644
--- a/run_zam347.py
+++ b/run_zam347.py
@@ -29,9 +29,10 @@ def load_stations():
 
 
 def main(parser_args):
+    experiment_date = parser_args.experiment_date
     with RunEnvironment():
-        ExperimentSetup(parser_args, stations=load_stations(), station_type='background', trainable=False,
-                        create_new_model=True)
+        ExperimentSetup(stations=load_stations(), station_type='background', trainable=False, create_new_model=True,
+                        experiment_date=experiment_date)
         PreProcessing()
 
         ModelSetup()
diff --git a/src/configuration/__init__.py b/src/configuration/__init__.py
index 48d9f38c..a14a815b 100644
--- a/src/configuration/__init__.py
+++ b/src/configuration/__init__.py
@@ -1,2 +1,3 @@
 """Collection of configuration functions, paths and classes."""
-from .path_config import ROOT_PATH, prepare_host, set_experiment_name, set_bootstrap_path, check_path_and_create, get_host
\ No newline at end of file
+from .path_config import ROOT_PATH, prepare_host, set_experiment_name, set_bootstrap_path, check_path_and_create, \
+    get_host, set_experiment_path
\ No newline at end of file
diff --git a/src/configuration/path_config.py b/src/configuration/path_config.py
index 85a536cc..7af25875 100644
--- a/src/configuration/path_config.py
+++ b/src/configuration/path_config.py
@@ -54,31 +54,43 @@ def prepare_host(create_new=True, data_path=None, sampling="daily") -> str:
         return path
 
 
-def set_experiment_name(experiment_name=None, experiment_path=None, sampling=None) -> Tuple[str, str]:
+def set_experiment_path(name: str, path: str = None) -> str:
     """
     Set name of experiment and its path.
 
-    * Experiment name is set to `TestExperiment` if not provided in kwargs. If a name is given, this string is expanded
+    * Experiment path is set to `<experiment_path>/<exp_name>` if provided or `ROOT_PATH/<exp_name>` otherwise
+
+    :param name: custom experiment name
+    :param path: custom experiment path
+
+    :return: full experiment path
+    """
+    if path is None:
+        experiment_path = os.path.abspath(os.path.join(ROOT_PATH, name))
+    else:
+        experiment_path = os.path.join(os.path.abspath(path), name)
+    return experiment_path
+
+
+def set_experiment_name(name: str = None, sampling: str = None) -> str:
+    """
+    Set name of experiment and its path.
+
+    * Experiment name is set to `TestExperiment` if not provided. If a name is given, this string is expanded
         by suffix `_network`. Experiment name is always expanded by `_<sampling>` as ending suffix if sampling is given.
-    * Experiment path is set to `ROOT_PATH/<exp_name>` if not provided or otherwise use `<experiment_path>/<exp_name>`
 
-    :param experiment_name: custom experiment name
-    :param experiment_path: custom experiment path
+    :param name: custom experiment name
     :param sampling: sampling rate as string to add to experiment name
 
-    :return: experiment name and full experiment path
+    :return: experiment name
     """
-    if experiment_name is None:
+    if name is None:
         experiment_name = "TestExperiment"
     else:
-        experiment_name = f"{experiment_name}_network"
+        experiment_name = f"{name}_network"
     if sampling is not None:
         experiment_name += f"_{sampling}"
-    if experiment_path is None:
-        experiment_path = os.path.abspath(os.path.join(ROOT_PATH, experiment_name))
-    else:
-        experiment_path = os.path.join(os.path.abspath(experiment_path), experiment_name)
-    return experiment_name, experiment_path
+    return experiment_name
 
 
 def set_bootstrap_path(bootstrap_path: str, data_path: str, sampling: str) -> str:
diff --git a/src/run.py b/src/run.py
index cd97217e..900ebb47 100644
--- a/src/run.py
+++ b/src/run.py
@@ -32,13 +32,8 @@ def run(stations=None,
     params = inspect.getfullargspec(ExperimentSetup).args
     kwargs = {k: v for k, v in locals().items() if k in params and v is not None}
 
-    parser = argparse.ArgumentParser()
-    parser.add_argument('--experiment_date', metavar='--exp_date', type=str, default="testrun",
-                        help="set experiment date as string")
-    args = parser.parse_args()
-
     with RunEnvironment():
-        ExperimentSetup(args, **kwargs)
+        ExperimentSetup(**kwargs)
         PreProcessing()
         PartitionCheck()
         ModelSetup()
diff --git a/src/run_modules/experiment_setup.py b/src/run_modules/experiment_setup.py
index e89926a6..a93fe403 100644
--- a/src/run_modules/experiment_setup.py
+++ b/src/run_modules/experiment_setup.py
@@ -206,7 +206,7 @@ class ExperimentSetup(RunEnvironment):
     """
 
     def __init__(self,
-                 parser_args=None,
+                 experiment_date=None,
                  stations: Union[str, List[str]] = None,
                  network: str = None,
                  station_type: str = None,
@@ -257,30 +257,29 @@ class ExperimentSetup(RunEnvironment):
         self._set_param("epochs", epochs, default=DEFAULT_EPOCHS)
 
         # set experiment name
-        exp_date = self._get_parser_args(parser_args).get("experiment_date")
-        exp_name, exp_path = path_config.set_experiment_name(experiment_name=exp_date, experiment_path=experiment_path,
-                                                                               sampling=sampling)
-        self._set_param("experiment_name", exp_name)
-        self._set_param("experiment_path", exp_path)
-        logging.info(f"Experiment path is: {exp_path}")
+        experiment_name = path_config.set_experiment_name(name=experiment_date, sampling=sampling)
+        experiment_path = path_config.set_experiment_path(name=experiment_name, path=experiment_path)
+        self._set_param("experiment_name", experiment_name)
+        self._set_param("experiment_path", experiment_path)
+        logging.info(f"Experiment path is: {experiment_path}")
         path_config.check_path_and_create(self.data_store.get("experiment_path"))
 
         # set model path
-        self._set_param("model_path", None, os.path.join(exp_path, "model"))
+        self._set_param("model_path", None, os.path.join(experiment_path, "model"))
         path_config.check_path_and_create(self.data_store.get("model_path"))
 
         # set plot path
-        default_plot_path = os.path.join(exp_path, "plots")
+        default_plot_path = os.path.join(experiment_path, "plots")
         self._set_param("plot_path", plot_path, default=default_plot_path)
         path_config.check_path_and_create(self.data_store.get("plot_path"))
 
         # set results path
-        default_forecast_path = os.path.join(exp_path, "forecasts")
+        default_forecast_path = os.path.join(experiment_path, "forecasts")
         self._set_param("forecast_path", forecast_path, default_forecast_path)
         path_config.check_path_and_create(self.data_store.get("forecast_path"))
 
         # set logging path
-        self._set_param("logging_path", None, os.path.join(exp_path, "logging"))
+        self._set_param("logging_path", None, os.path.join(experiment_path, "logging"))
         path_config.check_path_and_create(self.data_store.get("logging_path"))
 
         # setup for data
@@ -358,22 +357,6 @@ class ExperimentSetup(RunEnvironment):
         self.data_store.set(param, value, scope)
         logging.debug(f"set experiment attribute: {param}({scope})={value}")
 
-    @staticmethod
-    def _get_parser_args(args: Union[Dict, argparse.Namespace]) -> Dict:
-        """
-        Transform args to dict if given as argparse.Namespace.
-
-        :param args: either a dictionary or an argument parser instance
-
-        :return: dictionary with all arguments
-        """
-        if isinstance(args, argparse.Namespace):
-            return args.__dict__
-        elif isinstance(args, dict):
-            return args
-        else:
-            return {}
-
     def _compare_variables_and_statistics(self):
         """
         Compare variables and statistics.
diff --git a/test/test_configuration/test_path_config.py b/test/test_configuration/test_path_config.py
index 557e59d7..acb43676 100644
--- a/test/test_configuration/test_path_config.py
+++ b/test/test_configuration/test_path_config.py
@@ -4,7 +4,8 @@ import os
 import mock
 import pytest
 
-from src.configuration import prepare_host, set_experiment_name, set_bootstrap_path, check_path_and_create
+from src.configuration import prepare_host, set_experiment_name, set_bootstrap_path, check_path_and_create, \
+    set_experiment_path, ROOT_PATH
 from src.helpers import PyTestRegex
 
 
@@ -54,23 +55,27 @@ class TestPrepareHost:
 
 class TestSetExperimentName:
 
-    def test_set_experiment(self):
-        exp_name, exp_path = set_experiment_name()
+    def test_set_experiment_name(self):
+        exp_name = set_experiment_name()
         assert exp_name == "TestExperiment"
-        assert exp_path == os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "TestExperiment"))
-        exp_name, exp_path = set_experiment_name(experiment_name="2019-11-14", experiment_path=os.path.join(
-            os.path.dirname(__file__), "test2"))
+        exp_name = set_experiment_name(name="2019-11-14")
         assert exp_name == "2019-11-14_network"
-        assert exp_path == os.path.abspath(os.path.join(os.path.dirname(__file__), "test2", exp_name))
 
-    def test_set_experiment_from_sys(self):
-        exp_name, _ = set_experiment_name(experiment_name="2019-11-14")
-        assert exp_name == "2019-11-14_network"
-
-    def test_set_experiment_hourly(self):
-        exp_name, exp_path = set_experiment_name(sampling="hourly")
+    def test_set_experiment_name_sampling(self):
+        exp_name = set_experiment_name(sampling="hourly")
         assert exp_name == "TestExperiment_hourly"
-        assert exp_path == os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "TestExperiment_hourly"))
+        exp_name = set_experiment_name(sampling="daily")
+        assert exp_name == "TestExperiment_daily"
+
+    def test_set_experiment_path(self):
+        exp_path = set_experiment_path("TestExperiment")
+        assert exp_path == os.path.abspath(os.path.join(ROOT_PATH, "TestExperiment"))
+        exp_path = set_experiment_path(name="2019-11-14_network", path=os.path.join(os.path.dirname(__file__), "test2"))
+        assert exp_path == os.path.abspath(os.path.join(os.path.dirname(__file__), "test2", "2019-11-14_network"))
+
+    def test_set_experiment_path_given_path(self):
+        exp_path = set_experiment_path("TestExperiment", path=os.path.dirname(__file__))
+        assert exp_path == os.path.abspath(os.path.join(os.path.dirname(__file__), "TestExperiment"))
 
 
 class TestSetBootstrapPath:
diff --git a/test/test_modules/test_experiment_setup.py b/test/test_modules/test_experiment_setup.py
index 847138bf..2ecd78f0 100644
--- a/test/test_modules/test_experiment_setup.py
+++ b/test/test_modules/test_experiment_setup.py
@@ -33,16 +33,6 @@ class TestExperimentSetup:
         empty_obj._set_param("AnotherNoneTester", None)
         assert empty_obj.data_store.get("AnotherNoneTester", "general") is None
 
-    def test_get_parser_args_from_dict(self, empty_obj):
-        res = empty_obj._get_parser_args({'test2': 2, 'test10str': "10"})
-        assert res == {'test2': 2, 'test10str': "10"}
-
-    def test_get_parser_args_from_parse_args(self, empty_obj):
-        parser = argparse.ArgumentParser()
-        parser.add_argument('--experiment_date', type=str)
-        parser_args = parser.parse_args(["--experiment_date", "TOMORROW"])
-        assert empty_obj._get_parser_args(parser_args) == {"experiment_date": "TOMORROW"}
-
     def test_init_default(self):
         exp_setup = ExperimentSetup()
         data_store = exp_setup.data_store
@@ -104,7 +94,7 @@ class TestExperimentSetup:
 
     def test_init_no_default(self):
         experiment_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "data", "testExperimentFolder"))
-        kwargs = dict(parser_args={"experiment_date": "TODAY"},
+        kwargs = dict(experiment_date= "TODAY",
                       statistics_per_var={'o3': 'dma8eu', 'relhum': 'average_values', 'temp': 'maximum'},
                       stations=['DEBY053', 'DEBW059', 'DEBW027'], network="INTERNET", station_type="background",
                       variables=["o3", "temp"], start="1999-01-01", end="2001-01-01", window_history_size=4,
@@ -183,7 +173,7 @@ class TestExperimentSetup:
 
     def test_compare_variables_and_statistics(self):
         experiment_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "data", "testExperimentFolder"))
-        kwargs = dict(parser_args={"experiment_date": "TODAY"},
+        kwargs = dict(experiment_date="TODAY",
                       statistics_per_var={'o3': 'dma8eu', 'temp': 'maximum'},
                       stations=['DEBY053', 'DEBW059', 'DEBW027'], variables=["o3", "relhum"],
                       experiment_path=experiment_path)
-- 
GitLab