interface.py
Go to the documentation of this file.
1 '''
2 ROS Anomaly Detector Framework
3 
4 Author:
5  Vedanth Narayanan
6 File:
7  Model Interface class
8 Date:
9  13 May, 2018
10 
11 
12 NOTE:
13  - This interface is built to help the user.
14  - This is meant to automate things. Since training files have
15  already been passed, the data can be made ready for the user
16  to use.
17 '''
18 
19 from pprint import pprint
20 import yaml
21 from sklearn import svm
22 import os
23 from data_process import DataProcess
24 from anomaly_detector import AnomalyDetector
25 from data_persist import DataPersist
26 
27 class Interface(object):
28 
29  def __init__(self, input_file):
30 
31  print '\n\n', os.getcwd()
32 
33 
34  with open(input_file, 'r') as fil:
35  local = yaml.load(fil)
36 
37  self.__fit_file = local["Files"]["fit_file"]
38  self.__unsupervised_train_file = local["Files"]["unsupervised_train"]
39  self.__supervised_train_file = local["Files"]["supervised_train"]
40  self.__testing = local["Files"]["testing"]
41  self.__processor = DataProcess(local["Operations"]["reduction"])
42 
44  self.__data_persist = DataPersist()
45 
48 
49 
51  '''
52  NOTE: Specifically for fitting models
53  - Split
54  - Scale
55  - Reduce
56  - BUG: Should this data be saved for future?
57 
58  NOTE: Functions below are specifically calling fit.
59  '''
60 
61  split_x, split_y = self.__processor.split(self.__fit_file["name"], \
62  self.__fit_file["input_col"], \
63  self.__fit_file["output_col"])
64 
65  scaled_x = self.__processor.scaler_fit(split_x)
66  reduced_x = self.__processor.reduction_fit(scaled_x)
67  self.__data_persist.dump_data(self.__fit_file["name"], \
68  reduced_x, split_y)
69 
70 
72  '''
73  Private function, user does not need to know this.
74  Use this function to get training data ready.
75  Checks to make sure a training file has been passed.
76  If filename is Null/None, it is skipped.
77 
78  '''
79 
80  if self.__unsupervised_train_file["name"]:
82 
83  if self.__supervised_train_file["name"]:
85 
86 
87  def __preprocess_data_helper(self, item):
88  '''
89  NOTE: Can only be called if __ready_processor has been called.
90  It should be, when the instance is created.
91  '''
92 
93  split_x, split_y = self.__processor.split(item["name"], \
94  item["input_col"], \
95  item["output_col"])
96  scaled_x = self.__processor.scaler_transform(split_x)
97  reduced_x = self.__processor.reduction_transform(scaled_x)
98  self.__data_persist.dump_data(item["name"], reduced_x, split_y)
99 
100 
102  '''
103  The input 'item' refers to an item in the list of testing datasets.
104 
105  The inputs and outputs from the datasets are first gathered.
106  Based on the training files passed in for those models, predictions
107  are returned.
108  '''
109 
110  unsuper_preds, super_preds = [], []
111 
112  x, y = self.__data_persist.retrieve_dumped_data(item["name"])
113 
114  if self.__unsupervised_train_file["name"]:
115  unsuper_preds = self.__anomaly_classifier.classify_unsupervised(
116  item["name"], x, y)
117 
118  if self.__supervised_train_file["name"]:
119  super_preds = self.__anomaly_classifier.classify_supervised(
120  item["name"], x, y)
121 
122  # print ''
123 
124  return unsuper_preds, super_preds
125 
126 
127  def print_all(self):
128  '''
129  This was written primarily as a sanity check.
130  Prints out datasets names for fitting, and training models.
131  '''
132 
133  print 'fit\t', self.__fit_file, type(self.__fit_file)
134  print 'unsuper\t', self.__unsupervised_train_file, type(self.__unsupervised_train_file)
135  print 'super\t', self.__supervised_train_file, type(self.__supervised_train_file)
136  print 'test\t', self.__testing, type(self.__testing)
137  pprint(self.__testing)
138  print '\n'
139 
140 
141  def genmodel_train(self, unsup_models, sup_models):
142  '''
143  Creates an instance of anomaly classifier.
144  Training data files already exist, so all models can and are
145  trained.
146  Models are ready for predictions after this stage.
147 
148  NOTE: Training predictions are grabbed and outputs are trying
149  to get printed.
150  Once the process is set, the predictions can be ridden.
151  '''
152 
153  self.__anomaly_classifier = AnomalyDetector(unsup_models, sup_models)
154 
155 
156  # Only need to do this, if an unsupervised method is passed
157  if self.__unsupervised_train_file["name"]:
158  # NOTE: The following can be scratched once the process is set
159  print '=====Unsupervised====='
160 
161  unsup_x, unsup_y = self.__data_persist.retrieve_dumped_data(
162  self.__unsupervised_train_file["name"])
163  self.__anomaly_classifier.fit_unsupervised_models(unsup_x)
164 
165  # preds = self.__anomaly_classifier.classify_unsupervised(
166  # self.__unsupervised_train_file["name"],
167  # unsup_x, unsup_y)
168  # self.__anomaly_classifier.call_output(unsup_y, preds)
169 
170 
171  # Only need to do this, if an supervised method is passed
172  if self.__supervised_train_file["name"]:
173  print '=====Supervised====='
174 
175  sup_x, sup_y = self.__data_persist.retrieve_dumped_data(
176  self.__supervised_train_file["name"])
177  self.__anomaly_classifier.fit_supervised_models(sup_x, sup_y)
178 
179  # preds = self.__anomaly_classifier.classify_supervised(
180  # self.__supervised_train_file["name"],
181  # sup_x, sup_y)
182  # self.__anomaly_classifier.call_output(sup_y, preds)
183 
184 
186  '''
187  Get predictions for testing files that was passed in
188  '''
189  u_preds = []
190  s_preds = []
191 
192  for item in self.__testing:
193  # print item["name"]
194  self.__preprocess_data_helper(item)
195  usup_preds, sup_preds = self.__testing_predictions_helper(item)
196 
197  u_preds.append(usup_preds)
198  s_preds.append(sup_preds)
199 
200  return u_preds, s_preds
201 
202 
203  def retrieve_data(self, loc):
204  return self.__data_persist.retrieve_dumped_data(loc)
def __preprocess_data_training(self)
Definition: interface.py:71
def __init__(self, input_file)
Definition: interface.py:29
def __preprocess_data_helper(self, item)
Definition: interface.py:87
def genmodel_train(self, unsup_models, sup_models)
Definition: interface.py:141
def __testing_predictions_helper(self, item)
Definition: interface.py:101
def retrieve_data(self, loc)
Definition: interface.py:203


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