From 69136fdccf4c1b470148275676bfc5d094f8012a Mon Sep 17 00:00:00 2001 From: James Brandreth Date: Tue, 3 Aug 2021 15:18:33 +0100 Subject: [PATCH] :recycle: Expanded *production* cli to an *online* cli to add test functions --- .../generate_air_quality_experiment.py | 84 ++++++++++++++++++- .../{production => online}/__init__.py | 2 +- .../parsers/urbanair_parser/online/online.py | 8 ++ .../main.py => online/production.py} | 22 +---- .../parsers/urbanair_parser/online/test.py | 33 ++++++++ .../parsers/urbanair_parser/online/utils.py | 20 +++++ containers/cleanair/cli/urbanair | 4 +- containers/scripts/mrdgp_dynamic.sh | 2 +- containers/scripts/mrdgp_static.sh | 2 +- containers/scripts/svgp_dynamic.sh | 2 +- containers/scripts/svgp_static.sh | 2 +- 11 files changed, 154 insertions(+), 27 deletions(-) rename containers/cleanair/cleanair/parsers/urbanair_parser/{production => online}/__init__.py (68%) create mode 100644 containers/cleanair/cleanair/parsers/urbanair_parser/online/online.py rename containers/cleanair/cleanair/parsers/urbanair_parser/{production/main.py => online/production.py} (51%) create mode 100644 containers/cleanair/cleanair/parsers/urbanair_parser/online/test.py create mode 100644 containers/cleanair/cleanair/parsers/urbanair_parser/online/utils.py diff --git a/containers/cleanair/cleanair/experiment/generate_air_quality_experiment.py b/containers/cleanair/cleanair/experiment/generate_air_quality_experiment.py index 3e0f17659..436f3ea7e 100644 --- a/containers/cleanair/cleanair/experiment/generate_air_quality_experiment.py +++ b/containers/cleanair/cleanair/experiment/generate_air_quality_experiment.py @@ -229,7 +229,6 @@ def production_mrdgp_static(secretfile: str) -> List[InstanceMixin]: ) return [instance] - def production_mrdgp_dynamic(secretfile: str) -> List[InstanceMixin]: """The production version of the dynamic MRDGP""" train_upto = datetime_from_word("today") @@ -599,3 +598,86 @@ def dryrun_svgp(secretfile: str) -> List[InstanceMixin]: input_dim=input_dim, ) return [InstanceMixin(full_data_config, ModelName.svgp, model_params, tag=Tag.test)] + + +def test_mrdgp_dynamic(secretfile: str) -> List[InstanceMixin]: + """The test version of the dynamic MRDGP""" + train_upto = datetime_from_word("today") + train_start = train_upto - timedelta(PRODUCTION_MRDGP_TRAIN_DAYS) + forecast_upto = train_upto + timedelta(PRODUCTION_FORECAST_DAYS) + data_config = DataConfig( + train_start_date=train_start, + train_end_date=train_upto, + pred_start_date=train_upto, + pred_end_date=forecast_upto, + include_prediction_y=True, + train_sources=PRODUCTION_MRDGP_TRAIN_SOURCES, + pred_sources=PRODUCTION_MRDGP_FORECAST_SOURCES, + train_interest_points=PRODUCTION_MRDGP_TRAIN_INTEREST_POINTS, + pred_interest_points=PRODUCTION_MRDGP_FORECAST_INTEREST_POINTS, + species=PRODUCTION_MRDGP_SPECIES, + static_features=PRODUCTION_STATIC_FEATURES, + dynamic_features=PRODUCTION_DYNAMIC_FEATURES, + buffer_sizes=PRODUCTION_BUFFER_SIZES, + norm_by=Source.laqn, + ) + + input_dim = total_num_features(data_config) + + model_config = ModelConfig(secretfile=secretfile) + model_config.validate_config(data_config) + full_data_config = model_config.generate_full_config(data_config) + + # create model params + model_params = default_mrdgp_model_params(input_dim) + + # create instance and add to list + instance = InstanceMixin( + full_data_config, + ModelName.mrdgp, + model_params, + tag=Tag.test, + cluster_id=ClusterId.nc6, + ) + return [instance] + +def test_mrdgp_static(secretfile: str) -> List[InstanceMixin]: + """The test version of the dynamic MRDGP""" + train_upto = datetime_from_word("today") + train_start = train_upto - timedelta(PRODUCTION_MRDGP_TRAIN_DAYS) + forecast_upto = train_upto + timedelta(PRODUCTION_FORECAST_DAYS) + data_config = DataConfig( + train_start_date=train_start, + train_end_date=train_upto, + pred_start_date=train_upto, + pred_end_date=forecast_upto, + include_prediction_y=True, + train_sources=PRODUCTION_MRDGP_TRAIN_SOURCES, + pred_sources=PRODUCTION_MRDGP_FORECAST_SOURCES, + train_interest_points=PRODUCTION_MRDGP_TRAIN_INTEREST_POINTS, + pred_interest_points=PRODUCTION_MRDGP_FORECAST_INTEREST_POINTS, + species=PRODUCTION_MRDGP_SPECIES, + static_features=PRODUCTION_STATIC_FEATURES, + dynamic_features=[], + buffer_sizes=PRODUCTION_BUFFER_SIZES, + norm_by=Source.laqn, + ) + + input_dim = total_num_features(data_config) + + model_config = ModelConfig(secretfile=secretfile) + model_config.validate_config(data_config) + full_data_config = model_config.generate_full_config(data_config) + + # create model params + model_params = default_mrdgp_model_params(input_dim) + + # create instance and add to list + instance = InstanceMixin( + full_data_config, + ModelName.mrdgp, + model_params, + tag=Tag.test, + cluster_id=ClusterId.nc6, + ) + return [instance] \ No newline at end of file diff --git a/containers/cleanair/cleanair/parsers/urbanair_parser/production/__init__.py b/containers/cleanair/cleanair/parsers/urbanair_parser/online/__init__.py similarity index 68% rename from containers/cleanair/cleanair/parsers/urbanair_parser/production/__init__.py rename to containers/cleanair/cleanair/parsers/urbanair_parser/online/__init__.py index 60d2dcb20..bc2f57901 100644 --- a/containers/cleanair/cleanair/parsers/urbanair_parser/production/__init__.py +++ b/containers/cleanair/cleanair/parsers/urbanair_parser/online/__init__.py @@ -1,5 +1,5 @@ """CLI for production models""" -from .main import app +from .online import app __all__ = ["app"] diff --git a/containers/cleanair/cleanair/parsers/urbanair_parser/online/online.py b/containers/cleanair/cleanair/parsers/urbanair_parser/online/online.py new file mode 100644 index 000000000..602343b2b --- /dev/null +++ b/containers/cleanair/cleanair/parsers/urbanair_parser/online/online.py @@ -0,0 +1,8 @@ +import typer + +from .production import app as production +from .test import app as test + +app = typer.Typer(help="Online Run CLI") +app.add_typer(production, name="production") +app.add_typer(test, name="test") diff --git a/containers/cleanair/cleanair/parsers/urbanair_parser/production/main.py b/containers/cleanair/cleanair/parsers/urbanair_parser/online/production.py similarity index 51% rename from containers/cleanair/cleanair/parsers/urbanair_parser/production/main.py rename to containers/cleanair/cleanair/parsers/urbanair_parser/online/production.py index 5e9f94f3c..016f86a7f 100644 --- a/containers/cleanair/cleanair/parsers/urbanair_parser/production/main.py +++ b/containers/cleanair/cleanair/parsers/urbanair_parser/online/production.py @@ -1,22 +1,14 @@ """CLI for production models""" -from enum import Enum from pathlib import Path import typer -from ..experiment.main import metrics, run, setup, update, upload from ....types import ExperimentName from ..shared_args import ExperimentDir +from .utils import StaticDynamic, run_experiment_online app = typer.Typer(help="Production CLI") -class StaticDynamic(str, Enum): - """Is the model static or dynamic?""" - - dynamic = "dynamic" - static = "static" - - @app.command() def svgp( static_or_dynamic: StaticDynamic, experiment_root: Path = ExperimentDir @@ -26,11 +18,7 @@ def svgp( experiment_name = ExperimentName.production_svgp_dynamic else: experiment_name = ExperimentName.production_svgp_static - setup(experiment_name, experiment_root=experiment_root) - run(experiment_name, experiment_root=experiment_root) - update(experiment_name, experiment_root=experiment_root) - metrics(experiment_name, experiment_root=experiment_root) - upload(experiment_name, experiment_root=experiment_root) + run_experiment_online(experiment_name, experiment_root=experiment_root) @app.command() @@ -42,8 +30,4 @@ def mrdgp( experiment_name = ExperimentName.production_mrdgp_dynamic else: experiment_name = ExperimentName.production_mrdgp_static - setup(experiment_name, experiment_root=experiment_root) - run(experiment_name, experiment_root=experiment_root) - update(experiment_name, experiment_root=experiment_root) - metrics(experiment_name, experiment_root=experiment_root) - upload(experiment_name, experiment_root=experiment_root) + run_experiment_online(experiment_name, experiment_root=experiment_root) diff --git a/containers/cleanair/cleanair/parsers/urbanair_parser/online/test.py b/containers/cleanair/cleanair/parsers/urbanair_parser/online/test.py new file mode 100644 index 000000000..ecf062621 --- /dev/null +++ b/containers/cleanair/cleanair/parsers/urbanair_parser/online/test.py @@ -0,0 +1,33 @@ +"""CLI for production models""" + +from pathlib import Path +import typer +from ....types import ExperimentName +from ..shared_args import ExperimentDir +from .utils import StaticDynamic, run_experiment_online + +app = typer.Typer(help="Test CLI") + +# The test_svgp functions are not implemented +# @app.command() +# def svgp( +# static_or_dynamic: StaticDynamic, experiment_root: Path = ExperimentDir +# ) -> None: +# """Run the test SVGP model""" +# if static_or_dynamic == StaticDynamic.dynamic: +# experiment_name = ExperimentName.production_svgp_dynamic +# else: +# experiment_name = ExperimentName.production_svgp_static +# run_experiment_online(experiment_name, experiment_root=experiment_root) +# +# +# @app.command() +def mrdgp( + static_or_dynamic: StaticDynamic, experiment_root: Path = ExperimentDir +) -> None: + """Run the test MRDGP model""" + if static_or_dynamic == StaticDynamic.dynamic: + experiment_name = ExperimentName.production_mrdgp_dynamic + else: + experiment_name = ExperimentName.production_mrdgp_static + run_experiment_online(experiment_name, experiment_root=experiment_root) diff --git a/containers/cleanair/cleanair/parsers/urbanair_parser/online/utils.py b/containers/cleanair/cleanair/parsers/urbanair_parser/online/utils.py new file mode 100644 index 000000000..8eee4c096 --- /dev/null +++ b/containers/cleanair/cleanair/parsers/urbanair_parser/online/utils.py @@ -0,0 +1,20 @@ +from enum import Enum +from pathlib import Path + +from ..experiment.main import metrics, run, setup, update, upload +from ....types import ExperimentName + + +class StaticDynamic(str, Enum): + """Is the model static or dynamic?""" + + dynamic = "dynamic" + static = "static" + + +def run_experiment_online(experiment_name: ExperimentName, experiment_root: Path): + setup(experiment_name, experiment_root=experiment_root) + run(experiment_name, experiment_root=experiment_root) + update(experiment_name, experiment_root=experiment_root) + metrics(experiment_name, experiment_root=experiment_root) + upload(experiment_name, experiment_root=experiment_root) diff --git a/containers/cleanair/cli/urbanair b/containers/cleanair/cli/urbanair index dce461dab..c8aff9288 100644 --- a/containers/cleanair/cli/urbanair +++ b/containers/cleanair/cli/urbanair @@ -11,7 +11,7 @@ from cleanair.parsers.urbanair_parser import ( features, metrics, model, - production, + online, ) has_prophet = importlib.util.find_spec("fbprophet") @@ -26,7 +26,7 @@ app.add_typer(logs.app, name="logs") app.add_typer(features.app, name="features") app.add_typer(metrics.app, name="metrics") app.add_typer(model.app, name="model") -app.add_typer(production.app, name="production") +app.add_typer(online.app, name="online") if has_prophet and has_pystan: from cleanair.parsers.urbanair_parser import processors diff --git a/containers/scripts/mrdgp_dynamic.sh b/containers/scripts/mrdgp_dynamic.sh index 5ca57cccd..1b83a23e7 100644 --- a/containers/scripts/mrdgp_dynamic.sh +++ b/containers/scripts/mrdgp_dynamic.sh @@ -15,6 +15,6 @@ check_exit() { # set the secretfile filepath (if on own machine, use 'init production' to write to the production database) urbanair init local --secretfile "$DB_SECRET_FILE" >> $LOGFILE 2>&1 -urbanair production mrdgp dynamic >> $LOGFILE 2>&1 +urbanair online production mrdgp dynamic >> $LOGFILE 2>&1 urbanair logs upload $LOGFILE diff --git a/containers/scripts/mrdgp_static.sh b/containers/scripts/mrdgp_static.sh index 282f92037..af98d704b 100644 --- a/containers/scripts/mrdgp_static.sh +++ b/containers/scripts/mrdgp_static.sh @@ -15,6 +15,6 @@ check_exit() { # set the secretfile filepath (if on own machine, use 'init production' to write to the production database) urbanair init local --secretfile "$DB_SECRET_FILE" >> $LOGFILE 2>&1 -urbanair production mrdgp static >> $LOGFILE 2>&1 +urbanair online production mrdgp static >> $LOGFILE 2>&1 urbanair logs upload $LOGFILE diff --git a/containers/scripts/svgp_dynamic.sh b/containers/scripts/svgp_dynamic.sh index 5575d3f0f..111d7ccc8 100755 --- a/containers/scripts/svgp_dynamic.sh +++ b/containers/scripts/svgp_dynamic.sh @@ -15,6 +15,6 @@ check_exit() { # set the secretfile filepath (if on own machine, use 'init production' to write to the production database) urbanair init local --secretfile "$DB_SECRET_FILE" >> $LOGFILE 2>&1 -urbanair production svgp dynamic >> $LOGFILE 2>&1 +urbanair online production svgp dynamic >> $LOGFILE 2>&1 urbanair logs upload $LOGFILE diff --git a/containers/scripts/svgp_static.sh b/containers/scripts/svgp_static.sh index a668cf63a..a1476c5a2 100755 --- a/containers/scripts/svgp_static.sh +++ b/containers/scripts/svgp_static.sh @@ -15,6 +15,6 @@ check_exit() { # set the secretfile filepath (if on own machine: init production) urbanair init local --secretfile "$DB_SECRET_FILE" >> $LOGFILE 2>&1 -urbanair production svgp static >> $LOGFILE 2>&1 +urbanair online production svgp static >> $LOGFILE 2>&1 urbanair logs upload $LOGFILE