@@ -43,11 +43,13 @@ def spectral_embedding_sparse(adjacency, k_max=14, mode='amg', take_first=True):
43
43
print 'amg'
44
44
sh = adjacency .shape [0 ]
45
45
adjacency = adjacency .copy ()
46
- diag = sparse .coo_matrix ((diag_weights .ravel (), (range (sh ), range (sh ))))
46
+ #diag = sparse.coo_matrix((diag_weights.ravel(), (range(sh), range(sh))))
47
+ diag = sparse .eye (sh , sh )
47
48
adjacency = - adjacency + diag
48
49
ml = smoothed_aggregation_solver (adjacency .tocsr ())
49
50
X = scipy .rand (adjacency .shape [0 ], k_max )
50
- X [:, 0 ] = 1. / np .sqrt (adjacency .shape [0 ])
51
+ #X[:, 0] = 1. / np.sqrt(adjacency.shape[0])
52
+ X [:, 0 ] = 1. / dd .ravel ()
51
53
M = ml .aspreconditioner ()
52
54
lambdas , diffusion_map = lobpcg (adjacency , X , M = M , tol = 1.e-12 , largest = False )
53
55
print lambdas
@@ -145,14 +147,32 @@ def q_score(adjacency, labels):
145
147
#q -= (weights[label == labels].sum()/total_weights)**2
146
148
return 2 * q
147
149
150
+ def n_cut (adjacency , labels ):
151
+ """ Returns the Q score of a clustering.
152
+ """
153
+ q = 0
154
+ """
155
+ if isinstance(adjacency, sparse.csc.csc_matrix):
156
+ adjacency = np.array(adjacency.todense())
157
+ """
158
+ weights = adjacency
159
+ total_weights = 0.5 * weights .sum ()
160
+ for label in np .unique (labels ):
161
+ inds = np .nonzero (labels == label )[0 ]
162
+ a = (weights [inds ][:, inds ]).sum ()
163
+ b = weights [inds ].sum ()
164
+ q += (b - a )/ b
165
+ return - q
166
+
148
167
149
168
def best_k_means (k , maps , adjacency , n_bst = 10 ):
150
169
from nipy .neurospin .clustering .clustering import _kmeans
151
170
best_score = - np .inf
152
171
for _ in range (n_bst ):
153
172
print "doing kmeans"
154
173
_ , labels , _ = _kmeans (maps , nbclusters = k )
155
- score = q_score (adjacency , labels )
174
+ #score = q_score(adjacency, labels)
175
+ score = n_cut (adjacency , labels )
156
176
if score > best_score :
157
177
best_score = score
158
178
best_labels = labels
@@ -196,12 +216,12 @@ def communities_clustering_sparse(adjacency, k_best=None, k_min=2, k_max=8, n_bs
196
216
this_maps = maps [:k_best - 1 ].T .copy ()
197
217
res , scores = best_k_means (k_best , this_maps , adjacency ,
198
218
n_bst = 4 * n_bst )
199
- print 'Final : k=%i, score=%s' % (k_best , score )
219
+ print 'Final : k=%i, score=%s' % (k_best , scores )
200
220
return res , scores
201
221
202
222
def separate_in_regions (data , mask , k_best = None , k_min = 2 , k_max = 8 , \
203
- center = None , only_connex = True , \
204
- take_first = True , beta = 10 , mode = 'bf' ):
223
+ center = None , only_connex = True , n_times = 4 , \
224
+ take_first = True , beta = 10 , mode = 'bf' ):
205
225
labs , nb_labels = ndimage .label (mask )
206
226
if nb_labels > 1 :
207
227
if center is None :
@@ -212,10 +232,10 @@ def separate_in_regions(data, mask, k_best=None, k_min=2, k_max=8, \
212
232
mask = labs == ind_max
213
233
lap , w = _build_laplacian (np .atleast_3d (data ), mask = np .atleast_3d (mask ), \
214
234
normed = True , beta = beta )
215
- return lap , w
216
235
print lap .shape
217
236
res , scores = communities_clustering_sparse (lap , k_best = k_best , \
218
- k_min = k_min , k_max = k_max , take_first = take_first , mode = mode )
237
+ k_min = k_min , k_max = k_max , n_bst = n_times , \
238
+ take_first = take_first , mode = mode )
219
239
if not only_connex :
220
240
if k_best == None :
221
241
labels = dict ()
0 commit comments