Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -62,8 +62,7 @@ def load_champion_models():
|
|
| 62 |
"Hãy đảm bảo 5 file .pkl nằm trong thư mục 'models/'.")
|
| 63 |
return []
|
| 64 |
|
| 65 |
-
@st.
|
| 66 |
-
def load_performance_data(file_path="data/final_5_day_results_df.csv"):
|
| 67 |
"""Tải dữ liệu hiệu suất đã tính toán trước cho Tab 3."""
|
| 68 |
try:
|
| 69 |
df = pd.read_csv(file_path)
|
|
@@ -80,10 +79,7 @@ models = load_champion_models()
|
|
| 80 |
perf_df = load_performance_data()
|
| 81 |
|
| 82 |
# --- TÙY CHỈNH QUAN TRỌNG ---
|
| 83 |
-
# Giả định tên các cột target (thực tế) trong file CSV của bạn
|
| 84 |
-
# Checklist không nói rõ, nên tôi giả định tên là 't+1', 't+2', v.v.
|
| 85 |
TARGET_COLS = ['temp_next_1_day', 'temp_next_2_day', 'temp_next_3_day', 'temp_next_4_day', 'temp_next_5_day']
|
| 86 |
-
# Giả định tên cột nhiệt độ của ngày HIỆN TẠI (dùng để vẽ lịch sử)
|
| 87 |
CURRENT_TEMP_COL = 'temp'
|
| 88 |
|
| 89 |
# Tách test set (dựa trên ngày trong checklist)
|
|
@@ -97,10 +93,11 @@ if not all_data_df.empty:
|
|
| 97 |
test_df = all_data_df.loc[TEST_START_DATE:TEST_END_DATE].copy()
|
| 98 |
|
| 99 |
# Giả định: 157 features là TẤT CẢ các cột KHÔNG PHẢI là target
|
| 100 |
-
feature_cols = [col for col in
|
| 101 |
|
| 102 |
# Tách X_test (features) và y_test (thực tế)
|
| 103 |
-
X_test
|
|
|
|
| 104 |
y_test = test_df[TARGET_COLS]
|
| 105 |
|
| 106 |
# Đổi tên cột y_test cho dễ hiểu (dùng trong Tab 3)
|
|
@@ -114,7 +111,6 @@ else:
|
|
| 114 |
st.error("Không thể tải dữ liệu chính, ứng dụng không thể tiếp tục.")
|
| 115 |
st.stop()
|
| 116 |
|
| 117 |
-
|
| 118 |
# --- 5. GIAO DIỆN SIDEBAR (THANH ĐIỀU HƯỚNG) ---
|
| 119 |
|
| 120 |
st.sidebar.title("Navigation")
|
|
@@ -189,8 +185,14 @@ elif app_section == "Live 5-Day Forecast":
|
|
| 189 |
|
| 190 |
# 1. Lấy Input Features
|
| 191 |
selected_date_ts = pd.Timestamp(selected_date)
|
| 192 |
-
input_features = X_test.loc[[selected_date_ts]]
|
| 193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
if input_features.empty:
|
| 195 |
st.error("Không tìm thấy dữ liệu cho ngày đã chọn.")
|
| 196 |
else:
|
|
@@ -210,13 +212,44 @@ elif app_section == "Live 5-Day Forecast":
|
|
| 210 |
|
| 211 |
for i in range(5):
|
| 212 |
with cols[i]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
st.metric(
|
| 214 |
label=f"Forecast for {forecast_dates[i].strftime('%b %d')}",
|
| 215 |
value=f"{predictions[i]:.1f}°C",
|
| 216 |
-
delta=
|
| 217 |
delta_color="off" # Màu xám trung tính
|
| 218 |
)
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
# 4. Biểu đồ (Optimal Suggestion)
|
| 221 |
st.subheader("Historical Context & Forecast")
|
| 222 |
|
|
@@ -264,20 +297,22 @@ elif app_section == "Model Performance & Diagnostics":
|
|
| 264 |
if not perf_df.empty and not y_test.empty:
|
| 265 |
st.subheader("Performance Degradation over 5 Days")
|
| 266 |
st.markdown("Hiệu suất mô hình thay đổi như thế nào khi dự báo xa hơn.")
|
| 267 |
-
|
|
|
|
| 268 |
MODEL_NAME = 'Champion (Stacking)'
|
| 269 |
champion_perf_df = perf_df[perf_df['Model'] == MODEL_NAME].copy()
|
|
|
|
| 270 |
# 1. Biểu đồ suy giảm hiệu suất (RMSE & R2)
|
| 271 |
|
| 272 |
# --- TÙY CHỈNH ---
|
| 273 |
# Đảm bảo 'RMSE' và 'R2' là tên cột chính xác trong file 'final_5_day_results_df.csv'
|
| 274 |
RMSE_COL_NAME = 'RMSE (Absolute Error)'
|
| 275 |
-
R2_COL_NAME = 'R-squared'
|
| 276 |
|
| 277 |
col1, col2 = st.columns(2)
|
| 278 |
with col1:
|
| 279 |
fig_rmse = diag.plot_performance_degradation(
|
| 280 |
-
champion_perf_df,
|
| 281 |
metric_column=RMSE_COL_NAME,
|
| 282 |
metric_name='RMSE (Temperature °C)',
|
| 283 |
color='blue'
|
|
@@ -285,7 +320,7 @@ elif app_section == "Model Performance & Diagnostics":
|
|
| 285 |
st.plotly_chart(fig_rmse, use_container_width=True)
|
| 286 |
with col2:
|
| 287 |
fig_r2 = diag.plot_performance_degradation(
|
| 288 |
-
champion_perf_df,
|
| 289 |
metric_column=R2_COL_NAME,
|
| 290 |
metric_name='R-squared (R²)',
|
| 291 |
color='green'
|
|
|
|
| 62 |
"Hãy đảm bảo 5 file .pkl nằm trong thư mục 'models/'.")
|
| 63 |
return []
|
| 64 |
|
| 65 |
+
@st.cache_datadef load_performance_data(file_path="data/final_5_day_results_df.csv"):
|
|
|
|
| 66 |
"""Tải dữ liệu hiệu suất đã tính toán trước cho Tab 3."""
|
| 67 |
try:
|
| 68 |
df = pd.read_csv(file_path)
|
|
|
|
| 79 |
perf_df = load_performance_data()
|
| 80 |
|
| 81 |
# --- TÙY CHỈNH QUAN TRỌNG ---
|
|
|
|
|
|
|
| 82 |
TARGET_COLS = ['temp_next_1_day', 'temp_next_2_day', 'temp_next_3_day', 'temp_next_4_day', 'temp_next_5_day']
|
|
|
|
| 83 |
CURRENT_TEMP_COL = 'temp'
|
| 84 |
|
| 85 |
# Tách test set (dựa trên ngày trong checklist)
|
|
|
|
| 93 |
test_df = all_data_df.loc[TEST_START_DATE:TEST_END_DATE].copy()
|
| 94 |
|
| 95 |
# Giả định: 157 features là TẤT CẢ các cột KHÔNG PHẢI là target
|
| 96 |
+
feature_cols = [col for col in all_data_df.columns if col not in TARGET_COLS]
|
| 97 |
|
| 98 |
# Tách X_test (features) và y_test (thực tế)
|
| 99 |
+
# Sửa lỗi logic: X_test phải được lấy từ test_df
|
| 100 |
+
X_test = test_df[feature_cols]
|
| 101 |
y_test = test_df[TARGET_COLS]
|
| 102 |
|
| 103 |
# Đổi tên cột y_test cho dễ hiểu (dùng trong Tab 3)
|
|
|
|
| 111 |
st.error("Không thể tải dữ liệu chính, ứng dụng không thể tiếp tục.")
|
| 112 |
st.stop()
|
| 113 |
|
|
|
|
| 114 |
# --- 5. GIAO DIỆN SIDEBAR (THANH ĐIỀU HƯỚNG) ---
|
| 115 |
|
| 116 |
st.sidebar.title("Navigation")
|
|
|
|
| 185 |
|
| 186 |
# 1. Lấy Input Features
|
| 187 |
selected_date_ts = pd.Timestamp(selected_date)
|
|
|
|
| 188 |
|
| 189 |
+
# Sửa lỗi logic: input_features phải được lấy từ X_test
|
| 190 |
+
if selected_date_ts in X_test.index:
|
| 191 |
+
input_features = X_test.loc[[selected_date_ts]]
|
| 192 |
+
else:
|
| 193 |
+
st.error("Không tìm thấy dữ liệu cho ngày đã chọn trong X_test.")
|
| 194 |
+
input_features = pd.DataFrame() # Tạo dataframe rỗng để tránh lỗi sau
|
| 195 |
+
|
| 196 |
if input_features.empty:
|
| 197 |
st.error("Không tìm thấy dữ liệu cho ngày đã chọn.")
|
| 198 |
else:
|
|
|
|
| 212 |
|
| 213 |
for i in range(5):
|
| 214 |
with cols[i]:
|
| 215 |
+
|
| 216 |
+
# --- SỬA LỖI 1: KIỂM TRA NaN CHO ACTUAL VALUE ---
|
| 217 |
+
actual_val = actual_values[i]
|
| 218 |
+
if pd.isna(actual_val):
|
| 219 |
+
delta_text = "Actual: --"
|
| 220 |
+
else:
|
| 221 |
+
delta_text = f"Actual: {actual_val:.1f}°C"
|
| 222 |
+
# --- KẾT THÚC SỬA LỖI 1 ---
|
| 223 |
+
|
| 224 |
st.metric(
|
| 225 |
label=f"Forecast for {forecast_dates[i].strftime('%b %d')}",
|
| 226 |
value=f"{predictions[i]:.1f}°C",
|
| 227 |
+
delta=delta_text, # Sử dụng delta_text đã kiểm tra
|
| 228 |
delta_color="off" # Màu xám trung tính
|
| 229 |
)
|
| 230 |
+
|
| 231 |
+
# --- THÊM MỚI 2: BIỂU ĐỒ DỮ LIỆU TRAINING (THEO YÊU CẦU) ---
|
| 232 |
+
st.subheader("Training Set Overview")
|
| 233 |
+
with st.expander("Hiển thị biểu đồ toàn bộ dữ liệu training (trước 2024-02-18)"):
|
| 234 |
+
|
| 235 |
+
# Xác định phạm vi training data
|
| 236 |
+
train_end_date = pd.Timestamp(TEST_START_DATE) - pd.Timedelta(days=1)
|
| 237 |
+
train_df = all_data_df.loc[:train_end_date][CURRENT_TEMP_COL]
|
| 238 |
+
|
| 239 |
+
fig_train = go.Figure()
|
| 240 |
+
fig_train.add_trace(go.Scatter(
|
| 241 |
+
x=train_df.index, y=train_df,
|
| 242 |
+
mode='lines', name='Training Data (Actual)',
|
| 243 |
+
line=dict(color='#005aa7', width=1) # Màu xanh
|
| 244 |
+
))
|
| 245 |
+
fig_train.update_layout(
|
| 246 |
+
title="Actual Temperature - Full Training Set",
|
| 247 |
+
xaxis_title="Date", yaxis_title="Temperature (°C)",
|
| 248 |
+
template="plotly_white"
|
| 249 |
+
)
|
| 250 |
+
st.plotly_chart(fig_train, use_container_width=True)
|
| 251 |
+
# --- KẾT THÚC THÊM MỚI 2 ---
|
| 252 |
+
|
| 253 |
# 4. Biểu đồ (Optimal Suggestion)
|
| 254 |
st.subheader("Historical Context & Forecast")
|
| 255 |
|
|
|
|
| 297 |
if not perf_df.empty and not y_test.empty:
|
| 298 |
st.subheader("Performance Degradation over 5 Days")
|
| 299 |
st.markdown("Hiệu suất mô hình thay đổi như thế nào khi dự báo xa hơn.")
|
| 300 |
+
|
| 301 |
+
# Lọc chỉ model Champion
|
| 302 |
MODEL_NAME = 'Champion (Stacking)'
|
| 303 |
champion_perf_df = perf_df[perf_df['Model'] == MODEL_NAME].copy()
|
| 304 |
+
|
| 305 |
# 1. Biểu đồ suy giảm hiệu suất (RMSE & R2)
|
| 306 |
|
| 307 |
# --- TÙY CHỈNH ---
|
| 308 |
# Đảm bảo 'RMSE' và 'R2' là tên cột chính xác trong file 'final_5_day_results_df.csv'
|
| 309 |
RMSE_COL_NAME = 'RMSE (Absolute Error)'
|
| 310 |
+
R2_COL_NAME = 'R-squared'
|
| 311 |
|
| 312 |
col1, col2 = st.columns(2)
|
| 313 |
with col1:
|
| 314 |
fig_rmse = diag.plot_performance_degradation(
|
| 315 |
+
champion_perf_df, # Dùng df đã lọc
|
| 316 |
metric_column=RMSE_COL_NAME,
|
| 317 |
metric_name='RMSE (Temperature °C)',
|
| 318 |
color='blue'
|
|
|
|
| 320 |
st.plotly_chart(fig_rmse, use_container_width=True)
|
| 321 |
with col2:
|
| 322 |
fig_r2 = diag.plot_performance_degradation(
|
| 323 |
+
champion_perf_df, # Dùng df đã lọc
|
| 324 |
metric_column=R2_COL_NAME,
|
| 325 |
metric_name='R-squared (R²)',
|
| 326 |
color='green'
|