Fixing bug in skipped packet analysis.
[pingpong.git] / python_ml / validate-detection.py
1 from datetime import datetime
2
3
4 path = "/scratch/July-2018/experimental_result/smarthome/"
5 #path = "/scratch/July-2018/experimental_result/standalone/"
6 # D-Link plug
7 #device = "dlink-plug/self-test"
8 #fileExperiment = "dlink-plug-nov-7-2018.timestamps"
9 #fileDetection = "device-side-detection"
10 #fileDetection = "phone-side-detection"
11 #device = "dlink-plug/timestamps"
12 #fileExperiment = "dlink-plug-smarthome-nov-8-2018.timestamps"
13 #fileDetection = "dlink-plug-smarthome-nov-8-2018.phone.wlan1.detections"
14 #fileDetection = "dlink-plug-smarthome-nov-8-2018.phone.eth0.detections"
15 #fileDetection = "dlink-plug-smarthome-nov-8-2018.device.eth0.detections"
16
17 # TP-Link plug
18 #device = "tplink-plug/self-test"
19 #fileExperiment = "tplink-plug-nov-8-2018.timestamps"
20 #fileDetection = "device-side-detection"
21 #fileDetection = "phone-side-detection"
22 #device = "tplink-plug/timestamps"
23 #fileExperiment = "tplink-plug-smarthome-nov-9-2018.timestamps"
24 #fileDetection = "tplink-plug-smarthome-nov-9-2018.eth0.device.detections"
25 #fileDetection = "tplink-plug-smarthome-nov-9-2018.wlan1.phone.detections"
26
27 # D-Link siren
28 #device = "dlink-siren/self-test"
29 #fileExperiment = "dlink-siren-nov-9-2018.timestamps"
30 #fileDetection = "phone-side-detection"
31 #device = "dlink-siren/timestamps"
32 #fileExperiment = "dlink-siren-smarthome-nov-10-2018.timestamps"
33 #fileDetection = "dlink-siren-smarthome-nov-10-2018.eth0.phone.detections"
34
35 # Kwikset door lock
36 #device = "kwikset-doorlock/self-test"
37 #fileExperiment = "kwikset-doorlock-nov-10-2018.timestamps"
38 #fileDetection = "phone-side-detection"
39 #device = "kwikset-doorlock/timestamps"
40 #fileExperiment = "kwikset-doorlock-smarthome-nov-10-2018.timestamps"
41 #fileDetection = "kwikset-doorlock-smarthome-nov-10-2018.eth0.phone.detections"
42
43 # SmartThings plug
44 #device = "st-plug/self-test"
45 #fileExperiment = "st-plug-nov-13-2018.timestamps"
46 #fileDetection = "phone-side-detection"
47 #device = "st-plug/timestamps"
48 #fileExperiment = "st-plug-smarthome-nov-13-2018.timestamps"
49 #fileDetection = "st-plug-smarthome-nov-13-2018.eth0.phone.detections"
50
51 # Arlo camera
52 #device = "arlo-camera/self-test"
53 #fileExperiment = "arlo-camera-nov-13-2018.timestamps"
54 #fileDetection = "phone-side-detection"
55 #device = "arlo-camera/timestamps"
56 #fileExperiment = "arlo-camera-smarthome-nov-15-2018.timestamps"
57 #fileDetection = "arlo-camera-smarthome-nov-15-2018.phone.eth0.detections"
58 #fileDetection = "arlo-camera-smarthome-nov-15-2018.phone.eth0.detections.range-based.complete"
59
60 # Nest Thermostat
61 #device = "nest-thermostat/self-test"
62 #fileExperiment = "nest-thermostat-nov-15-2018.timestamps"
63 #fileDetection = "phone-side-detection"
64 device = "nest-thermostat/timestamps"
65 fileExperiment = "nest-thermostat-smarthome-nov-16-2018.timestamps"
66 #fileDetection = "nest-thermostat-smarthome-nov-16-2018.phone.eth0.detections"
67 fileDetection = "nest-thermostat-smarthome-nov-16-2018.phone.eth0.range-based.detections"
68
69 # TP-Link Bulb
70 #device = "tplink-bulb/self-test"
71 #fileExperiment = "tplink-bulb-nov-16-2018.timestamps"
72 #fileDetection = "phone-side-detection"
73 #device = "tplink-bulb/timestamps"
74 #fileExperiment = "tplink-bulb-smarthome-nov-19-2018.timestamps"
75 #fileDetection = "tplink-bulb-smarthome-nov-19-2018.phone.wlan1.detections"
76
77 # Hue bulb
78 #device = "hue-bulb/self-test"
79 #fileExperiment = "hue-bulb-aug-7-2018.timestamps"
80 #fileDetection = "phone-side-detection"
81
82 # WeMo plug
83 #device = "wemo-plug/self-test"
84 #fileExperiment = "wemo-plug-nov-20-2018.timestamps"
85 #fileDetection = "phone-side-detection"
86 #device = "wemo-plug/timestamps"
87 #fileExperiment = "wemo-plug-smarthome-nov-21-2018.timestamps"
88 #fileDetection = "wemo-plug-smarthome-nov-21-2018.phone.wlan1.detections"
89
90 # WeMo Insight plug
91 #device = "wemo-insight-plug/self-test"
92 #fileExperiment = "wemo-insight-plug-nov-21-2018.timestamps"
93 #fileDetection = "phone-side-detection"
94 #device = "wemo-insight-plug/timestamps"
95 #fileExperiment = "wemo-insight-plug-smarthome-nov-22-2018.timestamps"
96 #fileDetection = "wemo-insight-plug-smarthome-nov-22-2018.phone.wlan1.detections"
97
98 # Blossom sprinkler
99 #device = "blossom-sprinkler/self-test"
100 #fileExperiment = "blossom-sprinkler-standalone-jan-9-2019.timestamps"
101 #fileExperiment = "blossom-sprinkler-standalone-jan-11-2019.timestamps"
102 #fileExperiment = "blossom-sprinkler-standalone-jan-14-2019.timestamps"
103 #fileDetection = "phone-side-detection"
104 #fileDetection = "device-side-detection"
105 #device = "blossom-sprinkler/timestamps"
106 #fileExperiment = "blossom-sprinkler-smarthome-jan-10-2019.timestamps"
107 #fileExperiment = "blossom-sprinkler-smarthome-jan-11-2019.timestamps"
108 #fileExperiment = "blossom-sprinkler-smarthome-jan-14-2019.timestamps"
109 #fileDetection = "blossom-sprinkler-smarthome-jan-10-2019.device.wlan1.detections"
110 #fileDetection = "blossom-sprinkler-smarthome-jan-11-2019.phone.wlan1.detections"
111 #fileDetection = "blossom-sprinkler-smarthome-jan-14-2019.device.eth0.detections"
112 #fileDetection = "blossom-sprinkler-smarthome-jan-14-2019.phone.eth0.detections"
113
114 TIME_WINDOW = 15 # detection/signature window of 15 seconds
115 #NEG_TIME_WINDOW = -15 # detection/signature window of 15 seconds
116
117 # Open training timestamps file and store into a list
118 with open(path + device + "/" + fileExperiment, "r") as experiment:
119         tsExperimentList = []
120         for line in experiment:
121                 # Format "%m/%d/%Y %I:%M:%S %p"
122                 tsE = datetime.strptime(line, "%m/%d/%Y %I:%M:%S %p\n")
123                 tsExperimentList.append(tsE)
124
125 # Open detection timestamps file and store into a list
126 with open(path + device + "/" + fileDetection, "r") as detection:
127         tsDetectionList = []
128         for line in detection:
129                 # Format "%b %d, %Y %I:%M:%S %p"
130                 tsD = datetime.strptime(line, "%b %d, %Y %I:%M:%S %p\n")
131                 tsDetectionList.append(tsD)
132                 
133 if (len(tsExperimentList) > len(tsDetectionList)):
134         maxTimestamps = len(tsExperimentList)
135 else:
136         maxTimestamps = len(tsDetectionList)
137
138 i = 0
139 j = 0
140 while i < maxTimestamps:
141         if(len(tsExperimentList) <= i or len(tsDetectionList) <= j):
142                 break;
143         
144         tsE = tsExperimentList[i]
145         tsD = tsDetectionList[j]
146         # Detection is always a bit later than training trigger
147         delta1 = tsD - tsE
148         delta2 = tsE - tsD
149         #print("tsE: " + str(tsE) + " - tsD: " + str(tsD) + " - delta1: " + str(delta1.seconds) + " - delta2: " + str(delta2.seconds))
150         # The following happens when we could detect less triggers than the experiment
151         if (delta1.seconds > TIME_WINDOW and delta2.seconds > TIME_WINDOW):
152                 print("Missing trigger at line: " + str(i) + ", t_experiment: " + str(tsE) + " and t_detection: " + str(tsD))
153                 #print(str(tsD))
154                 i = i + 1
155         # The following should not happen (we have more detected triggers than the experiment)
156         #elif (delta.seconds < NEG_TIME_WINDOW):
157         #       print("Mismatch at t_experiment: " + str(tsE) + " and t_detection: " + str(tsD))
158         #       j = j + 1
159         i = i + 1
160         j = j + 1
161
162 print("Done parsing: " + str(i) + " lines")