-
-
Notifications
You must be signed in to change notification settings - Fork 21
add horizon to csv route & format func #114
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import logging | ||
|
||
import pandas as pd | ||
from fastapi import HTTPException | ||
import pytest | ||
|
||
from india_api.internal import PredictedPower, ActualPower, SiteProperties | ||
|
||
from pvsite_datamodel.sqlmodels import APIRequestSQL | ||
|
||
from .client import Client | ||
from .conftest import forecast_values | ||
from ...models import ForecastHorizon | ||
from ...service.csv import format_csv_and_created_time | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
# TODO add list of test that are here | ||
|
||
|
||
@pytest.fixture() | ||
def client(engine, db_session): | ||
"""Hooks Client into pytest db_session fixture""" | ||
client = Client(database_url=str(engine.url)) | ||
client.session = db_session | ||
|
||
return client | ||
|
||
# Skip for now | ||
@pytest.mark.skip(reason="Not finished yet") | ||
class TestCsvExport: | ||
def test_format_csv_and_created_time(self, client, forecast_values_wind) -> None: | ||
"""Test the format_csv_and_created_time function.""" | ||
forecast_values_wind = client.get_predicted_wind_power_production_for_location( | ||
location="testID" | ||
) | ||
assert forecast_values_wind is not None | ||
assert len(forecast_values_wind) > 0 | ||
assert isinstance(forecast_values_wind[0], PredictedPower) | ||
|
||
result = format_csv_and_created_time( | ||
forecast_values_wind, | ||
ForecastHorizon.latest, | ||
) | ||
assert isinstance(result, tuple) | ||
assert isinstance(result[0], pd.DataFrame) | ||
assert isinstance(result[1], pd.Timestamp) | ||
logging.info(f"CSV created at: {result[1]}") | ||
logging.info(f"CSV content: {result[0].head()}") | ||
# Check the shape of the DataFrame | ||
# The shape should match the number of forecast values | ||
# and the number of columns in the DataFrame | ||
# The DataFrame should have 3 columns: Date [IST], Time, PowerMW | ||
assert result[0].shape[1] == 3 | ||
# Check the first row of the DataFrame | ||
# The date of the first row should be the nearest rounded 15min from now | ||
rounded_15_min = pd.Timestamp.now(tz="Asia/Kolkata").round("15min") | ||
assert result[0].iloc[0]["Time"] == rounded_15_min.strftime("%H:%M") | ||
# Check the number of rows in the DataFrame | ||
# For the latest forecast, it should be the number of | ||
# forecast values after now | ||
forecast_values_from_now = [ | ||
value for value in forecast_values_wind if value.Time >= rounded_15_min | ||
] | ||
assert result[0].shape[0] == len(forecast_values_from_now) | ||
# Check the column names | ||
assert list(result[0].columns) == ["Date [IST]", "Time", "PowerMW"] | ||
# Check the data types of the columns | ||
assert result[0]["Date [IST]"].dtype == "datetime64[ns, Asia/Kolkata]" | ||
assert result[0]["Time"].dtype == "object" | ||
assert result[0]["PowerMW"].dtype == "float64" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -186,35 +186,61 @@ def get_forecast_timeseries_route( | |
response_class=FileResponse, | ||
include_in_schema=False, | ||
) | ||
def get_forecast_da_csv( | ||
def get_forecast_csv( | ||
source: ValidSourceDependency, | ||
region: str, | ||
db: DBClientDependency, | ||
auth: dict = Depends(auth), | ||
forecast_horizon: Optional[ForecastHorizon] = ForecastHorizon.latest, | ||
forecast_horizon_minutes: Optional[int] = 0, | ||
): | ||
""" | ||
Route to get the day ahead forecast as a CSV file. | ||
By default, the CSV file will be for the latest forecast, from now forwards. | ||
The forecast_horizon can be set to 'latest', 'day_ahead' or 'horizon'. | ||
- latest: The latest forecast, from now forwards. | ||
- day_ahead: The forecast for the next day, from midnight. | ||
- horizon: The forecast for the next horizon_horizon_minutes minutes, from default forecast history start. | ||
The forecast_horizon_minutes is only used if the forecast_horizon is set to 'horizon'. | ||
""" | ||
|
||
forcasts: GetForecastGenerationResponse = get_forecast_timeseries_route( | ||
if forecast_horizon is not None: | ||
if forecast_horizon not in [ForecastHorizon.latest, ForecastHorizon.day_ahead, ForecastHorizon.horizon]: | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
detail=f"Invalid forecast_horizon {forecast_horizon}. Must be 'latest', 'day_ahead', or 'horizon.", | ||
) | ||
|
||
forecasts: GetForecastGenerationResponse = get_forecast_timeseries_route( | ||
source=source, | ||
region=region, | ||
db=db, | ||
auth=auth, | ||
forecast_horizon=ForecastHorizon.day_ahead, | ||
forecast_horizon=forecast_horizon, | ||
forecast_horizon_minutes=forecast_horizon_minutes, | ||
smooth_flag=False, | ||
) | ||
|
||
# format to dataframe | ||
df, created_time = format_csv_and_created_time(forcasts.values) | ||
df, created_time = format_csv_and_created_time(forecasts.values, forecast_horizon=forecast_horizon) | ||
|
||
# make file format | ||
now_ist = pd.Timestamp.now(tz="Asia/Kolkata") | ||
tomorrow_ist = df["Date [IST]"].iloc[0] | ||
csv_file_path = f"{region}_{source}_da_{tomorrow_ist}.csv" | ||
match forecast_horizon: | ||
case ForecastHorizon.latest: | ||
forecast_type = "intraday" | ||
case ForecastHorizon.day_ahead: | ||
forecast_type = "da" | ||
case ForecastHorizon.horizon: | ||
forecast_type = f"horizon_{forecast_horizon_minutes}" | ||
case _: | ||
# this shouldn't happen but will handle if class is changed | ||
|
||
forecast_type = "default" | ||
csv_file_path = f"{region}_{source}_{forecast_type}_{tomorrow_ist}.csv" | ||
|
||
description = ( | ||
f"Forecast for {region} for {source} for {tomorrow_ist}. " | ||
f"Forecast for {region} for {source}, {forecast_type}, for {tomorrow_ist}. " | ||
f"The Forecast was created at {created_time} and downloaded at {now_ist}" | ||
) | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.