-
Notifications
You must be signed in to change notification settings - Fork 497
/
Copy pathbenchmark_time_dataset.py
484 lines (413 loc) · 15.7 KB
/
benchmark_time_dataset.py
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
import logging
import os
import pathlib
import time
from itertools import product
import pandas as pd
import torch.utils.benchmark as benchmark
from torch.utils.data import DataLoader
from neuralprophet import NeuralProphet, df_utils, utils, utils_time_dataset
from neuralprophet.data.process import _check_dataframe, _handle_missing_data
from neuralprophet.data.transform import _normalize
# from neuralprophet.forecaster import
log = logging.getLogger("NP.test")
# log.setLevel("INFO")
# log.parent.setLevel("INFO")
# log.setLevel("WARNING")
# log.parent.setLevel("WARNING")
log.setLevel("ERROR")
log.parent.setLevel("ERROR")
DIR = pathlib.Path(__file__).parent.parent.parent.absolute()
DATA_DIR = os.path.join(DIR, "tests", "test-data")
PEYTON_FILE = os.path.join(DATA_DIR, "wp_log_peyton_manning.csv")
AIR_FILE = os.path.join(DATA_DIR, "air_passengers.csv")
YOS_FILE = os.path.join(DATA_DIR, "yosemite_temps.csv")
NROWS = 1000
EPOCHS = 1
BATCH_SIZE = 10
LR = 1.0
def print_input_shapes(inputs):
tabularized_input_shapes_str = ""
for key, value in inputs.items():
if key in [
"seasonalities",
"covariates",
"events",
"regressors",
]:
for name, period_features in value.items():
tabularized_input_shapes_str += f" {name} {key} {period_features.shape}\n"
else:
tabularized_input_shapes_str += f" {key} {value.shape} \n"
print(f"Tabularized inputs shapes: \n{tabularized_input_shapes_str}")
def load(nrows=NROWS, epochs=EPOCHS, batch=BATCH_SIZE, season=True, iterations=1):
tic = time.perf_counter()
df = pd.read_csv(YOS_FILE, nrows=nrows)
freq = "5min"
num_workers = 0
n_lags = (12,)
n_forecasts = (6,)
m = NeuralProphet(
n_lags=n_lags,
n_forecasts=n_forecasts,
epochs=epochs,
batch_size=batch,
learning_rate=LR,
yearly_seasonality=season,
weekly_seasonality=season,
daily_seasonality=season,
)
# Mimick m.fit(df) behavior
df = df.copy(deep=True)
df, _, _, m.id_list = df_utils.check_multiple_series_id(df)
df = _check_dataframe(m, df, check_y=True, exogenous=True)
m.data_freq = df_utils.infer_frequency(df, n_lags=m.config_model.max_lags, freq=freq)
df = _handle_missing_data(
df=df,
freq=m.data_freq,
n_lags=m.config_ar.n_lags,
n_forecasts=m.config_model.n_forecasts,
config_missing=m.config_missing,
config_regressors=m.config_regressors,
config_lagged_regressors=m.config_lagged_regressors,
config_events=m.config_events,
config_seasonality=m.config_seasonality,
predicting=False,
)
# mimick _init_train_loader
m.config_normalization.init_data_params(
df=df,
config_lagged_regressors=m.config_lagged_regressors,
config_regressors=m.config_regressors,
config_events=m.config_events,
config_seasonality=m.config_seasonality,
)
df = _normalize(df=df, config_normalization=m.config_normalization)
df_merged = df_utils.merge_dataframes(df)
m.config_seasonality = utils.set_auto_seasonalities(df_merged, config_seasonality=m.config_seasonality)
if m.config_country_holidays is not None:
m.config_country_holidays.init_holidays(df_merged)
components_stacker = utils_time_dataset.ComponentStacker(
n_lags=m.config_ar.n_lags,
n_forecasts=m.config_model.n_forecasts,
max_lags=m.config_model.max_lags,
config_seasonality=m.config_seasonality,
lagged_regressor_config=m.config_lagged_regressors,
)
dataset = m._create_dataset(
df, predict_mode=False, components_stacker=components_stacker
) # needs to be called after set_auto_seasonalities
# Determine the max_number of epochs
m.config_train.set_auto_batch_epoch(n_data=len(dataset))
loader = DataLoader(
dataset,
batch_size=m.config_train.batch_size,
shuffle=True,
num_workers=num_workers,
)
# dataset_size = len(df)
# print(dataset_size)
dataloader_iterator = iter(loader)
toc = time.perf_counter()
print(f"######## Time: {toc - tic:0.4f} for setup")
tic = time.perf_counter()
for i in range(iterations):
data, target, meta = next(dataloader_iterator)
try:
data, target, meta = next(dataloader_iterator)
except StopIteration:
dataloader_iterator = iter(loader)
data, target, meta = next(dataloader_iterator)
# do_something()
toc = time.perf_counter()
# print_input_shapes(data)
# print(len(meta["df_name"]))
print(f"######## Time: {toc - tic:0.4f} for iterating {iterations} batches of size {batch}")
load(nrows=1010, batch=100, iterations=10)
def yosemite(nrows=NROWS, epochs=EPOCHS, batch=BATCH_SIZE, season=True):
# log.info("testing: Uncertainty Estimation Yosemite Temps")
df = pd.read_csv(YOS_FILE, nrows=nrows)
m = NeuralProphet(
n_lags=12,
n_forecasts=6,
quantiles=[0.01, 0.99],
epochs=epochs,
batch_size=batch,
learning_rate=LR,
yearly_seasonality=season,
weekly_seasonality=season,
daily_seasonality=season,
)
# tic = time.perf_counter()
m.fit(df, freq="5min")
# toc = time.perf_counter()
# print(f"######## Time: {toc - tic:0.4f} for fit")
# tic = time.perf_counter()
# future = m.make_future_dataframe(df, periods=6, n_historic_predictions=3 * 24 * 12)
# toc = time.perf_counter()
# print(f"######## Time: {toc - tic:0.4f} for make_future_dataframe")
# tic = time.perf_counter()
# m.predict(future)
# toc = time.perf_counter()
# print(f"######## Time: {toc - tic:0.4f} for predict")
m.highlight_nth_step_ahead_of_each_forecast(m.config_model.n_forecasts)
def peyton(nrows=NROWS, epochs=EPOCHS, batch=BATCH_SIZE, season=True):
# log.info("testing: Uncertainty Estimation Peyton Manning")
df = pd.read_csv(PEYTON_FILE, nrows=nrows)
playoffs = pd.DataFrame(
{
"event": "playoff",
"ds": pd.to_datetime(
[
"2008-01-13",
"2009-01-03",
"2010-01-16",
"2010-01-24",
"2010-02-07",
"2011-01-08",
"2013-01-12",
"2014-01-12",
"2014-01-19",
"2014-02-02",
"2015-01-11",
"2016-01-17",
"2016-01-24",
"2016-02-07",
]
),
}
)
superbowls = pd.DataFrame(
{
"event": "superbowl",
"ds": pd.to_datetime(["2010-02-07", "2014-02-02", "2016-02-07"]),
}
)
events_df = pd.concat((playoffs, superbowls))
m = NeuralProphet(
n_forecasts=1,
loss_func="SmoothL1Loss",
quantiles=[0.01, 0.99],
epochs=epochs,
batch_size=batch,
learning_rate=LR,
yearly_seasonality=season,
weekly_seasonality=season,
# daily_seasonality=False,
)
# add lagged regressors
# # if m.config_ar.n_lags > 0:
# df["A"] = df["y"].rolling(7, min_periods=1).mean()
# df["B"] = df["y"].rolling(30, min_periods=1).mean()
# m = m.add_lagged_regressor(name="A", n_lags=10)
# m = m.add_lagged_regressor(name="B", only_last_value=True)
# add events
m = m.add_events(["superbowl", "playoff"], lower_window=-1, upper_window=1, regularization=0.1)
m = m.add_country_holidays("US", mode="additive", regularization=0.1)
df["C"] = df["y"].rolling(7, min_periods=1).mean()
df["D"] = df["y"].rolling(30, min_periods=1).mean()
m = m.add_future_regressor(name="C", regularization=0.1)
m = m.add_future_regressor(name="D", regularization=0.1)
history_df = m.create_df_with_events(df, events_df)
m.fit(history_df, freq="D")
# periods = 90
# regressors_future_df = pd.DataFrame(data={"C": df["C"][:periods], "D": df["D"][:periods]})
# future_df = m.make_future_dataframe(
# df=history_df,
# regressors_df=regressors_future_df,
# events_df=events_df,
# periods=periods,
# n_historic_predictions=nrows,
# )
# m.predict(df=future_df)
def peyton_minus_events(nrows=NROWS, epochs=EPOCHS, batch=BATCH_SIZE, season=True):
# log.info("testing: Uncertainty Estimation Peyton Manning")
df = pd.read_csv(PEYTON_FILE, nrows=nrows)
m = NeuralProphet(
n_forecasts=1,
loss_func="SmoothL1Loss",
quantiles=[0.01, 0.99],
epochs=epochs,
batch_size=batch,
learning_rate=LR,
yearly_seasonality=season,
weekly_seasonality=season,
# daily_seasonality=False,
)
# add lagged regressors
if m.config_ar.n_lags > 0:
df["A"] = df["y"].rolling(7, min_periods=1).mean()
df["B"] = df["y"].rolling(30, min_periods=1).mean()
m = m.add_lagged_regressor(name="A")
m = m.add_lagged_regressor(name="B", only_last_value=True)
df["C"] = df["y"].rolling(7, min_periods=1).mean()
df["D"] = df["y"].rolling(30, min_periods=1).mean()
m = m.add_future_regressor(name="C", regularization=0.1)
m = m.add_future_regressor(name="D", regularization=0.1)
history_df = df
m.fit(history_df, freq="D")
# periods = 90
# regressors_future_df = pd.DataFrame(data={"C": df["C"][:periods], "D": df["D"][:periods]})
# future_df = m.make_future_dataframe(
# df=history_df,
# regressors_df=regressors_future_df,
# periods=periods,
# n_historic_predictions=nrows,
# )
# m.predict(df=future_df)
def peyton_minus_regressors(nrows=NROWS, epochs=EPOCHS, batch=BATCH_SIZE, season=True):
# log.info("testing: Uncertainty Estimation Peyton Manning")
df = pd.read_csv(PEYTON_FILE, nrows=nrows)
playoffs = pd.DataFrame(
{
"event": "playoff",
"ds": pd.to_datetime(
[
"2008-01-13",
"2009-01-03",
"2010-01-16",
"2010-01-24",
"2010-02-07",
"2011-01-08",
"2013-01-12",
"2014-01-12",
"2014-01-19",
"2014-02-02",
"2015-01-11",
"2016-01-17",
"2016-01-24",
"2016-02-07",
]
),
}
)
superbowls = pd.DataFrame(
{
"event": "superbowl",
"ds": pd.to_datetime(["2010-02-07", "2014-02-02", "2016-02-07"]),
}
)
events_df = pd.concat((playoffs, superbowls))
m = NeuralProphet(
n_forecasts=1,
loss_func="SmoothL1Loss",
quantiles=[0.01, 0.99],
epochs=epochs,
batch_size=batch,
learning_rate=LR,
yearly_seasonality=season,
weekly_seasonality=season,
# daily_seasonality=False,
)
# add events
m = m.add_events(["superbowl", "playoff"], lower_window=-1, upper_window=1, regularization=0.1)
m = m.add_country_holidays("US", mode="additive", regularization=0.1)
history_df = m.create_df_with_events(df, events_df)
m.fit(history_df, freq="D")
# periods = 90
# future_df = m.make_future_dataframe(
# df=history_df,
# events_df=events_df,
# periods=periods,
# n_historic_predictions=nrows,
# )
# m.predict(df=future_df)
#######################################
# tic = time.perf_counter()
# test_uncertainty_estimation_yosemite_temps()
# toc = time.perf_counter()
# print(f"#### Time: {toc - tic:0.4f} for test_uncertainty_estimation_yosemite_temps")
# tic = time.perf_counter()
# test_uncertainty_estimation_peyton_manning()
# toc = time.perf_counter()
# print(f"#### Time: {toc - tic:0.4f} for test_uncertainty_estimation_peyton_manning")
# tic = time.perf_counter()
# test_uncertainty_estimation_air_travel()
# toc = time.perf_counter()
# print(f"#### Time: {toc - tic:0.4f} for test_uncertainty_estimation_air_travel")
# tic = time.perf_counter()
# test_uncertainty_estimation_multiple_quantiles()
# toc = time.perf_counter()
# print(f"#### Time: {toc - tic:0.4f} for test_uncertainty_estimation_multiple_quantiles")
# tic = time.perf_counter()
# test_split_conformal_prediction()
# toc = time.perf_counter()
# print(f"#### Time: {toc - tic:0.4f} for test_split_conformal_prediction")
# tic = time.perf_counter()
# test_asymmetrical_quantiles()
# toc = time.perf_counter()
# print(f"#### Time: {toc - tic:0.4f} for test_asymmetrical_quantiles")
# t0 = benchmark.Timer(
# stmt='test_uncertainty_estimation_yosemite_temps(x)',
# setup='from __main__ import test_uncertainty_estimation_yosemite_temps',
# globals={'x': x}
# )
# t1 = benchmark.Timer(
# stmt='test_uncertainty_estimation_peyton_manning(x)',
# setup='from __main__ import test_uncertainty_estimation_peyton_manning',
# # globals={'x': x}
# )
# print(t0.timeit(1))
# print(t1.timeit(1))
###############################
def measure_times():
# Compare takes a list of measurements which we'll save in results.
results = []
epochs = [5]
sizes = [100, 1000]
# sizes = [100, 1000, 10000]
batches = [128]
seasons = [False, True]
for ep, nrows, b, season in product(epochs, sizes, batches, seasons):
# label and sub_label are the rows
# description is the column
label = "tests"
sub_label = f"[rows: {nrows}, epochs:{ep}, batch:{b}, season:{season}]"
for num_threads in [1]: # [1, 4, 16, 64]
results.append(
benchmark.Timer(
stmt="yosemite(nrows, epochs, batch, season)",
setup="from __main__ import yosemite",
globals={"epochs": ep, "nrows": nrows, "batch": b, "season": season},
num_threads=num_threads,
label=label,
sub_label=sub_label,
description="yosemite",
).blocked_autorange(min_run_time=1)
)
results.append(
benchmark.Timer(
stmt="peyton(nrows, epochs, batch, season)",
setup="from __main__ import peyton",
globals={"nrows": nrows, "epochs": ep, "batch": b, "season": season},
num_threads=num_threads,
label=label,
sub_label=sub_label,
description="peyton",
).blocked_autorange(min_run_time=1)
)
results.append(
benchmark.Timer(
stmt="peyton_minus_events(nrows, epochs, batch, season)",
setup="from __main__ import peyton_minus_events",
globals={"nrows": nrows, "epochs": ep, "batch": b, "season": season},
num_threads=num_threads,
label=label,
sub_label=sub_label,
description="peyton_minus_events",
).blocked_autorange(min_run_time=1)
)
results.append(
benchmark.Timer(
stmt="peyton_minus_regressors(nrows, epochs, batch, season)",
setup="from __main__ import peyton_minus_regressors",
globals={"nrows": nrows, "epochs": ep, "batch": b, "season": season},
num_threads=num_threads,
label=label,
sub_label=sub_label,
description="peyton_minus_regressors",
).blocked_autorange(min_run_time=1)
)
compare = benchmark.Compare(results)
compare.print()
measure_times()