-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasureGLMfits_Mease.m
293 lines (212 loc) · 8.76 KB
/
measureGLMfits_Mease.m
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
rng(20190322);
load('Results/Mease/MeaseSims.mat','downsampleRate','sigMuRatio','StimLevels','sigMuRatio','stim_mu','xs', 'frFunctions')
load('Results/Mease/MeaseGLMs.mat','stimBasis','spkHistBases');
fr0 = squeeze(frFunctions(xs==0,:,:));
DEBUG = false;
addpath Utils/;
addpath GLMtools;
addpath kGLM;
addGLMPaths;
if(DEBUG)
TT_start = 1*1000;
TT_post = 1*1000;
TT_sim = 1*1000;
TT = TT_sim + TT_start + TT_post;
alphas = [0 1:0.5:2];
else
TT_start = 4*1000;
TT_post = 4*1000;
TT_sim = 32*1000;
TT = TT_sim + TT_start + TT_post;
%alphas = [0 1:0.5:5 10];
alphas = [0 1:1:5 10];
end
NA = length(alphas);
NH = length(spkHistBases);
poolobj = gcp('nocreate'); delete(poolobj);
parpool(NA);
smooth_sig = 5;
ff_smooth = exp(-(-500:500).^2./(2*smooth_sig^2));
ff_smooth = ff_smooth(:)./sum(ff_smooth);
NS = 1e3;
Gs = 600:100:2000;
dt = 1e-3;
T_idx = TT_start+(1:TT_sim);
TT2 = length(T_idx);
X2 = randn(TT,1);
y2 = zeros(length(X2)*downsampleRate,1);
for ii = 1:length(X2)
y2((1:downsampleRate) + (ii-1)*downsampleRate) = X2(ii);
end
X2_c = conv2(X2,stimBasis);
X2_c = X2_c(1:TT,:);
psthSmooth_hh = nan(TT,4,15,15);
%psth_glm = nan(TT,4,15,15,5,NH,NA);
N_Bspks = zeros(NH,1);
for H = 1:NH
N_Bspks(H) = size(spkHistBases{H},2);
end
glm_k_stims = nan(size(stimBasis,2),15,15,4+1,NH,NA);
glm_h_spks = nan(max(N_Bspks),15,15,4+1,NH,NA);
glm_bs = nan(15,15,4+1,NH,NA);
for alphaCtr = 1:NA
alpha = alphas(alphaCtr);
if(alpha > 0)
saveDir = sprintf('Results/Mease/GLMs_p%d/',floor(alpha*10));
else
saveDir = sprintf('Results/Mease/');
end
if(~isfolder(saveDir))
mkdir(saveDir);
end
load(sprintf("%sMeaseGLMs.mat",saveDir),'glm_h_spk','glm_k_stim','glm_b');
glm_h_spks(:,:,:,:,:,alphaCtr) = glm_h_spk;
glm_k_stims(:,:,:,:,:,alphaCtr) = glm_k_stim;
glm_bs(:,:,:,:,alphaCtr) = glm_b;
end
freqCutoff = 64;
[wcoh,wcs,pp] = wcoherence(randn(TT,1),randn(TT,1),1e3);
freqs = pp(pp <= freqCutoff);
c_test = nan(length(freqs),4,15,15,5,NH,NA);
p_test = nan(length(freqs),4,15,15,5,NH,NA);
p2_test = nan(length(freqs),4,15,15,5,NH,NA);
p3_test = nan(length(freqs),4,15,15,5,NH,NA);
%%
load('Results/Mease/glmFitMetrics.mat');
if(exist('Results/Mease/glmFitMetrics_part.mat','file'))
load('Results/Mease/glmFitMetrics_part.mat')
nn_init = nn;
kk_init = kk;
hh_init = hh+1;
else
kk_init = 1;
nn_init = 1;
hh_init = 1;
end
%%
for nn = nn_init:15
fprintf('nn = %d\n',nn);
%%
for kk = kk_init:15
fprintf(' kk = %d\n',kk);
kk_clock = tic;
if(fr0(nn,kk) > 1)
continue;
end
%% simulate HH model, all stim levels
for ii = 1:4
psthSmooth_hh(:,ii,nn,kk) = conv(psth_hh(:,ii,nn,kk),ff_smooth,'same');
end
psthSmooth_hh_c = psthSmooth_hh(:,:,nn,kk);
psth_hh_c = psth_hh(:,:,nn,kk);
startSpikes = psth_hh_c(1:TT_start,:);
ps_hh = psthSmooth_hh_c(T_idx,:);
Fs = 1e3;
cwtx_hh = wcoherence_getWaveletTransform(psth_hh(:,1),Fs);
for ii = 2:4
cwtx_hh(ii) = wcoherence_getWaveletTransform(psth_hh(:,ii),Fs);
end
%% for each type of GLM
for hh = hh_init:NH
if(isnan(glm_bs(nn,kk,1,hh,1)))
continue;
end
fprintf(' hh = %d\n',hh);
hh_clock = tic;
spkHistBasis_c = spkHistBases{hh};
%%
glm_k_cs = squeeze(glm_k_stims(:,nn,kk,:,hh,:));
glm_h_cs = squeeze(glm_h_spks(1:N_Bspks(hh),nn,kk,:,hh,:));
glm_b_cs = squeeze(glm_bs(nn,kk,:,hh,:));
parfor aa = 1:NA
devNum = mod(aa,4)+1;
gpuDevice(devNum);
reset(gpuDevice(devNum));
startSpikes_c = startSpikes;
ps_hh_c =ps_hh;
cwtx_hh_c = cwtx_hh;
StimLevels_c = StimLevels;
devNum = mod(aa,4);
alpha = alphas(aa);
%% simulate GLMs (all 5 x 10)
% simulate on 60s of HH data
% use the first 5s to initialize GLM spk hist!
glm_k_c = glm_k_cs(:,:,aa);
glm_h_c = glm_h_cs(:,:,aa);
glm_b_c = glm_b_cs(:,aa)';
for tt = 1:5
%% sim glm
GLM = struct;
GLM.spkHistBasisVectors = spkHistBasis_c;
GLM.dt = dt;
GLM.stimBasisVectors = stimBasis;
if(alpha <= 0)
GLM.NLtype = 1;
elseif(alpha == 1)
GLM.NLtype = 2;
else
GLM.NLtype = 3;
GLM.NLpow = alpha;
end
GLM.k_stim = glm_k_c(:,tt);
GLM.h_spk = glm_h_c(:,tt);
GLM.b = glm_b_c(tt);
%%
%tic;
stim = (X2_c*GLM.k_stim)*StimLevels_c(:)'+GLM.b;
h_s = GLM.spkHistBasisVectors*GLM.h_spk;
psth_glm = kcSimGLMs(h_s,GLM.dt, alpha, stim',startSpikes_c', NS, devNum);
%[~,psth_glm] = simGLMs_cpu(h_s,GLM.dt,alpha,NS,stim',startSpikes');
psth_glm = psth_glm';
%toc
%%
% psthSmooth_glm = zeros(size(psth_glm));
% for ii = 1:4
% psthSmooth_glm(:,ii) = conv(psth_glm(:,ii),ff_smooth,'same');
% end
psthSmooth_glm = conv2(psth_glm,ff_smooth,'same');
%% compare PSTH R^2
ps_glm = psthSmooth_glm(T_idx,:);
r2s = nan(4,1);
rs = nan(4,1);
for ii = 1:4
[r2s(ii),rs(ii)] = getR2(ps_hh_c(:,ii),ps_glm(:,ii));
end
R2_test(:,nn,kk,tt,hh,aa) = r2s;
R_test(:,nn,kk,tt,hh,aa) = rs;
%% compare coherence
mcohs = nan(length(freqs),4);
mcss = nan(length(freqs),4);
mcs2s = nan(length(freqs),4);
mcs3s = nan(length(freqs),4);
for ii = 1:4
Fs = 1e3;
% [Cxy,~] = mscohere(ps_hh(:,ii),ps_glm(:,ii),16*Fs,12*Fs,freqs(:),Fs);
% [Pxy,F] = cpsd( ps_hh(:,ii),ps_glm(:,ii),16*Fs,12*Fs,freqs(:),Fs);
[wcoh,wcs,pp] = wcoherence_alt(cwtx_hh_c(ii),psth_glm(:,ii),Fs);
mcohs(:,ii) = mean(real(wcoh(pp<=freqCutoff,T_idx)),2);
mcss(:,ii) = mean( (angle(wcs(pp<=freqCutoff,T_idx))),2);
mcs2s(:,ii) = mean(abs(angle(wcs(pp<=freqCutoff,T_idx))),2);
mcs3s(:,ii) = mean(abs( (wcs(pp<=freqCutoff,T_idx))),2);
end
c_test(:,:,nn,kk,tt,hh,aa) = mcohs;%Cxy(1:size(c_test,1));
p_test(:,:,nn,kk,tt,hh,aa) = mcss;%angle(Pxy(1:size(p_test,1)));
p2_test(:,:,nn,kk,tt,hh,aa) = mcs2s;
p3_test(:,:,nn,kk,tt,hh,aa) = mcs3s;
end
end
if(~DEBUG && (mod(hh,8) == 0 || hh == NH))
save('Results/Mease/glmFitMetrics_part.mat','pseudo_R2_test','nn','kk','hh','alphas','X2','T_idx','c_test','p_test','p2_test','p3_test','ll_test','R_test','R2_test','freqs','psth_hh','smooth_sig','bitsPerSpike_test','-v7.3');
end
hh_time = toc(hh_clock);
fprintf(' time for one hh set: %.2f\n',hh_time);
end
hh_init = 1;
kk_time = toc(kk_clock);
fprintf(' time for one kk set: %.2f\n',kk_time);
end
kk_init = 1;
end
if(~DEBUG)
save('Results/Mease/glmFitMetrics2.mat','alphas','X2','pseudo_R2_test','T_idx','c_test','p_test','p2_test','p3_test','ll_test','R_test','R2_test','freqs','psth_hh','smooth_sig','bitsPerSpike_test','-v7.3');
end