-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbscan.py
321 lines (237 loc) · 10 KB
/
dbscan.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
import collections
import itertools
import os.path
import pickle
import sys
import config
import pyroprinting
import fullsearch
import spatial
def dbscan(points, radii, minNeighbors):
clusters = []
unclassifiedPoints = set(points)
while len(unclassifiedPoints) > 0:
point = unclassifiedPoints.pop()
neighbors = points.getNeighborsOf(point, radii)
if len(neighbors) >= minNeighbors:
clusters.append(expandCluster(points, radii, minNeighbors, unclassifiedPoints, neighbors) | {point})
return clusters
def expandCluster(points, radii, minNeighbors, unclassifiedPoints, reachable):
cluster = set(reachable)
unclassifiedPoints -= reachable
while len(reachable) > 0:
point = reachable.pop()
neighbors = points.getNeighborsOf(point, radii)
if len(neighbors) >= minNeighbors:
unclassifiedNeighbors = set(neighbor for neighbor in neighbors if neighbor in unclassifiedPoints)
unclassifiedPoints -= unclassifiedNeighbors # or -= neighbors
reachable |= unclassifiedNeighbors
cluster |= neighbors
return cluster
def popDBSCAN(points, radii, minNeighbors):
clusters = []
unclassifiedPoints = points #TODO: copy spatial tree instead of assuming they are ok with us consuming it
while len(unclassifiedPoints) > 0:
point = unclassifiedPoints.pop()
# print("popped {}".format(point))
neighbors = unclassifiedPoints.popNeighborsOf(point, radii)
clusters.extend(investigateNeighbors(unclassifiedPoints, radii, minNeighbors, point, neighbors))
return clusters
def investigateNeighbors(unseenPoints, radii, minNeighbors, startPoint, startNeighbors):
clusters = []
seen = collections.defaultdict(set)
for neighbor in startNeighbors:
seen[neighbor].add(startPoint)
#TODO: probably better for reachable and noise to be ordered. either bf or df?
# perhaps to the point of combining them and not doing a single cluster at a time.
# though we want to do more dense areas first right?
# or do we want to find edges first to keep the queues short?
if len(startNeighbors) >= minNeighbors:
reachable = startNeighbors
noise = set()
currentCluster = {startPoint} | set(startNeighbors)
clusters.append(currentCluster)
else:
noise = startNeighbors
reachable = set()
currentCluster = None
while len(reachable) + len(noise) > 0:
#exhaust reachable before investigating noise
if len(reachable) > 0:
point = reachable.pop()
else:
currentCluster = None
point = noise.pop()
unseenNeighbors = unseenPoints.popNeighborsOf(point, radii)
# need to check all points in queue because they have been removed from unseen and havent ran their own queries yet
# in the degererate case (where all noise and clusters are connected by radii) this could make whole algorithm n^2
for queuedPoint in itertools.chain(reachable, noise):
#TODO: try to shortcut this comparison
# maybe if unseen = 0?
# maybe if unseen + seen is > minNeighbors? (for both)
# maybe can only skip points in reachable
# maybe already have distances to neighbors so combine for bestcase distance and skip if bestcase doesnt pass threshold?
# maybe these are stored in spatial trees?
# maybe depends if point came from noise/reachable
if point.isWithinRadiiOf(queuedPoint, radii):
assert(queuedPoint not in seen[point])
seen[point].add(queuedPoint)
assert(point not in seen[queuedPoint])
seen[queuedPoint].add(point)
for neighbor in unseenNeighbors:
assert(point not in seen[neighbor])
seen[neighbor].add(point)
if len(unseenNeighbors) + len(seen[point]) >= minNeighbors:
if currentCluster is None:
currentCluster = {point}
clusters.append(currentCluster)
for newlyReachable in seen[point] & noise:
noise.remove(newlyReachable)
reachable.add(newlyReachable)
reachable |= unseenNeighbors
currentCluster |= unseenNeighbors | seen[point]
else:
noise |= unseenNeighbors
return clusters
def verifyClusters(pointsList, radii, minNeighbors, clusters):
correct = True
for i, cluster in enumerate(clusters):
print("verifying cluster {}/{}...".format(i, len(clusters)))
correct &= verifyClusterConnectivity(radii, minNeighbors, cluster)
correct &= verifyClusterMaximality(pointsList, radii, minNeighbors, cluster)
print("verifying clusters existence...")
correct = verifyClustersExistence(pointsList, radii, minNeighbors, clusters)
return correct
def verifyClustersExistence(pointsList, neighborMap, minNeighbors, clusters):
correct = True
clusterMap = collections.defaultdict(list)
for cluster in clusters:
for point in cluster:
clusterMap[point].append(cluster)
for i, point1 in enumerate(pointsList):
print("{}/{} - {}".format(i+1, len(pointsList), point1))
for j, point2 in enumerate(pointsList):
if i != j:
if isDensityReachable(neighborMap, minNeighbors, point1, point2):
thisCorrect = len(clusterMap[point1]) == 1
correct &= thisCorrect
if not thisCorrect:
print("\t\tbad {}, {}".format(point1, point2))
continue
return correct
def verifyClusterMaximality(pointsList, neighborMap, minNeighbors, cluster):
correct = True
for point1 in cluster:
for point2 in pointsList:
if point1 != point2:
if isDensityReachable(neighborMap, minNeighbors, point1, point2):
thisCorrect = point2 in cluster
correct &= thisCorrect
if not thisCorrect:
print("\t\tbad {}, {}".format(point1, point2))
return correct
def verifyClusterConnectivity(neighborMap, minNeighbors, cluster):
correct = True
clusterPointsList = list(cluster)
for i, point1 in enumerate(clusterPointsList):
for point2 in clusterPointsList[i+1:]:
thisCorrect = areDensityConnected(neighborMap, minNeighbors, point1, point2)
correct &= thisCorrect
if not thisCorrect:
print("\t\tbad {}, {}".format(point1, point2))
return correct
def isDensityReachable(neighborMap, minNeighbors, fromPoint, point):
assert(fromPoint != point)
queue = collections.deque()
if len(neighborMap[fromPoint]) >= minNeighbors:
queue.append(fromPoint)
seen = {fromPoint}
#breadth first search all reachable points
while len(queue) > 0:
neighbors = neighborMap[queue.popleft()]
if len(neighbors) >= minNeighbors:
if point in neighbors:
return True
queue.extend({neighbor for neighbor in neighbors if neighbor not in seen})
seen |= neighbors
return False
def areDensityConnected(neighborMap, minNeighbors, point1, point2):
assert(point1 != point2)
if point2 in neighborMap[point1]:
if len(neighborMap[point1]) >= minNeighbors or len(neighborMap[point2]) >= minNeighbors:
return True
#look for a point from which both point1 and point2 are reachable
for point in neighborMap[point1]:
if point is not point2 and isDensityReachable(neighborMap, minNeighbors, point, point1) and isDensityReachable(neighborMap, minNeighbors, point, point2):
return True
return False
def getMultipleClusterPoints(points, clusters):
clusterMap = collections.defaultdict(list)
multipleClusterPoints = []
for cluster in clusters:
for point in cluster:
clusterMap[point].append(cluster)
for point in points:
if len(clusterMap[point]) > 1:
multipleClusterPoints.append(point)
print(point)
return multipleClusterPoints
def printClusters(clusters):
for i, cluster in enumerate(clusters):
print("{}/{}: {}".foramt(i+1, len(clusters), cluster))
print()
def computeDBscanClusters(isolates, cfg):
print("clustering for a subset of size {}...".format(cfg.isolateSubsetSize))
correctNeighbors = fullsearch.getNeighborsMap(isolates, cfg)
precomputedSearcher = fullsearch.PrecomputedIndex(correctNeighbors)
return dbscan(precomputedSearcher, cfg.radii, cfg.minNeighbors)
def getDBscanClustersCacheFileName(cfg):
return "dbscan{}_{}_{}.pickle".format(cfg.isolateSubsetSize, "_".join(str(region.clusterThreshold) for region in cfg.regions), cfg.minNeighbors)
def loadDBscanClustersFromFile(cacheFileName):
with open(cacheFileName, mode='r+b') as cacheFile:
dbscanClusters = pickle.load(cacheFile)
return dbscanClusters
def getDBscanClusters(isolates, cfg):
cacheFileName = getDBscanClustersCacheFileName(cfg)
if os.path.isfile(cacheFileName):
return loadDBscanClustersFromFile(cacheFileName)
else:
return computeDBscanClusters(isolates, cfg)
def indexComparison(cfg, isolates, correctNeighbors):
spatialIndex = spatial.SpatialIndex(isolates, cfg)
# fullSearcher = fullsearch.FullSearchIndex(isolates)
precomputedSearcher = fullsearch.PrecomputedIndex(correctNeighbors)
# spatialGetClusters = dbscan(spatialIndex, cfg.radii, cfg.minNeighbors)
spatialPopClusters = popDBSCAN(spatialIndex, cfg.radii, cfg.minNeighbors)
# fullGetClusters = dbscan(fullSearcher, cfg.radii, cfg.minNeighbors)
# fullPopClusters = popDBSCAN(fullSearcher, cfg.radii, cfg.minNeighbors)
preFullClusters = dbscan(precomputedSearcher, cfg.radii, cfg.minNeighbors)
# spatialGetClusters = {frozenset(cluster) for cluster in spatialGetClusters}
spatialPopClusters = {frozenset(cluster) for cluster in spatialPopClusters}
# fullGetClusters = {frozenset(cluster) for cluster in fullGetClusters}
# fullPopClusters = {frozenset(cluster) for cluster in fullPopClusters}
preFullClusters = {frozenset(cluster) for cluster in preFullClusters}
# printClusters(preFullClusters)
# assert len(getMultipleClusterPoints(isolates, preFullClusters)) == 0
assert spatialPopClusters == preFullClusters
# assert spatialGetClusters == spatialPopClusters
# assert fullGetClusters == fullPopClusters
# assert verifyClusters(isolates, correctNeighbors, cfg.minNeighbors, clusters)
if __name__ == '__main__':
cfg = config.loadConfig()
# isolates = pyroprinting.loadIsolates(cfg)
isolates = pyroprinting.loadIsolatesFromFile("isolates{}.pickle".format(sys.argv[1]))
# correctNeighbors = fullsearch.getNeighborsMap(isolates, cfg)
print(sys.argv)
if sys.argv[2] == "spatial":
index = spatial.SpatialIndex(isolates, cfg)
elif sys.argv[2] == "fullsearch":
index = fullsearch.FullSearchIndex(isolates)
else:
print("bad index. use 'spatial' or 'fullsearch'")
if sys.argv[3] == "dbscan":
dbscan(index, cfg.radii, cfg.minNeighbors)
elif sys.argv[3] == "popDBSCAN":
popDBSCAN(index, cfg.radii, cfg.minNeighbors)
else:
print("bad method. use 'dbscan' or 'popDBSCAN'")