File size: 7,061 Bytes
fe3e959
 
 
1251295
fe3e959
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4e9e14
fe3e959
 
 
 
 
 
1351d6c
fe3e959
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c7702c
 
0231a93
6c7702c
 
0231a93
aa85654
 
 
6c7702c
 
 
 
 
 
6c7af43
 
72b0594
a32a295
 
 
 
 
 
fe3e959
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc3c9b
fe3e959
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25f8166
 
 
fe3e959
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45181e6
 
 
 
 
 
 
 
 
 
fe3e959
 
 
 
 
 
 
 
45181e6
fe3e959
 
 
 
 
 
 
 
 
45181e6
fe3e959
 
 
 
 
 
 
 
 
45181e6
fe3e959
 
 
45181e6
fe3e959
 
 
 
 
 
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
import os
import gradio as gr
import pandas as pd
import io

from gluonts.dataset.pandas import PandasDataset
from gluonts.dataset.split import split
from gluonts.torch.model.deepar import DeepAREstimator
from gluonts.torch.distributions import (
    NegativeBinomialOutput,
    StudentTOutput,
    NormalOutput,
)
from gluonts.evaluation import Evaluator, make_evaluation_predictions

from make_plot import plot_forecast, plot_train_test


def offset_calculation(prediction_length, rolling_windows, length):
    row_offset = -1 * prediction_length * rolling_windows
    if abs(row_offset) > 0.95 * length:
        raise gr.Error("Reduce prediction_length * rolling_windows")
    return row_offset


def preprocess(
    input_data,
    prediction_length,
    rolling_windows,
    epochs,
    progress=gr.Progress(track_tqdm=True),
):
    df = pd.read_csv(input_data.name, index_col=0, parse_dates=True)
    df.sort_index(inplace=True)
    row_offset = offset_calculation(prediction_length, rolling_windows, len(df))
    return plot_train_test(df.iloc[:row_offset], df.iloc[row_offset:])


def train_and_forecast(
    input_data,
    file_data,
    prediction_length,
    rolling_windows,
    epochs,
    distribution,
    progress=gr.Progress(track_tqdm=True),
):
    if not input_data and not file_data:
        raise gr.Error("Upload a file with the Upload button")
    try:
        if input_data:
            df = pd.read_csv(input_data.name, index_col=0, parse_dates=True)
        else:
            df = pd.read_csv(file_data.name, index_col=0, parse_dates=True)
        df.sort_index(inplace=True)
    except AttributeError:
        raise gr.Error("Upload a file with the Upload button")

    freq = pd.infer_freq(df.index[:3])
    print(freq)
    date_range = pd.date_range(df.index[0], pd.DateOffset(months=prediction_length) + df.index[-1], freq=freq)
    print(date_range)
    new_df = df.reindex(date_range)
    # new_df = new_df.interpolate(method='spline', order=2)
    new_df.reset_index(inplace=True)
    new_df.columns = ['ds', 'y']

    # trying this hack to fix pandas beginMonth stuff
    buffer = io.BytesIO()
    new_df.to_csv(buffer, index=False)
    buffer.seek(0)
    df = pd.read_csv(buffer, index_col=0, parse_dates=True)
    df.sort_index(inplace=True)
    print(df)
    gluon_df = PandasDataset(df, target=df.columns[0])
    print(gluon_df)
    row_offset = offset_calculation(prediction_length, rolling_windows, len(df))
    # prediction_length += int(len(df) * 0.1)
    # try:
    #     gluon_df = PandasDataset(df, target=df.columns[0])
    # except TypeError:
     
    training_data, test_gen = split(gluon_df, offset=row_offset)

    if distribution == "StudentT":
        distr_output = StudentTOutput()
    elif distribution == "Normal":
        distr_output = NormalOutput()
    else:
        distr_output = NegativeBinomialOutput()
    estimator = DeepAREstimator(
        distr_output=distr_output,
        prediction_length=prediction_length,
        freq=gluon_df.freq,
        trainer_kwargs=dict(max_epochs=epochs),
    )

    predictor = estimator.train(
        training_data=training_data,
    )

    test_data = test_gen.generate_instances(
        prediction_length=prediction_length, windows=rolling_windows
    )

    evaluator = Evaluator(num_workers=0)
    forecast_it, ts_it = make_evaluation_predictions(
        dataset=test_data.input, predictor=predictor
    )
    agg_metrics, _ = evaluator(ts_it, forecast_it)

    forecasts = list(predictor.predict(test_data.input))

    return plot_forecast(df, forecasts), agg_metrics


with gr.Blocks() as demo:
    gr.Markdown(
        """
    # Probabilistic Time Series Forecasting
    
    ## How to use

    Upload a *univariate* csv where the first column contains date-times and the second column is your data for example:

    | ds    | y        | 
    |------------|---------------|
    | 2007-12-10 | 9.590761      |
    | 2007-12-11 | 8.519590      |
    | 2007-12-12 | 8.183677      |
    | 2007-12-13 | 8.072467      |
    | 2007-12-14 | 7.893572      |

    ## Steps

    1. Click **Upload** to upload your data and visualize it **or** select one of the example CSV files.
    2. Click **Run**
        - This app will then train an estimator and show its predictions as well as evaluation metrics.
    """
    )
    with gr.Accordion(label="Hyperparameters"):
        with gr.Row():
            prediction_length = gr.Number(
                value=12, label="Prediction Length", precision=0
            )
            windows = gr.Number(value=3, label="Number of Windows", precision=0)
            epochs = gr.Number(value=10, label="Number of Epochs", precision=0)
            distribution = gr.Radio(
                choices=["StudentT", "Negative Binomial", "Normal"],
                value="StudentT",
                label="Distribution",
            )

    with gr.Row(label="ds"):
        upload_btn = gr.UploadButton(label="Upload")
        train_btn = gr.Button(label="Train and Forecast")
    plot = gr.Plot()
    json = gr.JSON(label="Evaluation Metrics")
    file_output = gr.File()
    upload_btn.upload(
        fn=preprocess,
        inputs=[upload_btn, prediction_length, windows],
        outputs=[plot],
    )
    train_btn.click(
        fn=train_and_forecast,
        inputs=[
            upload_btn,
            file_output,
            prediction_length,
            windows,
            epochs,
            distribution,
        ],
        outputs=[plot, json],
    )
    with gr.Row(label="Example Data"):
        examples = gr.Examples(
            examples=[
                [
                    os.path.join(
                        os.path.dirname(__file__),
                        "examples",
                        "kazakhstan_astana_realestate.csv",
                    ),
                    12,
                    3,
                    30,
                ],
                [
                    os.path.join(
                        os.path.dirname(__file__),
                        "examples",
                        "example_air_passengers.csv",
                    ),
                    12,
                    3,
                    10,
                ],
                [
                    os.path.join(
                        os.path.dirname(__file__),
                        "examples",
                        "example_retail_sales.csv",
                    ),
                    12,
                    3,
                    10,
                ],
                [
                    os.path.join(
                        os.path.dirname(__file__),
                        "examples",
                        "example_pedestrians_covid.csv",
                    ),
                    12,
                    3,
                    10,
                ],
            ],
            fn=preprocess,
            inputs=[file_output, prediction_length, windows, epochs],
            outputs=[plot],
            run_on_click=True,
        )

if __name__ == "__main__":
    demo.queue().launch()