diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index dec6a0fb2ab9eb51fef737c50d5beab4270c8942..32d8f138020e175ed4e17077713ed7cb26c5c533 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 572e59e2f75c4068bc63ecb6bb54a687bafebf4b..2395064c25fcaf5fed1ede6a919b06bbc62f27e9 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 559bf1a1056928f55f9ff3527805da121091d830..df3266405bc195cbe4c3546b4c7fd0c6b2925a84 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 d95067bb84a91230b0877f7a2b3d0cac5dc495e1..69b3cd6fee0e60212de9c74c2dd29b720bc81b81 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 48d9f38ce01d168dc8ed3019e98f48e9ab19bf72..a14a815b49fa536c0a223fbc55d136680c764eab 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 85a536ccf2e0711eea119072122b7a852de5fa90..7af25875eea58de081012fc6040a76a04f001d54 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 cd97217efd74b81bcaf79b4b2351c4c063efcbf0..900ebb47bc868b1364d10021e6b4fe8dc7186c7d 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 e89926a67ea4d63847fdc3eab71d56c45a99f6b3..a93fe403e11b6483832df417b0e97462bc252a4a 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 557e59d7c9cd1ed45e2fc80c93ede020976e2976..acb43676cb86ca76aded88aa0d46f62dd78d9992 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 847138bf01c8901754717750559c3dc7d45a7876..2ecd78f0c7e2b14c0b9c64192c06014e2a0da75f 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) diff --git a/test/test_modules/test_partition_check.py b/test/test_modules/test_partition_check.py index d862b9e879c1121c78bc72ee1c9b8ba08f73a239..b04e01d13e9e160553f8ff66af8d97f65aa24bf0 100644 --- a/test/test_modules/test_partition_check.py +++ b/test/test_modules/test_partition_check.py @@ -24,7 +24,7 @@ class TestPartitionCheck: @mock.patch("os.path.exists", return_value=False) @mock.patch("os.makedirs", side_effect=None) def obj_with_exp_setup_login(self, mock_host, mock_user, mock_path, mock_check): - ExperimentSetup(parser_args={}, stations=['DEBW107', 'DEBY081', 'DEBW013', 'DEBW076', 'DEBW087', 'DEBW001'], + ExperimentSetup(stations=['DEBW107', 'DEBY081', 'DEBW013', 'DEBW076', 'DEBW087', 'DEBW001'], statistics_per_var={'o3': 'dma8eu', 'temp': 'maximum'}, station_type="background") pre = object.__new__(PartitionCheck) super(PartitionCheck, pre).__init__() @@ -37,7 +37,7 @@ class TestPartitionCheck: @mock.patch("os.path.exists", return_value=False) @mock.patch("os.makedirs", side_effect=None) def obj_with_exp_setup_compute(self, mock_host, mock_user, mock_path, mock_check): - ExperimentSetup(parser_args={}, stations=['DEBW107', 'DEBY081', 'DEBW013', 'DEBW076', 'DEBW087', 'DEBW001'], + ExperimentSetup(stations=['DEBW107', 'DEBY081', 'DEBW013', 'DEBW076', 'DEBW087', 'DEBW001'], statistics_per_var={'o3': 'dma8eu', 'temp': 'maximum'}, station_type="background") pre = object.__new__(PartitionCheck) super(PartitionCheck, pre).__init__() @@ -45,7 +45,7 @@ class TestPartitionCheck: RunEnvironment().__del__() def test_init(self, caplog): - ExperimentSetup(parser_args={}, stations=['DEBW107', 'DEBY081', 'DEBW013', 'DEBW076', 'DEBW087'], + ExperimentSetup(stations=['DEBW107', 'DEBY081', 'DEBW013', 'DEBW076', 'DEBW087'], statistics_per_var={'o3': 'dma8eu', 'temp': 'maximum'}) caplog.clear() caplog.set_level(logging.INFO) diff --git a/test/test_modules/test_pre_processing.py b/test/test_modules/test_pre_processing.py index 6abc722273613a1f4d6727396b114939b4d6a552..29811fb04789f32a1e2cc1b3affb6f8d4ae99730 100644 --- a/test/test_modules/test_pre_processing.py +++ b/test/test_modules/test_pre_processing.py @@ -26,7 +26,7 @@ class TestPreProcessing: @pytest.fixture def obj_with_exp_setup(self): - ExperimentSetup(parser_args={}, stations=['DEBW107', 'DEBY081', 'DEBW013', 'DEBW076', 'DEBW087', 'DEBW001'], + ExperimentSetup(stations=['DEBW107', 'DEBY081', 'DEBW013', 'DEBW076', 'DEBW087', 'DEBW001'], statistics_per_var={'o3': 'dma8eu', 'temp': 'maximum'}, station_type="background") pre = object.__new__(PreProcessing) super(PreProcessing, pre).__init__() @@ -34,7 +34,7 @@ class TestPreProcessing: RunEnvironment().__del__() def test_init(self, caplog): - ExperimentSetup(parser_args={}, stations=['DEBW107', 'DEBY081', 'DEBW013', 'DEBW076', 'DEBW087'], + ExperimentSetup(stations=['DEBW107', 'DEBY081', 'DEBW013', 'DEBW076', 'DEBW087'], statistics_per_var={'o3': 'dma8eu', 'temp': 'maximum'}) caplog.clear() caplog.set_level(logging.INFO)