Adding array out of bound check for timestamps comparison.
[pingpong.git] / python_ml / validate-detection.py
1 from datetime import datetime
2
3
4 path = "/scratch/July-2018/training/"
5 device = "dlink-plug/self-test"
6 #fileExperiment = "dlink-plug-8hr-data-oct-8-2018.timestamps"
7 #fileDetection = "dlink-plug.detection.timestamps"
8 #fileExperiment = "dlink-siren-aug-14-2018.timestamps"
9 #fileDetection = "dlink-siren.2018-08-14_experiment.phone_signature_detected_events.txt"
10 fileExperiment = "dlink-plug-oct-17-2018.timestamps"
11 fileDetection = "detection-on-training-device-side"
12 TIME_WINDOW = 15 # detection/signature window of 15 seconds
13 #NEG_TIME_WINDOW = -15 # detection/signature window of 15 seconds
14
15 # Open training timestamps file and store into a list
16 with open(path + device + "/" + fileExperiment, "r") as experiment:
17         tsExperimentList = []
18         for line in experiment:
19                 # Format "%m/%d/%Y %I:%M:%S %p"
20                 tsE = datetime.strptime(line, "%m/%d/%Y %I:%M:%S %p\n")
21                 tsExperimentList.append(tsE)
22
23 # Open detection timestamps file and store into a list
24 with open(path + device + "/" + fileDetection, "r") as detection:
25         tsDetectionList = []
26         for line in detection:
27                 # Format "%b %d, %Y %I:%M:%S %p"
28                 tsD = datetime.strptime(line, "%b %d, %Y %I:%M:%S %p\n")
29                 tsDetectionList.append(tsD)
30                 
31 if (len(tsExperimentList) > len(tsDetectionList)):
32         maxTimestamps = len(tsExperimentList)
33 else:
34         maxTimestamps = len(tsDetectionList)
35
36 i = 0
37 j = 0
38 while i < maxTimestamps:
39         if(len(tsExperimentList) <= i or len(tsDetectionList) <= j):
40                 break;
41         
42         tsE = tsExperimentList[i]
43         tsD = tsDetectionList[j]
44         # Detection is always a bit later than training trigger
45         delta = tsD - tsE
46         # The following happens when we could detect less triggers than the experiment
47         if (delta.seconds > TIME_WINDOW):
48                 print("Missing trigger at line: " + str(i) + ", t_experiment: " + str(tsE) + " and t_detection: " + str(tsD))
49                 i = i + 1
50         # The following should not happen (we have more detected triggers than the experiment)
51         #elif (delta.seconds < NEG_TIME_WINDOW):
52         #       print("Mismatch at t_experiment: " + str(tsE) + " and t_detection: " + str(tsD))
53         #       j = j + 1
54         i = i + 1
55         j = j + 1
56
57 print("Done parsing: " + str(i) + " lines")