/
explore.py
177 lines (146 loc) · 5.22 KB
/
explore.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
import os, sys, time, subprocess, h5py, argparse, logging, pickle, random
import numpy as np
from os.path import join as oj
import matplotlib.pyplot as plt
import seaborn as sns
from nilearn import datasets
from nilearn import plotting
import cvxpy as cvx
import cvxopt as cvxopt
X = np.loadtxt('data/Caltech_0051461_rois_dosenbach160.1D')[:, :-1]
print(X.shape)
# plot covariance matrix
def plot_cov(covs, tv=False):
sns.clustermap(covs)
if tv:
plt.savefig('figs/covs_tv.pdf')
else:
plt.savefig('figs/covs.pdf')
def plot_idxs(idxs_list):
for idx in idxs_list:
plt.plot([2 * i for i in range(X[:, idx].size)], X[:, idx])
plt.xlabel('Time (s)')
plt.ylabel('Neural response')
plt.savefig('figs/time_course.pdf')
def plot_connectome(covs, tv=False):
# g = np.zeros(shape=(160, 160))
g = covs
dos_coords = datasets.fetch_coords_dosenbach_2010()
dos_coords = dos_coords.rois
dos_coords_table = [[x, y, z] for (x, y, z) in dos_coords] # Reformat the atlas coordinates
f = plt.figure(figsize=(2.3, 3.5)) # 2.2,2.3
if tv:
plotting.plot_connectome(g, dos_coords_table, display_mode='z',
output_file='figs/connectome_tv.pdf', edge_threshold="0.0005%",
annotate=True, figure=f, node_size=18)
else:
plotting.plot_connectome(g, dos_coords_table, display_mode='z',
output_file='figs/connectome.pdf', edge_threshold="0.0005%",
annotate=True, figure=f, node_size=18)
def plot_tv_vary_lambda():
y = X[:, 58]
plt.plot([2 * i for i in range(y.size)], y, label='original')
lambdas = [1, 10, 25]
for vlambda in lambdas:
# vlambda = 50
x = cvx.Variable(y.size)
obj = cvx.Minimize(0.5 * cvx.sum_squares(y - x)
+ vlambda * cvx.tv(x))
prob = cvx.Problem(obj)
# ECOS and SCS solvers fail to converge before
# the iteration limit. Use CVXOPT instead.
prob.solve(solver=cvx.CVXOPT, verbose=True)
if prob.status != cvx.OPTIMAL:
raise Exception("Solver did not converge!")
plt.plot([2 * i for i in range(y.size)], x.value, label='TV $\lambda=$' + str(vlambda))
plt.legend()
plt.xlim([0, 150])
plt.xlabel('Time (s)')
plt.ylabel('Neural response')
plt.savefig('figs/tv_vary.pdf')
plt.show()
def plot_tv_vary_lambda_diff():
y = X[:, 58]
plt.plot([2 * i for i in range(np.diff(y).size)], np.diff(y), label='original')
lambdas = [1, 10, 25]
for vlambda in lambdas:
# vlambda = 50
x = cvx.Variable(y.size)
obj = cvx.Minimize(0.5 * cvx.sum_squares(y - x)
+ vlambda * cvx.tv(x))
prob = cvx.Problem(obj)
# ECOS and SCS solvers fail to converge before
# the iteration limit. Use CVXOPT instead.
prob.solve(solver=cvx.CVXOPT, verbose=True)
if prob.status != cvx.OPTIMAL:
raise Exception("Solver did not converge!")
xv = np.array(x.value).flatten()
plt.plot([2 * i for i in range(np.diff(xv).size)], np.diff(xv), label='TV $\lambda=$' + str(vlambda))
plt.legend()
plt.xlim([0, 150])
plt.xlabel('Time (s)')
plt.ylabel('Neural response 1st derivative')
plt.savefig('figs/tv_vary_diff.pdf')
plt.show()
def tv_X(X):
X_out = np.zeros(shape=(146, 160))
for i in range(160):
print(i)
Y = X[:, i]
vlambda = 10
x = cvx.Variable(Y.size)
obj = cvx.Minimize(0.5 * cvx.sum_squares(Y - x)
+ vlambda * cvx.tv(x))
prob = cvx.Problem(obj)
# ECOS and SCS solvers fail to converge before
# the iteration limit. Use CVXOPT instead.
prob.solve(solver=cvx.CVXOPT, verbose=False)
if prob.status != cvx.OPTIMAL:
raise Exception("Solver did not converge!")
X_out[:, i] = np.array(x.value).ravel()
return X_out
def tv_full(X):
Y = X[:, :10]
vlambda = 10
x = cvx.Variable(Y.shape[0], 10)
obj = cvx.Minimize(0.5 * cvx.sum_squares(Y - x)
+ vlambda * cvx.tv(x))
prob = cvx.Problem(obj)
# ECOS and SCS solvers fail to converge before
# the iteration limit. Use CVXOPT instead.
prob.solve(solver=cvx.CVXOPT, verbose=False)
if prob.status != cvx.OPTIMAL:
raise Exception("Solver did not converge!")
return x.value
# x2 = tv_full(X)
# pickle.dump(x2, open('x2.pkl', 'wb'))
# covs = np.cov(X.transpose())
# plot_cov(covs)
# print('num nonzero', np.sum(covs != 0))
# cflat = covs.flatten()
# ind = np.argpartition(cflat, -10)[-10:]
# print(ind)
# cflat[ind] = -1
# cflat[cflat != -1] = 0
# covs = cflat.reshape(160, 160)
# for r in range(160):
# for c in range(160):
# if covs[r, c] != 0:
# covs[c, r] = -1
# print(np.sum(covs))
# plot_connectome(covs)
# x = tv_X(X)
# pickle.dump(x, open('x.pkl', 'wb'))
# print(x.shape)
# x = pickle.load(open('x.pkl', 'rb'))
# covs = np.cov(x.transpose())
# plt.plot(x[:, 58])
# plt.plot(X[:, 58])
# plt.show()
# plot_cov(covs, tv=True)
# plot_connectome(covs, tv=True)
# individual plots
# plot_idxs([58, 139])
# plot_tv_vary_lambda()
plot_tv_vary_lambda_diff()
# plt.show()