Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -163,7 +163,7 @@ def nse_kategori(v):
|
|
| 163 |
|
| 164 |
|
| 165 |
# ββ Tab 1: Prediksi ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 166 |
-
def jalankan_prediksi(station_label: str, n_history_days: int):
|
| 167 |
try:
|
| 168 |
station_id = [k for k, v in STATION_LABELS.items() if v == station_label][0]
|
| 169 |
df = _load_data()
|
|
@@ -172,10 +172,34 @@ def jalankan_prediksi(station_label: str, n_history_days: int):
|
|
| 172 |
from pytorch_forecasting import TimeSeriesDataSet
|
| 173 |
|
| 174 |
df_station = df[df["station_id"] == station_id].reset_index(drop=True)
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
].copy()
|
| 180 |
|
| 181 |
pred_dataset = TimeSeriesDataSet.from_dataset(
|
|
@@ -596,6 +620,15 @@ with gr.Blocks(
|
|
| 596 |
value=STATION_LABELS["sutami"],
|
| 597 |
label="π Pilih Stasiun / Bendungan",
|
| 598 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 599 |
history_sl = gr.Slider(
|
| 600 |
minimum=1, maximum=7, value=3, step=1,
|
| 601 |
label="π
Tampilkan histori (hari)",
|
|
@@ -609,7 +642,7 @@ with gr.Blocks(
|
|
| 609 |
|
| 610 |
run_btn.click(
|
| 611 |
fn=jalankan_prediksi,
|
| 612 |
-
inputs=[station_dd, history_sl],
|
| 613 |
outputs=[plot_out, metrik_out],
|
| 614 |
)
|
| 615 |
|
|
|
|
| 163 |
|
| 164 |
|
| 165 |
# ββ Tab 1: Prediksi ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 166 |
+
def jalankan_prediksi(station_label: str, n_history_days: int, periode: str):
|
| 167 |
try:
|
| 168 |
station_id = [k for k, v in STATION_LABELS.items() if v == station_label][0]
|
| 169 |
df = _load_data()
|
|
|
|
| 172 |
from pytorch_forecasting import TimeSeriesDataSet
|
| 173 |
|
| 174 |
df_station = df[df["station_id"] == station_id].reset_index(drop=True)
|
| 175 |
+
n_steps = df_station["time_idx"].max() + 1
|
| 176 |
+
train_cutoff = int(n_steps * 0.75)
|
| 177 |
+
|
| 178 |
+
# Pilih window berdasarkan periode
|
| 179 |
+
# Gunakan time_idx absolut dari df asli (bukan reset index)
|
| 180 |
+
df_station_full = df[df["station_id"] == station_id]
|
| 181 |
+
abs_max_idx = df_station_full["time_idx"].max()
|
| 182 |
+
abs_min_val_idx = df_station_full[
|
| 183 |
+
df_station_full["time_idx"] > train_cutoff
|
| 184 |
+
]["time_idx"].min()
|
| 185 |
+
|
| 186 |
+
PERIODE_OFFSETS = {
|
| 187 |
+
"Awal Validasi (Okt 2022)": abs_min_val_idx + MAX_ENCODER_LENGTH,
|
| 188 |
+
"Tengah Validasi (Apr 2023)": abs_min_val_idx + int((abs_max_idx - abs_min_val_idx) * 0.4),
|
| 189 |
+
"Akhir Validasi (Des 2023)": abs_max_idx - MAX_PREDICTION_LENGTH,
|
| 190 |
+
}
|
| 191 |
+
anchor_idx = PERIODE_OFFSETS.get(periode, PERIODE_OFFSETS["Tengah Validasi (Apr 2023)"])
|
| 192 |
+
# anchor_idx = titik akhir encoder (mulai prediction dari sini)
|
| 193 |
+
anchor_idx = max(anchor_idx, abs_min_val_idx + MAX_ENCODER_LENGTH)
|
| 194 |
+
anchor_idx = min(anchor_idx, abs_max_idx - MAX_PREDICTION_LENGTH)
|
| 195 |
+
|
| 196 |
+
start_idx = anchor_idx - MAX_ENCODER_LENGTH
|
| 197 |
+
end_idx = anchor_idx + MAX_PREDICTION_LENGTH
|
| 198 |
+
|
| 199 |
+
df_window = df[
|
| 200 |
+
(df["station_id"] == station_id) &
|
| 201 |
+
(df["time_idx"] >= start_idx) &
|
| 202 |
+
(df["time_idx"] < end_idx)
|
| 203 |
].copy()
|
| 204 |
|
| 205 |
pred_dataset = TimeSeriesDataSet.from_dataset(
|
|
|
|
| 620 |
value=STATION_LABELS["sutami"],
|
| 621 |
label="π Pilih Stasiun / Bendungan",
|
| 622 |
)
|
| 623 |
+
periode_dd = gr.Dropdown(
|
| 624 |
+
choices=[
|
| 625 |
+
"Awal Validasi (Okt 2022)",
|
| 626 |
+
"Tengah Validasi (Apr 2023)",
|
| 627 |
+
"Akhir Validasi (Des 2023)",
|
| 628 |
+
],
|
| 629 |
+
value="Tengah Validasi (Apr 2023)",
|
| 630 |
+
label="π Pilih Periode Prediksi",
|
| 631 |
+
)
|
| 632 |
history_sl = gr.Slider(
|
| 633 |
minimum=1, maximum=7, value=3, step=1,
|
| 634 |
label="π
Tampilkan histori (hari)",
|
|
|
|
| 642 |
|
| 643 |
run_btn.click(
|
| 644 |
fn=jalankan_prediksi,
|
| 645 |
+
inputs=[station_dd, history_sl, periode_dd],
|
| 646 |
outputs=[plot_out, metrik_out],
|
| 647 |
)
|
| 648 |
|