-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainScript_assemble.m
291 lines (257 loc) · 13.4 KB
/
mainScript_assemble.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
% assembly a matfile compiling data across units
%%TODO
%load data
%align number of rows of population data
%% get ready
limSuffix = '';%'_woSuccess';%_woSuccess';
load('/media/daisuke/cuesaccade_data/param20240625.mat');
[saveServer, rootFolder] = getReady();
saveSuffix_p = ['fitPSTH_pop' limSuffix];
%% recorded data
animal = 'hugo';
dataType = 0;%0: each channel, 1: all channels per day
tWin = [0 0.5];%[s]
for yy = 1:3
switch yy
case 1
year = '2021';
case 2
year = '2022';
case 3
year = '2023';
end
[loadNames, months, dates, channels] = getMonthDateCh(animal, year, rootFolder);
% to obtain index of specified month&date&channel
% thisdata = find(1-cellfun(@isempty, regexp(loadNames, ...
% regexptranslate('wildcard',fullfile(rootFolder, year, 'cuesaccade_data','08August','02','*_ch21')))));
nData = length(channels);
mFiringRate_pop = cell(nData,1); %needed for inclusionCriteria
kernelInfo_pop = cell(nData,1);
%kernel_pop = cell(nData,1);
%expval_pop = cell(nData,1); %needed for inclusionCriteria
%corrcoef_pop = cell(nData,1);
corrcoef_pred_spk_pop = cell(nData,1);
%PtonsetResp_pop = cell(nData,1); %needed for inclusionCriteria
%PsaccResp_pop = cell(nData,1);
cellclassInfo_pop = cell(nData,1);
expval_ind_pop = cell(nData,1);
expval_tgt_pop = cell(nData,1);
corr_avgtgt_pop = cell(nData,1);
expval_avgtgt_pop = cell(nData,1);
ntargetTrials_pop = cell(nData,1); %needed for inclusionCriteria
ntotTrials_pop = cell(nData,1);
id_pop = cell(nData,1);
%Rsqadj_pop = cell(nData,1); %needed for pickUnitsByClass
%Rsqadj_tgt_pop = cell(nData,1);
gainInfo_pop = cell(nData,1);
expval_trig_pop = cell(nData,1);
corr_trig_pop = cell(nData,1);
latency_r_pop = cell(nData,1);
latency_neuro_pop = cell(nData,1);
stats_stratified_pop = cell(nData,1);
corr_tgt_pop = cell(nData,1);
corr_tgt_rel_pop = cell(nData,1);
expval_tgt_rel_pop = cell(nData,1);
mdiffCueFOnset_pop = cell(nData,1);
stddiffCueFOnset_pop = cell(nData,1);
diffCueFOnset_pop = cell(nData,1);
errorIDs= cell(1);
for idata = 1:length(channels)
datech = [months{idata} '/' dates{idata} '/' num2str(channels{idata})];
thisid = [animal '/' year '/' datech];
disp(thisid);
saveSuffix = [animal replace(datech,'/','_') '_linear_rReg' limSuffix];
thisDate = [months{idata} '_' dates{idata}];
saveFolder = fullfile(saveServer, year,animal);%17/6/23
saveName = fullfile(saveFolder, [saveSuffix '.mat']);
if exist(saveName, 'file')
%datech_pop{idata} = datech;
try
%result of mainScript.m and mainScript_latency.m
S = load(saveName, 'PSTH_f','predicted_all', 'predicted', ...
'kernelInfo','t_r','cellclassInfo','mFiringRate','t_cat'); %param
if isfield(S,'kernelInfo')
% % %retrieve just once
% % tlags = S.kernelInfo.tlags;
% % % param = S.param;
% %
% % mFiringRate_pop{idata} = S.mFiringRate;
% % %kernel_pop{idata} = S.kernelInfo.kernel;
% % %expval_pop{idata} = S.kernelInfo.expval;
% % %corrcoef_pop{idata} = S.kernelInfo.corrcoef;
% % kernelInfo_pop{idata} = S.kernelInfo;
% %
% % R = corrcoef(S.PSTH_f, S.predicted_all);
% % corrcoef_pred_spk_pop{idata} = R(1,2);
% %id_pop{numel(id_pop)+1} = thisid;
id_pop{idata} = thisid;
eyeName = fullfile(saveFolder,['eyeCat_' animal thisDate '.mat']);
eyeData = load(eyeName,'catEvTimes', 'onsets_cat'); %'eyeData_rmotl_cat','startSaccNoTask'
% % ddData = load(loadNames{idata},'dd'); %slow to read
%% Rsq adj of subjset of variables
%%predictorInfoName = fullfile(saveFolder,['predictorInfo_' animal thisDate '.mat']);
% % predData = load(predictorInfoName, 'predictorInfo');
nPredictorNames = numel(param.predictorNames);
% % nsub=4;
% % Rsqadj = zeros(nsub,1);
% % for jj = 1:nsub
% % switch jj
% % case 1 %full model
% % tgtGroups = 1:nPredictorNames;
% % case 2 %omit vision %added 26/10/2023
% % tgtGroups = setxor(1:nPredictorNames, 1);
% % case 3 %omit eye speed
% % tgtGroups = setxor(1:nPredictorNames, 2);
% % case 4 %omit eye position
% % tgtGroups = setxor(1:nPredictorNames, 3);
% % end
% % [Rsqadjusted,rr,r0] = fitSubset(S.PSTH_f, predData.predictorInfo, ...
% % tgtGroups, param);%, idxTgtOnsets);
% %
% % Rsqadj(jj) = Rsqadjusted;
% % end
% % Rsqadj_pop{idata} = Rsqadj;
% % predData = [];
% % %% Rsq_adjusted computed from pre-target
% % % periods - NG. can be < 0
% % nPredictors_all = sum(cellfun(@numel, S.kernelInfo.kernel));
% % Rsqadj_tgt = nan(1, 4);
% % [Rsqadj_tgt(1,1)] = getRsqadj_tgt(S.PSTH_f, S.predicted_all, ...
% % eyeData.catEvTimes, S.t_r, tWin, nPredictors_all);
% % for jj = 1:3
% % nPredictors = numel(S.kernelInfo.kernel{jj});
% % [Rsqadj_tgt(1,jj+1)] = getRsqadj_tgt(S.PSTH_f, S.predicted(:,jj), ...
% % eyeData.catEvTimes, S.t_r, tWin, nPredictors);
% % end
% % Rsqadj_tgt_pop{idata} = Rsqadj_tgt;
%% response to target & saccade
%PtonsetResp_pop{idata} = S.cellclassInfo.PtonsetResp;
%PsaccResp_pop = [PsaccResp_pop S.cellclassInfo.PsaccResp];
% % cellclassInfo_pop{idata} = S.cellclassInfo;
%% explained variance per kernel
expval_ind = nan(size(S.predicted,2)+1,1);
expval_ind(1,1) = getExpVal(S.PSTH_f, S.predicted_all);
% expval_ind(1,1) = getExpVal(S.PSTH_f-mean(S.PSTH_f), ...
% S.predicted_all-mean(S.predicted_all));
for ivar = 1:nPredictorNames
expval_ind(ivar+1,1) = getExpVal(S.PSTH_f, S.predicted(:,ivar));
% expval_ind(ivar+1,1) = getExpVal(S.PSTH_f-mean(S.PSTH_f), ...
% S.predicted(:,ivar)-mean(S.predicted(:,ivar)));
end
expval_ind_pop{idata} = expval_ind;
%% explained variance for target response
expval_tgt = zeros(nPredictorNames, 1);
corr_tgt = zeros(nPredictorNames, 1);
[expval_tgt(1,1), corr_tgt(1,1)] = ...
getExpVal_tgt(S.PSTH_f, S.predicted_all, eyeData.catEvTimes, S.t_r, tWin);
[expval_tgt(2:nPredictorNames+1,1), corr_tgt(2:nPredictorNames+1,1)] = ...
getExpVal_tgt(S.PSTH_f, S.predicted, eyeData.catEvTimes, S.t_r, tWin);
expval_tgt_pop{idata} = expval_tgt;
expval_tgt_rel_pop{idata} = 100*expval_tgt(2:4)./expval_tgt(1);
corr_tgt_pop{idata} = corr_tgt;
corr_tgt_rel_pop{idata} = 100*corr_tgt(2:4)./corr_tgt(1);
%% cue-tgt interval
[diffCueFOnset, mdiffCueFOnset,stddiffCueFOnset] = ...
getDiffCueTgtOnset(eyeData.onsets_cat, eyeData.catEvTimes); %3/6/24
diffCueFOnset_pop{idata} = diffCueFOnset;
mdiffCueFOnset_pop{idata} = mdiffCueFOnset;
stddiffCueFOnset_pop{idata} = stddiffCueFOnset;
%% explained variance for target response averaged across trials
% [expval_avgtgt(1,1), corr_avgtgt(1,1)] = getExpVal_avgtgt(S.PSTH_f, S.predicted_all, ...
% eyeData.catEvTimes, S.t_r, [0 0.5], param.cardinalDir, dd);
% [expval_avgtgt(2:6,1), corr_avgtgt(2:6,1)] = getExpVal_avgtgt(S.PSTH_f, S.predicted, ...
% eyeData.catEvTimes, S.t_r, [0 0.5], param.cardinalDir, dd);
% expval_avgtgt_pop = [expval_avgtgt_pop expval_avgtgt];
% corr_avgtgt_pop = [corr_avgtgt_pop corr_avgtgt];
% %% compute time-resolved explained variance
% [choiceOutcome] = getChoiceOutcome(dd);
%
% expval_trig = []; corr_trig = [];
% for ievtype = 1:4
% % 1: success trial
% % 2: failed quiescent trial
% % 3: failed wrong saccade direction
% % 4: saccade outside trials
%
% if ievtype <= 3
% theseEvents = find(choiceOutcome==ievtype);
% eventTimes = eyeData.catEvTimes.tOnset(theseEvents);
% elseif ievtype == 4
% eventTimes = startSaccNoTask;
% end
% [expval_trig(:,:,ievtype), corr_trig(:,:,ievtype), winSamps] = ...
% getExpVal_trig(S.PSTH_f, [S.predicted_all S.predicted], S.t_r, eventTimes, [-0.5 0.5]);
%
% %ax_expval_trig(ievtype) = subplot(4,1,ievtype);
% %plot(winSamps, squeeze(expval_trig(:,:,ievtype))');
% end
% expval_trig_pop = cat(4, expval_trig_pop, expval_trig);
% corr_trig_pop = cat(4, corr_trig_pop, expval_trig);
% % %% number of target trials
% % ntargetTrials_pop{idata} = sum(~isnan(eyeData.catEvTimes.tOnset));
% %
% % %% number of total trials
% % ntotTrials_pop{idata} = numel(eyeData.catEvTimes.tOnset);
% %
% % %% compute gain
% % figTWin = [-0.5 0.5];
% % onlySuccess = 0;
% % respWin = [0.05 0.35]; %[s]
% % y_r = cat(2,S.PSTH_f,S.predicted_all);
% % gainInfo = getGainInfo(S.t_r, y_r, param.cardinalDir, eyeData.catEvTimes, ...
% % ddData.dd, figTWin, onlySuccess, respWin);
% % gainInfo_pop{idata} = gainInfo;
% %
% %
% % %% latency
% % S = load(saveName, 'latency_bhv','latency_neuro','latency_r','stats_stratified');
% % latency_bhv_pop{idata} = S.latency_bhv;
% % latency_neuro_pop{idata} = S.latency_neuro;
% % latency_r_pop{idata} = S.latency_r;
% % stats_stratified_pop{idata} = S.stats_stratified;
errorIDs{idata} = 0;
S = []; eyeData = []; ddData = [];
end
catch err
errorIDs{idata} = 1;
disp(err.message);
disp(err.stack);
S = []; eyeData = []; ddData = [];
%continue;
end
end
end
%dataByYear = dataByYear(~isnan(dataByYear));
%alldata = [alldata dataByYear(:)];
%% make a giant structure
% assembly.mFiringRate_pop = mFiringRate_pop; %needed for inclusionCriteria
% assembly.kernelInfo_pop = kernelInfo_pop;
% assembly.corrcoef_pred_spk_pop = corrcoef_pred_spk_pop;
% assembly.cellclassInfo_pop = cellclassInfo_pop;
% assembly.expval_ind_pop = expval_ind_pop;
% assembly.expval_tgt_pop = expval_tgt_pop;
% assembly.corr_avgtgt_pop = corr_avgtgt_pop;
% assembly.expval_avgtgt_pop = expval_avgtgt_pop;
% assembly.ntargetTrials_pop = ntargetTrials_pop; %needed for inclusionCriteria
% assembly.ntotTrials_pop = ntotTrials_pop;
% assembly.id_pop = id_pop;
% assembly.Rsqadj_pop = Rsqadj_pop; %needed for pickUnitsByClass
% assembly.Rsqadj_tgt_pop = Rsqadj_tgt_pop;
% assembly.gainInfo_pop = gainInfo_pop;
% assembly.expval_trig_pop = expval_trig_pop;
% assembly.corr_trig_pop = corr_trig_pop;
% assembly.latency_bhv_pop = latency_bhv_pop;
% assembly.latency_neuro_pop = latency_neuro_pop;
% assembly.latency_r_pop = latency_r_pop;
% assembly.stats_stratified_pop = stats_stratified_pop;
assembly.expval_tgt_rel_pop = expval_tgt_rel_pop;
assembly.corr_tgt_rel_pop = corr_tgt_rel_pop;
assembly.corr_tgt_pop = corr_tgt_pop;
assembly.mdiffCueFOnset_pop = mdiffCueFOnset_pop;
assembly.stddiffCueFOnset_pop = stddiffCueFOnset_pop;
%%
% save('fitPSTH_pop20220202','avgPupilResp_pop', '-append');
%save(fullfile(saveServer,[saveSuffix_p animal '.mat']),'assembly');
save(fullfile(saveFolder, 'assembly_tmp.mat'),'assembly','param');
assembly = [];
end