File size: 14,612 Bytes
4577714
 
5a35cdb
4577714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a35cdb
4577714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a35cdb
4577714
 
 
 
5a35cdb
 
 
 
 
4577714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a35cdb
4577714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a35cdb
 
f53f4e4
 
5a35cdb
 
 
 
 
 
 
 
 
 
 
 
 
 
4577714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import matplotlib.pyplot as plt
import os


def update_num_variable(dataset_choice):
    reset_value = 1
    if not dataset_choice :
        return gr.update(minimum=1, maximum=10, step=1, value=reset_value)
    if dataset_choice in ['Electricity', 'Traffic']:
        num_variable_range = (1,10,1)
    elif dataset_choice in ['ETTh1', 'ETTh2', 'ETTm1', 'ETTm2', 'National_illness']:
        num_variable_range = (1,7,1)
    elif dataset_choice == "Weather":
        num_variable_range = (1,21,1)
    elif dataset_choice == "Exchange":
        num_variable_range = (1,8,1)
    return gr.update(minimum=num_variable_range[0], maximum=num_variable_range[1], step=num_variable_range[2],
                     value=reset_value)
def update_time_horizon(dataset_choice):
    if dataset_choice == "National_illness":
        choices = [24, 36, 48, 60]
    else:
        choices = [96, 192, 336, 720]

    #default value = maximum choice
    default_value = max(choices)

    return gr.update(choices=choices, value=default_value)

def update_starting_point(model_choice, dataset_choice, time_horizon):
    reset_value = 0
    starting_point_range = (0, 300, 20)  # Standardwert
    if not model_choice or not dataset_choice or not time_horizon:
        return gr.update(minimum=0, maximum=300, step=20, value = reset_value)

    if model_choice == "NLinear":
        if dataset_choice == "Electricity":
            if time_horizon == 96:
                starting_point_range = (0, 160, 20)
            elif time_horizon in [192, 336, 720]:
                starting_point_range = (0, 140, 20)
        elif dataset_choice == "ETTh1":
            if time_horizon in [96, 192]:
                starting_point_range = (0, 80, 20)
            else:
                starting_point_range = (0, 60, 20)
        elif dataset_choice == "ETTh2":
            if time_horizon in [96, 192]:
                starting_point_range = (0, 80, 20)
            else:
                starting_point_range = (0, 60, 20)
        elif dataset_choice == "Traffic":
            if time_horizon in [96, 192]:
                starting_point_range = (0, 100, 20)
            else:
                starting_point_range = (0, 80, 20)
        elif dataset_choice == "Weather":
            if time_horizon in [96, 192]:
                starting_point_range = (0, 320, 20)
            else:
                starting_point_range = (0, 300, 20)
        elif dataset_choice == "Exchange":
            if time_horizon in [96, 192]:
                starting_point_range = (0,40,20)
            else:
                starting_point_range = (0, 20, 20)
        elif dataset_choice == "National_illness":
            starting_point_range = (0, 0, 0)
        elif dataset_choice == "ETTm2":
            if time_horizon == 720:
                starting_point_range = (0, 320, 20)
            else:
                starting_point_range = (0,340,20)
        elif dataset_choice == "ETTm1":
            if time_horizon == 720:
                starting_point_range = (0, 320, 20)
            else:
                starting_point_range = (0,340,20)
    else:
        if dataset_choice == "National_illness":
            if time_horizon == 24:
                starting_point_range = (0, 160, 20)
            elif time_horizon in [36, 48]:
                starting_point_range = (0, 140, 20)
            elif time_horizon == 60:
                starting_point_range = (0, 120, 20)
        elif dataset_choice == "Electricity":
            starting_point_range = (0,4320, 720)
        elif dataset_choice in ["ETTh1", "ETTh2"]:
            if time_horizon == 96:
                starting_point_range = (0, 2700, 100)
            elif time_horizon == 192:
                starting_point_range = (0, 2600, 100)
            elif time_horizon == 336:
                starting_point_range = (0, 2300, 100)
            elif time_horizon == 720:
                starting_point_range = (0, 2100, 100)
        elif dataset_choice in ["ETTm1", "ETTm2"]:
            starting_point_range = (0,10800, 720)
        elif dataset_choice == "Exchange":
            if time_horizon == 96:
                starting_point_range = (0, 1300, 100)
            elif time_horizon == 192:
                starting_point_range = (0, 1100, 100)
            elif time_horizon == 336:
                starting_point_range = (0, 1000, 100)
            elif time_horizon == 720:
                starting_point_range = (0, 700, 100)
        elif dataset_choice == "Traffic":
            if time_horizon == 96:
                starting_point_range = (0, 3300, 100)
            elif time_horizon == 192:
                starting_point_range = (0, 3200, 100)
            elif time_horizon == 336:
                starting_point_range = (0, 3000, 100)
            elif time_horizon == 720:
                starting_point_range = (0, 2100, 100)
        elif dataset_choice == "Weather":
            starting_point_range = (0, 9360, 720)
        else:
            starting_point_range = (0, 300, 20)
    return gr.update(minimum = starting_point_range[0], maximum = starting_point_range[1], step = starting_point_range[2], value = reset_value)



def find_csv_file(dataset_choice, time_horizon, look_back_window):
    dir = "../../../Desktop/Project/Models/LTBoost"
    for root, dirs, files in os.walk(dir):
        for dir_name in dirs:
            if dataset_choice.lower() in dir_name.lower():
                full_dir_path = os.path.join(root, dir_name)
                for file_name in os.listdir(full_dir_path):
                    if f"pl_{time_horizon}" in file_name:
                        csv_file_path = os.path.join(full_dir_path, file_name)
                        if os.path.exists(csv_file_path):
                            return csv_file_path
    return None

def plot_forecast_vs_ground_truth(df_forecast, df_ground_truth, dataset_choice, model_choice, lookback_window,time_horizon, starting_point, num_variable):
    plt.figure(figsize=(12, 6))

    plt.plot(df_ground_truth, label='Forecast')
    plt.plot(df_forecast, label='Ground Truth')
    # Marking the transition between look-back window and forecast horizon
    transition_point = lookback_window
    plt.axvline(x=transition_point, color='black', linestyle='-')
    plt.title(f'Forecast vs Ground Truth for {dataset_choice} using {model_choice} Model')
    #plt.xlabel('Time')
    #plt.ylabel('Value')
    plt.legend()
    plt.grid(True)
    # Create directory path based on dataset name, lookback window, and time horizon
    save_dir = f"results_ltboost/{dataset_choice}_sl{lookback_window}_pl{time_horizon}/{num_variable}"
    os.makedirs(save_dir, exist_ok=True)  # Ensure directory exists, create if not

    # Save the plot as PNG file
    path = os.path.join(save_dir, f"img_{starting_point}.png")
    plt.savefig(path)

    plt.close()
    return path

def find_image_path(dataset_choice, model_choice, time_horizon, look_back_window, starting_point, num_variable):
    directory = "results_nlinear"
    for root, dirs, files in os.walk(directory):
        for dir_name in dirs:
            if dataset_choice.lower() in dir_name.lower() and model_choice in dir_name and f"pl{time_horizon}" in dir_name and f"sl{look_back_window}" in dir_name:
                variable_dir = os.path.join(root, dir_name, str(num_variable))
                if os.path.exists(variable_dir):
                    image_path = os.path.join(variable_dir, f"{starting_point}.png")
                    if os.path.exists(image_path):
                        return image_path
    return None

def find_image_path_LTBoost(dataset_choice, time_horizon, look_back_window, starting_point, num_variable):
    directory = "results_ltboost"
    for root, dirs, files in os.walk(directory):
        for dir_name in dirs:
            # Check if the folder name contains the desired dataset, time horizon, and look-back window
            if (dataset_choice.lower() in dir_name.lower() and
                    f"pl{time_horizon}" in dir_name and
                    f"sl{look_back_window}" in dir_name):

                variable_dir = os.path.join(root, dir_name, str(num_variable))

                if os.path.exists(variable_dir):
                    image_path = os.path.join(variable_dir, f"img_{starting_point}.png")

                    if os.path.exists(image_path):
                        return image_path

    return None


def perform_forecasting(dataset_choice, model_choice, time_horizon, starting_point, num_variable):
    if model_choice == "NLinear":
        if dataset_choice == "National_illness":
            look_back_window = 104
        else:
            look_back_window = 336
    else:
        if dataset_choice == "National_illness":
            look_back_window = 104
        elif dataset_choice == "Exchange" or dataset_choice == "ETTh1":
            look_back_window = 336
        else:
            look_back_window = 720

    if model_choice == "NLinear":
        # check if already have a png image
        img = find_image_path(dataset_choice, model_choice, time_horizon, look_back_window, starting_point, num_variable)
        if img:
            return img
        else:
            return "No image found"

    elif model_choice == "LTBoost":
        # check if already have a png image
        img = find_image_path_LTBoost(dataset_choice, time_horizon, look_back_window, starting_point, num_variable)
        if img:
            return img
        else:
            return "Image not found"




def main():
    theme = gr.themes.Base(
        primary_hue="blue",
        secondary_hue="blue",
        neutral_hue="gray"
    )
    theme.set(
        body_background_fill="*primary_50",
        body_background_fill_dark="*checkbox_background_color_focus",
        body_text_color_dark="white",
        body_text_color="*neutral_800",
        background_fill_secondary_dark="*checkbox_border_color_hover",
        block_background_fill="*background_fill_primary",
        block_background_fill_dark="*neutral_800",
        block_border_color_dark="*primary_100",
        block_border_width_dark="4px",
        block_border_width="4px",
        block_border_color="*secondary_200"
    )
    with gr.Blocks(theme=theme) as demo:
        gr.Markdown("""
            <h1 style="text-align: center; font-size: 1.5em;">LTBoost</h1>
            <div style="font-size: 16px; letter-spacing: 1.2px; line-height: 1.8; text-align: justify;">
                LTBoost: Boosted Hybrids of Ensemble Linear and Gradient Algorithms for the Long-term Time Series Forecasting<br>
                <strong>LTBoost repository:</strong> <a href="https://github.com/hubtru/LTBoost" target="_blank">LTBoost</a><br>
                You can choose between the dataset, model, time horizon, and starting point. <br>
                <strong>Options:</strong> <br>
                <ul>
                    <li><strong>Datasets:</strong> Electricity, Exchange Rate, Traffic, Weather, ILI, 4 ETTs</li>
                    <li><strong>Models:</strong> LTBoost or NLinear</li>
                    <li><strong>Time horizon:</strong> {96, 192, 336, 720} (different for ILI)</li>
                    <li><strong>Starting point:</strong> {0, ..., end}</li>
                    <li><strong>Variate:</strong> 10 last variates (including the OT which is always the last)</li>
                </ul>
                Number of figures for each of the dataset depends on the dataset_trainset length and the starting_point_resolution (offset).<br>
                Due to the high number of the figures the starting_point_resolution has been limited to {20, 100, 720}.<br>
            </div>
            """)

        with gr.Row():
            with gr.Column():
                dataset_choice = gr.Dropdown(
                    ["Electricity", "Traffic", "Weather", "Exchange", "National_illness", "ETTh1", "ETTh2", "ETTm1", "ETTm2"],
                    label="Dataset Choice"
                )
                model_choice = gr.Dropdown(["NLinear", "LTBoost"], label="Model Choice", value= "LTBoost")
                time_horizon = gr.Dropdown([96, 192, 336, 720], label="Time Horizon")
                starting_point = gr.Slider(minimum=0, maximum=300, step=20, label="Starting Point", value= 0)
                num_variable = gr.Slider(minimum=1,maximum=10,step=1,label="Variable")

                input_components = [dataset_choice, model_choice, time_horizon, starting_point, num_variable]

                dataset_choice.change(update_time_horizon, inputs=dataset_choice, outputs=time_horizon)

                dataset_choice.change(update_starting_point, inputs=[model_choice, dataset_choice, time_horizon],
                                      outputs=starting_point)
                model_choice.change(update_starting_point, inputs=[model_choice, dataset_choice, time_horizon],
                                    outputs=starting_point)
                time_horizon.change(update_starting_point, inputs=[model_choice, dataset_choice, time_horizon],
                                      outputs=starting_point)
                dataset_choice.change(update_num_variable, inputs = dataset_choice, outputs = num_variable)

                gr.ClearButton(input_components)

            with gr.Column():
                # Automatically update the output image whenever any input component changes
                output_png = gr.Image(label="Forecast Results")
                for component in input_components:
                    component.change(perform_forecasting, inputs=input_components, outputs=output_png)
        with gr.Row():
            with gr.Column():
                gr.Markdown("Examples for the user input:")
                gr.Examples([
                    ['ETTh1', 'NLinear', 720, 0],

                    ['ETTh2', 'NLinear', 720, 0],

                    ['ETTm1', 'NLinear', 720, 0],

                    ['ETTm2', 'NLinear', 720, 0],

                    ['Weather', 'NLinear', 720, 0],

                    ['Traffic', 'NLinear', 720, 0],

                    ['Electricity', 'NLinear', 720, 0],

                    ['ETTh1', 'LTBoost', 720, 0],

                    ['ETTh2', 'LTBoost', 720, 0],

                    ['ETTm1', 'LTBoost', 720, 0],

                    ['ETTm2', 'LTBoost', 720, 0],

                    ['Weather', 'LTBoost', 720, 0],

                    ['Traffic', 'LTBoost', 720, 0],

                    ['Electricity', 'LTBoost', 720, 0],

                            ],

                            inputs=input_components)



    demo.launch()

if __name__ == "__main__":
    main()