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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import os
import math
import tempfile
import warnings
import streamlit as st
import pandas as pd
import torch
import plotly.express as px

from torch.optim import AdamW
from torch.optim.lr_scheduler import OneCycleLR
from transformers import (
    EarlyStoppingCallback,
    Trainer,
    TrainingArguments,
    set_seed,
)
from transformers.integrations import INTEGRATION_TO_CALLBACK

from tsfm_public import (
    TimeSeriesPreprocessor,
    TrackingCallback,
    count_parameters,
    get_datasets,
)
from tsfm_public.toolkit.get_model import get_model
from tsfm_public.toolkit.lr_finder import optimal_lr_finder
from tsfm_public.toolkit.visualization import plot_predictions

# For M4 Hourly Example
from tsfm_public.models.tinytimemixer import TinyTimeMixerForPrediction

# Suppress warnings and set a reproducible seed
warnings.filterwarnings("ignore")
SEED = 42
set_seed(SEED)

# Default model parameters and output directory
TTM_MODEL_PATH = "ibm-granite/granite-timeseries-ttm-r2"
DEFAULT_CONTEXT_LENGTH = 512
DEFAULT_PREDICTION_LENGTH = 96
OUT_DIR = "dashboard_outputs"
os.makedirs(OUT_DIR, exist_ok=True)


# --------------------------
# Helper: Interactive Plot
def interactive_plot(actual, forecast, title="Forecast vs Actual"):
    df = pd.DataFrame(
        {"Time": range(len(actual)), "Actual": actual, "Forecast": forecast}
    )
    fig = px.line(df, x="Time", y=["Actual", "Forecast"], title=title)
    return fig


# --------------------------
# Mode 1: Zero-shot Evaluation
def run_zero_shot_forecasting(
    data,
    context_length,
    prediction_length,
    batch_size,
    selected_target_columns,
    selected_conditional_columns,
    rolling_forecast_extension,
    selected_forecast_index,
):
    st.write("### Preparing Data for Forecasting")
    timestamp_column = "date"
    id_columns = []  # Modify if needed.
    # Use selected target columns; default to all columns (except "date") if not provided.
    if not selected_target_columns:
        target_columns = [col for col in data.columns if col != timestamp_column]
    else:
        target_columns = selected_target_columns

    # Incorporate exogenous/control columns.
    conditional_columns = selected_conditional_columns

    # Define column specifiers (if your preprocessor supports static columns, add here)
    column_specifiers = {
        "timestamp_column": timestamp_column,
        "id_columns": id_columns,
        "target_columns": target_columns,
        "control_columns": conditional_columns,
    }

    n = len(data)
    split_config = {
        "train": [0, int(n * 0.7)],
        "valid": [int(n * 0.7), int(n * 0.8)],
        "test": [int(n * 0.8), n],
    }

    tsp = TimeSeriesPreprocessor(
        **column_specifiers,
        context_length=context_length,
        prediction_length=prediction_length,
        scaling=True,
        encode_categorical=False,
        scaler_type="standard",
    )
    dset_train, dset_valid, dset_test = get_datasets(tsp, data, split_config)
    st.write("Data split into train, validation, and test sets.")

    st.write("### Loading the Pre-trained TTM Model")
    model = get_model(
        TTM_MODEL_PATH,
        context_length=context_length,
        prediction_length=prediction_length,
    )
    temp_dir = tempfile.mkdtemp()
    training_args = TrainingArguments(
        output_dir=temp_dir,
        per_device_eval_batch_size=batch_size,
        seed=SEED,
        report_to="none",
    )
    trainer = Trainer(model=model, args=training_args)

    st.write("### Running Zero-shot Evaluation")
    st.info("Evaluating on the test set...")
    eval_output = trainer.evaluate(dset_test)
    st.write("**Zero-shot Evaluation Metrics:**")
    st.json(eval_output)

    st.write("### Generating Forecast Predictions")
    predictions_dict = trainer.predict(dset_test)
    try:
        predictions_np = predictions_dict.predictions[0]
    except Exception as e:
        st.error("Error extracting predictions: " + str(e))
        return
    st.write("Predictions shape:", predictions_np.shape)

    if rolling_forecast_extension > 0:
        st.write(
            f"### Rolling Forecast Extension: {rolling_forecast_extension} extra steps"
        )
        st.info("Rolling forecast logic can be implemented here.")

    # Interactive plot for a selected forecast index.
    idx = selected_forecast_index
    try:
        # This example assumes dset_test[idx] is a dict with a "target" key; adjust as needed.
        actual = (
            dset_test[idx]["target"]
            if isinstance(dset_test[idx], dict)
            else dset_test[idx][0]
        )
    except Exception:
        actual = predictions_np[idx]  # Fallback if actual is not available.
    fig = interactive_plot(
        actual, predictions_np[idx], title=f"Forecast vs Actual for index {idx}"
    )
    st.plotly_chart(fig)

    # Static plots (generated via plot_predictions)
    plot_dir = os.path.join(OUT_DIR, "zero_shot_plots")
    os.makedirs(plot_dir, exist_ok=True)
    try:
        plot_predictions(
            model=trainer.model,
            dset=dset_test,
            plot_dir=plot_dir,
            plot_prefix="test_zeroshot",
            indices=[idx],
            channel=0,
        )
    except Exception as e:
        st.error("Error during static plotting: " + str(e))
        return
    for file in os.listdir(plot_dir):
        if file.endswith(".png"):
            st.image(os.path.join(plot_dir, file), caption=file)


# --------------------------
# Mode 2: Channel-Mix Finetuning Example
def run_channel_mix_finetuning():
    st.write("## Channel-Mix Finetuning Example (Bike Sharing Data)")
    # Load bike sharing dataset
    target_dataset = "bike_sharing"
    DATA_ROOT_PATH = (
        "https://raw.githubusercontent.com/blobibob/bike-sharing-dataset/main/hour.csv"
    )
    timestamp_column = "dteday"
    id_columns = []
    try:
        data = pd.read_csv(DATA_ROOT_PATH, parse_dates=[timestamp_column])
    except Exception as e:
        st.error("Error loading bike sharing dataset: " + str(e))
        return
    data[timestamp_column] = pd.to_datetime(data[timestamp_column])
    # Adjust timestamps (to add hourly information)
    data[timestamp_column] = data[timestamp_column] + pd.to_timedelta(
        data.groupby(data[timestamp_column].dt.date).cumcount(), unit="h"
    )
    st.write("### Bike Sharing Data Preview")
    st.dataframe(data.head())

    # Define columns: targets and conditional (exogenous) channels
    column_specifiers = {
        "timestamp_column": timestamp_column,
        "id_columns": id_columns,
        "target_columns": ["casual", "registered", "cnt"],
        "conditional_columns": [
            "season",
            "yr",
            "mnth",
            "holiday",
            "weekday",
            "workingday",
            "weathersit",
            "temp",
            "atemp",
            "hum",
            "windspeed",
        ],
    }
    n = len(data)
    split_config = {
        "train": [0, int(n * 0.5)],
        "valid": [int(n * 0.5), int(n * 0.75)],
        "test": [int(n * 0.75), n],
    }
    context_length = 512
    forecast_length = 96

    tsp = TimeSeriesPreprocessor(
        **column_specifiers,
        context_length=context_length,
        prediction_length=forecast_length,
        scaling=True,
        encode_categorical=False,
        scaler_type="standard",
    )
    train_dataset, valid_dataset, test_dataset = get_datasets(tsp, data, split_config)
    st.write("Data split completed.")

    # For channel-mix finetuning, we use TTM-R1 (as per provided script)
    TTM_MODEL_PATH_CM = "ibm-granite/granite-timeseries-ttm-r1"
    finetune_forecast_model = get_model(
        TTM_MODEL_PATH_CM,
        context_length=context_length,
        prediction_length=forecast_length,
        num_input_channels=tsp.num_input_channels,
        decoder_mode="mix_channel",
        prediction_channel_indices=tsp.prediction_channel_indices,
    )
    st.write(
        "Number of params before freezing backbone:",
        count_parameters(finetune_forecast_model),
    )
    for param in finetune_forecast_model.backbone.parameters():
        param.requires_grad = False
    st.write(
        "Number of params after freezing backbone:",
        count_parameters(finetune_forecast_model),
    )

    num_epochs = 50
    batch_size = 64
    learning_rate = 0.001
    optimizer = AdamW(finetune_forecast_model.parameters(), lr=learning_rate)
    scheduler = OneCycleLR(
        optimizer,
        learning_rate,
        epochs=num_epochs,
        steps_per_epoch=math.ceil(len(train_dataset) / batch_size),
    )
    out_dir = os.path.join(OUT_DIR, target_dataset)
    os.makedirs(out_dir, exist_ok=True)
    finetune_args = TrainingArguments(
        output_dir=os.path.join(out_dir, "output"),
        overwrite_output_dir=True,
        learning_rate=learning_rate,
        num_train_epochs=num_epochs,
        do_eval=True,
        evaluation_strategy="epoch",
        per_device_train_batch_size=batch_size,
        per_device_eval_batch_size=batch_size,
        dataloader_num_workers=8,
        report_to="none",
        save_strategy="epoch",
        logging_strategy="epoch",
        save_total_limit=1,
        logging_dir=os.path.join(out_dir, "logs"),
        load_best_model_at_end=True,
        metric_for_best_model="eval_loss",
        greater_is_better=False,
        seed=SEED,
    )
    early_stopping_callback = EarlyStoppingCallback(
        early_stopping_patience=10,
        early_stopping_threshold=1e-5,
    )
    tracking_callback = TrackingCallback()
    finetune_trainer = Trainer(
        model=finetune_forecast_model,
        args=finetune_args,
        train_dataset=train_dataset,
        eval_dataset=valid_dataset,
        callbacks=[early_stopping_callback, tracking_callback],
        optimizers=(optimizer, scheduler),
    )
    finetune_trainer.remove_callback(INTEGRATION_TO_CALLBACK["codecarbon"])
    st.write("Starting channel-mix finetuning...")
    finetune_trainer.train()
    st.write("Evaluating finetuned model on test set...")
    eval_output = finetune_trainer.evaluate(test_dataset)
    st.write("Few-shot (channel-mix) evaluation metrics:")
    st.json(eval_output)
    # Plot predictions
    plot_dir = os.path.join(out_dir, "channel_mix_plots")
    os.makedirs(plot_dir, exist_ok=True)
    try:
        plot_predictions(
            model=finetune_trainer.model,
            dset=test_dataset,
            plot_dir=plot_dir,
            plot_prefix="test_channel_mix",
            indices=[0],
            channel=0,
        )
    except Exception as e:
        st.error("Error plotting channel mix predictions: " + str(e))
        return
    for file in os.listdir(plot_dir):
        if file.endswith(".png"):
            st.image(os.path.join(plot_dir, file), caption=file)


# --------------------------
# Mode 3: M4 Hourly Example
def run_m4_hourly_example():
    st.write("## M4 Hourly Example")
    st.info("This example reproduces a simplified version of the M4 hourly evaluation.")
    # For demonstration, we attempt to load an M4 hourly dataset from a URL.
    # (In practice, you would need to download and prepare the dataset.)
    M4_DATASET_URL = "https://raw.githubusercontent.com/IBM/TSFM-public/main/tsfm_public/notebooks/ETTh1.csv"  # Placeholder URL
    try:
        m4_data = pd.read_csv(M4_DATASET_URL, parse_dates=["date"])
    except Exception as e:
        st.error("Could not load M4 hourly dataset: " + str(e))
        return
    st.write("### M4 Hourly Data Preview")
    st.dataframe(m4_data.head())
    context_length = 512
    forecast_length = 48  # M4 hourly forecast horizon
    timestamp_column = "date"
    id_columns = []
    target_columns = [col for col in m4_data.columns if col != timestamp_column]
    n = len(m4_data)
    split_config = {
        "train": [0, int(n * 0.7)],
        "valid": [int(n * 0.7), int(n * 0.85)],
        "test": [int(n * 0.85), n],
    }
    column_specifiers = {
        "timestamp_column": timestamp_column,
        "id_columns": id_columns,
        "target_columns": target_columns,
        "control_columns": [],
    }
    tsp = TimeSeriesPreprocessor(
        **column_specifiers,
        context_length=context_length,
        prediction_length=forecast_length,
        scaling=True,
        encode_categorical=False,
        scaler_type="standard",
    )
    dset_train, dset_valid, dset_test = get_datasets(tsp, m4_data, split_config)
    st.write("Data split completed.")

    # Load model from Hugging Face TTM Model Repository (TTM-V1 for M4)
    device = "cuda" if torch.cuda.is_available() else "cpu"
    model = TinyTimeMixerForPrediction.from_pretrained(
        "ibm-granite/granite-timeseries-ttm-v1",
        revision="main",
        prediction_filter_length=forecast_length,
    ).to(device)
    st.write("Running zero-shot evaluation on M4 hourly data...")
    temp_dir = tempfile.mkdtemp()
    trainer = Trainer(
        model=model,
        args=TrainingArguments(
            output_dir=temp_dir,
            per_device_eval_batch_size=64,
            report_to="none",
        ),
    )
    eval_output = trainer.evaluate(dset_test)
    st.write("Zero-shot evaluation metrics on M4 hourly:")
    st.json(eval_output)
    plot_dir = os.path.join(OUT_DIR, "m4_hourly", "zero_shot")
    os.makedirs(plot_dir, exist_ok=True)
    try:
        plot_predictions(
            model=trainer.model,
            dset=dset_test,
            plot_dir=plot_dir,
            plot_prefix="m4_zero_shot",
            indices=[0],
            channel=0,
        )
    except Exception as e:
        st.error("Error plotting M4 zero-shot predictions: " + str(e))
        return
    for file in os.listdir(plot_dir):
        if file.endswith(".png"):
            st.image(os.path.join(plot_dir, file), caption=file)
    st.info("Fine-tuning on M4 hourly data can be added similarly.")


# --------------------------
# Main UI
def main():
    st.title("Interactive Time-Series Forecasting Dashboard")
    st.markdown(
        """
        This dashboard lets you run advanced forecasting experiments using the Granite-TimeSeries-TTM model.
        Select one of the modes below:
        - **Zero-shot Evaluation**
        - **Channel-Mix Finetuning Example**
        - **M4 Hourly Example**
        """
    )

    mode = st.selectbox(
        "Select Evaluation Mode",
        options=[
            "Zero-shot Evaluation",
            "Channel-Mix Finetuning Example",
            "M4 Hourly Example",
        ],
    )

    if mode == "Zero-shot Evaluation":
        # Allow user to choose dataset source
        dataset_source = st.radio(
            "Dataset Source", options=["Default (ETTh1)", "Upload CSV"]
        )
        if dataset_source == "Default (ETTh1)":
            DATASET_PATH = "https://raw.githubusercontent.com/zhouhaoyi/ETDataset/main/ETT-small/ETTh1.csv"
            try:
                data = pd.read_csv(DATASET_PATH, parse_dates=["date"])
            except Exception as e:
                st.error("Error loading default dataset.")
                return
            st.write("### Default Dataset Preview")
            st.dataframe(data.head())
            selected_target_columns = [
                "HUFL",
                "HULL",
                "MUFL",
                "MULL",
                "LUFL",
                "LULL",
                "OT",
            ]
        else:
            uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
            if not uploaded_file:
                st.info("Awaiting CSV file upload.")
                return
            data = pd.read_csv(uploaded_file, parse_dates=["date"])
            st.write("### Uploaded Data Preview")
            st.dataframe(data.head())
            available_columns = [col for col in data.columns if col != "date"]
            selected_target_columns = st.multiselect(
                "Select Target Column(s)",
                options=available_columns,
                default=available_columns,
            )

        # Advanced options
        available_exog = [
            col
            for col in data.columns
            if col not in (["date"] + selected_target_columns)
        ]
        selected_conditional_columns = st.multiselect(
            "Select Exogenous/Control Columns", options=available_exog, default=[]
        )
        rolling_extension = st.number_input(
            "Rolling Forecast Extension (Extra Steps)", value=0, min_value=0, step=1
        )
        forecast_index = st.slider(
            "Select Forecast Index for Plotting",
            min_value=0,
            max_value=len(data) - 1,
            value=0,
        )
        context_length = st.number_input(
            "Context Length", value=DEFAULT_CONTEXT_LENGTH, step=64
        )
        prediction_length = st.number_input(
            "Prediction Length", value=DEFAULT_PREDICTION_LENGTH, step=1
        )
        batch_size = st.number_input("Batch Size", value=64, step=1)
        if st.button("Run Zero-shot Evaluation"):
            with st.spinner("Running zero-shot evaluation..."):
                run_zero_shot_forecasting(
                    data,
                    context_length,
                    prediction_length,
                    batch_size,
                    selected_target_columns,
                    selected_conditional_columns,
                    rolling_extension,
                    forecast_index,
                )

    elif mode == "Channel-Mix Finetuning Example":
        if st.button("Run Channel-Mix Finetuning Example"):
            with st.spinner("Running channel-mix finetuning..."):
                run_channel_mix_finetuning()

    elif mode == "M4 Hourly Example":
        if st.button("Run M4 Hourly Example"):
            with st.spinner("Running M4 hourly example..."):
                run_m4_hourly_example()


if __name__ == "__main__":
    main()