forked from juhuntenburg/myelinconnect
/
clustering.py
140 lines (120 loc) · 5.2 KB
/
clustering.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
def recort(n_vertices, data, cortex, increase):
import numpy as np
d = np.zeros(n_vertices)
count = 0
for i in cortex:
d[i] = data[count] + increase
count = count +1
return d
def embedding(upper_corr, full_shape, mask, n_components):
import numpy as np
from mapalign import embed
# reconstruct full matrix
print '...full matrix'
full_corr = np.zeros(tuple(full_shape))
full_corr[np.triu_indices_from(full_corr, k=1)] = np.nan_to_num(upper_corr)
full_corr += full_corr.T
all_vertex=range(full_corr.shape[0])
# apply mask
print '...mask'
masked_corr = np.delete(full_corr, mask, 0)
del full_corr
masked_corr = np.delete(masked_corr, mask, 1)
cortex=np.delete(all_vertex, mask)
# run actual embedding
print '...embed'
K = (masked_corr + 1) / 2.
del masked_corr
K[np.where(np.eye(K.shape[0])==1)]=1.0
#v = np.sqrt(np.sum(K, axis=1))
#A = K/(v[:, None] * v[None, :])
#del K, v
#A = np.squeeze(A * [A > 0])
#embedding_results = runEmbed(A, n_components_embedding)
embedding_results, embedding_dict = embed.compute_diffusion_map(K, n_components=n_components, overwrite=True)
# reconstruct masked vertices as zeros
embedding_recort=np.zeros((len(all_vertex),embedding_results.shape[1]))
for e in range(embedding_results.shape[1]):
embedding_recort[:,e]=recort(len(all_vertex), embedding_results[:,e], cortex, 0)
return embedding_recort, embedding_dict
def t1embedding(upper_corr, full_shape, mask, n_components):
import numpy as np
from mapalign import embed
# reconstruct full matrix
print '...full matrix'
full_corr = np.zeros(tuple(full_shape))
full_corr[np.triu_indices_from(full_corr, k=1)] = np.nan_to_num(upper_corr)
full_corr += full_corr.T
all_vertex=range(full_corr.shape[0])
# apply mask
print '...mask'
masked_corr = np.delete(full_corr, mask, 0)
del full_corr
masked_corr = np.delete(masked_corr, mask, 1)
cortex=np.delete(all_vertex, mask)
# run actual embedding
print '...embed'
K=1-(masked_corr/masked_corr.max())
#K = (masked_corr + 1) / 2.
del masked_corr
K[np.where(np.eye(K.shape[0])==1)]=1.0
#v = np.sqrt(np.sum(K, axis=1))
#A = K/(v[:, None] * v[None, :])
#del K, v
#A = np.squeeze(A * [A > 0])
#embedding_results = runEmbed(A, n_components_embedding)
embedding_results, embedding_dict = embed.compute_diffusion_map(K, n_components=n_components, overwrite=True)
# reconstruct masked vertices as zeros
embedding_recort=np.zeros((len(all_vertex),embedding_results.shape[1]))
for e in range(embedding_results.shape[1]):
embedding_recort[:,e]=recort(len(all_vertex), embedding_results[:,e], cortex, 0)
return embedding_recort, embedding_dict
def kmeans(embedding,n_components, mask):
import numpy as np
from sklearn.cluster import KMeans
all_vertex=range(embedding.shape[0])
masked_embedding = np.delete(embedding, mask, 0)
cortex=np.delete(all_vertex, mask)
est = KMeans(n_clusters=n_components, n_jobs=-2, init='k-means++', n_init=300)
est.fit_transform(masked_embedding)
labels = est.labels_
kmeans_results = labels.astype(np.float)
kmeans_recort = recort(len(all_vertex), kmeans_results, cortex, 1)
return kmeans_recort
def subcluster(kmeans, triangles):
import numpy as np
# make a dictionary for kmeans clusters and subclusters
subclust={}
# loop through all kmeans clusters (and the mask cluster with value zero)
for c in range(int(kmeans.max()+1)):
# add dic entry
subclust['k'+str(c)]=[]
# extract all nodes of the cluster
clust=list(np.where(kmeans==c)[0])
# while not all nodes have been removed from the cluster
while len(clust)>0:
#start at currently first node in cluster
neighbours=[clust[0]]
# go through growing list of neighbours in the subcluster
for i in neighbours:
#find all triangles that contain current
for j in range(triangles.shape[0]):
if i in triangles[j]:
# add all nodes of in this triangle to the neighbours list
n=list(triangles[j])
# but only if they aren't already in the list and if they are in clust
[neighbours.append(x) for x in n if x in clust and x not in neighbours]
# remove assigned nodes from the cluster list
[clust.remove(x) for x in n if x in clust]
# when no new neighbours can be found, add subcluster to subcluster list
# and start new subcluster from first node in remaining cluster list
subclust['k'+str(c)].append(neighbours)
# make array with subclusters
subclust_full = np.zeros((kmeans.shape[0], int(kmeans.max()+1)))
count = 0
for c in range(len(subclust.keys())):
for i in range(len(subclust['k'+str(c)])):
for j in subclust['k'+str(c)][i]:
subclust_full[j][c] = i+1
#subclust_arr=np.hstack((np.reshape(kmeans, (kmeans.shape[0],1)), subclust_full))
return subclust_full