Skip to content

Commit

Permalink
♻️ Expanded *production* cli to an *online* cli to add test functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbrandreth committed Aug 4, 2021
1 parent 39625c4 commit 1c9489a
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""CLI for production models"""

from .main import app
from .online import app

__all__ = ["app"]
Original file line number Diff line number Diff line change
@@ -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")
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand All @@ -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)
Original file line number Diff line number Diff line change
@@ -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.test_mrdgp_dynamic
else:
experiment_name = ExperimentName.test_mrdgp_static
run_experiment_online(experiment_name, experiment_root=experiment_root)
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions containers/cleanair/cleanair/types/experiment_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class ExperimentName(str, Enum):
production_svgp_dynamic = "production_svgp_dynamic"
production_svgp_static = "production_svgp_static"

# test names
test_mrdgp_dynamic = "test_mrdgp_dynamic"
test_mrdgp_static = "test_mrdgp_static"


class ExperimentConfig(BaseModel):
"""Settings for an experiment"""
Expand Down
4 changes: 2 additions & 2 deletions containers/cleanair/cli/urbanair
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ from cleanair.parsers.urbanair_parser import (
features,
metrics,
model,
production,
online,
)

has_prophet = importlib.util.find_spec("fbprophet")
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion containers/scripts/mrdgp_dynamic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion containers/scripts/mrdgp_static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion containers/scripts/svgp_dynamic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion containers/scripts/svgp_static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1c9489a

Please sign in to comment.