-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_RF.m
420 lines (366 loc) · 13.8 KB
/
map_RF.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
function map_RF(time_stamp, basic_cell, pd_basis, decomp, varargin)
% Use spots to map out RF.
% Inputs:
% basic_cell, pd_basis: basis parameters
% decomp: decomposition methods: {'svd';'false'; 'rpca';
% 'grammian'; 'global'; 'rpca_Phi'};
% * Choose appropriate stimulus contrast.
% * For rpca, choose the gamma; for global, choose the c.
% Mengchen Zhu
%% Parse inputs
% Default parameter values
options = struct('lca_pars', 'os1_s', 'whitened', 'whitened', ...
'stim_contrast', 1, 'SVD_terms', NaN, 'c_global', NaN);
options = parse_inputs(options, varargin);
%% Load RPCA decomposition
dir_decompose = fullfile('data');
data_decompose = fullfile(dir_decompose, ['decompose_', time_stamp, ...
'.mat']);
load(data_decompose);
%% Local parameters
% Local control paramters
pd_mapRF = options;
% Local hidden control paramters
ph_mapRF = struct;
% Load lca parameters
[pd_mapRF ph_mapRF] = load_lca_par(pd_mapRF, ph_mapRF, options.lca_pars);
% Copy over double or normal basis size
pd_mapRF.basis_size = pd_basis.basis_size;
% Load stimulus;
if strcmp(pd_mapRF.whitened, 'whitened')
load(fullfile('data','whitened_spot_stim.mat'));
end
pd_mapRF.decomp = decomp;
pd_mapRF.approx = pd_decompose.approx;
switch pd_mapRF.decomp
case {'rpca', 'arpca'}
% Load the RPCA decomposition.
load(pd_decompose.rpca_file);
pd_mapRF.gamma_rpca = pd_decompose.gamma_rpca;
case 'rpca_Phi'
pd_mapRF.gamma_rpca = 0.2;
otherwise
pd_mapRF.gamma_rpca = NaN;
end
switch pd_mapRF.approx
case 'approx_RPCA'
pd_mapRF.S_threshold = pd_decompose.S_threshold;
end
% Display LCA reconstruction?
ph_mapRF.display_lca = 0;
if ph_mapRF.display_lca
ph_mapRF.h_lca = figure;
end
% First check if the directory already exists.
dir_mapRF = fullfile('data', 'mapRF');
if (~exist(dir_mapRF,'dir'))
mkdir(dir_mapRF);
end
% the data file with the time stamp
data_mapRF = fullfile(dir_mapRF, ['mapRF_', time_stamp, '.mat']);
%% Calculate decomposition
switch pd_mapRF.decomp
case 'svd'
% SVD; note here to make the V here the equivalent of V in the
% report111009, we switched the sequence of output.
[V, Sigma, U] = svd(basic_cell.basis', 0);
% Decompose matrices into positive and negative components
Phi_p = basic_cell.basis;
Phi_p(Phi_p < 0) = 0 ;
Phi_n = basic_cell.basis;
Phi_n(Phi_n > 0) = 0 ;
V_p = V;
V_p(V_p < 0) = 0;
V_n = V;
V_n(V_n > 0) = 0;
case {'rpca', 'arpca'}
if strcmp(pd_mapRF.approx, 'approx_RPCA')
diag_S_L = diag(S_L);
S(abs(S)<pd_mapRF.S_threshold) = 0;
S_L = diag([diag_S_L(1:pd_mapRF.SVD_terms); zeros(size(S_L,1) ...
- pd_mapRF .SVD_terms,1)]);
approx_matrix = U_L * S_L * V_L' + S - eye(size(S_L));
end
% The projection to the inhibitory subpopulation of the low rank interneurons.
V_L_p = V_L;
V_L_p(V_L_p < 0) = 0;
% The projection to the inhibitory subpopulation of the sparse
% interneurons.
S_p = S;
S_p(S_p < 0) = 0;
switch pd_mapRF.decomp
case 'arpca'
% In the case of arpca, the sparse connections to the
% interneurons are just identity matrix, with nonzero rows
% matching the S matrix.
temp_zeros = zeros(size(S,1),1);
nonzero_idx = find(all(S_p == 0,2) == 0);
temp_zeros(nonzero_idx) = 1;
S_p = diag(temp_zeros);
end
case 'rpca_Phi'
[L_temp S_temp iter] = inexact_alm_rpca(basic_cell.basis', ...
pd_mapRF.gamma_rpca);
L = L_temp';
S = S_temp';
[U_L, S_L, V_L] = svd(L, 0);
% The projection to the inhibitory subpopulation of the low rank interneurons.
V_L_p = V_L;
V_L_p(V_L_p < 0) = 0;
% The projection to the inhibitory subpopulation of the sparse
% interneurons.
S_p = S;
S_p(S_p < 0) = 0;
end
% $$$ mapRF = zeros(size(basis_cell.basis, 2), size(basic_cell.basis, 1));
%% Map the RF
% Calculate response for each of the spot stimulus
for stim_idx = 1:size(basic_cell.basis, 1)
% $$$ for stim_idx = 1:size(basic_cell.basis, 1)
disp(strcat('Mapping RF for stimulus', num2str(stim_idx)));
% Map out the positive part of the RF using negative stimuli.
img_p = pd_mapRF.stim_contrast * ...
reshape(squeeze(stimulus(stim_idx,:,:)), [], 1);
if strcmp(pd_mapRF.approx, 'approx_RPCA')
mapRF_p(:, stim_idx) = lca(img_p, basic_cell.basis, pd_mapRF, ...
ph_mapRF, 'approx', approx_matrix);
else
mapRF_p(:, stim_idx) = lca(img_p, basic_cell.basis, pd_mapRF, ...
ph_mapRF);
end
switch pd_mapRF.decomp
case 'svd'
% Calculate the "response" of the two
% populations of inhibitory cells.
inhib_response_1_p(:, stim_idx) = Sigma^2*(-V_n') * ...
mapRF_p(:, stim_idx);
inhib_response_2_p(:, stim_idx) = Sigma^2 * V_p' * ...
mapRF_p(:, stim_idx);
case {'rpca', 'arpca'}
% Here we only look at one of the two inhibitory
% populations? (111107)
rpca_inhib_response_p(:, stim_idx) = S_L * V_L_p' * mapRF_p(:, ...
stim_idx);
rpca_sparse_p(:, stim_idx) = S_p * mapRF_p(:, stim_idx);
case 'grammian'
grammian_inhib_response_p(:, stim_idx) = basic_cell.basis * ...
mapRF_p(:, stim_idx);
case 'global'
global_inhib_response_p(stim_idx) = sum(mapRF_p(:, stim_idx), ...
1);
case 'rpca_Phi'
rpca_inhib_response_p(:, stim_idx) = S_L * V_L_p' * mapRF_p(:, ...
stim_idx);
rpca_sparse_p(:, stim_idx) = S_p * mapRF_p(:, stim_idx);
end
% Map out the negative part of the RF using negative stimuli.
img_n = - pd_mapRF.stim_contrast * ...
reshape(squeeze(stimulus(stim_idx,:,:)), [], 1);
mapRF_n(:, stim_idx) = lca(img_n, basic_cell.basis, pd_mapRF, ...
ph_mapRF);
switch pd_mapRF.decomp
case 'svd'
% Calculate the "response" of the two
% populations of inhibitory cells.
inhib_response_1_n(:, stim_idx) = Sigma^2*(-V_n') * ...
mapRF_n(:, stim_idx);
inhib_response_2_n(:, stim_idx) = Sigma^2 * V_p' * ...
mapRF_n(:, stim_idx);
case {'rpca', 'arpca'}
rpca_inhib_response_n(:, stim_idx) = S_L * V_L_p' * mapRF_n(:, ...
stim_idx);
rpca_sparse_n(:, stim_idx) = S_p * mapRF_n(:, stim_idx);
case 'grammian'
grammian_inhib_response_n(:, stim_idx) = basic_cell.basis * ...
mapRF_n(:, stim_idx);
case 'global'
global_inhib_response_n(stim_idx) = sum(mapRF_n(:, stim_idx), 1);
case 'rpca_Phi'
rpca_inhib_response_n(:, stim_idx) = S_L * V_L_p' * mapRF_n(:, ...
stim_idx);
rpca_sparse_n(:, stim_idx) = S_p * mapRF_n(:, stim_idx);
end
end
% Save the RF. Extract the response of bases in the range
% basis_range.
mapRF = mapRF_p - mapRF_n;
% $$$ pd_mapRF.completed = true;
% $$$ % New parameter values
% $$$ par_values = [{time_stamp}, struct2cell(pd_mapRF)'];
% $$$ log_cell(end, :) = par_values;
% $$$ cell2csv(fullfile(dir_mapRF, log_file), log_cell);
switch pd_mapRF.decomp
case 'svd'
inhib_response_1 = inhib_response_1_p - inhib_response_1_n;
inhib_response_2 = inhib_response_2_p - inhib_response_2_n;
save(data_mapRF, 'mapRF', 'inhib_response_1', 'inhib_response_2', ...
'pd_mapRF', 'ph_mapRF');
case {'rpca', 'rpca_Phi', 'arpca'}
rpca_inhib_response = rpca_inhib_response_p - ...
rpca_inhib_response_n;
rpca_sparse = rpca_sparse_n - rpca_sparse_p;
save(data_mapRF, 'mapRF', 'rpca_inhib_response', 'pd_mapRF', 'ph_mapRF', ...
'rpca_sparse');
case 'grammian'
grammian_inhib_response = grammian_inhib_response_p - ...
grammian_inhib_response_n;
save(data_mapRF, 'mapRF', 'grammian_inhib_response', 'pd_mapRF', 'ph_mapRF');
case 'global'
global_inhib_response = global_inhib_response_p - global_inhib_response_n;
save(data_mapRF, 'mapRF', 'global_inhib_response', 'pd_mapRF', 'ph_mapRF');
otherwise
save(data_mapRF, 'mapRF', 'pd_mapRF', 'ph_mapRF');
end
%% Plot the RF
% First check if the directory already exists.
dir_mapRF_plot = fullfile('plots', 'mapRF');
if (~exist(dir_mapRF_plot,'dir'))
mkdir(dir_mapRF_plot);
end
% Make subdirectory with time stamp
sub_dir = fullfile(dir_mapRF_plot, time_stamp);
if (~exist(sub_dir,'dir'))
mkdir(sub_dir);
end
img_sz = sqrt(size(basic_cell.basis, 1)); %The width and height of
%each basis
if strcmp(pd_mapRF.approx, 'approx_RPCA')
basis_range = 1:pd_mapRF.SVD_terms;
else
basis_range = 1:100;
end
disp_sz = [10 length(basis_range)/10];
% subset of FF RFs
h = figure;
img = basis2img2(mapRF(basis_range, :)', [img_sz img_sz], disp_sz);
imagesc(img)
colormap gray
axis image
axis off
set(gcf, 'Color', 'w');
export_fig(h, fullfile(sub_dir, strcat('FF_RF_', time_stamp, '.pdf')));
close(h)
h2 = figure;
img_dict = basis2img2(basic_cell.basis(:, basis_range), [img_sz, ...
img_sz], disp_sz);
imagesc(img_dict)
colormap gray
axis image
axis off
set(gcf, 'Color', 'w');
export_fig(h2, fullfile(sub_dir, strcat('dict_', time_stamp, '.pdf')));
close(h2)
switch(pd_mapRF.decomp)
case 'svd'
h = figure;
img = basis2img2(inhib_response_1(basis_range, :)', [img_sz, ...
img_sz], disp_sz);
imagesc(img)
colormap gray
axis image
axis off
set(gcf, 'Color', 'w');
export_fig(h, fullfile(sub_dir, strcat('svd_inhib_RF_1_', time_stamp, ...
'.pdf')));
close(h)
h = figure;
img = basis2img2(inhib_response_2(basis_range, :)', [img_sz, ...
img_sz], disp_sz);
imagesc(img)
colormap gray
axis image
axis off
set(gcf, 'Color', 'w');
export_fig(h, fullfile(sub_dir, strcat('inhib_RF_2_', time_stamp, ...
'.pdf')));
close(h)
case {'rpca', 'rpca_Phi', 'arpca'}
% Plot the low rank RFs.
h = figure;
img = basis2img2(rpca_inhib_response(basis_range, :)', [img_sz, ...
img_sz], disp_sz);
imagesc(img)
colormap gray
axis image
axis off
set(gcf, 'Color', 'w');
export_fig(h, fullfile(sub_dir, strcat('rpca_lowrank_RF_', time_stamp, ...
'.pdf')));
close(h)
% Plot sparse RFs.
basis_range = 1:100;
h = figure;
% The indices of the non-zero rows
nonzero_idx = find(all(S_p == 0,2) == 0);
% Display the first 100 RFs in a panel.
sparse_RF_subset = rpca_sparse(nonzero_idx, :);
sparse_RF_panel = basis2img2(sparse_RF_subset(basis_range, :)', ...
[img_sz img_sz], ...
[sqrt(length(basis_range)), ...
sqrt(length(basis_range))], 1);
imagesc(sparse_RF_panel)
axis equal; axis off
set(gcf, 'Color', 'w');
export_fig(h, fullfile(sub_dir, strcat('sparse_RF_', time_stamp, '.pdf')));
close(h)
% $$$ % testing: does the FF RFs match those of the sparse RF?
% $$$ h = figure;
% $$$ img = basis2img2(mapRF(nonzero_idx, :)', [img_sz img_sz], disp_sz);
% $$$ imagesc(img)
% $$$ colormap gray
% $$$ axis image
% $$$ axis off
% $$$ set(gcf, 'Color', 'w');
% $$$ export_fig(h, fullfile(sub_dir, strcat('FF_S_RF_', time_stamp, '.pdf')));
% $$$ close(h)
case 'grammian'
h = figure;
img = basis2img2(grammian_inhib_response(basis_range, :)', [img_sz, ...
img_sz], disp_sz);
imagesc(img)
colormap gray
axis image
axis off
set(gcf, 'Color', 'w');
export_fig(h, fullfile(sub_dir, strcat('grammian_inhib_RF_', ...
time_stamp, '.pdf')));
close(h)
case 'global'
h = figure;
img = reshape(global_inhib_response, img_sz, img_sz);
imagesc(img)
colormap gray
axis image
axis off
set(gcf, 'Color', 'w');
export_fig(h, fullfile(sub_dir, strcat('global_inhib_RF_', ...
time_stamp, '.pdf')));
close(h);
end
%% Log
% check if the log file already exists.
log_file = fullfile(dir_mapRF, 'mapRF_log.csv');
write_log_file(log_file, time_stamp, {pd_mapRF}, 'sort_pars', true);
% $$$ log_file = 'mapRF_log.csv';
% $$$ if(~exist(fullfile(dir_mapRF, log_file), 'file'))
% $$$ % If not, generate the file and the first row (parameter
% $$$ % names): strip out the field names as column headings
% $$$ log_cell = par_name;
% $$$ cell2csv(fullfile(dir_mapRF, log_file), log_cell);
% $$$ else
% $$$ % If exists, open it and read to a cell.
% $$$ log_cell = csv2cell(fullfile(dir_mapRF, log_file), 'fromfile'); ...
% $$$ % If the par_name does not agree with the old one, add a new
% $$$ % line of parameter names and new dimensions.
% $$$ if size(log_cell, 2)<size(par_name,2)
% $$$ log_cell = [log_cell, cell(size(log_cell,1), (size(par_name,2) ...
% $$$ - size(log_cell,2))); ...
% $$$ par_name];
% $$$ end
% $$$ end
% $$$ % Generate the folder name from the time stamp; log the time;
% $$$ % Write the time stamp and the parameters to the last row.
% $$$ log_cell = [log_cell; par_values];
% $$$ % Write the new log_file
% $$$ cell2csv(fullfile(dir_mapRF, log_file), log_cell);
% $$$ matlabpool close