mmmapms's picture
Create app.py
e67fcfa verified
raw
history blame
5.86 kB
import streamlit as st
import pandas as pd
import numpy as np
import plotly.graph_objs as go
from io import BytesIO
# Function to convert df to csv for download
def convert_df_to_csv(df):
return df.to_csv(index=False).encode('utf-8')
# Load your data
df = pd.read_csv('Predictions.csv')
df['Date'] = pd.to_datetime(df['Date'], dayfirst=True)
df_filtered = df.dropna(subset=['Price'])
# Load input dataC:\Users\mmascare\Documents\Code\Forecast_API\Scrape\datasets\DATA_ELIA.csv
df_input = pd.read_csv('DATA_ELIA.csv')
df_input['Date'] = pd.to_datetime(df_input['Date'], dayfirst=True)
# Determine the first and last date
min_date_allowed = df_input['Date'].min().date()
max_date_allowed = df_input['Date'].max().date()
min_date_allowed_pred = df_filtered['Date'].min().date()
max_date_allowed_pred = df_filtered['Date'].max().date()
end_date = df['Date'].max().date()
start_date = end_date - pd.Timedelta(days=7)
st.title("Belgium: Electricity Price Forecasting")
# Sidebar for inputs
with st.sidebar:
st.write("### Variables Selection for Graph")
st.write("Select which variables you'd like to include in the graph. This will affect the displayed charts and available data for download.")
selected_variables = st.multiselect("Select variables to display:", options=['Price', 'DNN', 'LEAR', 'Persis'], default=['Price', 'DNN', 'LEAR', 'Persis'])
st.write("### Date Range for Metrics Calculation")
st.write("Select the date range to calculate the metrics for the predictions. This will influence the accuracy metrics displayed below. The complete dataset ranges from 10/03/2024 until today.")
start_date_pred, end_date_pred = st.date_input("Select Date Range for Metrics Calculation:", [min_date_allowed_pred, max_date_allowed_pred])
# Main content
if not selected_variables:
st.warning("Please select at least one variable to display.")
else:
# Plotting
st.write("## Belgian Day-Ahead Electricity Prices")
temp_df = df[(df['Date'] >= pd.Timestamp(start_date)) & (df['Date'] <= pd.Timestamp(end_date))]
fig = go.Figure()
# Updated labels for each variable
variable_labels = {
'Price': 'Real Price',
'DNN': 'DNN Forecast',
'LEAR': 'LEAR Forecast',
'Persis': 'Persistence Forecast'
}
for variable in selected_variables:
fig.add_trace(go.Scatter(x=temp_df['Date'], y=temp_df[variable], mode='lines', name=variable_labels[variable]))
fig.update_layout(xaxis_title="Date", yaxis_title="Price [EUR/MWh]")
st.plotly_chart(fig, use_container_width=True)
st.write("The graph presented here illustrates the day-ahead electricity price forecasts for Belgium, covering the period from one week ago up to tomorrow. It incorporates predictions from three distinct models: DNN (Deep Neural Networks), LEAR (Lasso Estimated AutoRegressive), and Persistence, alongside the actual electricity prices up until today. The Persistence model, a seasonal naive forecaster, bases its predictions on the prices from one week ago. More information regarding the DNN and LEAR models can be found in article titled: Forecasting day-ahead electricity prices: A review of state-of-the-art algorithms, best practices and an open-access benchmark, published in Applied Energy, volume 293, pages 116983, in 2021. Authored by Jesus Lago, Grzegorz Marcjasz, Bart De Schutter, and Rafał Weron.")
# Download Predictions Button
st.write("## Download Predictions")
st.write("Download Day-Ahead Price & Model Predictions: Receive a detailed CSV file comprising the actual day-ahead electricity prices and the corresponding LEAR and DNN model forecasts. This data enables you to compare performance and accuracy.")
csv = convert_df_to_csv(df)
st.download_button(
label="Download Predictions CSV",
data=csv,
file_name='predictions.csv',
mime='text/csv',
)
# Calculating and displaying metrics
if start_date_pred and end_date_pred:
st.header("Accuracy Metrics")
st.write("Evaluate the forecasting accuracy of our models with key performance indicators. The table summarizes the Mean Absolute Error (MAE), Symmetric Mean Absolute Percentage Error (SMAPE), and Root Mean Square Error (RMSE) for the Persistence, DNN and LEAR models over your selected date range. Lower values indicate higher precision and reliability of the forecasts.")
filtered_df = df_filtered[(df_filtered['Date'] >= pd.Timestamp(start_date_pred)) & (df_filtered['Date'] <= pd.Timestamp(end_date_pred))]
# Here you would calculate your metrics based on filtered_df
# For demonstration, let's assume these are your metrics
p_real = filtered_df['Price']
p_pred_dnn = filtered_df['DNN']
p_pred_lear = filtered_df['LEAR']
p_pred_persis = filtered_df['Persis']
# Recalculate the metrics
mae_dnn = np.mean(np.abs(p_real - p_pred_dnn))
smape_dnn = 100 * np.mean(np.abs(p_real - p_pred_dnn) / ((np.abs(p_real) + np.abs(p_pred_dnn)) / 2))
rmse_dnn = np.sqrt(np.mean((p_real - p_pred_dnn) ** 2))
mae_lear = np.mean(np.abs(p_real - p_pred_lear))
smape_lear = 100 * np.mean(np.abs(p_real - p_pred_lear) / ((np.abs(p_real) + np.abs(p_pred_lear)) / 2))
rmse_lear = np.sqrt(np.mean((p_real - p_pred_lear) ** 2))
mae_persis = np.mean(np.abs(p_real - p_pred_persis))
smape_persis = 100 * np.mean(np.abs(p_real - p_pred_persis) / ((np.abs(p_real) + np.abs(p_pred_persis)) / 2))
rmse_persis = np.sqrt(np.mean((p_real - p_pred_persis) ** 2))
new_metrics_df = pd.DataFrame({
'Metric': ['MAE', 'SMAPE', 'RMSE'],
'Persistence': [f"{mae_persis:.2f}", f"{smape_persis:.2f}%", f"{rmse_persis:.2f}"],
'DNN': [f"{mae_dnn:.2f}", f"{smape_dnn:.2f}%", f"{rmse_dnn:.2f}"],
'LEAR': [f"{mae_lear:.2f}", f"{smape_lear:.2f}%", f"{rmse_lear:.2f}"]
})
st.dataframe(new_metrics_df, hide_index=True)