jerin commited on
Commit
0c3f62c
1 Parent(s): d0f2767

update rtu anomalizer2

Browse files
src/rtu/RTUAnomalizer1.py CHANGED
@@ -40,9 +40,12 @@ class RTUAnomalizer1:
40
  prediction_model_path, clustering_model_paths, pca_model_paths
41
  )
42
 
43
- self.actual_list, self.pred_list, self.resid_list, self.resid_pca_list = (
44
  self.initialize_lists()
45
  )
 
 
 
46
 
47
  def initialize_lists(self, size=30):
48
  """
@@ -56,11 +59,13 @@ class RTUAnomalizer1:
56
  """
57
  initial_values = [[0] * self.num_outputs] * size
58
  initial_values1 = [[0] * 4] * size
 
59
  return (
60
  initial_values.copy(),
61
  initial_values.copy(),
62
  initial_values.copy(),
63
  initial_values1.copy(),
 
64
  )
65
 
66
  def load_models(
@@ -192,11 +197,24 @@ class RTUAnomalizer1:
192
  axis=1,
193
  )
194
  )
 
 
195
  resid_pcas = np.array(resid_pcas).flatten().tolist()
196
  self.resid_pca_list.pop(0)
197
  self.resid_pca_list.append(resid_pcas)
198
 
199
  return np.array(dist)
 
 
 
 
 
 
 
 
 
 
 
200
 
201
  def pipeline(self, df_new, df_trans, scaler):
202
  """
@@ -217,12 +235,17 @@ class RTUAnomalizer1:
217
  actual, pred = self.inverse_transform(scaler, pred, df_trans)
218
  actual_list, pred_list, resid_list = self.update_lists(actual, pred, resid)
219
  dist = self.calculate_distances(resid)
220
- over_threshold = dist > 0.5
221
  return (
222
  actual_list,
223
  pred_list,
224
  resid_list,
225
  self.resid_pca_list,
226
  dist,
227
- over_threshold,
 
 
 
228
  )
 
 
 
40
  prediction_model_path, clustering_model_paths, pca_model_paths
41
  )
42
 
43
+ self.actual_list, self.pred_list, self.resid_list, self.resid_pca_list, self.distance_list = (
44
  self.initialize_lists()
45
  )
46
+
47
+ self.fault_1 = None
48
+ self.fault_2 = None
49
 
50
  def initialize_lists(self, size=30):
51
  """
 
59
  """
60
  initial_values = [[0] * self.num_outputs] * size
61
  initial_values1 = [[0] * 4] * size
62
+ initial_values2 = [[0] * 2] * size*2
63
  return (
64
  initial_values.copy(),
65
  initial_values.copy(),
66
  initial_values.copy(),
67
  initial_values1.copy(),
68
+ initial_values2.copy(),
69
  )
70
 
71
  def load_models(
 
197
  axis=1,
198
  )
199
  )
200
+ self.distance_list.pop(0)
201
+ self.distance_list.append(dist)
202
  resid_pcas = np.array(resid_pcas).flatten().tolist()
203
  self.resid_pca_list.pop(0)
204
  self.resid_pca_list.append(resid_pcas)
205
 
206
  return np.array(dist)
207
+ def fault_windowing(self):
208
+ rtu_1_dist = np.array(self.distance_list).T[0]>1 #rtu_1_threshold
209
+ if sum(rtu_1_dist)>80*60: # 80% of the 60 min window
210
+ self.fault_1 = 1
211
+ else:
212
+ self.fault_1 = 0
213
+ rtu_2_dist = np.array(self.distance_list).T[1]>2.5 #rtu_2_threshold
214
+ if sum(rtu_2_dist)>80*60: # 80% of the 60 min window
215
+ self.fault_2 = 1
216
+ else:
217
+ self.fault_2 = 0
218
 
219
  def pipeline(self, df_new, df_trans, scaler):
220
  """
 
235
  actual, pred = self.inverse_transform(scaler, pred, df_trans)
236
  actual_list, pred_list, resid_list = self.update_lists(actual, pred, resid)
237
  dist = self.calculate_distances(resid)
238
+
239
  return (
240
  actual_list,
241
  pred_list,
242
  resid_list,
243
  self.resid_pca_list,
244
  dist,
245
+ np.array(self.distance_list[30:]).T[0]>1, #rtu_1_threshold
246
+ np.array(self.distance_list[30:]).T[1]>2.5, #rtu_2_threshold
247
+ self.fault_1,
248
+ self.fault_2
249
  )
250
+
251
+
src/rtu/RTUAnomalizer2.py CHANGED
@@ -3,15 +3,11 @@ from tensorflow.keras.models import load_model
3
  import joblib
4
 
5
 
6
- class RTUAnomalizer2:
7
  """
8
  Class for performing anomaly detection on RTU (Roof Top Unit) data.
9
  """
10
 
11
- model = None
12
- kmeans_models = []
13
- pca_models = []
14
-
15
  def __init__(
16
  self,
17
  prediction_model_path=None,
@@ -29,12 +25,27 @@ class RTUAnomalizer2:
29
  num_inputs (int): Number of input features.
30
  num_outputs (int): Number of output features.
31
  """
 
 
 
 
32
  self.num_inputs = num_inputs
33
  self.num_outputs = num_outputs
34
- if prediction_model_path is not None and clustering_model_paths is not None and pca_model_paths is not None:
35
- self.load_models(prediction_model_path, clustering_model_paths, pca_model_paths)
36
-
37
- self.actual_list, self.pred_list, self.resid_list = self.initialize_lists()
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  def initialize_lists(self, size=30):
40
  """
@@ -46,10 +57,20 @@ class RTUAnomalizer2:
46
  Returns:
47
  tuple: A tuple containing three lists initialized with zeros.
48
  """
49
- initial_values = [[0]*self.num_outputs] * size
50
- return initial_values.copy(), initial_values.copy(), initial_values.copy()
 
 
 
 
 
 
 
 
51
 
52
- def load_models(self, prediction_model_path, clustering_model_paths, pca_model_paths):
 
 
53
  """
54
  Load the prediction and clustering models.
55
 
@@ -61,7 +82,7 @@ class RTUAnomalizer2:
61
 
62
  for path in clustering_model_paths:
63
  self.kmeans_models.append(joblib.load(path))
64
-
65
  for path in pca_model_paths:
66
  self.pca_models.append(joblib.load(path))
67
 
@@ -75,7 +96,7 @@ class RTUAnomalizer2:
75
  Returns:
76
  array: Predicted values.
77
  """
78
- return self.model.predict(df_new,verbose=0)
79
 
80
  def calculate_residuals(self, df_trans, pred):
81
  """
@@ -161,16 +182,39 @@ class RTUAnomalizer2:
161
  array: Array of distances.
162
  """
163
  dist = []
 
164
  for i, model in enumerate(self.kmeans_models):
 
 
 
 
 
 
165
  dist.append(
166
  np.linalg.norm(
167
- self.pca_models[i].transform(resid[:, (i * 7) + 1 : (i * 7) + 8]) - model.cluster_centers_[0],
168
  ord=2,
169
  axis=1,
170
  )
171
  )
 
 
 
 
 
172
 
173
  return np.array(dist)
 
 
 
 
 
 
 
 
 
 
 
174
 
175
  def pipeline(self, df_new, df_trans, scaler):
176
  """
@@ -184,12 +228,24 @@ class RTUAnomalizer2:
184
  Returns:
185
  tuple: A tuple containing the lists of actual, predicted, and residual values, and the distances.
186
  """
187
-
188
  pred = self.predict(df_new)
189
  actual, resid = self.calculate_residuals(df_trans, pred)
190
  pred = self.resize_prediction(pred, df_trans)
191
  actual, pred = self.inverse_transform(scaler, pred, df_trans)
192
- actual_list, pred_list, resid_list = self.update_lists(
193
- actual, pred, resid)
194
  dist = self.calculate_distances(resid)
195
- return actual_list, pred_list, resid_list, dist
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import joblib
4
 
5
 
6
+ class RTUAnomalizer1:
7
  """
8
  Class for performing anomaly detection on RTU (Roof Top Unit) data.
9
  """
10
 
 
 
 
 
11
  def __init__(
12
  self,
13
  prediction_model_path=None,
 
25
  num_inputs (int): Number of input features.
26
  num_outputs (int): Number of output features.
27
  """
28
+ self.model = None
29
+ self.kmeans_models = []
30
+ self.pca_models = []
31
+
32
  self.num_inputs = num_inputs
33
  self.num_outputs = num_outputs
34
+ if (
35
+ prediction_model_path is not None
36
+ and clustering_model_paths is not None
37
+ and pca_model_paths is not None
38
+ ):
39
+ self.load_models(
40
+ prediction_model_path, clustering_model_paths, pca_model_paths
41
+ )
42
+
43
+ self.actual_list, self.pred_list, self.resid_list, self.resid_pca_list, self.distance_list = (
44
+ self.initialize_lists()
45
+ )
46
+
47
+ self.fault_1 = None
48
+ self.fault_2 = None
49
 
50
  def initialize_lists(self, size=30):
51
  """
 
57
  Returns:
58
  tuple: A tuple containing three lists initialized with zeros.
59
  """
60
+ initial_values = [[0] * self.num_outputs] * size
61
+ initial_values1 = [[0] * 4] * size
62
+ initial_values2 = [[0] * 2] * size*2
63
+ return (
64
+ initial_values.copy(),
65
+ initial_values.copy(),
66
+ initial_values.copy(),
67
+ initial_values1.copy(),
68
+ initial_values2.copy(),
69
+ )
70
 
71
+ def load_models(
72
+ self, prediction_model_path, clustering_model_paths, pca_model_paths
73
+ ):
74
  """
75
  Load the prediction and clustering models.
76
 
 
82
 
83
  for path in clustering_model_paths:
84
  self.kmeans_models.append(joblib.load(path))
85
+
86
  for path in pca_model_paths:
87
  self.pca_models.append(joblib.load(path))
88
 
 
96
  Returns:
97
  array: Predicted values.
98
  """
99
+ return self.model.predict(df_new, verbose=0)
100
 
101
  def calculate_residuals(self, df_trans, pred):
102
  """
 
182
  array: Array of distances.
183
  """
184
  dist = []
185
+ resid_pcas = []
186
  for i, model in enumerate(self.kmeans_models):
187
+ resid_pca = self.pca_models[i].transform(
188
+ resid[:, (i * 7) + 1 : (i * 7) + 8]
189
+ )
190
+
191
+ resid_pcas = resid_pcas + resid_pca.tolist()
192
+
193
  dist.append(
194
  np.linalg.norm(
195
+ resid_pca - model.cluster_centers_[0],
196
  ord=2,
197
  axis=1,
198
  )
199
  )
200
+ self.distance_list.pop(0)
201
+ self.distance_list.append(dist)
202
+ resid_pcas = np.array(resid_pcas).flatten().tolist()
203
+ self.resid_pca_list.pop(0)
204
+ self.resid_pca_list.append(resid_pcas)
205
 
206
  return np.array(dist)
207
+ def fault_windowing(self):
208
+ rtu_1_dist = np.array(self.distance_list).T[0]>1.5 #rtu_3_threshold
209
+ if sum(rtu_1_dist)>80*60: # 80% of the 60 min window
210
+ self.fault_1 = 1
211
+ else:
212
+ self.fault_1 = 0
213
+ rtu_2_dist = np.array(self.distance_list).T[1]>1.5 #rtu_4_threshold
214
+ if sum(rtu_2_dist)>80*60: # 80% of the 60 min window
215
+ self.fault_2 = 1
216
+ else:
217
+ self.fault_2 = 0
218
 
219
  def pipeline(self, df_new, df_trans, scaler):
220
  """
 
228
  Returns:
229
  tuple: A tuple containing the lists of actual, predicted, and residual values, and the distances.
230
  """
231
+
232
  pred = self.predict(df_new)
233
  actual, resid = self.calculate_residuals(df_trans, pred)
234
  pred = self.resize_prediction(pred, df_trans)
235
  actual, pred = self.inverse_transform(scaler, pred, df_trans)
236
+ actual_list, pred_list, resid_list = self.update_lists(actual, pred, resid)
 
237
  dist = self.calculate_distances(resid)
238
+
239
+ return (
240
+ actual_list,
241
+ pred_list,
242
+ resid_list,
243
+ self.resid_pca_list,
244
+ dist,
245
+ np.array(self.distance_list[30:]).T[0]>1.5, #rtu_3_threshold
246
+ np.array(self.distance_list[30:]).T[1]>1.5, #rtu_4_threshold
247
+ self.fault_1,
248
+ self.fault_2
249
+ )
250
+
251
+