-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathnabTestSimulator.py
140 lines (103 loc) · 5.15 KB
/
nabTestSimulator.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
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Contextual Anomaly Detector - Open Source Edition
# Copyright (C) 2016, Mikhail Smirnov [email protected]
# https://github.com/smirmik/CAD
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero Public License version 3 as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero Public License for more details.
#
# You should have received a copy of the GNU Affero Public License along with
# this program. If not, see http://www.gnu.org/licenses.
#
# ------------------------------------------------------------------------------
import csv
import datetime
import os
import math
from CAD_OSE import CAD_OSE
if __name__ == '__main__':
TESTSET = 1
baseDataDir = "../NAB/data"
baseResultsDir = "../NAB/results"
nullResultsDir = baseResultsDir + "/null"
numResultTypes = 1
numReturnedAnomalyValues = 1
startAnomalyValueNumber = 0
if TESTSET == 1 :
maxLeftSemiContextsLenght = 7
maxActiveNeuronsNum = 15
numNormValueBits = 3
baseThreshold = 0.75
elif TESTSET == 0 :
maxLeftSemiContextsLenght = 8
maxActiveNeuronsNum = 16
numNormValueBits = 3
baseThreshold = 1.0
projectDirDescriptors = []
for valuesVersion in xrange(numResultTypes) :
projectDirDescriptors.append("CAD-{0:%Y%m%d%H%M}-Set{1:1d}".format(datetime.datetime.now(), TESTSET))
dataDirTree = os.walk(baseDataDir)
dirNames = []
fullFileNames = []
for i, dirDescr in enumerate(dataDirTree) :
if i == 0 :
dirNames = dirDescr[1]
else :
for fileName in dirDescr[2] :
fullFileNames.append(dirDescr[0] + "/" + fileName)
for projDirDescr in projectDirDescriptors :
for directory in dirNames :
os.makedirs(baseResultsDir +"/" + projDirDescr + "/" + directory )
for fileNumber, fullFileName in enumerate(fullFileNames, start=1) :
print("-----------------------------------------")
print("[ " + str(fileNumber) + " ] " + fullFileName)
minValue = float("inf")
maxValue = -float("inf")
with open(fullFileName, 'rb') as csvfile:
csvReader = csv.reader(csvfile, delimiter=',')
next(csvReader)
for rowNumber, row in enumerate(csvReader, start = 1):
inputDataValue = float(row[1])
minValue = min(inputDataValue, minValue)
maxValue = max(inputDataValue, maxValue)
learningPeriod = min( math.floor(0.15 * rowNumber), 0.15 * 5000)
print("minValue = " + str(minValue) + " : maxValue = " + str(maxValue))
cad = CAD_OSE (
minValue = minValue,
maxValue = maxValue,
baseThreshold = baseThreshold,
restPeriod = learningPeriod / 5.0,
maxLeftSemiContextsLenght = maxLeftSemiContextsLenght,
maxActiveNeuronsNum = maxActiveNeuronsNum,
numNormValueBits = numNormValueBits
)
anomalyMassiv = []
numSteps = 0
outFileDsc = fullFileName[len(baseDataDir)+1:].split("/")
labelsFile = open(nullResultsDir + "/" + outFileDsc[0] + "/" + "null_" + outFileDsc[1], 'rb')
csvLabelsReader = csv.reader(labelsFile, delimiter=',')
next(csvLabelsReader)
with open(fullFileName, 'rb') as csvfile:
csvReader = csv.reader(csvfile, delimiter=',')
next(csvReader)
for row in csvReader:
numSteps+=1
currentLabel = next(csvLabelsReader)[3]
inputDataDate = datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S")
inputDataValue = float(row[1])
inputData = {"timestamp" : inputDataDate, "value" : inputDataValue}
results = cad.getAnomalyScore(inputData)
anomalyMassiv.append([numSteps, row[0], row[1], currentLabel, [results]])
for i, projDirDescr in enumerate(projectDirDescriptors, start=startAnomalyValueNumber) :
newFileName = baseResultsDir + "/" + projDirDescr + "/" + outFileDsc[0] + "/" + projDirDescr + "_" + outFileDsc[1]
with open(newFileName, 'w') as csvOutFile:
csvOutFile.write("timestamp,value,anomaly_score,label\n")
for anomalyScores in anomalyMassiv:
csvOutFile.write(anomalyScores[1]+ "," + anomalyScores[2] + "," + str(anomalyScores[4][i]) + "," + anomalyScores[3] + "\n")
print ("saved to: " + newFileName)