raflisbk commited on
Commit
aed7e9a
Β·
verified Β·
1 Parent(s): 60f6959

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +39 -6
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
- last_idx = df_station["time_idx"].max()
176
- start_idx = last_idx - MAX_ENCODER_LENGTH - MAX_PREDICTION_LENGTH + 1
177
- df_window = df[
178
- (df["station_id"] == station_id) & (df["time_idx"] >= start_idx)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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