Merging changes.
[pingpong.git] / python_ml / plotting-dbscan-complete.py
1 from sklearn.cluster import DBSCAN
2 from sklearn import metrics
3 import matplotlib.cm as cm
4 import numpy as np
5 import matplotlib.pyplot as plt
6
7 # Create a subplot with 1 row and 2 columns
8 fig, (ax2) = plt.subplots(1, 1)
9 fig.set_size_inches(7, 7)
10
11
12 # Read from file
13 # TODO: Just change the following path and filename 
14 #       when needed to read from a different file
15 path = "/scratch/July-2018/Pairs/"
16 device1 = "tplink-bulb-on"
17 device2 = "tplink-bulb-off"
18 filename1 = device1 + ".txt"
19 filename2 = device2 + ".txt"
20
21 # Number of triggers
22 trig = 50
23
24 # PLOTTING FOR DEVICE ON EVENT
25 # Read and create an array of pairs
26 with open(path + filename1, "r") as pairs:
27         pairsArr = []
28         for line in pairs:
29                 # We will see a pair and we need to split it into xpoint and ypoint
30                 xpoint, ypoint = line.split(", ")
31                 pair = [int(xpoint), int(ypoint)]
32                 pairsArr.append(pair)
33
34 # Formed array of pairs         
35 #print(pairsArr)
36 X = np.array(pairsArr);
37
38 # Compute DBSCAN
39 # eps = distances
40 # min_samples = minimum number of members of a cluster
41 db = DBSCAN(eps=30, min_samples=trig - 5).fit(X)
42 core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
43 core_samples_mask[db.core_sample_indices_] = True
44 labels = db.labels_
45
46 # Number of clusters in labels, ignoring noise if present.
47 n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
48 #print('Estimated number of clusters: %d' % n_clusters_)
49
50 # Black removed and is used for noise instead.
51 unique_labels = set(labels)
52 #print("Labels: " + str(labels))
53
54 colors = [plt.cm.Spectral(each)
55               for each in np.linspace(0, 1, len(unique_labels))]
56 for k, col in zip(unique_labels, colors):
57         if k == -1:
58             # Black used for noise.
59             col = [0, 0, 0, 1]
60
61         class_member_mask = (labels == k)
62
63         print("Unique label: " + str(k) + " with freq: " + str(labels.tolist().count(k)))
64         xy = X[class_member_mask & core_samples_mask]
65         plt.plot(xy[:, 0], xy[:, 1], 'o',
66                  markeredgecolor='k', markersize=10)
67
68         xy = X[class_member_mask & ~core_samples_mask]
69         plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
70                  markeredgecolor='k', markersize=6)
71
72 count = 0
73 for pair in pairsArr:
74         plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]) + 
75                 "\nFreq: " + str(labels.tolist().count(labels[count])), fontsize=10)
76         count = count + 1
77
78 #====================================================================================================
79
80 # PLOTTING FOR DEVICE ON EVENT
81 # Read and create an array of pairs
82 with open(path + filename2, "r") as pairs:
83         pairsArr = []
84         for line in pairs:
85                 # We will see a pair and we need to split it into xpoint and ypoint
86                 xpoint, ypoint = line.split(", ")
87                 pair = [int(xpoint), int(ypoint)]
88                 pairsArr.append(pair)
89
90 # Formed array of pairs         
91 #print(pairsArr)
92 X = np.array(pairsArr);
93
94 # Compute DBSCAN
95 # eps = distances
96 # min_samples = minimum number of members of a cluster
97 db = DBSCAN(eps=10, min_samples=trig - 5).fit(X)
98 core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
99 core_samples_mask[db.core_sample_indices_] = True
100 labels = db.labels_
101
102 # Number of clusters in labels, ignoring noise if present.
103 n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
104 #print('Estimated number of clusters: %d' % n_clusters_)
105
106 import matplotlib.pyplot as plt
107
108 # Black removed and is used for noise instead.
109 unique_labels = set(labels)
110 #print("Labels: " + str(labels))
111
112 colors = [plt.cm.Spectral(each)
113               for each in np.linspace(0, 1, len(unique_labels))]
114 for k, col in zip(unique_labels, colors):
115         if k == -1:
116             # Black used for noise.
117             col = [0, 0, 0, 1]
118
119         class_member_mask = (labels == k)
120
121         print("Unique label: " + str(k) + " with freq: " + str(labels.tolist().count(k)))
122         xy = X[class_member_mask & core_samples_mask]
123         plt.plot(xy[:, 0], xy[:, 1], 'o',
124                  markeredgecolor='k', markersize=10)
125
126         xy = X[class_member_mask & ~core_samples_mask]
127         plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
128                  markeredgecolor='k', markersize=6)
129
130 count = 0
131 for pair in pairsArr:
132         plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]) + 
133                 "\nFreq: " + str(labels.tolist().count(labels[count])), fontsize=10)
134         count = count + 1
135
136
137         
138 plt.title(device1 + ' & ' + device2 + ' - Estimated number of clusters: %d' % n_clusters_)
139 plt.show()
140
141