Adding a method to delete a bad sequence in a signature after we test the produced...
[pingpong.git] / python_ml / validate-detection.py
1 from datetime import datetime
2
3
4 path = "/scratch/July-2018/training/"
5 # D-Link plug
6 #device = "dlink-plug/self-test"
7 #fileExperiment = "dlink-plug-oct-17-2018.timestamps"
8 #fileDetection = "detection-on-training-device-side"
9 #fileDetection = "detection-on-training-phone-side"
10 # Arlo camera
11 #device = "arlo-camera/self-test"
12 #fileExperiment = "arlo-camera-aug-10-2018.timestamps"
13 #fileDetection = "detection-on-training-phone-side"
14 # Blossom sprinkler
15 #device = "blossom-sprinkler/self-test"
16 #fileExperiment = "blossom-sprinkler-aug-13-2018.timestamps"
17 #fileDetection = "detection-on-training-device-side"
18 # Nest thermostat
19 device = "nest-thermostat/self-test"
20 fileExperiment = "nest-aug-15-2018.timestamps"
21 fileDetection = "detection-on-training-phone-side"
22 # Hue bulb
23 #device = "hue-bulb/self-test"
24 #fileExperiment = "hue-bulb-aug-7-2018.timestamps"
25 #fileDetection = "detection-on-training-phone-side"
26 # TPLink bulb
27 #device = "tplink-bulb/self-test"
28 #fileExperiment = "tplink-bulb-aug-3-2018.timestamps"
29 #fileDetection = "detection-on-training-phone-side"
30 # WeMo Insight Plug
31 #device = "wemo-insight-plug/self-test"
32 #fileExperiment = "wemo-insight-july-31-2018.timestamps"
33 #fileDetection = "detection-on-training-device-side"
34
35 TIME_WINDOW = 15 # detection/signature window of 15 seconds
36 #NEG_TIME_WINDOW = -15 # detection/signature window of 15 seconds
37
38 # Open training timestamps file and store into a list
39 with open(path + device + "/" + fileExperiment, "r") as experiment:
40         tsExperimentList = []
41         for line in experiment:
42                 # Format "%m/%d/%Y %I:%M:%S %p"
43                 tsE = datetime.strptime(line, "%m/%d/%Y %I:%M:%S %p\n")
44                 tsExperimentList.append(tsE)
45
46 # Open detection timestamps file and store into a list
47 with open(path + device + "/" + fileDetection, "r") as detection:
48         tsDetectionList = []
49         for line in detection:
50                 # Format "%b %d, %Y %I:%M:%S %p"
51                 tsD = datetime.strptime(line, "%b %d, %Y %I:%M:%S %p\n")
52                 tsDetectionList.append(tsD)
53                 
54 if (len(tsExperimentList) > len(tsDetectionList)):
55         maxTimestamps = len(tsExperimentList)
56 else:
57         maxTimestamps = len(tsDetectionList)
58
59 i = 0
60 j = 0
61 while i < maxTimestamps:
62         if(len(tsExperimentList) <= i or len(tsDetectionList) <= j):
63                 break;
64         
65         tsE = tsExperimentList[i]
66         tsD = tsDetectionList[j]
67         # Detection is always a bit later than training trigger
68         delta1 = tsD - tsE
69         delta2 = tsE - tsD
70         #print("tsE: " + str(tsE) + " - tsD: " + str(tsD) + " - delta1: " + str(delta1.seconds) + " - delta2: " + str(delta2.seconds))
71         # The following happens when we could detect less triggers than the experiment
72         if (delta1.seconds > TIME_WINDOW and delta2.seconds > TIME_WINDOW):
73                 print("Missing trigger at line: " + str(i) + ", t_experiment: " + str(tsE) + " and t_detection: " + str(tsD))
74                 #print(str(tsD))
75                 i = i + 1
76         # The following should not happen (we have more detected triggers than the experiment)
77         #elif (delta.seconds < NEG_TIME_WINDOW):
78         #       print("Mismatch at t_experiment: " + str(tsE) + " and t_detection: " + str(tsD))
79         #       j = j + 1
80         i = i + 1
81         j = j + 1
82
83 print("Done parsing: " + str(i) + " lines")