1 from sklearn.cluster import DBSCAN
2 from sklearn import metrics
3 import matplotlib.cm as cm
5 import matplotlib.pyplot as plt
7 # Create a subplot with 1 row and 2 columns
8 fig, (ax2) = plt.subplots(1, 1)
9 fig.set_size_inches(7, 7)
12 # TODO: Just change the following path and filename
13 # when needed to read from a different file
14 path = "/scratch/July-2018/Pairs2/"
15 # TODO: Change the order of the files below to generate
16 # the diff plot reversedly
17 device1 = "dlink-siren-device-off"
18 device2 = "dlink-siren-device-on"
19 filename1 = device1 + ".txt"
20 filename2 = device2 + ".txt"
27 # PLOTTING FOR DEVICE ON EVENT
28 # Read and create an array of pairs
29 with open(path + filename1, "r") as pairs:
32 # We will see a pair and we need to split it into xpoint and ypoint
33 xpoint, ypoint = line.split(", ")
34 pair = [int(xpoint), int(ypoint)]
35 pairsArr1.append(pair)
37 # PLOTTING FOR DEVICE ON EVENT
38 # Read and create an array of pairs
39 with open(path + filename2, "r") as pairs:
42 # We will see a pair and we need to split it into xpoint and ypoint
43 xpoint, ypoint = line.split(", ")
44 pair = [int(xpoint), int(ypoint)]
45 pairsArr2.append(pair)
47 diff12 = [i for i in pairsArr1 if i not in pairsArr2]
53 # min_samples = minimum number of members of a cluster
54 db = DBSCAN(eps=10, min_samples=trig - 45).fit(X)
55 core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
56 core_samples_mask[db.core_sample_indices_] = True
59 # Number of clusters in labels, ignoring noise if present.
60 n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
62 # Black removed and is used for noise instead.
63 unique_labels = set(labels)
65 colors = [plt.cm.Spectral(each)
66 for each in np.linspace(0, 1, len(unique_labels))]
67 for k, col in zip(unique_labels, colors):
68 cluster_col = [1, 0, 0, 1]
70 # Black used for noise.
73 class_member_mask = (labels == k)
75 # print("Unique label: " + str(k) + " with freq: " + str(labels.tolist().count(k)))
76 xy = X[class_member_mask & core_samples_mask]
77 plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(cluster_col),
78 markeredgecolor='k', markersize=10)
80 xy = X[class_member_mask & ~core_samples_mask]
81 plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
82 markeredgecolor='k', markersize=6)
86 if labels[count] == -1:
87 plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]), fontsize=10)
89 # Only print the frequency when this is a real cluster
90 plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]) +
91 " - Freq:" + str(labels.tolist().count(labels[count])), fontsize=10)
94 plt.title(device1 + ' - diff - ' + device2)