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

End date + NMAE by capacity #156

Merged
merged 4 commits into from
May 17, 2024
Merged
Changes from 2 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
46 changes: 33 additions & 13 deletions src/pvsite_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import pandas as pd
import streamlit as st
from datetime import datetime, timedelta, time, timezone
from pvsite_datamodel.read import get_site_by_uuid
from sqlalchemy.orm import Session
from pvsite_datamodel.connection import DatabaseConnection
from pvsite_datamodel.read import (
get_all_sites,
Expand All @@ -20,15 +22,17 @@ def pvsite_forecast_page():
unsafe_allow_html=True,
)
# get site_uuids from database
url = 'os.environ["SITES_DB_URL"]'
url = 'postgresql://main:vPV%[email protected]:5433/indiadbdevelopment'
ADIMANV marked this conversation as resolved.
Show resolved Hide resolved
connection = DatabaseConnection(url=url, echo=True)
with connection.get_session() as session:
site_uuids = get_all_sites(session=session)
site_uuids = [
sites.site_uuid for sites in site_uuids if sites.site_uuid is not None
]
site_selection = st.sidebar.selectbox("Select sites by site_uuid", site_uuids,)
day_after_tomorrow = datetime.today() + timedelta(days=3)
starttime = st.sidebar.date_input("Start Date", min_value=datetime.today() - timedelta(days=365), max_value=datetime.today())
endtime = st.sidebar.date_input("End Date",day_after_tomorrow)

forecast_type = st.sidebar.selectbox("Select Forecast Type", ["Latest", "Forecast_horizon", "DA"], 0)

Expand All @@ -39,7 +43,7 @@ def pvsite_forecast_page():
created = datetime.now()
else:
created = datetime.fromisoformat(created)
st.write("Forecast for", site_selection, "starting on", starttime, "created by", created)
st.write("Forecast for", site_selection, "starting on", starttime, "created by", created, "ended on", endtime)
else:
created = None

Expand Down Expand Up @@ -70,9 +74,9 @@ def pvsite_forecast_page():
created_by=created,
forecast_horizon_minutes=forecast_horizon,
day_ahead_hours=day_ahead_hours,
day_ahead_timezone_delta_hours=day_ahead_timezone_delta_hours
day_ahead_timezone_delta_hours=day_ahead_timezone_delta_hours,
end_utc=endtime,
)

forecasts = forecasts.values()

for forecast in forecasts:
Expand All @@ -85,7 +89,9 @@ def pvsite_forecast_page():
session=session,
site_uuids=[site_selection],
start_utc=starttime,
end_utc=endtime,
)
capacity = get_site_capacity(session = session, site_uuidss = site_selection)

yy = [generation.generation_power_kw for generation in generations if generation is not None]
xx = [generation.start_utc for generation in generations if generation is not None]
Expand Down Expand Up @@ -152,37 +158,51 @@ def convert_df(df: pd.DataFrame):
csv = convert_df(df)
now = datetime.now().isoformat()

#MAE Calculator
#MAE and NMAE Calculator
mae_kw = (df['generation_power_kw'] - df['forecast_power_kw']).abs().mean()
mean_generation = df['generation_power_kw'].mean()
nmae = mae_kw / mean_generation
nmae_rounded = round(nmae,ndigits=4)
nmae_rounded = round(nmae*100,ndigits=4)
nma2 = (df['generation_power_kw'] - df['forecast_power_kw']).abs()
gen = df['generation_power_kw']
nmae2 = nma2/gen
nmae2_mean = nmae2.mean()
nmae2_rounded = round(nmae2_mean,ndigits=4)
mae_rounded_kw = round(mae_kw,ndigits=3)
nmae2_rounded = round(nmae2_mean*100,ndigits=4)
mae_rounded_kw = round(mae_kw*100,ndigits=3)
mae_rounded_mw = round(mae_kw/1000,ndigits=3)
nmae_capacity = mae_kw / capacity
nmae_rounded_capcity = round(nmae_capacity*100,ndigits=3)

if resample is None:
st.caption("Please resample to '15T' to get MAE")

elif mae_rounded_kw < 2000:
st.write(f"Mean Absolute Error {mae_rounded_kw} KW")
st.write(f"Normalised Mean Absolute Error is : {nmae_rounded*100} %")
st.write(f"Normalised Mean Absolute Error is : {nmae_rounded} %")
st.caption(f"NMAE is calculated by MAE / (mean generation)")
st.write(f"Normalised Mean Absolute Error is : {nmae2_rounded*100} %")
st.write(f"Normalised Mean Absolute Error is : {nmae2_rounded} %")
st.caption(f"NMAE is calculated by current generation (kw)")
st.write(f"Normalised Mean Absolute Error is : {nmae_rounded_capcity} %")
st.caption(f"NMAE is calculated by generation capacity (mw)")


else:
st.write(f"Mean Absolute Error {mae_rounded_mw} MW")
st.write(f"Normalised Mean Absolute Error is : {nmae_rounded*100} %")
st.write(f"Normalised Mean Absolute Error is : {nmae_rounded} %")
st.caption(f"NMAE is calculated by MAE / (mean generation)")
st.write(f"Normalised Mean Absolute Error is : {nmae2_rounded*100} %")
st.write(f"Normalised Mean Absolute Error is : {nmae2_rounded} %")
st.caption(f"NMAE is calculated by current generation (kw)")
st.write(f"Normalised Mean Absolute Error is : {nmae_rounded_capcity} %")
st.caption(f"NMAE is calculated by generation capacity (mw)")

#CSV download button
st.download_button(
label="Download data as CSV",
data=csv,
file_name=f'site_forecast_{site_selection}_{now}.csv',
mime='text/csv',
)
)
def get_site_capacity(session : Session , site_uuidss: str) -> float:
site = get_site_by_uuid(session, site_uuidss)
capacity_kw = site.capacity_kw
return capacity_kw
Loading