3fdacb348a225c3ff0f6c417c01fdc2bca14d0d5
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / evaluation / DetectionResultsAnalyzer.java
1 package edu.uci.iotproject.evaluation;
2
3 import edu.uci.iotproject.analysis.TriggerTrafficExtractor;
4 import edu.uci.iotproject.analysis.UserAction;
5 import edu.uci.iotproject.io.TriggerTimesFileReader;
6
7 import java.io.BufferedReader;
8 import java.io.File;
9 import java.io.FileReader;
10 import java.io.IOException;
11 import java.time.Instant;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Optional;
15
16 /**
17  * Utility for comparing detected events to logged (actual) events.
18  *
19  * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
20  * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
21  */
22 public class DetectionResultsAnalyzer {
23
24     public static void main(String[] args) throws IOException {
25         // -------------------------------------- Parse the input files --------------------------------------
26
27         String triggerTimesFile = args[0];
28         // Read the trigger times.
29         // The trigger times file does not contain event types as we initially assumed that we would just be alternating
30         // between ON and OFF.
31         List<Instant> triggerTimestamps = new TriggerTimesFileReader().readTriggerTimes(triggerTimesFile, false);
32         // Now generate user actions based on this alternating ON/OFF pattern.
33         List<UserAction> triggers = new ArrayList<>();
34         for (int i = 0; i < triggerTimestamps.size(); i++) {
35             // NOTE: assumes triggers alternate between ON and OFF
36             UserAction.Type actionType = i % 2 == 0 ? UserAction.Type.TOGGLE_ON : UserAction.Type.TOGGLE_OFF;
37             triggers.add(new UserAction(actionType, triggerTimestamps.get(i)));
38         }
39         // Read the detection output file, assuming a format as specified in UserAction.toString()
40         File detectionOutputFile = new File(args[1]);
41         List<UserAction> detectedEvents = new ArrayList<>();
42         try (BufferedReader br = new BufferedReader(new FileReader(detectionOutputFile))) {
43             String s;
44             while ((s = br.readLine()) != null) {
45                 detectedEvents.add(UserAction.fromString(s));
46             }
47         }
48
49         // -----------------  Now ready to compare the detected events with the logged events -----------------
50
51         // To contain all detected events that could be mapped to a trigger
52         List<UserAction> truePositives = new ArrayList<>();
53         for (UserAction detectedEvent : detectedEvents) {
54             Optional<UserAction> matchingTrigger = triggers.stream()
55                     .filter(t -> t.getType() == detectedEvent.getType() &&
56                             t.getTimestamp().isBefore(detectedEvent.getTimestamp()) &&
57                             t.getTimestamp().plusMillis(TriggerTrafficExtractor.INCLUSION_WINDOW_MILLIS).
58                                     isAfter(detectedEvent.getTimestamp())
59                     ).findFirst();
60             matchingTrigger.ifPresent(mt -> {
61                 // We've consumed the trigger (matched it with a detected event), so remove it so we don't match with
62                 // another detected event.
63                 triggers.remove(mt);
64                 // The current detected event was a true positive as we could match it with a trigger.
65                 truePositives.add(detectedEvent);
66             });
67         }
68         // Now the false positives are those elements in detectedEvents that are not in truePositives
69         List<UserAction> falsePositives = new ArrayList<>();
70         falsePositives.addAll(detectedEvents);
71         falsePositives.removeAll(truePositives);
72         // Print the results...
73         System.out.println("---------- False negatives (events that where not detected) ----------");
74         for (UserAction missing : triggers) {
75             System.out.println(missing);
76         }
77         System.out.println("Total of " + Integer.toString(triggers.size()));
78         System.out.println();
79         System.out.println("---------- False positives (detected, but no matching trigger) ----------");
80         for (UserAction fp : falsePositives) {
81             System.out.println(fp);
82         }
83         System.out.println("Total of " + Integer.toString(falsePositives.size()));
84     }
85
86 }