@@ -188,48 +188,50 @@ def cluster(self, distances, max_dis, min_dis, max_id, density_threshold, distan
188
188
assert not (dc is not None and auto_select_dc )
189
189
rho , dc = self .local_density (distances , max_dis , min_dis , max_id , dc = dc , auto_select_dc = auto_select_dc )
190
190
delta , nneigh = min_distance (max_id , max_dis , distances , rho )
191
+ nneigh = nneigh .astype (int ) # nneigh will be used as indexing so it should be int
191
192
logger .info ("PROGRESS: start cluster" )
192
- cluster , ccenter = {}, {} # cl/icl in cluster_dp.m
193
193
194
- for idx , (ldensity , mdistance , nneigh_item ) in enumerate (zip (rho , delta , nneigh )):
195
- if idx == 0 :
196
- continue
197
- if ldensity >= density_threshold and mdistance >= distance_threshold :
198
- ccenter [idx ] = idx
199
- cluster [idx ] = idx
200
- else :
201
- cluster [idx ] = - 1
194
+ # cl/icl in cluster_dp.m
195
+ cluster = np .zeros (max_id + 1 , int )
196
+ ccenter = np .zeros (max_id + 1 , int )
197
+
198
+ index = np .logical_and (rho >= density_threshold , delta >= distance_threshold )
199
+ index_val = np .where (index )[0 ]
200
+ ccenter [index ] = index_val
201
+ cluster [index ] = index_val
202
+ cluster [np .invert (index )] = - 1
202
203
203
204
# assignation
204
205
ordrho = np .argsort (- rho )
205
206
for i in range (ordrho .shape [0 ] - 1 ):
206
- if ordrho [i ] == 0 : continue
207
+ if ordrho [i ] == 0 :
208
+ continue
207
209
if cluster [ordrho [i ]] == - 1 :
208
210
cluster [ordrho [i ]] = cluster [nneigh [ordrho [i ]]]
209
211
if i % (max_id / 10 ) == 0 :
210
- logger .info ("PROGRESS: at index #%i" % ( i ) )
212
+ logger .info ("PROGRESS: assignation at #%i" % i )
211
213
212
214
# halo
213
- halo , bord_rho = {}, {}
214
- for i in range (1 , ordrho .shape [0 ]):
215
- halo [i ] = cluster [i ]
215
+ halo = np .zeros (max_id + 1 )
216
+ bord_rho = np .zeros (max_id + 1 )
217
+ halo [1 :] = cluster [1 :]
218
+
216
219
if len (ccenter ) > 0 :
217
- for idx in ccenter .keys ():
218
- bord_rho [idx ] = 0.0
219
220
for i in range (1 , rho .shape [0 ] - 1 ):
220
221
for j in range (i + 1 , rho .shape [0 ]):
221
- if cluster [i ] != cluster [j ] and distances [i - 1 , j - 1 ] <= dc :
222
+ if cluster [i ] != cluster [j ] and distances [i - 1 , j - 1 ] <= dc :
222
223
rho_aver = (rho [i ] + rho [j ]) / 2.0
223
224
if rho_aver > bord_rho [cluster [i ]]:
224
225
bord_rho [cluster [i ]] = rho_aver
225
226
if rho_aver > bord_rho [cluster [j ]]:
226
227
bord_rho [cluster [j ]] = rho_aver
228
+ if i % (max_id / 10 ) == 0 :
229
+ logger .info ("PROGRESS: halo at #%i" % i )
227
230
for i in range (1 , rho .shape [0 ]):
228
231
if rho [i ] < bord_rho [cluster [i ]]:
229
232
halo [i ] = 0
230
- for i in range (1 , rho .shape [0 ]):
231
- if halo [i ] == 0 :
232
- cluster [i ] = - 1
233
+
234
+ cluster [halo == 0 ] = - 1
233
235
234
236
self .cluster , self .ccenter = cluster , ccenter
235
237
self .distances = distances
0 commit comments