-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcluster.py
377 lines (325 loc) · 16.2 KB
/
cluster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
from clusteringnmap.vectorize import Vectorizer, vectorize
from clusteringnmap.clustering import cluster_with_dbscan, cluster_with_kmeans, precompute_distances, cluster_with_agglomerative, cluster_interactive
from clusteringnmap.validation import validate_clusters, get_average_distance_per_cluster
from clusteringnmap.analysis import get_common_features_from_cluster, get_common_feature_stats
from clusteringnmap.display import print_cluster_details, generate_dot_graph_for_gephi, create_plot, display_vector_index_details, display_shared_vector_indeces_details
from clusteringnmap.optimizing import sort_items_by_multiple_keys
from clusteringnmap.reduction import pca
from sklearn.preprocessing import normalize
import clusteringnmap.interactive_state
# bit of a hack to allow custom static resources in bokeh
from os.path import abspath
from tornado.web import StaticFileHandler
import bokeh.core.templates
from jinja2 import Template
with open("webstatic/file.html", "r") as f:
bokeh.core.templates.FILE = Template(f.read())
import bokeh.server.urls
from bokeh.settings import settings
class newStaticHandler(StaticFileHandler):
''' Implements a custom Tornado static file handler for BokehJS
JavaScript and CSS resources.
'''
def __init__(self, tornado_app, *args, **kw):
kw['path'] = abspath("webstatic/static")
# Note: tornado_app is stored as self.application
super(newStaticHandler, self).__init__(tornado_app, *args, **kw)
# We aren't using tornado's built-in static_path function
# because it relies on TornadoApplication's autoconfigured
# static handler instead of our custom one. We have a
# custom one because we think we might want to serve
# static files from multiple paths at once in the future.
@classmethod
def append_version(cls, path):
# this version is cached on the StaticFileHandler class,
# keyed by absolute filesystem path, and only invalidated
# on an explicit StaticFileHandler.reset(). The reset is
# automatic on every request if you set
# static_hash_cache=False in TornadoApplication kwargs.
version = StaticFileHandler.get_version(dict(static_path=settings.bokehjsdir()), path)
return ("%s?v=%s" % (path, version))
bokeh.server.urls.toplevel_patterns[1] = (r'/static/(.*)', newStaticHandler)
from bokeh.command.subcommands.serve import Serve
import logging
from json import loads as jloads
from pprint import pprint
def cluster(
vector_names,
vectors,
reduced_vectors,
normalized_vectors,
vectorizer,
strategy="automatic",
cluster_method="kmeans",
n_clusters=2,
epsilon=0.5,
min_samples=5,
metric="euclidean",
):
"""
Clustering options:
Manual:
The user supplies all required information to do the clustering. This includes the clustering algorithm and
hyper parameters
Assisted:
The user assists the algorithm by suggesting that some samples should or should not be clustered together
Automatic:
The multiple clustering strategies and parameters are used in an attempt to get the best clusters
"""
if strategy == "manual":
if cluster_method == "kmeans":
return cluster_with_kmeans(normalized_vectors, n_clusters=n_clusters)
elif cluster_method == "dbscan":
return cluster_with_dbscan(normalized_vectors, epsilon=epsilon, min_samples=min_samples, metric=metric)
elif cluster_method == "agglomerative":
return cluster_with_agglomerative(normalized_vectors, n_clusters=n_clusters, metric=metric)
else:
# Unknown clustering method
raise NotImplementedError()
elif strategy == "assisted":
"""
To display a information about a vector to a user, you can use the following:
display_vector_index_details(vector_index, vectors, vector_names, vectorizer)
"""
# todo Try with normalized vectors
return cluster_interactive(reduced_vectors, vectorizer, vectors, vector_names)
elif strategy == "viz":
clusteringnmap.interactive_state._vector_names = vector_names
clusteringnmap.interactive_state._vectors = vectors
clusteringnmap.interactive_state._vectorizer = vectorizer
clusteringnmap.interactive_state._reduced_vectors = reduced_vectors
clusteringnmap.interactive_state._normalized_vectors = normalized_vectors
clusteringnmap.interactive_state._labels = [0] * vectors.shape[0]
print "Go to http://localhost:5006/Interactive_Clustering_bokeh"
print "Press CTRL-C to exit web interface"
parser = argparse.ArgumentParser(Serve.args)
s = Serve(parser)
s.invoke(parser.parse_args(["clusteringnmap/Interactive_Clustering_bokeh.py"]))
return clusteringnmap.interactive_state._labels
elif strategy == "automatic":
results = []
smallest_cluster_count = vectors.shape[0]
for cluster_method in [
"kmeans",
"dbscan",
"agglomerative",
]:
if cluster_method == "kmeans":
logging.debug("Starting prospective KMeans clusterings")
move_to_next_method = False
for n_clusters in xrange(2, smallest_cluster_count):
logging.debug("Trying {0}".format("kmeans(n_clusters={0})".format(n_clusters)))
labels = cluster_with_kmeans(reduced_vectors, n_clusters=n_clusters)
overall_score, per_cluster_score = validate_clusters(vectors, labels)
mean_distance = get_average_distance_per_cluster(vectors, labels)[0]
tsp, msp, msn = get_common_feature_stats(vectors, labels, vectorizer)
# If any cluster has 0 shared features, we just ignore the result
if msp <= tsp:
logging.debug("Not all clusters are informative")
continue
if len(set(labels)) > smallest_cluster_count:
move_to_next_method = True
break
if len(set(labels)) < smallest_cluster_count:
smallest_cluster_count = len(set(labels))
logging.debug(repr((
overall_score,
min(per_cluster_score.values()),
mean_distance,
labels,
len(set(labels)),
tsp,
msp,
msn,
"kmeans(n_clusters={0})".format(n_clusters)
)))
results.append(
(
overall_score,
min(per_cluster_score.values()),
mean_distance,
labels,
len(set(labels)),
tsp,
msp,
msn,
"kmeans(n_clusters={0})".format(n_clusters)
)
)
if move_to_next_method:
continue
if cluster_method == "agglomerative":
logging.debug("Starting prospective Agglomerative clusterings")
move_to_next_method = False
for n_clusters in xrange(2, smallest_cluster_count):
logging.debug("Trying {0}".format("agglomerative(n_clusters={0})".format(n_clusters)))
labels = cluster_with_agglomerative(reduced_vectors, n_clusters=n_clusters, metric=metric)
overall_score, per_cluster_score = validate_clusters(vectors, labels)
mean_distance = get_average_distance_per_cluster(vectors, labels)[0]
tsp, msp, msn = get_common_feature_stats(vectors, labels, vectorizer)
# If any cluster has 0 shared features, we just ignore the result
if msp <= tsp:
logging.debug("Not all clusters are informative")
continue
if len(set(labels)) > smallest_cluster_count:
move_to_next_method = True
break
if len(set(labels)) < smallest_cluster_count:
smallest_cluster_count = len(set(labels))
logging.debug(repr((
overall_score,
min(per_cluster_score.values()),
mean_distance,
labels,
len(set(labels)),
tsp,
msp,
msn,
"agglomerative(n_clusters={0})".format(n_clusters)
)))
results.append(
(
overall_score,
min(per_cluster_score.values()),
mean_distance,
labels,
len(set(labels)),
tsp,
msp,
msn,
"agglomerative(n_clusters={0})".format(n_clusters)
)
)
if move_to_next_method:
continue
if cluster_method == "dbscan":
logging.debug("Starting prospective DBSCAN clusterings")
distance_matrix = precompute_distances(vectors, metric=metric)
min_distance = sorted(set(list(distance_matrix.flatten())))[1]
max_distance = sorted(set(list(distance_matrix.flatten())))[-1]
num_steps = 25.0
step_size = float(max_distance - min_distance) / float(num_steps)
epsilon = min_distance
while True:
logging.debug("Trying {0}".format("dbscan(epsilon={0})".format(epsilon)))
labels = cluster_with_dbscan(reduced_vectors, epsilon=epsilon, min_samples=1, distances=distance_matrix)
if len(set(labels)) == 1 and list(set(labels))[0] == 0:
break
overall_score, per_cluster_score = validate_clusters(vectors, labels)
mean_distance = get_average_distance_per_cluster(vectors, labels)[0]
tsp, msp, msn = get_common_feature_stats(vectors, labels, vectorizer)
# If any cluster has 0 shared features, we just ignore the result
if msp <= tsp:
logging.debug("Not all clusters are informative")
epsilon += step_size
continue
logging.debug(repr((
overall_score,
min(per_cluster_score.values()),
mean_distance,
labels,
len(set(labels)),
tsp,
msp,
msn,
"dbscan(epsilon={0})".format(epsilon)
)))
results.append(
(
overall_score,
min(per_cluster_score.values()),
mean_distance,
labels,
len(set(labels)),
tsp,
msp,
msn,
"dbscan(epsilon={0})".format(epsilon)
)
)
epsilon += step_size
# Pick best result
"""
We want to maximize the silhouette score while minimizing the number of labels
"""
sorted_results = sort_items_by_multiple_keys(
results,
{
#0: True, # AVG Silhouette
#1: True, # Min Silhouette
#2: False, # Average distance
4: False, # Number of clusters
#6: True, # Min common features per cluster
},
{
#0: 1,
#1: 1,
#2: 1,
4: 1,
#6: 1
}
)
logging.debug(sorted_results)
best_result = results[sorted_results[0][0]]
logging.debug(best_result)
best_method = best_result[-1]
best_silhouette = best_result[0]
best_labels = best_result[3]
logging.info("Best clustering method: {0} (adjusted silhouette == {1})".format(best_method, best_silhouette))
return best_labels
else:
# Unknown strategy
raise NotImplementedError()
if __name__ == "__main__":
# todo Parse arguments
import argparse
parser = argparse.ArgumentParser(description=u'Cluster NMap Output')
parser.add_argument('path', metavar='path', type=str, nargs='+', default=None,
help="Paths to files or directories to scan")
parser.add_argument('-s', '--strategy', default="automatic", choices=["manual", "automatic", "assisted", "viz"])
parser.add_argument('-c', '--method', default="kmeans", choices=["kmeans", "dbscan", "agglomerative"])
parser.add_argument('--metric', default="euclidean", choices=["euclidean", "cosine", "jaccard"])
parser.add_argument('-n', '--n_clusters', type=int, default=2, help='Number of kmeans clusters to aim for')
parser.add_argument('-e', '--epsilon', type=float, default=0.5, help='DBSCAN Epsilon')
parser.add_argument('-m', '--min_samples', type=int, default=5, help='DBSCAN Minimum Samples')
parser.add_argument('-p', '--plot', default=False, required=False, action='store_true',
help='Plot clusters on 2D plane')
parser.add_argument("-v", "--verbosity", action="count", help="increase output verbosity")
args = parser.parse_args()
logging.basicConfig(format='%(asctime)s %(process)s %(module)s %(funcName)s %(levelname)-8s :%(message)s',
datefmt='%m-%d %H:%M')
if args.verbosity == 1:
logging.getLogger().setLevel(logging.INFO)
elif args.verbosity > 1:
logging.getLogger().setLevel(logging.DEBUG)
# Vectorize our input
logging.info("Vectorizing")
vector_names, vectors, vectorizer = vectorize(args.path)
logging.debug("Loaded {0} vectors with {1} features".format(len(vector_names), vectors.shape[1]))
logging.info("Vectorizing complete")
logging.info("Reducing vector dimensions with PCA")
normalized_vectors = normalize(vectors)
reduced_vectors = pca(vectors)
# Cluster the vectors
logging.info("Clustering")
labels = cluster(vector_names, vectors, reduced_vectors, normalized_vectors, vectorizer, args.strategy, args.method, args.n_clusters, args.epsilon, args.min_samples, args.metric)
logging.info("Clustering Complete")
# Test cluster validity
overall_score, per_cluster_score = validate_clusters(vectors, labels)
# Analysis relevant to the person reading results
universal_positive_features, universal_negative_features, shared_features = get_common_features_from_cluster(vectors, labels, vectorizer)
# Reduce results and relevant information to per cluster data
cluster_details = {}
for cluster_id in per_cluster_score.keys():
cluster_details[cluster_id] = {
"silhouette": per_cluster_score[cluster_id],
"shared_positive_features": shared_features[cluster_id]['positive'],
#"shared_negative_features": shared_features[cluster_id]['negative'],
"ips": [vector_names[x] for x in xrange(len(vector_names)) if labels[x] == cluster_id]
}
print_cluster_details(cluster_details, shared_features)
if args.plot:
create_plot(reduced_vectors, labels, vector_names)
# Write DOT diagram out to cluster.dot, designed for input into Gephi (https://gephi.org/)
with open("cluster.dot", "w") as f:
f.write(generate_dot_graph_for_gephi(precompute_distances(vectors, metric=args.metric), vector_names, labels))