-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathtest_utils.py
79 lines (64 loc) · 2.12 KB
/
test_utils.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
#!/usr/bin/env python3
import logging
import os
import pathlib
import pandas as pd
import pytest
from neuralprophet import NeuralProphet, df_utils, load, save
log = logging.getLogger("NP.test")
log.setLevel("DEBUG")
log.parent.setLevel("WARNING")
DIR = pathlib.Path(__file__).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 = 512
EPOCHS = 10
LR = 1.0
BATCH_SIZE = 64
PLOT = False
def test_create_dummy_datestamps():
df = pd.read_csv(PEYTON_FILE, nrows=NROWS)
df_drop = df.drop("ds", axis=1)
df_dummy = df_utils.create_dummy_datestamps(df_drop)
df["ds"] = pd.NA
with pytest.raises(ValueError):
_ = df_utils.create_dummy_datestamps(df)
m = NeuralProphet(epochs=EPOCHS, batch_size=BATCH_SIZE, learning_rate=LR)
_ = m.fit(df_dummy)
_ = m.make_future_dataframe(df_dummy, periods=365, n_historic_predictions=True)
def test_save_load():
df = pd.read_csv(PEYTON_FILE, nrows=NROWS)
m = NeuralProphet(
epochs=EPOCHS,
batch_size=BATCH_SIZE,
learning_rate=LR,
n_lags=6,
n_forecasts=3,
n_changepoints=0,
)
_ = m.fit(df, freq="D")
future = m.make_future_dataframe(df, periods=3)
forecast = m.predict(df=future)
log.info("testing: save")
save(m, "test_model.pt")
log.info("testing: load")
m2 = load("test_model.pt")
forecast2 = m2.predict(df=future)
# Check that the forecasts are the same
pd.testing.assert_frame_equal(forecast, forecast2)
# TODO: add functionality to continue training
# def test_continue_training():
# df = pd.read_csv(PEYTON_FILE, nrows=NROWS)
# m = NeuralProphet(
# epochs=EPOCHS,
# batch_size=BATCH_SIZE,
# learning_rate=LR,
# n_lags=6,
# n_forecasts=3,
# n_changepoints=0,
# )
# metrics = m.fit(df, freq="D")
# metrics2 = m.fit(df, freq="D", continue_training=True)
# assert metrics1["Loss"].sum() >= metrics2["Loss"].sum()