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
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
import json
import io
import os
import tempfile
import datetime

import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sktime.utils.plotting import plot_series
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf

from src.forecaster import Forecaster
from src.forecaster.models import XGBoost
from src.analyser import Analyser
from src.idsc import IDSC

from src.forecaster.models import ProphetForecaster


class GradioApp():
    def __init__(
        self
    ) -> None:
        self.forecaster = Forecaster()
        self.analyser = Analyser()
        self.idsc = IDSC()

        self.historical_demo_data = 'data/multivariate/demo_historical.csv'
        self.future_demo_data = 'data/multivariate/demo_future.csv'

        self.data: pd.DataFrame = None
        self.n_predict = 3
        self.window_length = 7
        self.target_column = 'y'
        self.exog_columns = []

        # Define if the model's result is going to be rounded
        self.round_results = True

        # Delete old temp files oder than n minutes
        self.delete_file_old_than_n_minutes = 10

        self.plot_figsize_full_screen = (20, 4)

        # -------------------- #
        # Model Related Params #
        # -------------------- #

        # XGBoost #
        self.xgboost = XGBoost()
        self.xgboost_cv = False
        self.xgboost_params = self.xgboost.cv_params
        self.xgboost_strategy = 'recursive'
        self.xgboost_forecast = None
        self.xgboost_test = None
        print('Init Gradio app')

        # Prophet #
        self.prophet = ProphetForecaster()
        self.prophet__seasonality_mode = 'multiplicative'
        self.prophet__add_country_holidays = {'country_name': 'Singapore'}
        self.prophet__yearly_seasonality = True
        self.prophet__weekly_seasonality = False
        self.prophet__daily_seasonality = False

    def checkbox__round_results__change(self, val):
        self.round_results = val

    def textbox__target_column__change(self, val):
        print('Updating textbox__target_column:', val)
        self.target_column = val

    def btn__profiling__click(self):
        self.analyser.fit(self.data)
        self.analyser.profiling()

        return (
            self.update__md__profiling(),
            self.update__plot__changepoints())

    def btn__plot_correlation__click(self):
        return (self.update__plot__correlation())

    def file__historical__upload(
        self,
        file
    ):
        self.data = pd.read_csv(
            file.name,
            index_col='datetime',
            parse_dates=['datetime'])

        print('[file__historical__upload]')

        return (
            self.update__df__table_view(),
            self.update__dropdown__chart_view_filter(),
            self.update__dropdown__seasonality_decompose(),
            self.update__plot__chart_view())

    def file__future__upload(
        self,
        file
    ):
        self.__handle_future_data_upload(file.name)

        return (
            self.update__df__table_view(),
            self.update__dropdown__chart_view_filter(),
            self.update__dropdown__seasonality_decompose(),
            self.update__plot__chart_view(),
            self.update__number__n_predict())

    def btn__load_future_demo__click(
        self
    ):
        self.__handle_future_data_upload(self.future_demo_data)

        # [df__table_view, number__n_predict]

        return (
            self.update__df__table_view(),
            self.update__dropdown__chart_view_filter(),
            self.update__dropdown__seasonality_decompose(),
            self.update__plot__chart_view(),
            self.update__number__n_predict())

    def __handle_future_data_upload(
        self,
        path
    ):
        data = pd.read_csv(
            path,
            index_col='datetime',
            parse_dates=['datetime'])
        self.exog_columns = data.columns.tolist()
        self.n_predict = len(data)

        print(
            f"[file__future__upload] with {self.exog_columns} columns")

        self.data = pd.concat(
            [self.data, data],
            axis=0)

    def number__n_predict__change(
        self,
        val
    ):
        print(f'[number__n_predict__change], {val}')
        self.n_predict = val

    def number__window_length__change(
            self,
            val):
        print(f'[number__window_length__change], {val}')
        self.window_length = val

    def btn__fit_data__click(
            self):
        data = self.data.drop(columns=self.exog_columns).dropna(how='any')

        self.forecaster.fit(
            data,
            target_col=self.target_column,
            n_predict=self.n_predict,
            window_length=self.window_length,
            exog=None if len(
                self.exog_columns) == 0 else self.data[self.exog_columns])
        return (
            gr.Number(interactive=False),  # number__n_predict
            gr.Number(interactive=False),  # number__window_length
            gr.File(interactive=False),  # file__historical
            gr.File(interactive=False),  # file__future
            gr.Button(visible=False),  # btn__fit_data
            gr.Column(visible=True),  # column__models
            gr.Button(visible=False),  # btn__load_historical_demo
            gr.Button(visible=False),  # btn__load_future_demo
            self.update__md__forecast_data_info()
        )

    def btn__load_historical_demo__click(
        self
    ):
        self.data = pd.read_csv(
            self.historical_demo_data,
            index_col='datetime',
            parse_dates=['datetime'])

        return (
            self.update__df__table_view(),
            self.update__dropdown__chart_view_filter(),
            self.update__dropdown__seasonality_decompose(),
            self.update__plot__chart_view()
        )

    def dropdown__chart_view_filter__change(self, options):
        return (self.update__plot__chart_view(options))

    def dropdown__seasonality_decompose__change(self, col):
        return (
            self.update__plot__seasonality_decompose(col),
            self.update__plot_acg_pacf(col))

    # ------------------------ #
    # XGboost Model Operations #
    # ------------------------ #

    def btn__train_xgboost__click(self):
        (test, forecast, best_params) = self.xgboost.fit_predict(
            y=self.forecaster.y,
            y_train=self.forecaster.y_train,
            window_length=self.forecaster.window_length,
            fh=self.forecaster.fh,
            fh_test=self.forecaster.fh_test,
            params=self.xgboost_params,
            X=self.forecaster.X,
            X_train=self.forecaster.X_train,
            X_test=self.forecaster.X_test,
            X_future=self.forecaster.X_future
        )

        print(test, forecast, best_params)

        self.xgboost_forecast = forecast
        self.xgboost_test = test

        return (
            self.update__plot__xgboost_result(test, forecast),
            self.update__file__xgboost_result(),
            self.update__df__xgboost_result())

    def btn__set_xgboost_params__click(self, text):
        params = json.loads(text.replace("'", '"'))
        self.xgboost_params = params

        return (
            self.update__json_xgboost_params()
        )

    def checkbox__xgboost_round__change(self, val):
        self.xgboost.round_result = val

    # ----------------------------------- #
    # Prophet Model Operations & Updaters #
    # ----------------------------------- #

    def btn__forecast_with_prophet__click(self):
        self.prophet.fit_predict(
            self.forecaster.y_train,
            self.forecaster.y,
            self.forecaster.fh,
            self.forecaster.fh_test,
            self.forecaster.period,
            self.forecaster.freq,
            X=self.forecaster.exog,
            seasonality_mode=self.prophet__seasonality_mode,
            add_country_holidays=self.prophet__add_country_holidays,
            yearly_seasonality=self.prophet__yearly_seasonality,
            weekly_seasonality=self.prophet__weekly_seasonality,
            daily_seasonality=self.prophet__daily_seasonality,
            round_val=self.round_results)

        return (
            self.update__plot__prophet_result(),
            self.update__file__prophet_result(),
            self.update__df__prophet_result())

    def update__plot__prophet_result(self):
        fig, ax = plt.subplots(figsize=self.plot_figsize_full_screen)

        plot_series(
            self.forecaster.y_train[-2 * self.forecaster.period:],
            self.forecaster.y_test,
            self.prophet.predict,
            self.prophet.forecast,
            pred_interval=self.prophet.forecast_interval,
            labels=['Train', 'Test', 'Predicted - Test', 'Forecast'],
            ax=ax)

        ax.set_title('Prophet Forecast Result')
        ax.legend(loc='upper left')
        fig.tight_layout()

        return gr.Plot(fig)

    def update__file__prophet_result(self):
        prophet_forecast_df = pd.DataFrame(self.prophet.forecast)
        path = self.__create_temp_csv_file(prophet_forecast_df)
        return gr.File(path)

    def update__df__prophet_result(self):
        prophet_forecast_df = self.prophet.forecast.reset_index()
        return gr.Dataframe(value=prophet_forecast_df)

    # =============================== #
    # || Gradio Component Updaters || #
    # =============================== #

    def update__plot__changepoints(self):
        fig, axs = plt.subplots(2, 1, figsize=(20, 8))

        axs[0].plot(self.data[['y']])

        axs[0].text(self.data.index[0],
                    axs[0].get_ylim()[1]*0.9,
                    self.analyser.quantity_predictability[0],
                    fontsize=20)

        for i, p in enumerate(self.analyser.quantity_change_points):
            axs[0].axvline(x=p)
            axs[0].text(p,
                        axs[0].get_ylim()[1]*0.9,
                        self.analyser.quantity_predictability[i+1],
                        fontsize=20)

        axs[1].plot(self.data[['y']])

        axs[1].text(self.data.index[0],
                    axs[1].get_ylim()[1]*0.9,
                    self.analyser.intermittent_predictability[0],
                    fontsize=20)

        for i, p in enumerate(self.analyser.intermittent_change_points):
            axs[1].axvline(x=p)
            axs[1].text(p,
                        axs[1].get_ylim()[1]*0.9,
                        self.analyser.intermittent_predictability[i+1],
                        fontsize=20)

        axs[0].set_title('Quantity Change Points & Predictability')
        axs[1].set_title('Intermittent Change Points & Predictability')

        fig.tight_layout()

        return gr.Plot(fig)

    def update__md__profiling(self):

        return (f""" 
            \n### Data Characteristic: 
            \n # {self.analyser.characteristic} 
            \n ---
            \n### Quantity Change Points: {self.analyser.quantity_change_points.astype(str).tolist()} 
            \n### Quantity Predictability: {self.analyser.quantity_predictability} 
            \n### Intermittent Change Points: {self.analyser.intermittent_change_points.astype(str).tolist()} 
            \n### Intermittent Predictability: {self.analyser.intermittent_predictability} 
            """)

    def update__md__forecast_data_info(self):
        return gr.Markdown(value=f' \
                                **Forecasting for these timestamps**: \
                                {self.forecaster.fh.to_pandas().astype(str).tolist()} \
                               \n **Data Period**: {self.forecaster.period} \
                               \n **Data Frequency**: {self.forecaster.freq} \
                           ')

    def update__plot__correlation(self):
        fig, ax = plt.subplots(figsize=(20, 8))
        corr = self.data.corr(numeric_only=True)
        mask = np.triu(np.ones_like(corr, dtype=bool))
        sns.heatmap(
            corr,
            mask=mask,
            square=True,
            annot=True,
            cmap='coolwarm',
            linewidths=.5,
            cbar_kws={"shrink": .5},
            ax=ax)

        fig.tight_layout()

        return gr.Plot(fig)

    def update__df__table_view(
        self
    ):
        data = self.data.reset_index()
        return gr.Dataframe(value=data)

    def update__number__n_predict(
        self
    ):
        return gr.Number(self.n_predict, interactive=False)

    def update__dropdown__chart_view_filter(self):
        options = self.data.columns.tolist()
        return gr.Dropdown(options, value=options)

    def update__dropdown__seasonality_decompose(self):
        options = self.data.columns.tolist()
        return gr.Dropdown(options)

    def update__plot__seasonality_decompose(self, col):
        seasonal = seasonal_decompose(self.data[col].dropna())

        fig = seasonal.plot()
        return gr.Plot(fig)

    def update__plot_acg_pacf(self, col):
        fig, axs = plt.subplots(2, 1, sharex=True, sharey=True)

        plot_acf(self.data[col].dropna(), ax=axs[0], zero=False)
        plot_pacf(self.data[col].dropna(), ax=axs[1], zero=False)

        axs[0].set_title('Auto Correlation')
        axs[1].set_title('Partial Auto Correlation')

        return gr.Plot(fig)

    # ---------------------- #
    # Update XGboost Results #
    # ---------------------- #

    def update__json_xgboost_params(self):
        return gr.JSON(value=self.xgboost_params)

    def update__plot__xgboost_result(self, test, predict):

        fig, ax = plt.subplots(figsize=self.plot_figsize_full_screen)

        plot_series(
            self.forecaster.y_train[-2*self.forecaster.period:],
            self.forecaster.y_test,
            test,
            predict,
            labels=["y_train (part)", "y_test", "y_pred", 'y_forecast'],
            x_label='Date',
            ax=ax)

        ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
        fig.tight_layout()

        return gr.Plot(fig)

    def update__plot__chart_view(self, cols=None):
        fig, ax = plt.subplots(figsize=self.plot_figsize_full_screen)

        _cols = cols

        if _cols is None:
            _cols = self.data.columns

        print('[update__plot__chart_view]')

        for col in _cols:
            ax.plot(self.data[[col]], label=col)

        fig.legend()
        fig.tight_layout()
        return gr.Plot(fig)

    def update__file__xgboost_result(self):
        path = self.__create_temp_csv_file(self.xgboost_forecast)
        return gr.File(path)

    def update__df__xgboost_result(self):

        # xgboost_forecast is actually a Series instead of proper DataFrame
        # Re constructing a proper dataframe for gradio to take
        data = pd.DataFrame(
            {"datetime": self.xgboost_forecast.index,
             "y": self.xgboost_forecast.values})

        return gr.Dataframe(value=data)

    # ------------- #
    # Util Function #
    # ------------- #

    def __create_temp_csv_file(self, df) -> str:
        time_format = "%Y%m%d%H%M%S"
        directory = 'temp'
        now = datetime.datetime.now()

        # Check if there are old files, remove them #
        for filename in os.listdir(directory):
            file_path = os.path.join(directory, filename)

            file_time = datetime.datetime.strptime(
                filename.split('.')[0], time_format)

            # If the file is older than 3 minutes, delete the file
            if now > datetime.timedelta(
                    minutes=self.delete_file_old_than_n_minutes) + file_time:
                print('deleting olde file: ', filename)
                os.remove(file_path)

        new_file_name = now.strftime(format=time_format) + '.csv'

        new_file_path = os.path.join(directory, new_file_name)

        df.to_csv(new_file_path)

        return new_file_path