-
Notifications
You must be signed in to change notification settings - Fork 17
/
functions_timeseries.py
171 lines (136 loc) · 5.62 KB
/
functions_timeseries.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
from engines.helpers import merge_two_dicts
from engines.var import anomaly_VAR, univariate_anomaly_VAR,univariate_forecast_VAR
from engines.holtwinter import anomaly_holt,forecast_holt
from engines.auto_arima import anomaly_AutoArima
from engines.lstm import anomaly_LSTM, anomaly_uni_LSTM
from engines.fbprophet import anomaly_fbprophet
import traceback
#from .server import app,celery
#from server import app
from server import celery
from engines.BBDD import new_model, get_best_model
from struct import *
@celery.task
def model_univariate(self,lista_datos,num_fut,desv_mse,train,name):
engines_output={}
debug = {}
if not train:
# filename = './models_temp/'+name
# with open(filename,'r') as f:
# winner = f.read()
# f.close()
(model_name,model,params)=get_best_model('winner_'+name)
# print ("recupero el motor " )
winner= model_name
if winner == 'LSTM':
try:
engines_output['LSTM'] = anomaly_uni_LSTM(lista_datos,num_fut,desv_mse,train,name)
debug['LSTM'] = engines_output['LSTM']['debug']
except Exception as e:
print(e)
print ('ERROR: exception executing LSTM univariate')
elif winner == 'VAR':
engines_output['VAR'] = univariate_forecast_VAR(lista_datos,num_fut,name)
debug['VAR'] = engines_output['VAR']['debug']
elif winner == 'Holtwinters':
engines_output['Holtwinters'] = forecast_holt(lista_datos,num_fut,desv_mse,name)
debug['Holtwinters'] = engines_output['Holtwinters']['debug']
else:
print ("Error")
else:
try:
engines_output['LSTM'] = anomaly_uni_LSTM(lista_datos,num_fut,desv_mse,train,name)
debug['LSTM'] = engines_output['LSTM']['debug']
except Exception as e:
print(e)
print ('ERROR: exception executing LSTM univariate:'+ str(e))
try:
engines_output['fbprophet'] = anomaly_fbprophet(lista_datos,num_fut,desv_mse,train,name)
debug['fbprophet'] = engines_output['fbprophet']['debug']
except Exception as e:
print ('ERROR: fbprophet univariate: ' + str(e) )
try:
if (len(lista_datos) > 100):
##new_length=
lista_datos_ari=lista_datos[len(lista_datos)-100:]
engines_output['arima'] = anomaly_AutoArima(lista_datos_ari,num_fut,len(lista_datos),desv_mse)
debug['arima'] = engines_output['arima']['debug']
except Exception as e:
print ('ERROR: exception executing Autoarima: '+ str(e))
try:
if (train):
engines_output['VAR'] = univariate_anomaly_VAR(lista_datos,num_fut,name)
debug['VAR'] = engines_output['VAR']['debug']
else:
engines_output['VAR'] = univariate_forecast_VAR(lista_datos,num_fut,name)
debug['VAR'] = engines_output['VAR']['debug']
except Exception as e:
print(e)
print ('ERROR: exception executing VAR: '+str(e))
try:
if (train ):
engines_output['Holtwinters'] = anomaly_holt(lista_datos,num_fut,desv_mse,name)
debug['Holtwinters'] = engines_output['Holtwinters']['debug']
else:
print ("entra en forecast")
engines_output['Holtwinters'] = forecast_holt(lista_datos,num_fut,desv_mse,name)
debug['Holtwinters'] = engines_output['Holtwinters']['debug']
except Exception as e:
print(e)
print ('ERROR: exception executing Holtwinters: '+ str(e))
best_mae=999999999
winner='VAR'
print ('The size is: ')
print (len(engines_output))
for key, value in engines_output.items():
print (key + " " + str(value['mae']))
if value['mae'] < best_mae:
best_mae=value['mae']
winner=key
print(winner)
# filename = './models_temp/'+name
# with open(filename,'w') as f:
# f.write(winner)
# f.close()
new_model('winner_'+name, winner, pack('N', 365),'',0)
print (winner)
print ("el ganador es " + str(winner))
print (engines_output[winner])
temp= {}
temp['debug']=debug
return merge_two_dicts(engines_output[winner] , temp)
def model_multivariate(list_var,num_fut,desv_mse):
engines_output={}
debug = {}
try:
engines_output['LSTM'] = anomaly_LSTM(list_var,num_fut,desv_mse)
debug['LSTM'] = engines_output['LSTM']['debug']
print (engines_output['LSTM'])
except Exception as e:
print(e)
print ('ERROR: exception executing LSTM')
try:
engines_output['VAR'] = anomaly_VAR(list_var,num_fut)
debug['VAR'] = engines_output['VAR']['debug']
print (engines_output['VAR'])
except Exception as e:
print(Exception)
print("type error: " + str(e))
print(traceback.format_exc())
print ('ERROR: exception executing VAR')
best_mae=999999999
winner='LSTM'
print ('The size is ')
print (len(engines_output))
print (debug)
for key, value in engines_output.items():
print (key)
print(str(value['mae']))
if value['mae'] < best_mae:
print (key + " " + str(value['mae']) + " best:" + str(best_mae) )
best_mae=value['mae']
winner=key
print ("el ganador es " + winner)
temp= {}
temp['debug']=debug
return merge_two_dicts(engines_output[winner] , temp)