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

pvsite-datamodel integration & fake forecasts #1

Merged
merged 18 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion india_forecast_app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""India Forecast App"""
__version__ = "0.1.0"
__version__ = "0.1.0"
140 changes: 135 additions & 5 deletions india_forecast_app/app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,140 @@
import datetime as dt
import logging

import click

from .model import DummyModel

log = logging.getLogger(__name__)


def _get_site_ids() -> list[str]:
"""
Gets all avaiable site_ids in India

Returns:
A list of site_ids
"""

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we add loggin statement here, saying found X sites

return [
"b0579f31-70d9-4682-962e-4e2b30fa1e85",
"d0146492-90d2-41bf-9e44-153032492bad",
]


def _get_model():
"""
Instantiates and returns the forecast model ready for running inference

Returns:
A forecasting model
"""

model = DummyModel()
return model


def _run_model(model, site_id: str, timestamp: dt.datetime):
"""
Runs inference on model for the given site & timestamp

Args:
model: A forecasting model
site_id: A specific site ID
timestamp: timestamp to run a forecast for

Returns:
A forecast or None if model inference fails
"""

try:
forecast = model.predict(site_id=site_id, timestamp=timestamp)
except Exception:
log.error(
f"Error while running model.predict for site_id={site_id}. Skipping",
exc_info=True,
)
return None

return forecast


def _save_forecast(site_id: str, timestamp: dt.datetime, forecast, write_to_db: bool):
"""
Saves a forecast for a given site & timestamp

Args:
site_id: A specific site ID
timestamp: timestamp to run a forecast for
forecast: a forecast containing predicted generation values for the given site
write_to_db: If true, forecast values are written to db, otherwise to stdout

Raises:
IOError: An error if database save fails
"""

if write_to_db:
pass
peterdudfield marked this conversation as resolved.
Show resolved Hide resolved
else:
log.info(
f"site_id={site_id}, timestamp={timestamp}, forecast values={forecast}"
)


@click.command()
@click.option("--site", help="Site ID")
def app(site):
"""Runs the forecast for a given site"""
print(f"Running forecast for site: {site}")
@click.option(
"--date",
"-d",
"timestamp",
type=click.DateTime(formats=["%Y-%m-%d-%H-%M"]),
default=None,
help='Date-time (UTC) at which we make the prediction. Defaults to "now".',
)
@click.option(
"--write-to-db",
is_flag=True,
default=False,
help="Set this flag to actually write the results to the database.",
)
@click.option(
"--log-level",
default="info",
help="Set the python logging log level",
show_default=True,
)
def app(timestamp: dt.datetime | None, write_to_db: bool, log_level: str):
"""
Main function for running forecasts for sites in India
"""
logging.basicConfig(level=getattr(logging, log_level.upper()))

if timestamp is None:
timestamp = dt.datetime.utcnow()
log.info('Timestamp omitted - will generate forecasts for "now"')

# 1. Get sites
log.info("Getting sites")
site_ids = _get_site_ids()

# 2. Load model
log.info("Loading model")
model = _get_model()

# 3. Run model for each site
log.info("Running model for each site")
for site_id in site_ids:
forecast = _run_model(model=model, site_id=site_id, timestamp=timestamp)

if forecast is not None:
# 4. Write forecast to DB or stdout
log.info(f"Writing forecast for site_id={site_id}")
_save_forecast(
site_id=site_id,
timestamp=timestamp,
forecast=forecast,
write_to_db=write_to_db,
)


if __name__ == "__main__":
app()
app()
103 changes: 103 additions & 0 deletions india_forecast_app/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import datetime as dt
import math
import random

# step defines the time interval between each data point
step: dt.timedelta = dt.timedelta(minutes=15)


class DummyModel:
def __init__(self):
pass

def predict(self, site_id: str, timestamp: dt.datetime):
return self._generate_dummy_forecast()

def _generate_dummy_forecast(self):
# Get the window
start, end = _getWindow()
numSteps = int((end - start) / step)
values: list[dict] = []

for i in range(numSteps):
time = start + i * step
_yield = _basicSolarYieldFunc(int(time.timestamp()))
values.append({"time": time, "power_kw": int(_yield)})

return values


def _getWindow() -> tuple[dt.datetime, dt.datetime]:
"""Returns the start and end of the window for timeseries data."""
# Window start is the beginning of the day two days ago
start = (dt.datetime.now(tz=dt.UTC) - dt.timedelta(days=2)).replace(
hour=0,
minute=0,
second=0,
microsecond=0,
)
# Window end is the beginning of the day two days ahead
end = (dt.datetime.now(tz=dt.UTC) + dt.timedelta(days=2)).replace(
hour=0,
minute=0,
second=0,
microsecond=0,
)
return (start, end)


def _basicSolarYieldFunc(timeUnix: int, scaleFactor: int = 10000) -> float:
"""Gets a fake solar yield for the input time.

The basic yield function is built from a sine wave
with a period of 24 hours, peaking at 12 hours.
Further convolutions modify the value according to time of year.

Args:
timeUnix: The time in unix time.
scaleFactor: The scale factor for the sine wave.
A scale factor of 10000 will result in a peak yield of 10 kW.
"""
# Create a datetime object from the unix time
time = dt.datetime.fromtimestamp(timeUnix, tz=dt.UTC)
# The functions x values are hours, so convert the time to hours
hour = time.day * 24 + time.hour + time.minute / 60 + time.second / 3600

# scaleX makes the period of the function 24 hours
scaleX = math.pi / 12
# translateX moves the minimum of the function to 0 hours
translateX = -math.pi / 2
# translateY modulates the base function based on the month.
# * + 0.5 at the summer solstice
# * - 0.5 at the winter solstice
translateY = math.sin((math.pi / 6) * time.month + translateX) / 2.0

# basefunc ranges between -1 and 1 with a period of 24 hours,
# peaking at 12 hours.
# translateY changes the min and max to range between 1.5 and -1.5
# depending on the month.
basefunc = math.sin(scaleX * hour + translateX) + translateY
# Remove negative values
basefunc = max(0, basefunc)
# Steepen the curve. The divisor is based on the max value
basefunc = basefunc**4 / 1.5**4

# Instead of completely random noise, apply based on the following process:
# * A base noise function which is the product of long and short sines
# * The resultant function modulates with very small amplitude around 1
noise = (math.sin(math.pi * time.hour) / 20) * (
math.sin(math.pi * time.hour / 3)
) + 1
noise = noise * random.random() / 20 + 0.97

# Create the output value from the base function, noise, and scale factor
output = basefunc * noise * scaleFactor

return output


def _basicWindYieldFunc(timeUnix: int, scaleFactor: int = 10000) -> float:
"""Gets a fake wind yield for the input time."""
output = min(scaleFactor, scaleFactor * 10 * random.random())

return output
Loading