Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -49,13 +49,17 @@ with st.sidebar:
|
|
49 |
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.")
|
50 |
start_date_pred, end_date_pred = st.date_input("Select Date Range for Metrics Calculation:", [min_date_allowed_pred, max_date_allowed_pred])
|
51 |
|
|
|
|
|
|
|
|
|
52 |
# Main content
|
53 |
if not selected_variables:
|
54 |
st.warning("Please select at least one variable to display.")
|
55 |
else:
|
56 |
# Plotting
|
57 |
st.write("## Belgian Day-Ahead Electricity Prices")
|
58 |
-
temp_df = df[(df['Date'] >= pd.Timestamp(start_date))]
|
59 |
fig = go.Figure()
|
60 |
|
61 |
# Updated labels for each variable
|
@@ -71,18 +75,40 @@ else:
|
|
71 |
|
72 |
fig.update_layout(xaxis_title="Date", yaxis_title="Price [EUR/MWh]")
|
73 |
st.plotly_chart(fig, use_container_width=True)
|
74 |
-
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
-
# Download Predictions Button
|
77 |
-
st.write("## Download Predictions")
|
78 |
-
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.")
|
79 |
-
csv = convert_df_to_csv(df)
|
80 |
-
st.download_button(
|
81 |
-
label="Download Predictions CSV",
|
82 |
-
data=csv,
|
83 |
-
file_name='predictions.csv',
|
84 |
-
mime='text/csv',
|
85 |
-
)
|
86 |
|
87 |
# Calculating and displaying metrics
|
88 |
if start_date_pred and end_date_pred:
|
@@ -102,18 +128,25 @@ if start_date_pred and end_date_pred:
|
|
102 |
smape_dnn = 100 * np.mean(np.abs(p_real - p_pred_dnn) / ((np.abs(p_real) + np.abs(p_pred_dnn)) / 2))
|
103 |
rmse_dnn = np.sqrt(np.mean((p_real - p_pred_dnn) ** 2))
|
104 |
|
|
|
105 |
mae_lear = np.mean(np.abs(p_real - p_pred_lear))
|
106 |
smape_lear = 100 * np.mean(np.abs(p_real - p_pred_lear) / ((np.abs(p_real) + np.abs(p_pred_lear)) / 2))
|
107 |
rmse_lear = np.sqrt(np.mean((p_real - p_pred_lear) ** 2))
|
108 |
|
|
|
109 |
mae_persis = np.mean(np.abs(p_real - p_pred_persis))
|
110 |
smape_persis = 100 * np.mean(np.abs(p_real - p_pred_persis) / ((np.abs(p_real) + np.abs(p_pred_persis)) / 2))
|
111 |
rmse_persis = np.sqrt(np.mean((p_real - p_pred_persis) ** 2))
|
112 |
|
|
|
113 |
new_metrics_df = pd.DataFrame({
|
114 |
'Metric': ['MAE', 'SMAPE', 'RMSE'],
|
115 |
'Persistence': [f"{mae_persis:.2f}", f"{smape_persis:.2f}%", f"{rmse_persis:.2f}"],
|
116 |
'DNN': [f"{mae_dnn:.2f}", f"{smape_dnn:.2f}%", f"{rmse_dnn:.2f}"],
|
117 |
'LEAR': [f"{mae_lear:.2f}", f"{smape_lear:.2f}%", f"{rmse_lear:.2f}"]
|
118 |
})
|
119 |
-
st.dataframe(new_metrics_df, hide_index=True)
|
|
|
|
|
|
|
|
|
|
49 |
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.")
|
50 |
start_date_pred, end_date_pred = st.date_input("Select Date Range for Metrics Calculation:", [min_date_allowed_pred, max_date_allowed_pred])
|
51 |
|
52 |
+
st.write("### Model Selection for Scatter Plot")
|
53 |
+
model_selection = st.selectbox("Select which model's predictions to display:", options=['DNN', 'LEAR', 'Persistence'], index=0) # Default to 'DNN'
|
54 |
+
|
55 |
+
|
56 |
# Main content
|
57 |
if not selected_variables:
|
58 |
st.warning("Please select at least one variable to display.")
|
59 |
else:
|
60 |
# Plotting
|
61 |
st.write("## Belgian Day-Ahead Electricity Prices")
|
62 |
+
temp_df = df[(df['Date'] >= pd.Timestamp(start_date))] #& (df['Date'] <= pd.Timestamp(end_date))]
|
63 |
fig = go.Figure()
|
64 |
|
65 |
# Updated labels for each variable
|
|
|
75 |
|
76 |
fig.update_layout(xaxis_title="Date", yaxis_title="Price [EUR/MWh]")
|
77 |
st.plotly_chart(fig, use_container_width=True)
|
78 |
+
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.")
|
79 |
+
|
80 |
+
|
81 |
+
if not selected_variables:
|
82 |
+
st.warning("Please select at least one variable to display.")
|
83 |
+
else:
|
84 |
+
# Plotting
|
85 |
+
st.write("## Scatter Plot: Real Price vs Model Predictions")
|
86 |
+
# Filter based on the selected date range for plotting
|
87 |
+
plot_df = df[(df['Date'] >= pd.Timestamp(min_date_allowed_pred)) & (df['Date'] <= pd.Timestamp(max_date_allowed_pred))]
|
88 |
+
|
89 |
+
model_column = model_selection
|
90 |
+
if model_selection == 'Persistence':
|
91 |
+
model_column = 'Persis' # Assuming the DataFrame uses 'Persis' as the column name
|
92 |
+
|
93 |
+
# Create the scatter plot
|
94 |
+
fig = go.Figure()
|
95 |
+
fig.add_trace(go.Scatter(x=plot_df['Price'], y=plot_df[model_column], mode='markers', name=f"Real Price vs {model_selection} Predictions"))
|
96 |
+
|
97 |
+
# Calculate the line of best fit
|
98 |
+
m, b = np.polyfit(plot_df['Price'], plot_df[model_column], 1)
|
99 |
+
# Calculate the y-values based on the line of best fit
|
100 |
+
regression_line = m * plot_df['Price'] + b
|
101 |
+
|
102 |
+
# Format the equation to display as the legend name
|
103 |
+
equation = f"y = {m:.2f}x + {b:.2f}"
|
104 |
+
|
105 |
+
# Add the line of best fit to the figure with the equation as the legend name
|
106 |
+
fig.add_trace(go.Scatter(x=plot_df['Price'], y=regression_line, mode='lines', name=equation, line=dict(color='black')))
|
107 |
+
|
108 |
+
# Update layout with appropriate titles
|
109 |
+
fig.update_layout(xaxis_title="Real Price [EUR/MWh]", yaxis_title=f"{model_selection} Predictions [EUR/MWh]", title=f"Scatter Plot of Real Price vs {model_selection} Predictions")
|
110 |
+
st.plotly_chart(fig, use_container_width=True)
|
111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
# Calculating and displaying metrics
|
114 |
if start_date_pred and end_date_pred:
|
|
|
128 |
smape_dnn = 100 * np.mean(np.abs(p_real - p_pred_dnn) / ((np.abs(p_real) + np.abs(p_pred_dnn)) / 2))
|
129 |
rmse_dnn = np.sqrt(np.mean((p_real - p_pred_dnn) ** 2))
|
130 |
|
131 |
+
|
132 |
mae_lear = np.mean(np.abs(p_real - p_pred_lear))
|
133 |
smape_lear = 100 * np.mean(np.abs(p_real - p_pred_lear) / ((np.abs(p_real) + np.abs(p_pred_lear)) / 2))
|
134 |
rmse_lear = np.sqrt(np.mean((p_real - p_pred_lear) ** 2))
|
135 |
|
136 |
+
|
137 |
mae_persis = np.mean(np.abs(p_real - p_pred_persis))
|
138 |
smape_persis = 100 * np.mean(np.abs(p_real - p_pred_persis) / ((np.abs(p_real) + np.abs(p_pred_persis)) / 2))
|
139 |
rmse_persis = np.sqrt(np.mean((p_real - p_pred_persis) ** 2))
|
140 |
|
141 |
+
|
142 |
new_metrics_df = pd.DataFrame({
|
143 |
'Metric': ['MAE', 'SMAPE', 'RMSE'],
|
144 |
'Persistence': [f"{mae_persis:.2f}", f"{smape_persis:.2f}%", f"{rmse_persis:.2f}"],
|
145 |
'DNN': [f"{mae_dnn:.2f}", f"{smape_dnn:.2f}%", f"{rmse_dnn:.2f}"],
|
146 |
'LEAR': [f"{mae_lear:.2f}", f"{smape_lear:.2f}%", f"{rmse_lear:.2f}"]
|
147 |
})
|
148 |
+
st.dataframe(new_metrics_df, hide_index=True)
|
149 |
+
|
150 |
+
# Download Predictions Button
|
151 |
+
st.write("## Access Predictions")
|
152 |
+
st.write("If you are interested in accessing the predictions made by the models, please contact Margarida Mascarenhas (KU Leuven PhD Student) at margarida.mascarenhas@kuleuven.be")
|