forked from badlogicmanpreet/htm-drivefailures
/
lifeguard_runner_dev.py
360 lines (285 loc) · 10.7 KB
/
lifeguard_runner_dev.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
__author__ = 'manpreet.singh'
import pandas as pd
import logging
import csv
from nupic.frameworks.opf.modelfactory import ModelFactory
from model_params.harddrive_smart_data_model_params import MODEL_PARAMS
import numpy as np
import os
from nupic.data.inference_shifter import InferenceShifter
import nupic_anomaly_output
import datetime
import anomaly_plot as anplt
import time
from nupic.research.TP import TP
_LOGGER = logging.getLogger(__name__)
_INPUT_FILE = 'harddrive-smart-data-pp-to-train.csv'
_INPUT_DATA_FILE = 'harddrive-smart-data.csv'
_OUTPUT_PATH = "anomaly_scores.csv"
_ANOMALY_THRESHOLD = 0.9
# '7/2/10 0:00'
DATE_FORMAT = "%m/%d/%y %H:%M:%S"
# utility to convert to float
def convertorToFloat(val):
if val == 'True':
val = 1
elif val == 'False':
val = 0
elif val is False:
val = 0
return val
# select feature set
def dataCleanser(inputFile):
df = pd.read_csv(inputFile)
colsToDrop = ['GList1', 'PList', 'Servo1', 'Servo2', 'Servo3', 'Servo5',
'ReadError1', 'ReadError2', 'ReadError3', 'FlyHeight5',
'ReadError18', 'ReadError19', 'Servo7', 'Servo8', 'ReadError20', 'GList2',
'GList3', 'Servo10']
df = df.drop(colsToDrop, axis=1)
df['class'] = df['class'].apply(convertorToFloat)
df.to_csv('harddrive-smart-data-temp.csv', sep=',', index=False)
df = pd.read_csv('harddrive-smart-data-temp.csv')
df = df.convert_objects(convert_numeric=True)
print(df.dtypes)
df.to_csv('harddrive-smart-data.csv', sep=',', index=False)
def get_train_test_inds(y,train_proportion=0.7):
'''Generates indices, making random stratified split into training set and testing sets
with proportions train_proportion and (1-train_proportion) of initial sample.
y is any iterable indicating classes of each observation in the sample.
Initial proportions of classes inside training and
testing sets are preserved (stratified sampling).
'''
y=np.array(y)
y.setflags(write=True)
train_inds = np.zeros(len(y),dtype=bool)
test_inds = np.zeros(len(y),dtype=bool)
values = np.unique(y)
for value in values:
value_inds = np.nonzero(y==value)[0]
value_inds.setflags(write=True)
np.random.shuffle(value_inds)
n = int(train_proportion*len(value_inds))
train_inds[value_inds[:n]]=True
test_inds[value_inds[n:]]=True
return train_inds,test_inds
# split data into good and bad drives, also training and test data
def dataSplit():
df = pd.read_csv('harddrive-smart-data.csv')
dfBad = df.loc[df['class'] == 1.0]
dfGood = df.loc[df['class'] == 0.0]
dfBad.to_csv('harddrive-smart-data-bad.csv', sep=',', index=False)
dfGood.to_csv('harddrive-smart-data-good.csv', sep=',', index=False)
print(dfBad.shape)
print(dfGood.shape)
# train_inds_bad, test_inds_bad = get_train_test_inds(dfBad)
# train_inds_good, test_inds_good = get_train_test_inds(dfGood)
#
# dfBadTrain = dfBad[train_inds_bad]
# dfBadTest = dfBad[test_inds_bad]
#
# dfGoodTrain = dfGood[train_inds_good]
# dfGoodTest = dfGood[test_inds_good]
#
# dfBadTrain.to_csv('harddrive-smart-data-bad-train.csv', sep=',', index=False)
# dfBadTest.to_csv('harddrive-smart-data-bad-test.csv', sep=',', index=False)
#
# dfGoodTrain.to_csv('harddrive-smart-data-good-train.csv', sep=',', index=False)
# dfGoodTest.to_csv('harddrive-smart-data-good-test.csv', sep=',', index=False)
'''
Create singleton models with active learning
'''
def getModel(flag):
modelDir = os.getcwd() + '/model/%s' % flag
_LOGGER.info(modelDir)
if os.path.exists(modelDir):
_LOGGER.info('model exists')
model = ModelFactory.loadFromCheckpoint(modelDir)
return model
else:
_LOGGER.info('creating new model')
model = ModelFactory.create(MODEL_PARAMS)
model.save(modelDir)
return model
'''
get saved models and process data sequentially. reset happens after 300 records/ drive
'''
def createModelAndProcess():
_LOGGER.info('processing good drives')
goodModel = getModel('good')
goodModel.enableInference({'predictedField': 'class'})
inputFile = open('harddrive-smart-data-good.csv', "rb")
csvReader = csv.reader(inputFile)
# skip header rows
csvReader.next()
counter = 0
for row in csvReader:
counter += 1
FlyHeight6 = float(row[0])
FlyHeight7 = float(row[1])
FlyHeight8 = float(row[2])
FlyHeight9 = float(row[3])
FlyHeight10 = float(row[4])
FlyHeight11 = float(row[5])
FlyHeight12 = float(row[6])
classV = float(row[7])
result = goodModel.run({
"FlyHeight6": FlyHeight6,
"FlyHeight7": FlyHeight7,
"FlyHeight8": FlyHeight6,
"FlyHeight9": FlyHeight6,
"FlyHeight10": FlyHeight6,
"FlyHeight11": FlyHeight6,
"FlyHeight12": FlyHeight6,
"class": classV
})
if counter % 300 == 0:
goodModel.resetSequenceStates()
print "Read %i lines and reset done..." % counter
inputFile.close()
_LOGGER.info('processing bad drives')
badModel = getModel('bad')
badModel.enableInference({'predictedField': 'class'})
inputFile = open('harddrive-smart-data-bad.csv', "rb")
csvReader = csv.reader(inputFile)
# skip header rows
csvReader.next()
counter = 0
for row in csvReader:
counter += 1
FlyHeight6 = float(row[0])
FlyHeight7 = float(row[1])
FlyHeight8 = float(row[2])
FlyHeight9 = float(row[3])
FlyHeight10 = float(row[4])
FlyHeight11 = float(row[5])
FlyHeight12 = float(row[6])
classV = float(row[7])
result = badModel.run({
"FlyHeight6": FlyHeight6,
"FlyHeight7": FlyHeight7,
"FlyHeight8": FlyHeight6,
"FlyHeight9": FlyHeight6,
"FlyHeight10": FlyHeight6,
"FlyHeight11": FlyHeight6,
"FlyHeight12": FlyHeight6,
"class": classV
})
if counter % 300 == 0:
badModel.resetSequenceStates()
print "Read %i lines and reset done..." % counter
inputFile.close()
_LOGGER.info('done processing drives')
'''
run temporal anomaly to get the predictions and anomaly score, plot them.
Learning is disabled.
Results are saved to CSV for post analytics
nupic_output - used for plotting
'''
def runHarddriveAnomaly(plot):
shifter = InferenceShifter()
_LOGGER.info('start with anomaly detection...')
model = getModel('good')
model.enableInference({'predictedField': 'class'})
# model.disableLearning()
_LOGGER.info('read data file...')
inputFile = open('harddrive-smart-data-good-test.csv', "rb")
csvReader = csv.reader(inputFile)
# skip header rows
csvReader.next()
csvWriter = csv.writer(open(_OUTPUT_PATH, "wa"))
csvWriter.writerow(["class", "Prediction", "anomaly_score"])
output = anplt.NuPICPlotOutput('HarddriveDetection')
for row in csvReader:
FlyHeight6 = float(row[0])
FlyHeight7 = float(row[1])
FlyHeight8 = float(row[2])
FlyHeight9 = float(row[3])
FlyHeight10 = float(row[4])
FlyHeight11 = float(row[5])
FlyHeight12 = float(row[6])
classV = float(row[7])
timestamp = datetime.datetime.strptime(datetime.datetime.now().strftime(DATE_FORMAT), DATE_FORMAT)
result = model.run({
"FlyHeight6": FlyHeight6,
"FlyHeight7": FlyHeight7,
"FlyHeight8": FlyHeight6,
"FlyHeight9": FlyHeight6,
"FlyHeight10": FlyHeight6,
"FlyHeight11": FlyHeight6,
"FlyHeight12": FlyHeight6,
"class": classV
})
if plot:
result = shifter.shift(result)
prediction = result.inferences["multiStepBestPredictions"][1]
anomalyScore = result.inferences['anomalyScore']
# output.write(timestamp, result.rawInput["class"], prediction, anomalyScore)
# csvWriter.writerow([row])
# csvWriter.writerow('--------------------------------------------------')
# csvWriter.writerow([result.rawInput['class'], prediction, anomalyScore])
output.write(timestamp, result.rawInput['class'], prediction, anomalyScore)
# _LOGGER.info('plotted')
if anomalyScore > _ANOMALY_THRESHOLD:
_LOGGER.info("Anomaly detected at [%s]. Anomaly score: %f.", result.rawInput["class"], anomalyScore)
# time.sleep(1)
def findGoodOrBad():
inputFile = open('harddrive-smart-data-goodbad-test.csv', "rb")
csvReader = csv.reader(inputFile)
# skip header rows
csvReader.next()
_LOGGER.info('evaluate models')
goodModel = getModel('good')
goodModel.enableInference({'predictedField': 'class'})
badModel = getModel('bad')
badModel.enableInference({'predictedField': 'class'})
for row in csvReader:
FlyHeight6 = float(row[0])
FlyHeight7 = float(row[1])
FlyHeight8 = float(row[2])
FlyHeight9 = float(row[3])
FlyHeight10 = float(row[4])
FlyHeight11 = float(row[5])
FlyHeight12 = float(row[6])
classV = float(row[7])
resultG = goodModel.run({
"FlyHeight6": FlyHeight6,
"FlyHeight7": FlyHeight7,
"FlyHeight8": FlyHeight6,
"FlyHeight9": FlyHeight6,
"FlyHeight10": FlyHeight6,
"FlyHeight11": FlyHeight6,
"FlyHeight12": FlyHeight6,
"class": classV
})
predictionG = resultG.inferences["multiStepBestPredictions"][1]
anomalyScoreG = resultG.inferences['anomalyScore']
print(predictionG, anomalyScoreG)
resultB = badModel.run({
"FlyHeight6": FlyHeight6,
"FlyHeight7": FlyHeight7,
"FlyHeight8": FlyHeight6,
"FlyHeight9": FlyHeight6,
"FlyHeight10": FlyHeight6,
"FlyHeight11": FlyHeight6,
"FlyHeight12": FlyHeight6,
"class": classV
})
predictionB = resultB.inferences["multiStepBestPredictions"][1]
anomalyScoreB = resultB.inferences['anomalyScore']
print(predictionB, anomalyScoreB)
print "Anomaly scores have been written to", _OUTPUT_PATH
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
'''
# select feature vector
dataCleanser(_INPUT_FILE)
# split data
dataSplit()
# create models and process all data sequentially
createModelAndProcess()
'''
# find drive is good or bad
findGoodOrBad()
# run harddrive anomaly
runHarddriveAnomaly(plot=True)
_LOGGER.info('Finally done with Anomaly Detection')