Skip to content

Commit

Permalink
load and run model together
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdudfield committed Nov 5, 2024
1 parent ee7611f commit 700c4f1
Showing 1 changed file with 29 additions and 33 deletions.
62 changes: 29 additions & 33 deletions india_forecast_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def app(timestamp: dt.datetime | None, write_to_db: bool, log_level: str):
app_run(timestamp=timestamp, write_to_db=write_to_db, log_level=log_level)


def app_run(timestamp: dt.datetime | None, write_to_db: bool=False, log_level: str= "info"):
def app_run(timestamp: dt.datetime | None, write_to_db: bool = False, log_level: str = "info"):
"""
Main function for running forecasts for sites in India
"""
Expand Down Expand Up @@ -308,7 +308,7 @@ def app_run(timestamp: dt.datetime | None, write_to_db: bool=False, log_level: s

# 2. Load data/models
all_model_configs = get_all_models(client_abbreviation=os.getenv("CLIENT_NAME", "ruvnl"))
models = []
successful_runs = 0
for model_config in all_model_configs.models:

asset_sites = pv_sites if model_config.asset_type == "pv" else wind_sites
Expand Down Expand Up @@ -337,39 +337,35 @@ def app_run(timestamp: dt.datetime | None, write_to_db: bool=False, log_level: s
)
ml_model.site_uuid = site.site_uuid

# TODO should we make this an object?
models.append(ml_model)
log.info(f"{asset_type} model loaded")

successful_runs = 0
for model in models:
# 3. Run model for each site
site_id = model.site_uuid
asset_type = model.asset_type
log.info(f"Running {asset_type} model for site={site_id}...")
forecast_values = run_model(model=model, site_id=site_id, timestamp=timestamp)

if forecast_values is None:
log.info(f"No forecast values for site_id={site_id}")
else:
# 4. Write forecast to DB or stdout
log.info(f"Writing forecast for site_id={site_id}")
forecast = {
"meta": {
"site_id": site_id,
"version": version,
"timestamp": timestamp,
},
"values": forecast_values,
}
save_forecast(
session,
forecast=forecast,
write_to_db=write_to_db,
ml_model_name=model.name,
ml_model_version=version,
)
successful_runs += 1
# 3. Run model for each site
site_id = ml_model.site_uuid
asset_type = ml_model.asset_type
log.info(f"Running {asset_type} model for site={site_id}...")
forecast_values = run_model(model=ml_model, site_id=site_id, timestamp=timestamp)

if forecast_values is None:
log.info(f"No forecast values for site_id={site_id}")
else:
# 4. Write forecast to DB or stdout
log.info(f"Writing forecast for site_id={site_id}")
forecast = {
"meta": {
"site_id": site_id,
"version": version,
"timestamp": timestamp,
},
"values": forecast_values,
}
save_forecast(
session,
forecast=forecast,
write_to_db=write_to_db,
ml_model_name=ml_model.name,
ml_model_version=version,
)
successful_runs += 1

log.info(f"Completed forecasts for {successful_runs} runs for {len(sites)} sites")
if successful_runs == len(sites):
Expand Down

0 comments on commit 700c4f1

Please sign in to comment.