akshayballal commited on
Commit
4a389dc
1 Parent(s): cea29ef

main works

Browse files
Files changed (4) hide show
  1. mqttpublisher.ipynb +12 -225
  2. src/main.py +13 -16
  3. src/rtu/RTUAnomalizer.py +39 -20
  4. src/rtu/RTUPipeline.py +21 -18
mqttpublisher.ipynb CHANGED
@@ -2,14 +2,14 @@
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
- "execution_count": 10,
6
  "metadata": {},
7
  "outputs": [
8
  {
9
  "name": "stderr",
10
  "output_type": "stream",
11
  "text": [
12
- "C:\\Users\\jerin\\AppData\\Local\\Temp\\ipykernel_4616\\2478473330.py:13: DeprecationWarning: Callback API version 1 is deprecated, update to latest version\n",
13
  " client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, clientId)\n"
14
  ]
15
  },
@@ -171,221 +171,8 @@
171
  "published!\n",
172
  "published!\n",
173
  "published!\n",
174
- "published!\n",
175
- "published!\n",
176
- "published!\n",
177
- "published!\n",
178
- "published!\n",
179
- "published!\n",
180
- "published!\n",
181
- "published!\n",
182
- "published!\n",
183
- "published!\n",
184
- "published!\n",
185
- "published!\n",
186
- "published!\n",
187
- "published!\n",
188
- "published!\n",
189
- "published!\n",
190
- "published!\n",
191
- "published!\n",
192
- "published!\n",
193
- "published!\n",
194
- "published!\n",
195
- "published!\n",
196
- "published!\n",
197
- "published!\n",
198
- "published!\n",
199
- "published!\n",
200
- "published!\n",
201
- "published!\n",
202
- "published!\n",
203
- "published!\n",
204
- "published!\n",
205
- "published!\n",
206
- "published!\n",
207
- "published!\n",
208
- "published!\n",
209
- "published!\n",
210
- "published!\n",
211
- "published!\n",
212
- "published!\n",
213
- "published!\n",
214
- "published!\n",
215
- "published!\n",
216
- "published!\n",
217
- "published!\n",
218
- "published!\n",
219
- "published!\n",
220
- "published!\n",
221
- "published!\n",
222
- "published!\n",
223
- "published!\n",
224
- "published!\n",
225
- "published!\n",
226
- "published!\n",
227
- "published!\n",
228
- "published!\n",
229
- "published!\n",
230
- "published!\n",
231
- "published!\n",
232
- "published!\n",
233
- "published!\n",
234
- "published!\n",
235
- "published!\n",
236
- "published!\n",
237
- "published!\n",
238
- "published!\n",
239
- "published!\n",
240
- "published!\n",
241
- "published!\n",
242
- "published!\n",
243
- "published!\n",
244
- "published!\n",
245
- "published!\n",
246
- "published!\n",
247
- "published!\n",
248
- "published!\n",
249
- "published!\n",
250
- "published!\n",
251
- "published!\n",
252
- "published!\n",
253
- "published!\n",
254
- "published!\n",
255
- "published!\n",
256
- "published!\n",
257
- "published!\n",
258
- "published!\n",
259
- "published!\n",
260
- "published!\n",
261
- "published!\n",
262
- "published!\n",
263
- "published!\n",
264
- "published!\n",
265
- "published!\n",
266
- "published!\n",
267
- "published!\n",
268
- "published!\n",
269
- "published!\n",
270
- "published!\n",
271
- "published!\n",
272
- "published!\n",
273
- "published!\n",
274
- "published!\n",
275
- "published!\n",
276
- "published!\n",
277
- "published!\n",
278
- "published!\n",
279
- "published!\n",
280
- "published!\n",
281
- "published!\n",
282
- "published!\n",
283
- "published!\n",
284
- "published!\n",
285
- "published!\n",
286
- "published!\n",
287
- "published!\n",
288
- "published!\n",
289
- "published!\n",
290
- "published!\n",
291
- "published!\n",
292
- "published!\n",
293
- "published!\n",
294
- "published!\n",
295
- "published!\n",
296
- "published!\n",
297
- "published!\n",
298
- "published!\n",
299
- "published!\n",
300
- "published!\n",
301
- "published!\n",
302
- "published!\n",
303
- "published!\n",
304
- "published!\n",
305
- "published!\n",
306
- "published!\n",
307
- "published!\n",
308
- "published!\n",
309
- "published!\n",
310
- "published!\n",
311
- "published!\n",
312
- "published!\n",
313
- "published!\n",
314
- "published!\n",
315
- "published!\n",
316
- "published!\n",
317
- "published!\n",
318
- "published!\n",
319
- "published!\n",
320
- "published!\n",
321
- "published!\n",
322
- "published!\n",
323
- "published!\n",
324
- "published!\n",
325
- "published!\n",
326
- "published!\n",
327
- "published!\n",
328
- "published!\n",
329
- "published!\n",
330
- "published!\n",
331
- "published!\n",
332
- "published!\n",
333
- "published!\n",
334
- "published!\n",
335
- "published!\n",
336
- "published!\n",
337
- "published!\n",
338
- "published!\n",
339
- "published!\n",
340
- "published!\n",
341
- "published!\n",
342
- "published!\n",
343
- "published!\n",
344
- "published!\n",
345
- "published!\n",
346
- "published!\n",
347
- "published!\n",
348
- "published!\n",
349
- "published!\n",
350
- "published!\n",
351
- "published!\n",
352
- "published!\n",
353
- "published!\n",
354
- "published!\n",
355
- "published!\n",
356
- "published!\n",
357
- "published!\n",
358
- "published!\n",
359
- "published!\n",
360
- "published!\n",
361
- "published!\n",
362
- "published!\n",
363
- "published!\n",
364
- "published!\n",
365
- "published!\n",
366
- "published!\n",
367
- "published!\n",
368
- "published!\n",
369
- "published!\n",
370
- "published!\n",
371
- "published!\n",
372
- "published!\n",
373
- "published!\n",
374
- "published!\n",
375
  "published!\n"
376
  ]
377
- },
378
- {
379
- "ename": "KeyboardInterrupt",
380
- "evalue": "",
381
- "output_type": "error",
382
- "traceback": [
383
- "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
384
- "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
385
- "Cell \u001b[1;32mIn[10], line 94\u001b[0m\n\u001b[0;32m 90\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(\u001b[38;5;241m2\u001b[39m)\n\u001b[0;32m 93\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m---> 94\u001b[0m \u001b[43mpublish_sensor_data\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 95\u001b[0m \u001b[38;5;66;03m# time.sleep(0.1)\u001b[39;00m\n\u001b[0;32m 96\u001b[0m client\u001b[38;5;241m.\u001b[39mdisconnect()\n",
386
- "Cell \u001b[1;32mIn[10], line 90\u001b[0m, in \u001b[0;36mpublish_sensor_data\u001b[1;34m()\u001b[0m\n\u001b[0;32m 55\u001b[0m client\u001b[38;5;241m.\u001b[39mpublish(topic, payload\u001b[38;5;241m=\u001b[39mjson\u001b[38;5;241m.\u001b[39mdumps({\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhp_hws_temp\u001b[39m\u001b[38;5;124m'\u001b[39m:hp_hws_temp,\n\u001b[0;32m 56\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrtu_003_sa_temp\u001b[39m\u001b[38;5;124m'\u001b[39m:rtu_003_sa_temp,\n\u001b[0;32m 57\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrtu_003_oadmpr_pct\u001b[39m\u001b[38;5;124m'\u001b[39m: rtu_003_oadmpr_pct,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 87\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrelative_humidity_set_1\u001b[39m\u001b[38;5;124m'\u001b[39m:relative_humidity_set_1,\n\u001b[0;32m 88\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124msolar_radiation_set_1\u001b[39m\u001b[38;5;124m'\u001b[39m:solar_radiation_set_1}))\n\u001b[0;32m 89\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpublished!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m---> 90\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(\u001b[38;5;241m2\u001b[39m)\n",
387
- "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
388
- ]
389
  }
390
  ],
391
  "source": [
@@ -399,7 +186,7 @@
399
  "broker_address = \"localhost\"\n",
400
  "broker_port = 1883\n",
401
  "\n",
402
- "df = pd.read_csv(\"sample_data_smooth_01.csv\")\n",
403
  "\n",
404
  "client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, clientId)\n",
405
  "client.connect(broker_address, broker_port)\n",
@@ -436,10 +223,10 @@
436
  " rtu_002_ma_temp = row['rtu_002_ma_temp']\n",
437
  " rtu_002_sf_vfd_spd_fbk_tn = row['rtu_002_sf_vfd_spd_fbk_tn']\n",
438
  " rtu_002_rf_vfd_spd_fbk_tn = row['rtu_002_rf_vfd_spd_fbk_tn']\n",
439
- " rtu_004_sat_sp_tn = row['rtu_004_sat_sp_tn']\n",
440
- " rtu_003_sat_sp_tn = row['rtu_003_sat_sp_tn']\n",
441
- " rtu_001_sat_sp_tn = row['rtu_001_sat_sp_tn']\n",
442
- " rtu_002_sat_sp_tn = row['rtu_002_sat_sp_tn']\n",
443
  " air_temp_set_1 = row['air_temp_set_1']\n",
444
  " air_temp_set_2 = row['air_temp_set_2']\n",
445
  " dew_point_temperature_set_1d = row['dew_point_temperature_set_1d']\n",
@@ -476,17 +263,17 @@
476
  " 'rtu_002_ma_temp':rtu_002_ma_temp,\n",
477
  " 'rtu_002_sf_vfd_spd_fbk_tn':rtu_002_sf_vfd_spd_fbk_tn,\n",
478
  " 'rtu_002_rf_vfd_spd_fbk_tn':rtu_002_rf_vfd_spd_fbk_tn,\n",
479
- " 'rtu_004_sat_sp_tn':rtu_004_sat_sp_tn,\n",
480
- " 'rtu_003_sat_sp_tn' :rtu_003_sat_sp_tn,\n",
481
- " 'rtu_001_sat_sp_tn':rtu_001_sat_sp_tn,\n",
482
- " 'rtu_002_sat_sp_tn':rtu_002_sat_sp_tn,\n",
483
  " 'air_temp_set_1':air_temp_set_1,\n",
484
  " 'air_temp_set_2':air_temp_set_2,\n",
485
  " 'dew_point_temperature_set_1d':dew_point_temperature_set_1d,\n",
486
  " 'relative_humidity_set_1':relative_humidity_set_1,\n",
487
  " 'solar_radiation_set_1':solar_radiation_set_1}))\n",
488
  " print(\"published!\")\n",
489
- " time.sleep(2)\n",
490
  "\n",
491
  "\n",
492
  "while True:\n",
 
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
+ "execution_count": 4,
6
  "metadata": {},
7
  "outputs": [
8
  {
9
  "name": "stderr",
10
  "output_type": "stream",
11
  "text": [
12
+ "C:\\Users\\arbal\\AppData\\Local\\Temp\\ipykernel_20872\\3033510885.py:13: DeprecationWarning: Callback API version 1 is deprecated, update to latest version\n",
13
  " client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, clientId)\n"
14
  ]
15
  },
 
171
  "published!\n",
172
  "published!\n",
173
  "published!\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  "published!\n"
175
  ]
 
 
 
 
 
 
 
 
 
 
 
 
176
  }
177
  ],
178
  "source": [
 
186
  "broker_address = \"localhost\"\n",
187
  "broker_port = 1883\n",
188
  "\n",
189
+ "df = pd.read_csv(\"data/sample_data_smooth_01.csv\")\n",
190
  "\n",
191
  "client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, clientId)\n",
192
  "client.connect(broker_address, broker_port)\n",
 
223
  " rtu_002_ma_temp = row['rtu_002_ma_temp']\n",
224
  " rtu_002_sf_vfd_spd_fbk_tn = row['rtu_002_sf_vfd_spd_fbk_tn']\n",
225
  " rtu_002_rf_vfd_spd_fbk_tn = row['rtu_002_rf_vfd_spd_fbk_tn']\n",
226
+ " # rtu_004_sat_sp_tn = row['rtu_004_sat_sp_tn']\n",
227
+ " # rtu_003_sat_sp_tn = row['rtu_003_sat_sp_tn']\n",
228
+ " # rtu_001_sat_sp_tn = row['rtu_001_sat_sp_tn']\n",
229
+ " # rtu_002_sat_sp_tn = row['rtu_002_sat_sp_tn']\n",
230
  " air_temp_set_1 = row['air_temp_set_1']\n",
231
  " air_temp_set_2 = row['air_temp_set_2']\n",
232
  " dew_point_temperature_set_1d = row['dew_point_temperature_set_1d']\n",
 
263
  " 'rtu_002_ma_temp':rtu_002_ma_temp,\n",
264
  " 'rtu_002_sf_vfd_spd_fbk_tn':rtu_002_sf_vfd_spd_fbk_tn,\n",
265
  " 'rtu_002_rf_vfd_spd_fbk_tn':rtu_002_rf_vfd_spd_fbk_tn,\n",
266
+ " # 'rtu_004_sat_sp_tn':rtu_004_sat_sp_tn,\n",
267
+ " # 'rtu_003_sat_sp_tn' :rtu_003_sat_sp_tn,\n",
268
+ " # 'rtu_001_sat_sp_tn':rtu_001_sat_sp_tn,\n",
269
+ " # 'rtu_002_sat_sp_tn':rtu_002_sat_sp_tn,\n",
270
  " 'air_temp_set_1':air_temp_set_1,\n",
271
  " 'air_temp_set_2':air_temp_set_2,\n",
272
  " 'dew_point_temperature_set_1d':dew_point_temperature_set_1d,\n",
273
  " 'relative_humidity_set_1':relative_humidity_set_1,\n",
274
  " 'solar_radiation_set_1':solar_radiation_set_1}))\n",
275
  " print(\"published!\")\n",
276
+ " time.sleep(0.2)\n",
277
  "\n",
278
  "\n",
279
  "while True:\n",
src/main.py CHANGED
@@ -4,28 +4,27 @@ from rtu.RTUPipeline import RTUPipeline
4
  import paho.mqtt.client as mqtt
5
 
6
 
7
-
8
  def main():
9
  rtu_data_pipeline = RTUPipeline(scaler_path="src/rtu/models/scaler_1.pkl")
10
  print(rtu_data_pipeline.scaler)
11
  rtu_anomalizer = RTUAnomalizer(
12
  prediction_model_path="src/rtu/models/lstm_4rtu_smooth_02.keras",
13
  clustering_model_paths=[
14
- "rtu/models/kmeans_model1.pkl",
15
- "rtu/models/kmeans_model2.pkl",
16
- "rtu/models/kmeans_model3.pkl",
17
- "rtu/models/kmeans_model4.pkl",
18
  ],
19
  num_inputs=rtu_data_pipeline.num_inputs,
20
- num_outputs=rtu_data_pipeline.num_outputs
21
  )
22
-
23
  def on_message(client, userdata, message):
24
- print(json.loads(message.payload.decode()))
25
  df_new, df_trans = rtu_data_pipeline.fit(message)
26
- out = rtu_anomalizer.predict(df_new, df_trans, rtu_data_pipeline.scaler)
27
- print(out)
28
-
29
  broker_address = "localhost"
30
  broker_port = 1883
31
  topic = "sensor_data"
@@ -34,9 +33,7 @@ def main():
34
  client.connect(broker_address, broker_port)
35
  client.subscribe(topic)
36
  client.loop_forever()
37
-
38
-
39
- if __name__=='__main__':
40
  main()
41
-
42
-
 
4
  import paho.mqtt.client as mqtt
5
 
6
 
 
7
  def main():
8
  rtu_data_pipeline = RTUPipeline(scaler_path="src/rtu/models/scaler_1.pkl")
9
  print(rtu_data_pipeline.scaler)
10
  rtu_anomalizer = RTUAnomalizer(
11
  prediction_model_path="src/rtu/models/lstm_4rtu_smooth_02.keras",
12
  clustering_model_paths=[
13
+ "src/rtu/models/kmeans_model1.pkl",
14
+ "src/rtu/models/kmeans_model2.pkl",
15
+ "src/rtu/models/kmeans_model3.pkl",
16
+ "src/rtu/models/kmeans_model4.pkl",
17
  ],
18
  num_inputs=rtu_data_pipeline.num_inputs,
19
+ num_outputs=rtu_data_pipeline.num_outputs,
20
  )
21
+
22
  def on_message(client, userdata, message):
23
+ # print(json.loads(message.payload.decode()))
24
  df_new, df_trans = rtu_data_pipeline.fit(message)
25
+ if not df_new is None and not df_trans is None:
26
+ out = rtu_anomalizer.pipeline(df_new, df_trans, rtu_data_pipeline.scaler)
27
+
28
  broker_address = "localhost"
29
  broker_port = 1883
30
  topic = "sensor_data"
 
33
  client.connect(broker_address, broker_port)
34
  client.subscribe(topic)
35
  client.loop_forever()
36
+
37
+
38
+ if __name__ == "__main__":
39
  main()
 
 
src/rtu/RTUAnomalizer.py CHANGED
@@ -2,59 +2,76 @@ import numpy as np
2
  from tensorflow.keras.models import load_model
3
  import joblib
4
 
 
5
  class RTUAnomalizer:
6
  model = None
7
  kmeans_models = []
8
 
9
- def __init__(self, prediction_model_path = None, clustering_model_paths= None, num_inputs = None, num_outputs = None):
10
-
 
 
 
 
 
 
11
  self.num_inputs = num_inputs
12
  self.num_outputs = num_outputs
13
  if not prediction_model_path is None and not clustering_model_paths is None:
14
  self.load_models(prediction_model_path, clustering_model_paths)
15
 
16
- def initialize_lists(size=30):
17
  initial_values = [0] * size
18
  return initial_values.copy(), initial_values.copy(), initial_values.copy()
19
 
20
  def load_models(self, prediction_model_path, clustering_model_paths):
21
  self.model = load_model(prediction_model_path)
22
-
23
  for path in clustering_model_paths:
24
  self.kmeans_models.append(joblib.load(path))
25
-
26
  def predict(self, df_new):
27
  return self.model.predict(df_new)
28
 
29
- def calculate_residuals(self,df_trans, pred):
30
- actual = df_trans[30,:self.num_outputs+1]
31
  resid = actual - pred
32
  return actual, resid
33
 
34
- def resize_prediction(self,pred, df_trans):
35
- pred.resize((pred.shape[0], pred.shape[1] + len(df_trans[30,self.num_outputs+1:])))
36
- pred[:, -len(df_trans[30,self.num_outputs+1:]):] = df_trans[30,self.num_outputs+1:]
 
 
 
 
37
  return pred
38
 
39
- def inverse_transform(scaler, pred, df_trans):
40
  pred = scaler.inverse_transform(np.array(pred))
41
- actual = scaler.inverse_transform(np.array([df_trans[30,:]]))
42
  return actual, pred
43
 
44
- def update_lists(actual_list, pred_list, resid_list, actual, pred, resid):
45
  actual_list.pop(0)
46
  pred_list.pop(0)
47
  resid_list.pop(0)
48
- actual_list.append(actual[0,1])
49
- pred_list.append(pred[0,1])
50
- resid_list.append(resid[0,1])
51
  return actual_list, pred_list, resid_list
52
 
53
- def calculate_distances(self,resid):
54
  dist = []
55
  for i, model in enumerate(self.kmeans_models):
56
- dist.append(np.linalg.norm(resid[:,(i*7)+1:(i*7)+8]-model.cluster_centers_[0], ord=2, axis=1))
57
-
 
 
 
 
 
 
58
  return np.array(dist)
59
 
60
  def pipeline(self, df_new, df_trans, scaler):
@@ -63,6 +80,8 @@ class RTUAnomalizer:
63
  actual, resid = self.calculate_residuals(df_trans, pred)
64
  pred = self.resize_prediction(pred, df_trans)
65
  actual, pred = self.inverse_transform(scaler, pred, df_trans)
66
- actual_list, pred_list, resid_list = self.update_lists(actual_list, pred_list, resid_list, actual, pred, resid)
 
 
67
  dist = self.calculate_distances(resid)
68
  return actual_list, pred_list, resid_list, dist
 
2
  from tensorflow.keras.models import load_model
3
  import joblib
4
 
5
+
6
  class RTUAnomalizer:
7
  model = None
8
  kmeans_models = []
9
 
10
+ def __init__(
11
+ self,
12
+ prediction_model_path=None,
13
+ clustering_model_paths=None,
14
+ num_inputs=None,
15
+ num_outputs=None,
16
+ ):
17
+
18
  self.num_inputs = num_inputs
19
  self.num_outputs = num_outputs
20
  if not prediction_model_path is None and not clustering_model_paths is None:
21
  self.load_models(prediction_model_path, clustering_model_paths)
22
 
23
+ def initialize_lists(self, size=30):
24
  initial_values = [0] * size
25
  return initial_values.copy(), initial_values.copy(), initial_values.copy()
26
 
27
  def load_models(self, prediction_model_path, clustering_model_paths):
28
  self.model = load_model(prediction_model_path)
29
+
30
  for path in clustering_model_paths:
31
  self.kmeans_models.append(joblib.load(path))
32
+
33
  def predict(self, df_new):
34
  return self.model.predict(df_new)
35
 
36
+ def calculate_residuals(self, df_trans, pred):
37
+ actual = df_trans[30, : self.num_outputs]
38
  resid = actual - pred
39
  return actual, resid
40
 
41
+ def resize_prediction(self, pred, df_trans):
42
+ pred = np.resize(
43
+ pred, (pred.shape[0], pred.shape[1] + len(df_trans[30, self.num_outputs :]))
44
+ )
45
+ pred[:, -len(df_trans[30, self.num_outputs :]) :] = df_trans[
46
+ 30, self.num_outputs :
47
+ ]
48
  return pred
49
 
50
+ def inverse_transform(self, scaler, pred, df_trans):
51
  pred = scaler.inverse_transform(np.array(pred))
52
+ actual = scaler.inverse_transform(np.array([df_trans[30, :]]))
53
  return actual, pred
54
 
55
+ def update_lists(self, actual_list, pred_list, resid_list, actual, pred, resid):
56
  actual_list.pop(0)
57
  pred_list.pop(0)
58
  resid_list.pop(0)
59
+ actual_list.append(actual[0, 1])
60
+ pred_list.append(pred[0, 1])
61
+ resid_list.append(resid[0, 1])
62
  return actual_list, pred_list, resid_list
63
 
64
+ def calculate_distances(self, resid):
65
  dist = []
66
  for i, model in enumerate(self.kmeans_models):
67
+ dist.append(
68
+ np.linalg.norm(
69
+ resid[:, (i * 7) + 1 : (i * 7) + 8] - model.cluster_centers_[0],
70
+ ord=2,
71
+ axis=1,
72
+ )
73
+ )
74
+
75
  return np.array(dist)
76
 
77
  def pipeline(self, df_new, df_trans, scaler):
 
80
  actual, resid = self.calculate_residuals(df_trans, pred)
81
  pred = self.resize_prediction(pred, df_trans)
82
  actual, pred = self.inverse_transform(scaler, pred, df_trans)
83
+ actual_list, pred_list, resid_list = self.update_lists(
84
+ actual_list, pred_list, resid_list, actual, pred, resid
85
+ )
86
  dist = self.calculate_distances(resid)
87
  return actual_list, pred_list, resid_list, dist
src/rtu/RTUPipeline.py CHANGED
@@ -50,8 +50,9 @@ class RTUPipeline:
50
  # "relative_humidity_set_1",
51
  # "solar_radiation_set_1",
52
  ]
53
-
54
- self.input_col_names = ["air_temp_set_1",
 
55
  "air_temp_set_2",
56
  "dew_point_temperature_set_1d",
57
  "relative_humidity_set_1",
@@ -59,17 +60,17 @@ class RTUPipeline:
59
  ]
60
  self.num_inputs = len(self.input_col_names)
61
  self.num_outputs = len(self.output_col_names)
62
- self.column_names = self.output_col_names+self.input_col_names
63
 
64
  if scaler_path:
65
  self.scaler = self.get_scaler(scaler_path)
66
- self.df = pd.DataFrame(columns = self.column_names)
67
 
68
  def get_scaler(self, scaler_path):
69
  return joblib.load(scaler_path)
70
 
71
  def get_window(self, df):
72
- len_df = np.len(df)
73
  if len_df > 30:
74
  return df[len_df - 31 : len_df].astype("float32")
75
  else:
@@ -78,12 +79,12 @@ class RTUPipeline:
78
  def transform_window(self, df_window):
79
  return self.scaler.transform(df_window)
80
 
81
- def prepare_input(self,df_trans):
82
  return df_trans[:30, :].reshape((1, 30, len(self.column_names)))
83
-
84
  def extract_data_from_message(self, message):
85
  payload = json.loads(message.payload.decode())
86
-
87
  len_df = len(self.df)
88
  # self.df.loc[len_df] = {'hp_hws_temp':payload['hp_hws_temp'],
89
  # 'rtu_003_sa_temp':payload['rtu_003_sa_temp'],
@@ -123,18 +124,20 @@ class RTUPipeline:
123
  # 'dew_point_temperature_set_1d':payload["dew_point_temperature_set_1d"],
124
  # 'relative_humidity_set_1':payload["relative_humidity_set_1"],
125
  # 'solar_radiation_set_1':payload["solar_radiation_set_1"]}
126
-
127
- self.df.loc[len_df] = {}
128
  for col in self.column_names:
129
- self.df.loc[len_df][col] = payload[col]
 
130
  return self.df
131
-
132
-
133
- def fit(self,message):
134
- len_df = np.len(df)
135
  df = self.extract_data_from_message(message)
136
- df_window = self.get_window(df, len_df)
137
  if df_window is not None:
138
- df_trans = self.transform_window(df_window, self.scaler)
139
  df_new = self.prepare_input(df_trans)
140
- return df_new,df_trans
 
 
 
 
50
  # "relative_humidity_set_1",
51
  # "solar_radiation_set_1",
52
  ]
53
+
54
+ self.input_col_names = [
55
+ "air_temp_set_1",
56
  "air_temp_set_2",
57
  "dew_point_temperature_set_1d",
58
  "relative_humidity_set_1",
 
60
  ]
61
  self.num_inputs = len(self.input_col_names)
62
  self.num_outputs = len(self.output_col_names)
63
+ self.column_names = self.output_col_names + self.input_col_names
64
 
65
  if scaler_path:
66
  self.scaler = self.get_scaler(scaler_path)
67
+ self.df = pd.DataFrame(columns=self.column_names)
68
 
69
  def get_scaler(self, scaler_path):
70
  return joblib.load(scaler_path)
71
 
72
  def get_window(self, df):
73
+ len_df = len(df)
74
  if len_df > 30:
75
  return df[len_df - 31 : len_df].astype("float32")
76
  else:
 
79
  def transform_window(self, df_window):
80
  return self.scaler.transform(df_window)
81
 
82
+ def prepare_input(self, df_trans):
83
  return df_trans[:30, :].reshape((1, 30, len(self.column_names)))
84
+
85
  def extract_data_from_message(self, message):
86
  payload = json.loads(message.payload.decode())
87
+
88
  len_df = len(self.df)
89
  # self.df.loc[len_df] = {'hp_hws_temp':payload['hp_hws_temp'],
90
  # 'rtu_003_sa_temp':payload['rtu_003_sa_temp'],
 
124
  # 'dew_point_temperature_set_1d':payload["dew_point_temperature_set_1d"],
125
  # 'relative_humidity_set_1':payload["relative_humidity_set_1"],
126
  # 'solar_radiation_set_1':payload["solar_radiation_set_1"]}
127
+
128
+ k = {}
129
  for col in self.column_names:
130
+ k[col] = payload[col]
131
+ self.df.loc[len_df] = k
132
  return self.df
133
+
134
+ def fit(self, message):
 
 
135
  df = self.extract_data_from_message(message)
136
+ df_window = self.get_window(df)
137
  if df_window is not None:
138
+ df_trans = self.transform_window(df_window)
139
  df_new = self.prepare_input(df_trans)
140
+ else:
141
+ df_new = None
142
+ df_trans = None
143
+ return df_new, df_trans