-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrun.py
97 lines (67 loc) · 3.58 KB
/
run.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
"""
This program runs the anomaly detection for EWMA, ARMA-GARCH and LSTM.
The program thereafter calculates the relevant scores.
"""
import sys
import os
import time
from ewma import *
from hl import *
from lstm_tbptt_hyperopt import *
from arma_garch import *
import matplotlib.pyplot as plt
#Function that loads data into a numpy matrix from csv file.
#These files are generated by running ./garch/garch_long.py
def getData(datatype="D1", size="100"):
D = genfromtxt('./garch/' + datatype + '_' + size + '.csv', delimiter=',')
D_no_anomalies = genfromtxt('./garch/' + datatype + '_unpolluted_' + size + '.csv', delimiter=',')
D_truth = genfromtxt('./garch/' + datatype + '_truth_' + size + '.csv', delimiter=',')
return D, D_no_anomalies, D_truth
if __name__ == "__main__":
plot = False
args = sys.argv[1:]
if "--plot" in args:
plot = True
hl = hl()
ewma = ewma()
lstm = lstm_tbptt()
arma_garch = arma_garch()
start_sample = 0 # first serie to evaluate
N = 6 # number of series to evaluate.
dataTypes = ["D3", "D2", "D4"]
dataSizes = ["1090"]
for dt in dataTypes:
for ds in dataSizes:
#Get data from file.
print("Starting Dataset:" + ds + " " + dt + "_" + dt + "_" + dt + "_" + dt + "_" + dt + "_" + dt + "_" + dt + "_" + dt + "_" + dt + "_" + dt + "_" + dt + "_" + dt + "_" + dt)
D, D_no_anomalies, D_truth = getData(dt, ds)
#HL
start_hl = time.time()
hl_guesses = hl.runAnomalyDetection(D[0:N], D_truth[0:N], D_no_anomalies[0:N], dt, ds)
end_hl = time.time()
np.savetxt(str(dt) + "_" + str(ds) + "_guesses_hl.csv", hl_guesses, delimiter=",")
print('Elapsed time for HL: ' + str(end_hl-start_hl))
#EWMA
start_ewma = time.time()
ewma_guesses = ewma.runAnomalyDetection(D[0:N], D_truth[0:N], D_no_anomalies[0:N], dt, ds)
end_ewma = time.time()
np.savetxt(str(dt) + "_" + str(ds) + "_guesses_ewma.csv", ewma_guesses, delimiter=",")
print('Elapsed time for EWMA: ' + str(end_ewma-start_ewma))
#LSTM
start_lstm = time.time()
lstm_guesses, lstm_preds, lstm_likelihoods, lstm_resudials = lstm.runAnomalyDetection(D[start_sample:N], D_truth[start_sample:N], D_no_anomalies[start_sample:N], dt, ds)
end_lstm = time.time()
np.savetxt(str(dt) + "_" + str(ds) + "_guesses_lstm.csv", lstm_guesses, delimiter=",")
print('Elapsed time for LSTM: ' + str(end_lstm-start_lstm))
#ARMA
start_arma = time.time()
arma_garch_guesses, arma_garch_means, arma_garch_variances = arma_garch.runAnomalyDetection(D[0:N], D_truth[0:N], D_no_anomalies[0:N], dt, ds)
end_arma = time.time()
np.savetxt(str(dt) + "_" + str(ds) + "_guesses_arma.csv", arma_garch_guesses, delimiter=",")
print('Elapsed time for ARMA: ' + str(end_arma-start_arma))
if plot:
print("Plotting")
hl.plotAnomalyDetection(D[0:N], D_truth[0:N], D_no_anomalies[0:N], ewma_guesses[0:N])
ewma.plotAnomalyDetection(D[0:N], D_truth[0:N], D_no_anomalies[0:N], ewma_guesses[0:N])
lstm.plotAnomalyDetection(D[0:N], D_truth[0:N], D_no_anomalies[0:N], lstm_guesses[0:N], lstm_preds[0:N], lstm_likelihoods[0:N], lstm_resudials[0:N])
arma_garch.plotAnomalyDetection(D[0:N], D_truth[0:N], D_no_anomalies[0:N], arma_garch_guesses[0:N], arma_garch_means[0:N], arma_garch_variances[0:N])