-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathanomalyExample.py
46 lines (34 loc) · 1.21 KB
/
anomalyExample.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
import json
from pprint import pprint as pp
from datetime import datetime, timedelta
FNAME = './littleTest/ratDigestPostAttack.json'
messages = json.load(open(FNAME))
# pp(messages)
m3 = messages[3]
# print m3
# print m3.keys()
# print m3['timestamp']
meterList = [x for x in messages if x.get('identifier','') == 'MS-GetLatestReadings']
pp(meterList)
voltage1 = [x['measured_voltage_1'] for x in messages if 'measured_voltage_1' in x]
voltage2 = [x['measured_voltage_2'] for x in messages if 'measured_voltage_2' in x]
allVolts = voltage1 + voltage2
# print allVolts
compVoltages = [complex(x.replace('d','j')) for x in allVolts]
# print compVoltages
voltageMags = [abs(x) for x in compVoltages]
meanVoltage = sum(voltageMags)/len(voltageMags)
# print meanVoltage
def voltMag(m):
if 'measured_voltage_1' in m:
return abs(complex(m['measured_voltage_1'].replace('d','j')))
elif 'measured_voltage_2' in m:
return abs(complex(m['measured_voltage_2'].replace('d','j')))
else:
return None
overVolts = [m for m in messages if voltMag(m) > meanVoltage]
pp(overVolts)
def parseDate(d):
return datetime.strptime(d[0:-4], '%Y-%m-%d %H:%M:%S')
m4 = messages[4]
# print parseDate(m3['timestamp']) - parseDate(m4['timestamp'])