New scripts to cluster based on C->S/S->C direction filter; improving the plot's...
[pingpong.git] / python_ml / plotting-dbscan.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/Pairs2/"
16 device = "dlink-siren-device-off"
17 filename = device + ".txt"
18 plt.ylim(0, 2000)
19 plt.xlim(0, 2000)
20
21 # Number of triggers
22 trig = 50
23
24 # Read and create an array of pairs
25 with open(path + filename, "r") as pairs:
26         pairsArr = []
27         for line in pairs:
28                 # We will see a pair and we need to split it into xpoint and ypoint
29                 xpoint, ypoint = line.split(", ")
30                 pair = [int(xpoint), int(ypoint)]
31                 pairsArr.append(pair)
32
33 # Formed array of pairs         
34 #print(pairsArr)
35 X = np.array(pairsArr);
36
37 # Compute DBSCAN
38 # eps = distances
39 # min_samples = minimum number of members of a cluster
40 #db = DBSCAN(eps=20, min_samples=trig - 5).fit(X)
41 # TODO: This is just for seeing more clusters
42 db = DBSCAN(eps=20, min_samples=trig - 45).fit(X)
43 core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
44 core_samples_mask[db.core_sample_indices_] = True
45 labels = db.labels_
46
47 # Number of clusters in labels, ignoring noise if present.
48 n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
49
50 #print('Estimated number of clusters: %d' % n_clusters_)
51
52 import matplotlib.pyplot as plt
53
54 # Black removed and is used for noise instead.
55 unique_labels = set(labels)
56 #print("Labels: " + str(labels))
57
58 colors = [plt.cm.Spectral(each)
59           for each in np.linspace(0, 1, len(unique_labels))]
60 for k, col in zip(unique_labels, colors):
61     cluster_col = [1, 0, 0, 1]
62     if k == -1:
63         # Black used for noise.
64         col = [0, 0, 0, 1]
65
66     class_member_mask = (labels == k)
67
68     xy = X[class_member_mask & core_samples_mask]
69     plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(cluster_col),
70              markeredgecolor='k', markersize=10)
71
72     xy = X[class_member_mask & ~core_samples_mask]
73     plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
74              markeredgecolor='k', markersize=6)
75
76 count = 0
77 for pair in pairsArr:
78         #if labels[count] != -1:
79         # If this is not a noise (i.e.,real data)
80         #       plt.text(pair[0], pair[1], "Freq: " + str(labels.tolist().count(labels[count])), fontsize=10)
81
82         if labels[count] == -1:
83                 plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]), fontsize=10)
84         else:
85         # Only print the frequency when this is a real cluster
86                 plt.text(pair[0], pair[1], str(pair[0]) + ", " + str(pair[1]) + 
87                         " : " + str(labels.tolist().count(labels[count])), fontsize=10)
88         count = count + 1
89
90         
91 plt.title(device + ' - Clusters: %d' % n_clusters_)
92 plt.show()
93
94