Repository navigation
Expand file tree
/
Copy pathkmeans.py
More file actions
179 lines (134 loc) · 7.99 KB
/
Copy pathkmeans.py
File metadata and controls
179 lines (134 loc) · 7.99 KB
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
import numpy as np
import scipy.sparse
def kmeans(peak_maps, n_states_min=1, n_states_max=12, use_eig=False, threshold=0.5,
random_state=None, n_runs=100, max_err=1e-6, max_iter=100):
"""
Computes microstates using modified k-means clustering algorithm, template
maps are updated either using eigenvector or weighted average method
(both results are nearly identical, second option is notably faster).
:param peak_maps: data from which to compute microstates, should be
average-referenced sensor data, and only data points at GFP peaks
:param n_states_min: minimum number of microstates to be computed
:param n_states_max: maximum number of microstates to be computed
:param use_eig: use eigenvector-based method if True, weighted average otherwise
:param threshold: samples with correlation to a microstate lower than this value,
will be left unassigned
:param random_state: for initialization of k-means run
:param n_runs: number of times to repeat k-means with new initialization
:param max_err: relative error that has to be reached before convergence
:param max_iter: max number of iterations of k-means run
:return: maps_all: dictionary with number of microstates as keys, and
corresponding template maps as values
"""
# dimensions of input data
n_peaks, n_channels = peak_maps.shape
# compute global field potential GFP
gfp_values = peak_maps.std(axis=1)
gfp2 = np.sum(gfp_values ** 2)
# store best maps for each number of microstates
maps_all = {}
# compute template maps with eigenvectors
if use_eig:
# compute template maps for different numbers of states
for n_states in range(n_states_min, n_states_max + 1):
gev_best = 0.0
print(f'Calculating for {n_states} microstates ...')
# run k-means algorithm a certain amount of times
for run in range(n_runs):
# randomly select initial template maps
if isinstance(random_state, int):
init_times = np.random.RandomState(random_state + run).choice(n_peaks, size=n_states, replace=False)
else:
init_times = np.random.RandomState(random_state).choice(n_peaks, size=n_states, replace=False)
maps = peak_maps[init_times]
maps /= np.sqrt(np.sum(maps ** 2, axis=1, keepdims=True)) # normalize template maps
# compute activation between initial template maps and GFP peaks, and determine labels
activation = maps @ peak_maps.T
labels = np.argmax(np.abs(activation), axis=0)
# compute spatial correlation (ignoring polarity) and set to zero where value is below threshold
spat_corr = np.abs(activation[labels, np.arange(n_peaks)]) / (np.sqrt(n_channels) * gfp_values)
spat_corr[spat_corr < threshold] = 0.0
# compute global explained variance
gev = np.sum((gfp_values * spat_corr) ** 2) / gfp2
# initiate parameters for iteration of modified k-means algorithm
n_iter = 0
gev_prev = 0.0001
# apply k-means algorithm until results stop improving or maximum number of iterations is reached
while (np.abs((gev - gev_prev) / gev_prev) > max_err) & (n_iter < max_iter):
# for each label
for label in range(n_states):
# get maps assigned to current label
label_maps = peak_maps[labels == label]
matrix = label_maps.T @ label_maps
# new map as eigenvector corresponding to largest eigenvalue
_, eigenvector = scipy.sparse.linalg.eigsh(matrix, k=1)
maps[label] = eigenvector[:, 0]
# compute activation between template maps and GFP peaks, and determine labels
activation = maps @ peak_maps.T
labels = np.argmax(np.abs(activation), axis=0)
# compute spatial correlation (ignoring polarity) and set to zero where value is below threshold
spat_corr = np.abs(activation[labels, np.arange(n_peaks)]) / (np.sqrt(n_channels) * gfp_values)
spat_corr[spat_corr < threshold] = 0.0
# compute global explained variance
gev_prev = gev
gev = np.sum((gfp_values * spat_corr) ** 2) / gfp2
n_iter += 1
# update best template maps
if gev > gev_best:
gev_best = gev
maps_best = maps
# store best maps for
maps_all[n_states] = maps_best
# compute template maps with weighted average
else:
# compute template maps for different numbers of states
for n_states in range(n_states_min, n_states_max + 1):
gev_best = 0.0
print(f'Calculating for {n_states} microstates ...')
# run k-means algorithm a certain amount of times
for run in range(n_runs):
# randomly select initial template maps
if isinstance(random_state, int):
init_times = np.random.RandomState(random_state + run).choice(n_peaks, size=n_states, replace=False)
else:
init_times = np.random.RandomState(random_state).choice(n_peaks, size=n_states, replace=False)
maps = peak_maps[init_times]
maps /= np.sqrt(np.sum(maps ** 2, axis=1, keepdims=True)) # normalize template maps
# compute activation between initial template maps and GFP peaks, and determine labels
activation = maps @ peak_maps.T
labels = np.argmax(np.abs(activation), axis=0)
# compute spatial correlation (ignoring polarity) and set to zero where value is below threshold
spat_corr = np.abs(activation[labels, np.arange(n_peaks)]) / (np.sqrt(n_channels) * gfp_values)
spat_corr[spat_corr < threshold] = 0.0
# compute global explained variance
gev = np.sum((gfp_values * spat_corr) ** 2) / gfp2
# initiate parameters for iteration of modified k-means algorithm
n_iter = 0
gev_prev = 0.0001
# apply k-means algorithm until results stop improving or maximum number of iterations is reached
while (np.abs((gev - gev_prev) / gev_prev) > max_err) & (n_iter < max_iter):
# create mask and apply to activation
mask = np.zeros(activation.shape, dtype=bool)
mask[labels, np.arange(n_peaks)] = True
mask *= np.tile(spat_corr > threshold, (n_states, 1))
activation[~mask] = 0.0
# create new average maps
maps = activation @ peak_maps
maps /= np.sqrt(np.sum(maps ** 2, axis=1, keepdims=True))
# compute activation between template maps and GFP peaks, and determine labels
activation = maps @ peak_maps.T
labels = np.argmax(np.abs(activation), axis=0)
# compute spatial correlation (ignoring polarity) and set to zero where value is below threshold
spat_corr = np.abs(activation[labels, np.arange(n_peaks)]) / (np.sqrt(n_channels) * gfp_values)
spat_corr[spat_corr < threshold] = 0.0
# compute global explained variance
gev_prev = gev
gev = np.sum((gfp_values * spat_corr) ** 2) / gfp2
n_iter += 1
# update best template maps
if gev > gev_best:
gev_best = gev
maps_best = maps
# store best maps for
maps_all[n_states] = maps_best
return maps_all