-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
273 lines (223 loc) · 9.02 KB
/
streamlit_app.py
File metadata and controls
273 lines (223 loc) · 9.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""
Streamlit Dashboard untuk Supply Chain ML Forecasting & Optimizer
Dashboard ini menampilkan:
- Ringkasan metrik evaluasi (WAPE, MASE)
- Visualisasi forecast vs actual
- Tabel evaluasi per SKU-location
- Detail forecast untuk SKU-location tertentu
"""
import json
from pathlib import Path
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import streamlit as st
# Config
st.set_page_config(
page_title="Supply Chain ML Dashboard",
page_icon="📊",
layout="wide",
initial_sidebar_state="expanded",
)
# Paths
PRED_PATH = Path("data/processed/predictions.csv")
FEAT_PATH = Path("data/processed/weekly_features.parquet")
STATS_PATH = Path("models/artifacts/demand_stats.json")
@st.cache_data
def load_predictions():
"""Load predictions.csv jika ada."""
if PRED_PATH.exists():
return pd.read_csv(PRED_PATH)
return None
@st.cache_data
def load_features():
"""Load weekly features jika ada."""
if FEAT_PATH.exists():
return pd.read_parquet(FEAT_PATH)
return None
@st.cache_data
def load_stats():
"""Load demand stats jika ada."""
if STATS_PATH.exists():
return json.loads(STATS_PATH.read_text())
return None
def main():
st.title("📊 Supply Chain ML Forecasting Dashboard")
st.markdown("---")
# Sidebar
with st.sidebar:
st.header("⚙️ Settings")
show_details = st.checkbox("Show SKU-Location details", value=False)
if st.button("🔄 Refresh Data"):
st.cache_data.clear()
st.rerun()
# Load data
pred_df = load_predictions()
feat_df = load_features()
stats = load_stats()
if pred_df is None:
st.warning(
"⚠️ File `data/processed/predictions.csv` belum ada. "
"Jalankan dulu: `python -m src.forecasting.evaluate`"
)
st.info("💡 Atau jalankan pipeline lengkap:\n```bash\npython etl/generate_dummy.py\npython etl/build_features.py\npython -m src.forecasting.train\npython -m src.forecasting.evaluate\n```")
return
# --- Summary Metrics ---
st.header("📈 Summary Metrics")
col1, col2, col3, col4 = st.columns(4)
# Global WAPE
naive_wape = ((pred_df["y_true"] - pred_df["y_pred_naive"]).abs().sum() /
pred_df["y_true"].abs().sum()) if pred_df["y_true"].abs().sum() > 0 else 0
seasonal_wape = ((pred_df["y_true"] - pred_df["y_pred_seasonal"]).abs().sum() /
pred_df["y_true"].abs().sum()) if pred_df["y_true"].abs().sum() > 0 else 0
model_wape = None
if "y_pred_model" in pred_df.columns:
model_wape = ((pred_df["y_true"] - pred_df["y_pred_model"]).abs().sum() /
pred_df["y_true"].abs().sum()) if pred_df["y_true"].abs().sum() > 0 else 0
with col1:
st.metric("Naive WAPE", f"{naive_wape:.4f}", delta=None)
with col2:
st.metric("Seasonal WAPE", f"{seasonal_wape:.4f}",
delta=f"{(seasonal_wape - naive_wape):.4f}" if seasonal_wape != naive_wape else None)
with col3:
if model_wape is not None:
improvement = naive_wape - model_wape
st.metric("Model WAPE", f"{model_wape:.4f}",
delta=f"{improvement:.4f} (better)" if improvement > 0 else None,
delta_color="inverse")
else:
st.metric("Model WAPE", "N/A", help="Model belum di-train")
with col4:
st.metric("Test Rows", len(pred_df))
st.metric("Unique SKU-Locations", pred_df[["store_id", "product_id"]].drop_duplicates().shape[0])
st.markdown("---")
# --- Forecast Comparison Chart ---
st.header("📉 Forecast vs Actual (Sample)")
# Pilih beberapa SKU-location untuk ditampilkan
unique_pairs = pred_df[["store_id", "product_id"]].drop_duplicates()
col_left, col_right = st.columns([3, 1])
with col_left:
selected_pair = st.selectbox(
"Pilih SKU-Location:",
options=[f"{row['store_id']} | {row['product_id']}"
for _, row in unique_pairs.head(20).iterrows()],
index=0
)
if selected_pair:
sid, pid = selected_pair.split(" | ")
pair_data = pred_df[(pred_df["store_id"] == sid) & (pred_df["product_id"] == pid)].copy()
pair_data = pair_data.sort_values(["year", "week"])
# Buat chart
fig = go.Figure()
fig.add_trace(go.Scatter(
x=list(range(len(pair_data))),
y=pair_data["y_true"],
mode="lines+markers",
name="Actual",
line=dict(color="#1f77b4", width=2),
marker=dict(size=8)
))
fig.add_trace(go.Scatter(
x=list(range(len(pair_data))),
y=pair_data["y_pred_naive"],
mode="lines+markers",
name="Naive Forecast",
line=dict(color="#ff7f0e", width=2, dash="dash"),
marker=dict(size=6)
))
fig.add_trace(go.Scatter(
x=list(range(len(pair_data))),
y=pair_data["y_pred_seasonal"],
mode="lines+markers",
name="Seasonal Forecast",
line=dict(color="#2ca02c", width=2, dash="dot"),
marker=dict(size=6)
))
if "y_pred_model" in pair_data.columns:
fig.add_trace(go.Scatter(
x=list(range(len(pair_data))),
y=pair_data["y_pred_model"],
mode="lines+markers",
name="Model Forecast",
line=dict(color="#d62728", width=2),
marker=dict(size=6)
))
fig.update_layout(
title=f"Forecast Comparison: {sid} | {pid}",
xaxis_title="Week Index (Test Period)",
yaxis_title="Units Sold",
hovermode="x unified",
height=400,
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
)
st.plotly_chart(fig, use_container_width=True)
st.markdown("---")
# --- Error Distribution ---
st.header("📊 Error Distribution")
col_err1, col_err2 = st.columns(2)
with col_err1:
pred_df["naive_error"] = (pred_df["y_true"] - pred_df["y_pred_naive"]).abs()
fig_err_naive = px.histogram(
pred_df,
x="naive_error",
nbins=30,
title="Naive Forecast Error Distribution",
labels={"naive_error": "Absolute Error", "count": "Frequency"}
)
st.plotly_chart(fig_err_naive, use_container_width=True)
with col_err2:
if "y_pred_model" in pred_df.columns:
pred_df["model_error"] = (pred_df["y_true"] - pred_df["y_pred_model"]).abs()
fig_err_model = px.histogram(
pred_df,
x="model_error",
nbins=30,
title="Model Forecast Error Distribution",
labels={"model_error": "Absolute Error", "count": "Frequency"},
color_discrete_sequence=["#d62728"]
)
st.plotly_chart(fig_err_model, use_container_width=True)
else:
st.info("Model forecast belum tersedia untuk error analysis.")
st.markdown("---")
# --- Per SKU-Location Metrics (if requested) ---
if show_details:
st.header("🔍 SKU-Location details")
# Hitung WAPE per pair
metrics_list = []
for (sid, pid), grp in pred_df.groupby(["store_id", "product_id"]):
y_true = grp["y_true"]
naive_wape_pair = ((y_true - grp["y_pred_naive"]).abs().sum() /
y_true.abs().sum()) if y_true.abs().sum() > 0 else 0
seasonal_wape_pair = ((y_true - grp["y_pred_seasonal"]).abs().sum() /
y_true.abs().sum()) if y_true.abs().sum() > 0 else 0
row = {
"store_id": sid,
"product_id": pid,
"naive_wape": naive_wape_pair,
"seasonal_wape": seasonal_wape_pair,
"mean_actual": y_true.mean(),
}
if "y_pred_model" in grp.columns:
model_wape_pair = ((y_true - grp["y_pred_model"]).abs().sum() /
y_true.abs().sum()) if y_true.abs().sum() > 0 else 0
row["model_wape"] = model_wape_pair
metrics_list.append(row)
metrics_df = pd.DataFrame(metrics_list)
metrics_df = metrics_df.sort_values("naive_wape", ascending=False)
st.dataframe(
metrics_df.style.format({
"naive_wape": "{:.4f}",
"seasonal_wape": "{:.4f}",
"model_wape": "{:.4f}" if "model_wape" in metrics_df.columns else None,
"mean_actual": "{:.2f}"
}),
use_container_width=True,
height=400
)
# --- Footer ---
st.markdown("---")
st.caption("💡 Dashboard ini membaca dari `data/processed/predictions.csv`. Refresh data setelah menjalankan evaluasi baru.")
if __name__ == "__main__":
main()