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