-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowTonsetResp_stratified.m
149 lines (121 loc) · 5.02 KB
/
showTonsetResp_stratified.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
function [fig, stats] = showTonsetResp_stratified(PSTH_f, t_r, onsets_cat, catEvTimes, tWin_t, ...
param, dd, validEvents, nDiv)
% fig = showTonsetResp_stratified(PSTH_f, t_r, onsets_cat, catEvTimes, tWin_t, param, dd, validEvents)
%only use successful trials without hesitation
%
% see also: getTgtNeuroLatency
if nargin < 9
nDiv = 2;
end
%% from showTonsetByCue:
onsetTimes_t = catEvTimes.tOnset(validEvents);
tgtDir = getTgtDir(dd.targetloc(validEvents), param.cardinalDir);
[~,dirIdx]=intersect(param.cardinalDir, unique(tgtDir));
% triggered by target onset
[~, winSamps_t, singleResp_t] ...
= eventLockedAvg(PSTH_f', t_r, onsetTimes_t, tgtDir, tWin_t);
%% behavioural latency for sorting trials
latency_bhv_srt = getTgtBhvLatency(onsets_cat, dd, validEvents, 1);
hesitant = getChoiceOutcome_hesitant(onsets_cat, dd, validEvents);
%% divide trials
[choiceOutcome] = getChoiceOutcome(dd);
success = (choiceOutcome(validEvents) == 1) .* ~hesitant;
%hesitant = (latency_bhv_cOn - latency_bhv_srt > 0.1); %1st saccade did NOT lead to choice
%fail = choiceOutcome(validEvents) >= 2; %quiescent + wrong
[prefDir, prefDirTrials_c] = getPrefDir(PSTH_f, t_r, onsetTimes_t, tgtDir, param);
prefDirTrials = zeros(numel(onsetTimes_t),1);
prefDirTrials(prefDirTrials_c) = 1;
[prevDir, prevDirTrials_c] = getPrevDir(tgtDir, param);
prevDirTrials = zeros(numel(onsetTimes_t),1);
prevDirTrials(prevDirTrials_c) = 1;
theseColors = cool(nDiv);
nCols = 4;
fig = figure('position',[1003 219 nCols*588 1200]);
stats = [];
dt = []; latency_neuro = []; thresh_neuro = [];
for istimtype = 1:nCols
if istimtype==1
stimtrials = prefDirTrials;
elseif istimtype == 2
stimtrials = 1-prefDirTrials;
elseif istimtype == 3
stimtrials = prevDirTrials;
elseif istimtype == 4
stimtrials = ones(size(prefDirTrials));
end
theseTrials_c = find(success.*stimtrials);
[~, idx] = sort(latency_bhv_srt(theseTrials_c));
theseTrials = theseTrials_c(idx);
if ceil(numel(theseTrials)/nDiv) <= 1
continue;
end
divIdx = 0;
for idiv = 1:nDiv
divIdx = divIdx(end)+1:ceil(idiv*numel(theseTrials)/nDiv);
theseTrials_div = theseTrials(divIdx);
%% stats per division
avgResp(idiv, istimtype, :) = median(singleResp_t(theseTrials_div,:),1);
avgLatency_bhv(idiv, istimtype) = median(latency_bhv_srt(theseTrials_div),1);
seResp_t(idiv, istimtype, :) = ste(singleResp_t(theseTrials_div,:), 1);
seLatency_bhv(idiv, istimtype) = ste(latency_bhv_srt(theseTrials_div), 1);
%% visualization
axes(istimtype, idiv) = subplot(nDiv+2, nCols, nCols*(idiv-1)+istimtype);
if sum(theseTrials_div)==0
continue;
end
boundedline(winSamps_t, squeeze(avgResp(idiv,istimtype, :)), squeeze(seResp_t(idiv, istimtype, :)), ...
'cmap',theseColors(idiv,:));
vline(avgLatency_bhv(idiv, istimtype), axes(istimtype,idiv), ':', 'k');
if istimtype==1
ylabel([num2str(idiv) '/' num2str(nDiv)]);
end
if idiv==1
if istimtype==1
title(['pref direction ' num2str(prefDir)]);
elseif istimtype==2
title('other directions');
elseif istimtype==3
title(['most frequent directions ' num2str(prevDir)]);
elseif istimtype==4
title('all directions');
end
end
end % end of idiv
%% stats across divisions
[latency_neuro(:,istimtype), thresh_neuro(:,istimtype)] = ...
getSingleTrialLatency(avgResp(:,istimtype,:), winSamps_t, tWin_t, param.threshParam);
for idiv = 1:nDiv
hline(thresh_neuro(idiv,istimtype), axes(istimtype, idiv), ':', theseColors(idiv,:));
end
%% summary across divisions
axes(istimtype, nDiv+1) = subplot(nDiv+2, nCols, nCols*nDiv+istimtype);
for idiv = 1:nDiv
plot(winSamps_t, squeeze(avgResp(idiv, istimtype, :)), 'Color', theseColors(idiv,:)); hold on;
end
for idiv = 1:nDiv
hold on; vline(avgLatency_bhv(idiv, istimtype), axes(istimtype,nDiv+1),':',theseColors(idiv,:));
end
%hline(thresh_neuro(:,istimtype));
if istimtype==1
ylabel('all divisions');
end
axes(istimtype, nDiv+2) = subplot(nDiv+2, 4, 4*(nDiv+1)+istimtype);
scatter(avgLatency_bhv(:,istimtype), latency_neuro(:,istimtype), [],theseColors)
nonan = ~isnan(latency_neuro(:,istimtype));
if sum(nonan)==0
avgCorr(istimtype) = nan; pCorr(istimtype) = nan;
else
[avgCorr(istimtype), pCorr(istimtype)] = corr(avgLatency_bhv(nonan,istimtype), latency_neuro(nonan,istimtype), 'type','Spearman');
end
title(['rank corr: ' num2str(avgCorr(istimtype)) ', p: ' num2str(pCorr(istimtype))]);
squareplot; axis padded;
end
linkaxes(axes(:,1:nDiv));
linkaxes(axes(:,nDiv+1));
%% summary stats
stats.latency = latency_neuro;
stats.thresh = thresh_neuro;
stats.avglatency_bhv = avgLatency_bhv;
stats.avgResp = avgResp;
stats.avgCorr = avgCorr;
stats.pCorr = pCorr;