-
Notifications
You must be signed in to change notification settings - Fork 0
/
neon_6cond_statistics_all.m
364 lines (328 loc) · 15.5 KB
/
neon_6cond_statistics_all.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
%%%%statistics
%% parameters
clear
tic
data_folder = 'D:\matwork\data\higher_vision';
mouse_numbers = [164];
session_name = 'neon';
session_num = 2; %for neon
area = 2; %visual area
plot_raster = 0; %%% plot raster for preferred angle?
plot_power = 0; %%% plot power spectrum
stim_angle = [0 45 90 135 180 225 270 315];
stim_type = [1 2];
neon_type = [1 2 3];
Ncond = length(stim_type)*length(stim_angle)*length(neon_type);
angle_loc = reshape(1:Ncond,length(stim_angle),length(stim_type)*length(neon_type));
area_names = {'V1';'LM';'LI';'LL'};
ave_act = [];
ave_act2 = [];
phase_angle = [];
first_comp = [];
zero_comp = [];
f1f0 = [];
f2f1f0 = [];
f1f2 = [];
latency_sliding = [];
latency4 = [];
latency2 = [];
latency_phy = [];
pref_ang = [];
pref_ang_phy = [];
pref_ang_neon = [];
exited = [];
cid = [];
ave_latency = [];
pv_simplicity = [];
h_simplicity = [];
S_S = [];
Depths = [];
TPlatency = [];
PTratio = [];
F1_dominant = [];
RF_size = [];
RF_loc = [];
RF_label = [];
Cell_area = [];
tag=cell(2,1);
dummy_all_neuron=0;
for mouse_number=1:length(mouse_numbers)
%% getting forders for analysis
%%% Kilosort foldels
mouse = ['M',num2str(mouse_numbers(mouse_number))];
KS_dirs = cell(1,1);
folders = dir(data_folder);
j=1;
for i=1:length(folders)
if length(folders(i).name)>4 && strcmp(folders(i).name(1:4),mouse)
KS_dirs{j,1}=fullfile(data_folder,folders(i).name);
j=j+1;
end
end
%%
for day=1:length(KS_dirs) %% loop for different days
%% loading
inputfile_stm = [KS_dirs{day},KS_dirs{day}(end-15:end),'_stimulus_time.mat'];
load(inputfile_stm)
load([KS_dirs{day},'\',session_name,'_spike_data.mat'])
load([KS_dirs{day},'\spike_Feature.mat'])
%% asigning previously calculated variables in each day
tf = stimulus_TemporalFreq{session_num}; % temporal frequency (cycle/s)
cycles = stimulus_cycles{session_num};
sourc_folder = fullfile(KS_dirs{day},session_name);
spikes = spike_data.spikes;
pdfs = spike_data.pdfs;
RFs = spike_data.RFs;
dx = spike_data.dx;
smoothed_pdfs = spike_data.smoothed_pdf;
xp = spike_data.xp;
stim_len = spike_data.stim_len;
% surround_sup = spike_data.surround_suppression;
evoked_cids = cell2mat(spike_data.evoked_cids);
evoked_ind = ismember(sp.cids,evoked_cids);
depth = sp.clusDepths(evoked_ind);
RF_cell = f_RF_select(KS_dirs{day},depth,0,'RF');
%% concatenating
Depths = vertcat(Depths,depth);
TPlatency = vertcat(TPlatency,sp.clusTPlatency(evoked_ind)./30);
PTratio = vertcat(PTratio,sp.clusTPratio(evoked_ind));
cid = vertcat(cid,cell2mat(spike_data.evoked_cids)'); %#ok<*AGROW>
ave_latency = vertcat(ave_latency,cell2mat(spike_data.average_latency)');
num_loc = regexp(KS_dirs{day},'_+[0-9]');
tarikh = ['D',KS_dirs{day}(num_loc(3)+1:num_loc(3)+2),'/',KS_dirs{day}(num_loc(2)+1:num_loc(2)+2)];
RF_size = vertcat(RF_size,RF_cell.RF_size);
RF_loc = vertcat(RF_loc,RF_cell.RF_loc);
RF_label = vertcat(RF_label,RF_cell.RF_label);
Cell_area = vertcat(Cell_area,RF_cell.area); %% area: 1 = V1, 2 = LM , 3 = LI , 4 = LL
[~, onset_ind] = min(abs(xp));
[~,offset_ind] = min(abs(xp-stim_len));
[~,halfset_ind] = min(abs(xp-stim_len/2));
Nc = length(pdfs); %number of clusters
ave_activity_cond = zeros(Nc,6);
ave_activity_cond2 = zeros(Nc,6);
phase_angle_cond = zeros(Nc,6);
first_comp_cond = nan(Nc,6);
zero_comp_cond = nan(Nc,6);
f1f0_cond = nan(Nc,6);
f2f1f0_cond = nan(Nc,6);
f1f2_cond = nan(Nc,6);
latency_cond_sliding = nan(Nc,6);
latency_cond4 = nan(Nc,6);
latency4_phy = nan(Nc,1);
latency_cond2 = nan(Nc,6);
response_type = zeros(Nc,1);
dominant_f1 = zeros(Nc,1);
for neuron=1:Nc %%%loop over clusters
dummy_all_neuron = dummy_all_neuron+1;
tag{dummy_all_neuron} = [mouse,tarikh,'id',num2str(spike_data.evoked_cids{neuron})];
spike = spikes{neuron};
Pdf = pdfs{neuron};
Pdf2 = pdfs{neuron};
RF = RFs{neuron};
smoothed_pdf = smoothed_pdfs{neuron};
%%% preferred_angle
[preferred_angle,preferred_ind]=f_find_preferred6(Pdf,stim_angle,angle_loc,onset_ind,offset_ind,'all');
pref_ang=[pref_ang;preferred_angle];
psth_locs=angle_loc(preferred_ind,:);
[preferred_angle_phy,~]=f_find_preferred6(Pdf,stim_angle,angle_loc,onset_ind,offset_ind,'phy');
pref_ang_phy=[pref_ang_phy;preferred_angle_phy];
[preferred_angle_neon,~]=f_find_preferred6(Pdf,stim_angle,angle_loc,onset_ind,offset_ind,'neon');
pref_ang_neon=[pref_ang_neon;preferred_angle_neon];
%%% ave_activity, phase and latency
transient_time=150; %in ms to exclude the transient component
transient_nbins=transient_time/(dx*1000);
bin_size=10;
FS=1/(dx*bin_size); % freq. of sampling.
pre_stimulus_baseline=mean(mean(Pdf(psth_locs,1:onset_ind-1)));
if pre_stimulus_baseline
Pdf=(Pdf-pre_stimulus_baseline);%./pre_stimulus_baseline; %% normalizing psth by pre_stimulus_baseline
end
pre_stimulus_baseline_smooth=mean(mean(smoothed_pdf(psth_locs,1:onset_ind-1)));
if pre_stimulus_baseline_smooth
smoothed_pdf=(smoothed_pdf-pre_stimulus_baseline_smooth);%./pre_stimulus_baseline_smooth; %% normalizing psth by pre_stimulus_baseline
end
if plot_power
neuron_name = [mouse,'_D',KS_dirs{day}(num_loc(3)+1:num_loc(3)+2),'-',KS_dirs{day}(num_loc(2)+1:num_loc(2)+2),'_ID',num2str(spike_data.evoked_cids{neuron})];
PS=figure('visible','off');
cond_names = {'NIG', 'LDG','NIG+ring','NIG+squar','NIG+circle','LDG+circle'};
cond_loc_plot = [1 2 6 5 3 4];
end
for cond=1:length(psth_locs) %%6 condition (1:neon 2:phy 3:ring block (ctrl) 4:squar block 5:circle 6:phy+circle) of preferred angle
ave_activity_cond(neuron,cond) = mean(Pdf(psth_locs(cond),onset_ind:offset_ind));
ave_activity_cond2(neuron,cond) = mean(Pdf2(psth_locs(cond),onset_ind:offset_ind));
cond_smoothed_pdf=smoothed_pdf(psth_locs(cond),onset_ind+transient_nbins-1:offset_ind+transient_nbins);
s1=length(cond_smoothed_pdf);
m = s1 - mod(s1, bin_size);
cond_smoothed_pdf=mean(reshape(cond_smoothed_pdf(1:m),bin_size,[]));
%% phase and modulation calculation
L=length(cond_smoothed_pdf);
freq=FS*(0:(L/2))/L;%%0:FS/length(x):FS-1;
[~,f1_ind]=min(abs(freq-tf));
[~,f2_ind]=min(abs(freq-tf*2));
f=fft(cond_smoothed_pdf)/L;
phase_angle_cond(neuron,cond)= angle(f(f1_ind));
f1= abs(f(f1_ind))*2;
f2= abs(f(f2_ind))*2;
f0=f(1);
first_comp_cond(neuron,cond) = f1;
zero_comp_cond(neuron,cond) = f0;
f1f0_cond(neuron,cond) = (f1-f0)/(f0+f1);
f2f1f0_cond(neuron,cond) = (f2+f1-f0)/(f0+f1+f2);
f1f2_cond(neuron,cond) = (f1-f2)/(f2+f1);
f(1)=0; f=abs(f);f=f(1:floor(L/2)+1);f(2:end-1)=2*f(2:end-1);
% if f1f0_cond(neuron,cond)>2
% f1f0_cond(neuron,cond)=nan;
% elseif f1f0_cond(neuron,cond)<-2
% f1f0_cond(neuron,cond)=nan;
% end
if cond==2
ff=fft(cond_smoothed_pdf-mean(cond_smoothed_pdf));ff=abs(ff/L);ff=ff(1:floor(L/2)+1);ff(2:end-1)=2*ff(2:end-1);
[~ ,domominat_loc]=max(ff);
if domominat_loc==f1_ind
dominant_f1(neuron)=1;
end
end
%% power spectrum plot
if plot_power
subplot(3,2,cond_loc_plot(cond))
plot(freq,f)
hold on
plot(f1_ind-1,f1,'r*')
plot((f2_ind-1),f2,'g*')
title([cond_names{cond},' f1f0=',num2str(f1f0_cond(neuron,cond)),',f1f2=',num2str(f1f2_cond(neuron,cond))])
xlim([0,25])
xlabel('frequency (Hz)')
ylabel('power')
end
cond_RF = RF(psth_locs(cond),:);
%% conditional response latency with sliding window
% win_len=20;
% step_size= 2;
% pre_stimulus_act=mean(Pdf(psth_locs(cond),onset_ind-win_len:onset_ind-1));
% y=Pdf(psth_locs(cond),:);
% teta=f_resp_latency_sliding(y,onset_ind,offset_ind/2,pre_stimulus_act,win_len,step_size);
% if ~isempty(teta)
% latency_cond_sliding(neuron,cond)=teta;
% end
%% conditional response latency
n=4; %number of merging bins
cond_RF4=cond_RF;
s1=length(cond_RF4);
m = s1 - mod(s1, n);
cond_RF4=reshape(cond_RF4(1:m),n,[]);
cond_RF4=sum(cond_RF4);
teta = f_resp_latency(cond_RF4,floor(onset_ind/n),0,floor(offset_ind/(n*2)));
if ~isempty(teta)
latency_cond4(neuron,cond)=teta*n;
end
cond_RF2 = movsum(cond_RF,2);
teta = f_resp_latency(cond_RF2,floor(onset_ind),0,floor(offset_ind/2));
if ~isempty(teta)
latency_cond2(neuron,cond)=teta;
end
end
if plot_power
saveas(PS,[data_folder,'/PS_',neuron_name,'.png'])
close(PS)
end
%% phy response latency
phy_RF =sum(RF(psth_locs(2:3),:));
n=4;%number of merging bins
s1=length(phy_RF);
m = s1 - mod(s1, n);
phy_RF=reshape(phy_RF(1:m),n,[]);
phy_RF=sum(phy_RF);
teta = f_resp_latency(phy_RF,floor(onset_ind/n),0,floor(offset_ind/(n*2)));
if ~isempty(teta)
latency4_phy(neuron)=teta*n;
end
if mean(ave_activity_cond(neuron,:))>=0
response_type(neuron)=1; %%%"Excited";
end
%% plot raster for preferred angle
if plot_raster
f_neon6_plot_pref_raster(KS_dirs{day},preferred_angle,psth_locs,spike_data,neuron,latency_cond4(neuron,:),latency_cond2(neuron,:),f1f0_cond(neuron,:),bin_size,area_names{RF_cell.area(neuron)}) %#ok<*UNRCH>
end
%% simplicity T-test
% tag{dummy_all_neuron}
phy_smoothed_pdf = (abs(smoothed_pdf(psth_locs(2),onset_ind+transient_nbins-1:offset_ind)));
xp_phy = xp(onset_ind+transient_nbins-1:offset_ind);
[~,h_simpl] = f_simplicity_test(phy_smoothed_pdf, xp_phy, spike, psth_locs);
h_simplicity = [h_simplicity;h_simpl];
end
ave_act = vertcat(ave_act,ave_activity_cond);
ave_act2 = vertcat(ave_act2,ave_activity_cond2);
exited = vertcat(exited,response_type);
phase_angle = vertcat(phase_angle,phase_angle_cond);
first_comp = vertcat(first_comp,first_comp_cond);
zero_comp = vertcat(zero_comp,zero_comp_cond);
f1f0 = vertcat(f1f0,f1f0_cond);
f2f1f0 = vertcat(f2f1f0,f2f1f0_cond);
f1f2 = vertcat(f1f2,f1f2_cond);
latency4 = vertcat(latency4,latency_cond4);
latency2 = vertcat(latency2,latency_cond2);
latency_phy = vertcat(latency_phy,latency4_phy);
latency_sliding = vertcat(latency_sliding,latency_cond_sliding);
F1_dominant =vertcat(F1_dominant,dominant_f1);
%% surround_suppression
SR_day=NaN(length(evoked_cids),1);
% for isr=1:length(evoked_cids)
% id_loc=ismember(surround_sup(1,:),evoked_cids(isr));
% if sum(id_loc)~=0
% SR_day(isr)=surround_sup(2,id_loc);
% end
% end
SR_day(SR_day<-1)=nan;
S_S = vertcat(S_S,SR_day);
end
end
phase_diff = rad2deg(angdiff(phase_angle(:,1),phase_angle(:,2)));
twopi_phase_diff = rad2deg(wrapTo2Pi((phase_angle(:,1)-phase_angle(:,2))));
norm_rel_amp_f1 = (first_comp(:,1)-first_comp(:,2))./(first_comp(:,1)+first_comp(:,2));
ave_IGR1 = ((ave_act2(:,1))-(ave_act2(:,2)))./((ave_act2(:,1))+(ave_act2(:,2)));
ave_IGR2 = (ave_act(:,1)-ave_act(:,3))./(ave_act(:,1)+ave_act(:,3));
% intersection_point = f_intersect_gauss2_for_hist(TPlatency,10000,.4,.5,1); %inputs(vect,Nbin,aa,bb,report) /sugest an in terval for the intersection [aa,bb]
intersection_point = 0.4278;
Icell_ind = TPlatency<=intersection_point;
how_fast = 80; %(ms)
fast_phy = latency4(:,2) <=how_fast;
fast_neon = latency4(:,1) <=how_fast;
fast_phy_neon = logical(fast_neon.*fast_phy);
shifted_ind = abs(phase_diff) >=120;
%% neurons type based on being evoked in different condition
evoke_phy_ind = ~isnan(latency2(:,2));
evoke_neon_ind = ~isnan(latency2(:,1));
both_evoked = evoke_neon_ind.*evoke_phy_ind;
only_phy = evoke_phy_ind & ~evoke_neon_ind;
only_neon = ~evoke_phy_ind & evoke_neon_ind;
%% saving
stat_data.ave_act = ave_act;
stat_data.phase_angle = phase_angle;
stat_data.latency = latency4;
stat_data.f1f0 = f1f0;
stat_data.f2f1f0 = f2f1f0;
stat_data.f1f2 = f1f2;
stat_data.pref_ang = pref_ang;
stat_data.cid = cid;
stat_data.tag = tag;
stat_data.ave_FR = mean(ave_act,2);
stat_data.exited = exited;
stat_data.ave_IGR1 = ave_IGR1;
stat_data.ave_IGR2 = ave_IGR2;
stat_data.ave_latency = ave_latency./1000;
stat_data.fast_phy_neon = fast_phy_neon;
stat_data.Icell = Icell_ind;
stat_data.shifted = shifted_ind;
stat_data.log_phase_diff = log(abs(phase_diff));
stat_data.phase_diff = phase_diff;
stat_data.twopi_phase_diff = twopi_phase_diff;
stat_data.h_simplicity = h_simplicity;
stat_data.norm_rel_amp_f1 = norm_rel_amp_f1;
stat_data.surround_suppression = S_S;
stat_data.TPlatency = TPlatency;
stat_data.Depth = Depths;
stat_data.PTratio = PTratio;
stat_data.F1_dominant = logical(F1_dominant);
save([data_folder,'/neon_stat_data.mat'],'stat_data')
%% plotting
toc