anomaly_detector.py
Go to the documentation of this file.
1 '''
2 ROS Anomaly Detector Framework
3 
4 Author:
5  Vedanth Narayanan
6 File:
7  Anomaly Detector class
8 Date:
9  4 May, 2018
10 
11 '''
12 
13 import os
14 from sklearn import svm
15 import numpy as np
16 from output_matrix import OutputMatrix
17 from sklearn.externals import joblib
18 # import rospy
19 from pprint import pprint
20 
21 
22 class AnomalyDetector(object):
23 
24  def __init__(self, unsup_models, sup_models):
25  # unsup_models = [('OC SVM', svm.OneClassSVM(nu=0.001, kernel="rbf", gamma=21))],
26  # sup_models = [('RBF SVM', svm.SVC(kernel='rbf', gamma=1000, C=100000))]):
27  self.__unsup_clfs = unsup_models
28  self.__sup_clfs = sup_models
29  self.__outputMat = OutputMatrix()
30  # rospy.set_param('unsupervised_model', str(['None']))
31  # rospy.set_param('supervised_model', str(['None']))
32 
33 
34  def __fit_unsupervised(self, data):
35  '''
36  'Private' fit unsupervised models method
37  Iterate through all models
38  '''
39  unsuper_clfs = []
40  for i in xrange(0, len(self.__unsup_clfs)):
41  current_model = self.__unsup_clfs[i]
42 
43  # NOTE: If model exists, load. or fit and save
44  saveloc = '../data/'+current_model[0]+'.pkl'
45  if os.path.exists(saveloc):
46  current_model = (current_model[0], joblib.load(saveloc))
47  else:
48  current_model[1].fit(data)
49  joblib.dump(current_model[1], saveloc)
50 
51  # Adding fitted model back to class member
52  self.__unsup_clfs[i] = current_model
53  unsuper_clfs.append(saveloc)
54  # rospy.set_param('unsupervised_model', str(unsuper_clfs))
55 
56 
57  def __fit_supervised(self, data_x, data_y):
58  '''
59  'Private' fit supervised models method
60  Iterate through all models
61  '''
62  super_clfs = []
63  for i in xrange(0, len(self.__sup_clfs)):
64  current_model = self.__sup_clfs[i]
65 
66  # NOTE: If model exists, load. or fit and save
67  saveloc = '../data/'+current_model[0]+'.pkl'
68  if os.path.exists(saveloc):
69  current_model = (current_model[0], joblib.load(saveloc))
70  else:
71  current_model[1].fit(data_x, data_y.ravel())
72  joblib.dump(current_model[1], saveloc)
73 
74  # Adding fitted model back to class member
75  self.__sup_clfs[i] = current_model
76  super_clfs.append(saveloc)
77  # rospy.set_param('supervised_model', str(super_clfs))
78 
79 
80  def __classify(self, model, data):
81  '''
82  Given a model, prediction is made.
83 
84  BUG: Assuming model is used as a binary classifier,
85  predictions are turned to 0/1 vs 1/-1
86  '''
87  predictions = model[1].predict(data)
88 
89  # errs = predictions[predictions == 1].size
90  # print model[0] + ' Errors: ', 100*float(errs)/data.shape[0]
91  return predictions
92 
93 
94  def fit_supervised_models(self, sup_train_x, sup_train_y):
95  '''
96  Unsupervised data is mixed. Labels (y) are passed in.
97  '''
98  self.__fit_supervised(sup_train_x, sup_train_y)
99 
100 
101  def fit_unsupervised_models(self, data_x):
102  '''
103  Supervised data is meant to all be safe. No labels.
104  '''
105  self.__fit_unsupervised(data_x)
106 
107 
108  def classify_supervised(self, dataset_name, in_data, out_label):
109  '''
110  Get predictions for unsupervised models.
111  Calls 'private' method with model for predictions.
112  For the dataset that is passed in, all supervised model
113  results are compiled.
114  With the output labels, the output matrix is called.
115  '''
116 
117  all_preds = []
118  for i in xrange(0, len(self.__sup_clfs)):
119  model = self.__sup_clfs[i]
120  preds = self.__classify(model, in_data)
121  all_preds.append(preds)
122 
123  all_preds = np.array(all_preds)
124 
125  print 'Supervised results for', dataset_name
126  print 'Order of results: ', [item[0] for item in self.__sup_clfs]
127  self.call_output(out_label, all_preds)
128  return all_preds
129 
130 
131  def classify_unsupervised(self, dataset_name, in_data, out_label):
132  '''
133  Get predictions for unsupervised models.
134  Calls 'private' method with model for predictions.
135  For the dataset that is passed in, all unsupervised model
136  results are compiled.
137  With the output labels, the output matrix is called.
138  '''
139 
140  all_preds = []
141  for i in xrange(0, len(self.__unsup_clfs)):
142  model = self.__unsup_clfs[i]
143  preds = self.__classify(model, in_data)
144  all_preds.append(preds)
145 
146  all_preds = np.array(all_preds)
147 
148  print 'Unsupervised results for', dataset_name
149  print 'Order of results: ', [item[0] for item in self.__unsup_clfs]
150  self.call_output(out_label, all_preds)
151  return all_preds
152 
153 
154  def call_output(self, true_y, pred_y):
155  '''
156  Predictions for multiple classifiers are being
157  passed, so need to iterate through them.
158  '''
159  for item in pred_y:
160  self.__outputMat.output_matrix(true_y, item)
def classify_unsupervised(self, dataset_name, in_data, out_label)
def __init__(self, unsup_models, sup_models)
def fit_supervised_models(self, sup_train_x, sup_train_y)
def __fit_supervised(self, data_x, data_y)
def classify_supervised(self, dataset_name, in_data, out_label)


mh5_anomaly_detector
Author(s): Vedanth Narayanan
autogenerated on Mon Jun 10 2019 13:49:20