Adding a proof of concept for clustering with source and destination (4 dimensional...
authorrtrimana <rtrimana@uci.edu>
Tue, 11 Sep 2018 00:05:12 +0000 (17:05 -0700)
committerrtrimana <rtrimana@uci.edu>
Tue, 11 Sep 2018 00:05:12 +0000 (17:05 -0700)
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java
python_ml/plotting-dbscan-diff.py
python_ml/plotting-dbscan-src-dst.py [new file with mode: 0644]
python_ml/plotting-dbscan.py

index a855f473d92e0c703e2393f2250e1862f7830404..1d3888a284e71f3ad4206466c01c302fb996e916 100644 (file)
@@ -126,10 +126,10 @@ public class Main {
 //        final String deviceIp = "192.168.1.246"; // .246 == phone; .229 == sprinkler
 
         // 13) DLink siren August 14 experiment
-//        final String inputPcapFile = path + "/2018-08/dlink-siren/dlink-siren.wlan1.local.pcap";
-//        final String outputPcapFile = path + "/2018-08/dlink-siren/dlink-siren-processed.pcap";
-//        final String triggerTimesFile = path + "/2018-08/dlink-siren/dlink-siren-aug-14-2018.timestamps";
-//        final String deviceIp = "192.168.1.246"; // .246 == phone; .183 == siren
+        final String inputPcapFile = path + "/2018-08/dlink-siren/dlink-siren.wlan1.local.pcap";
+        final String outputPcapFile = path + "/2018-08/dlink-siren/dlink-siren-processed.pcap";
+        final String triggerTimesFile = path + "/2018-08/dlink-siren/dlink-siren-aug-14-2018.timestamps";
+        final String deviceIp = "192.168.1.183"; // .246 == phone; .183 == siren
 
         // 14) Nest thermostat August 15 experiment
 //        final String inputPcapFile = path + "/2018-08/nest/nest.wlan1.local.pcap";
index ba89a152a3aebc0e83d232f3a337b2458bd72da6..15d7728d3bcdda3f61999036bb189f5e151609a9 100644 (file)
@@ -65,6 +65,7 @@ unique_labels = set(labels)
 colors = [plt.cm.Spectral(each)
              for each in np.linspace(0, 1, len(unique_labels))]
 for k, col in zip(unique_labels, colors):
+       cluster_col = [1, 0, 0, 1]
        if k == -1:
            # Black used for noise.
            col = [0, 0, 0, 1]
@@ -73,7 +74,7 @@ for k, col in zip(unique_labels, colors):
 
        # print("Unique label: " + str(k) + " with freq: " + str(labels.tolist().count(k)))
        xy = X[class_member_mask & core_samples_mask]
-       plt.plot(xy[:, 0], xy[:, 1], 'o',
+       plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(cluster_col),
                 markeredgecolor='k', markersize=10)
 
        xy = X[class_member_mask & ~core_samples_mask]
diff --git a/python_ml/plotting-dbscan-src-dst.py b/python_ml/plotting-dbscan-src-dst.py
new file mode 100644 (file)
index 0000000..ff6ceaf
--- /dev/null
@@ -0,0 +1,94 @@
+from sklearn.cluster import DBSCAN
+from sklearn import metrics
+import matplotlib.cm as cm
+import numpy as np
+import matplotlib.pyplot as plt
+
+# Create a subplot with 1 row and 2 columns
+fig, (ax2) = plt.subplots(1, 1)
+fig.set_size_inches(7, 7)
+
+
+# Read from file
+# TODO: Just change the following path and filename 
+#      when needed to read from a different file
+path = "/scratch/July-2018/Pairs2/"
+device = "dlink-siren-off2"
+filename = device + ".txt"
+plt.ylim(0, 2000)
+plt.xlim(0, 2000)
+
+# Number of triggers
+trig = 50
+
+# Read and create an array of pairs
+with open(path + filename, "r") as pairs:
+       pairsArr = []
+       for line in pairs:
+               # We will see a pair and we need to split it into xpoint and ypoint
+               xpoint, ypoint, src, dst = line.split(", ")
+               pair = [int(xpoint), int(ypoint), int(src), int(dst)]
+               pairsArr.append(pair)
+
+# Formed array of pairs                
+#print(pairsArr)
+X = np.array(pairsArr);
+
+# Compute DBSCAN
+# eps = distances
+# min_samples = minimum number of members of a cluster
+#db = DBSCAN(eps=20, min_samples=trig - 5).fit(X)
+# TODO: This is just for seeing more clusters
+db = DBSCAN(eps=20, min_samples=trig - 49).fit(X)
+core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
+core_samples_mask[db.core_sample_indices_] = True
+labels = db.labels_
+
+# Number of clusters in labels, ignoring noise if present.
+n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
+
+#print('Estimated number of clusters: %d' % n_clusters_)
+
+import matplotlib.pyplot as plt
+
+# Black removed and is used for noise instead.
+unique_labels = set(labels)
+print("Labels: " + str(labels))
+
+colors = [plt.cm.Spectral(each)
+          for each in np.linspace(0, 1, len(unique_labels))]
+for k, col in zip(unique_labels, colors):
+    cluster_col = [1, 0, 0, 1]
+    if k == -1:
+        # Black used for noise.
+        col = [0, 0, 0, 1]
+
+    class_member_mask = (labels == k)
+
+    xy = X[class_member_mask & core_samples_mask]
+    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(cluster_col),
+             markeredgecolor='k', markersize=10)
+
+    xy = X[class_member_mask & ~core_samples_mask]
+    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
+             markeredgecolor='k', markersize=6)
+
+count = 0
+for pair in pairsArr:
+       #if labels[count] != -1:
+       # If this is not a noise (i.e.,real data)
+       #       plt.text(pair[0], pair[1], "Freq: " + str(labels.tolist().count(labels[count])), fontsize=10)
+
+       if labels[count] == -1:
+               plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]), fontsize=10)
+       else:
+       # Only print the frequency when this is a real cluster
+               plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]) + 
+                       " f: " + str(labels.tolist().count(labels[count])), fontsize=10)
+       count = count + 1
+
+       
+plt.title(device + ' - Clusters: %d' % n_clusters_)
+plt.show()
+
+
index 9b5d1abb0e18ee3f1b7a4cea76f2a5b48eb87469..580d4df90e4d7baa386cccdfa30b2a2eef18778f 100644 (file)
@@ -58,6 +58,7 @@ unique_labels = set(labels)
 colors = [plt.cm.Spectral(each)
           for each in np.linspace(0, 1, len(unique_labels))]
 for k, col in zip(unique_labels, colors):
+    cluster_col = [1, 0, 0, 1]
     if k == -1:
         # Black used for noise.
         col = [0, 0, 0, 1]
@@ -65,7 +66,7 @@ for k, col in zip(unique_labels, colors):
     class_member_mask = (labels == k)
 
     xy = X[class_member_mask & core_samples_mask]
-    plt.plot(xy[:, 0], xy[:, 1], 'o',
+    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(cluster_col),
              markeredgecolor='k', markersize=10)
 
     xy = X[class_member_mask & ~core_samples_mask]
@@ -83,7 +84,7 @@ for pair in pairsArr:
        else:
        # Only print the frequency when this is a real cluster
                plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]) + 
-                       " - Freq: " + str(labels.tolist().count(labels[count])), fontsize=10)
+                       " f: " + str(labels.tolist().count(labels[count])), fontsize=10)
        count = count + 1