forked from winedarksea/AutoTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
290 lines (249 loc) · 8.01 KB
/
test.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
"""Informal testing script."""
from time import sleep
import timeit
import platform
import pandas as pd
from autots.datasets import ( # noqa
load_daily,
load_hourly,
load_monthly,
load_yearly,
load_weekly,
load_weekdays,
load_zeroes,
load_linear,
load_sine,
)
from autots import AutoTS, create_regressor, model_forecast # noqa
import matplotlib.pyplot as plt
# raise ValueError("aaargh!")
use_template = False
force_univariate = False # long = False
back_forecast = False
graph = True
# this is the template file imported:
example_filename = "example_export.csv" # .csv/.json
forecast_length = 8
long = False
# df = load_linear(long=long, shape=(200, 500), introduce_nan=0.2)
df = load_daily(long=long)
n_jobs = "auto"
verbose = 2
validation_method = "similarity"
frequency = "infer"
drop_most_recent = 0
if use_template:
generations = 5
num_validations = 0
else:
generations = 2
num_validations = 3
if force_univariate:
df = df.iloc[:, 0]
transformer_list = (
"superfast" # ["bkfilter", "STLFilter", "HPFilter", 'StandardScaler']
)
transformer_max_depth = 1
models_mode = "default" # "regressor"
model_list = "fast"
# model_list = "regressor" # fast_parallel, all
# model_list = ["SeasonalNaive", 'AverageValueNaive']
metric_weighting = {
'smape_weighting': 5,
'mae_weighting': 2,
'rmse_weighting': 2,
'made_weighting': 1,
'containment_weighting': 0,
'runtime_weighting': 0.05,
'spl_weighting': 2,
'contour_weighting': 1,
}
model = AutoTS(
forecast_length=forecast_length,
frequency=frequency,
prediction_interval=0.9,
ensemble=["horizontal-max", "dist", "simple"], # "subsample"
constraint=None,
max_generations=generations,
num_validations=num_validations,
validation_method=validation_method,
model_list=model_list,
transformer_list=transformer_list,
transformer_max_depth=transformer_max_depth,
initial_template="Random",
metric_weighting=metric_weighting,
models_to_validate=0.35,
max_per_model_class=None,
model_interrupt="end_generation",
n_jobs=n_jobs,
drop_most_recent=drop_most_recent,
introduce_na=True,
# prefill_na=0,
# subset=5,
verbose=verbose,
models_mode=models_mode,
)
regr_train, regr_fcst = create_regressor(
df,
forecast_length=forecast_length,
frequency=frequency,
drop_most_recent=drop_most_recent,
scale=True,
summarize="auto",
backfill="bfill",
fill_na="pchip",
holiday_countries=["US"],
datepart_method="recurring",
)
# model = model.import_results('test.pickle')
if use_template:
model = model.import_template(
example_filename, method="only", enforce_model_list=True
)
start_time_for = timeit.default_timer()
model = model.fit(
df,
future_regressor=regr_train,
weights="mean",
# result_file='test.pickle',
validation_indexes=[
pd.date_range("2021-01-01", "2022-05-02"),
pd.date_range("2021-01-01", "2022-02-02"),
pd.date_range("2021-01-01", "2022-03-03"),
],
date_col="datetime" if long else None,
value_col="value" if long else None,
id_col="series_id" if long else None,
)
elapsed_for = timeit.default_timer() - start_time_for
prediction = model.predict(future_regressor=regr_fcst, verbose=1)
# point forecasts dataframe
forecasts_df = prediction.forecast
# accuracy of all tried model results (not including cross validation)
initial_results = model.results()
# validation results
validation_results = model.results("validation")
initial_results["TransformationRuntime"] = initial_results[
"TransformationRuntime"
].dt.total_seconds()
initial_results["FitRuntime"] = initial_results["FitRuntime"].dt.total_seconds()
initial_results["PredictRuntime"] = initial_results["PredictRuntime"].dt.total_seconds()
initial_results["TotalRuntime"] = initial_results["TotalRuntime"].dt.total_seconds()
sleep(5)
print(model)
print(model.validation_test_indexes)
print(f"Model failure rate is {model.failure_rate() * 100:.1f}%")
print("Slowest models:")
print(
initial_results[initial_results["Ensemble"] < 1]
.groupby("Model")
.agg({"TotalRuntime": ["mean", "max"]})
.idxmax()
)
initial_results.to_csv("general_template_" + str(platform.node()) + ".csv")
if graph:
prediction.plot(
model.df_wide_numeric,
series=model.df_wide_numeric.columns[2],
remove_zeroes=False,
start_date="2018-09-26",
)
plt.show()
model.plot_generation_loss()
if model.best_model["Ensemble"].iloc[0] == 2:
plt.show()
model.plot_horizontal_transformers(method="fillna")
plt.show()
model.plot_horizontal_transformers()
plt.show()
model.plot_horizontal()
plt.show()
if "mosaic" in model.best_model["ModelParameters"].iloc[0].lower():
mosaic_df = model.mosaic_to_df()
print(mosaic_df[mosaic_df.columns[0:5]].head(5))
plt.show()
if back_forecast:
model.plot_backforecast(n_splits="auto", start_date="2019-01-01")
df_wide_numeric = model.df_wide_numeric
df = df_wide_numeric.tail(100).fillna(0).astype(float)
print("test run complete")
"""
df_forecast = model_forecast(
model_name="SectionalMotif",
model_param_dict={},
model_transform_dict={
'fillna': 'mean',
'transformations': {'0': 'ClipOutliers'},
'transformation_params': {'0': {"method": "clip", "std_threshold": 3, "fillna": None}}
},
df_train=df,
forecast_length=5,
frequency='infer',
prediction_interval=0.9,
no_negatives=False,
# future_regressor_train=future_regressor_train2d,
# future_regressor_forecast=future_regressor_forecast2d,
random_seed=321,
verbose=1,
n_jobs="auto",
)
df_forecast.forecast.head(5)
"""
"""
# Import/Export
model.export_template(example_filename, models='all',
n=15, max_per_model_class=3)
del(model)
model = model.import_template(example_filename, method='only')
print("Overwrite template is: {}".format(str(model.initial_template)))
# default save location of files is apparently root
systemd-run --unit=background_cmd_service --remain-after-exit /home/colin/miniconda3/envs/openblas/bin/python /home/colin/AutoTS/test.py
systemd-run --unit=background_cmd_service --remain-after-exit /home/colin/miniconda3/envs/openblas/bin/python /home/colin/AutoTS/local_example.py
journalctl -r -n 10 -u background_cmd_service
journalctl -f -u background_cmd_service
journalctl -b -u background_cmd_service
systemctl stop background_cmd_service
systemctl reset-failed
systemctl kill background_cmd_service
scp [email protected]:/home/colin/AutoTS/general_template_colin-1135.csv ./Documents/AutoTS
scp [email protected]:/general_template_colin-1135.csv ./Documents/AutoTS
Edgey Cases:
Single Time Series
Forecast Length of 1
Very short training data
Lots of NaN
PACKAGE RELEASE
# update version in setup.py, /docs/conf.py, /autots/_init__.py
set PYTHONPATH=%PYTHONPATH%;C:/Users/Colin/Documents/AutoTS
python -m unittest discover ./tests
python ./autots/evaluator/benchmark.py
cd <project dir>
black ./autots -l 88 -S
https://github.com/sphinx-doc/sphinx/issues/3382
# pip install sphinx==2.4.4
# m2r does not yet work on sphinx 3.0
# pip install m2r
cd <project dir>
# delete docs/source and /build (not tutorial or intro.rst)
sphinx-apidoc -f -o docs/source autots
cd ./docs
make html
https://winedarksea.github.io/AutoTS/build/index.html
"""
"""
https://packaging.python.org/tutorials/packaging-projects/
python -m pip install --user --upgrade setuptools wheel
cd /to project directory
python setup.py sdist bdist_wheel
twine upload dist/*
Merge dev to master on GitHub and create release (include .tar.gz)
"""
# Help correlate errors with parameters
"""
test = initial_results[initial_results['TransformationParameters'].str.contains('FastICA')]
cols = ['Model', 'ModelParameters', 'TransformationParameters', 'Exceptions']
if (~initial_results['Exceptions'].isna()).sum() > 0:
test_corr = error_correlations(
initial_results[cols], result='corr'
) # result='poly corr'
"""