Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add smooth blocks options #140

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion india_forecast_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def get_model(
hf_repo: str,
hf_version: str,
name: str,
smooth_blocks: Optional[int] = 0,
) -> PVNetModel:
"""
Instantiates and returns the forecast model ready for running inference
Expand All @@ -160,6 +161,7 @@ def get_model(
hf_repo: ID of the ML model used for the forecast
hf_version: Version of the ML model used for the forecast
name: Name of the ML model used for the forecast
smooth_blocks: Number of blocks to smooth the forecast by

Returns:
A forecasting model
Expand All @@ -169,7 +171,13 @@ def get_model(
model_cls = PVNetModel

model = model_cls(
asset_type, timestamp, generation_data, hf_repo=hf_repo, hf_version=hf_version, name=name
asset_type,
timestamp,
generation_data,
hf_repo=hf_repo,
hf_version=hf_version,
name=name,
smooth_blocks=smooth_blocks,
)
return model

Expand Down Expand Up @@ -363,6 +371,7 @@ def app_run(timestamp: dt.datetime | None, write_to_db: bool = False, log_level:
hf_repo=model_config.id,
hf_version=model_config.version,
name=model_config.name,
smooth_blocks=model_config.smooth_blocks,
)
ml_model.site_uuid = site.site_uuid

Expand Down
4 changes: 4 additions & 0 deletions india_forecast_app/models/all_models.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@ models:
version: ae07c15de064e1d03cf4bc02618b65c6d5b17e8e
client: ruvnl
asset_type: wind
smooth_blocks: 4
- name: windnet_india_mo
type: pvnet
id: openclimatefix/windnet_india
version: 546baded3d4216736d8ee8d6798d47235bd72b08
client: ruvnl
asset_type: wind
smooth_blocks: 7
- name: windnet_india_mo_v2
type: pvnet
id: openclimatefix/windnet_india
version: 165267d34500cf0c881ed70d9318421f4e0d10f1
client: ruvnl
asset_type: wind
smooth_blocks: 4
- name: windnet_india_mo_v3
type: pvnet
id: openclimatefix/windnet_india
version: 990bed9ad1dbb10515f830c181609b10e72c975e
client: ruvnl
asset_type: wind
smooth_blocks: 7
# RU client solar
- name: pvnet_india
type: pvnet
Expand Down
16 changes: 9 additions & 7 deletions india_forecast_app/models/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import datetime as dt
import math
import random
from typing import Optional

import pandas as pd
import pytz
Expand All @@ -21,13 +22,14 @@ def version(self):
return "0.0.0"

def __init__(
self,
asset_type: str,
timestamp: dt.datetime,
generation_data: dict[str, pd.DataFrame] = None,
hf_version: str = None,
hf_repo: str = None,
name: str = None,
self,
asset_type: str,
timestamp: dt.datetime,
generation_data: dict[str, pd.DataFrame] = None,
hf_version: str = None,
hf_repo: str = None,
name: str = None,
smooth_blocks: Optional[int] = 0,
):
"""Initializer for the model"""
self.asset_type = asset_type
Expand Down
16 changes: 11 additions & 5 deletions india_forecast_app/models/pvnet/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import shutil
import tempfile
from typing import Optional

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -63,6 +64,7 @@ def __init__(
hf_repo: str,
hf_version: str,
name: str,
smooth_blocks: Optional[int] = 0,
):
"""Initializer for the model"""

Expand All @@ -72,6 +74,7 @@ def __init__(
self.name = name
self.site_uuid = None
self.t0 = timestamp
self.smooth_blocks = smooth_blocks
log.info(f"Model initialised at t0={self.t0}")

self.client = os.getenv("CLIENT_NAME", "ruvnl")
Expand Down Expand Up @@ -181,14 +184,17 @@ def predict(self, site_id: str, timestamp: dt.datetime):
) * smooth_values[final_gen_index + idx]
log.debug(f"New values are {values_df['forecast_power_kw']}")

if self.asset_type == "wind":
# Smooth with a 1 hour rolling window
# Only smooth the wind else we introduce too much of a lag in the solar
# going up and down throughout the day
if self.smooth_blocks > 0:
log.info(f"Smoothing the forecast with {self.smooth_blocks} blocks")
values_df["forecast_power_kw"] = (
values_df["forecast_power_kw"].rolling(4, min_periods=1).mean().astype(int)
values_df["forecast_power_kw"]
.rolling(window=self.smooth_blocks, min_periods=1, center=True)
.mean()
)

# convert to int
values_df["forecast_power_kw"] = values_df["forecast_power_kw"].astype(int)

# remove any negative values
values_df["forecast_power_kw"] = values_df["forecast_power_kw"].clip(lower=0.0)

Expand Down
16 changes: 12 additions & 4 deletions india_forecast_app/models/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,22 @@ class Model(BaseModel):
60,
title="Average Minutes",
description="The number of minutes that results are average over when "
"calculating adjuster values. "
"For solar site with regular data, 15 should be used. "
"For wind sites, 60 minutes should be used.",
"calculating adjuster values. "
"For solar site with regular data, 15 should be used. "
"For wind sites, 60 minutes should be used.",
)
smooth_blocks: int = Field(
0,
title="Smooth Blocks",
description="The number of blocks to smooth the forecast by. "
"We use a central smoothing and each block is 15mins, "
"so 5 would be smoothing from 30s before and afterwards",
)


class Models(BaseModel):
""" A group of ml models """
"""A group of ml models"""

models: List[Model] = Field(
..., title="Models", description="A list of models to use for the forecast"
)
Expand Down
Loading