-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBehaviour_analysis.m
More file actions
222 lines (175 loc) · 5.6 KB
/
Behaviour_analysis.m
File metadata and controls
222 lines (175 loc) · 5.6 KB
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
function Behaviour_analysis(cfg)
% function Behaviour_analysis(cfg)
%
% cfg.subjectID = subject name
% cfg.root = root directory
% cfg.plot = plot results or not (true/false)
% cfg.outputDir = name of output folder
% data paths
dataPath = fullfile(cfg.dataDir,cfg.subjectID,'Behaviour');
outputDir = fullfile(cfg.root,cfg.subjectID,cfg.outputDir);
if ~exist(outputDir,'dir'); mkdir(outputDir); end
%% Get the data for UPCP
data = str2fullfile(dataPath,'*_UPCP_*');
if ~iscell(data); tmp{1} = data; data = tmp; end
animacy = []; visibility = []; trialMatrix = [];
% load the data
for d = 1:length(data)
load(data{d},'A','V','P');
% if wrong buttons were used
tmp = unique(A(:,1)); tmp(isnan(tmp)) = [];
if tmp == 2 % shifted buttons to the right
fprintf('Wrong buttons were used, adjust them now. \n')
A(A(:,1)==2,1) = 1; % shift them back
A(isnan(A(:,1)),1) = 2;
end
animacy = [animacy; A];
visibility = [visibility; V];
trialMatrix = [trialMatrix; P.trialMatrix];
clear A B T P
end
% Reverse scales
nTrials = length(animacy);
for t = 1:nTrials
if animacy(t,3)
animacy(t,1) = 3-animacy(t,1);
end
if visibility(t,3)
visibility(t,1) = 5-visibility(t,1);
end
end
% Calculate animacy scores per SOA
SOA = trialMatrix(:,2);
nSOAs = length(unique(SOA));
% recode animacy
At = trialMatrix(:,1) < 3;
Ab = animacy(:,1) == 1;
% get accuracies
Ar = zeros(nSOAs,1);
for s = 1:nSOAs
Ar(s) = sum(Ab(SOA==s)==At(SOA==s))/(sum(SOA==s));
end
% calculate d prime
H = zeros(nSOAs,1); Fa = zeros(nSOAs,1);
D = zeros(nSOAs+1,1);
for s = 1:nSOAs
H(s) = sum(Ab(At == 1 & SOA == s))/length(Ab(At == 1 & SOA == s));
if H(s) == 0; H(s) = 0.00001; elseif H(s) == 1; H(s) = 1-0.00001; end % can't handle 0 or 1
Fa(s) = sum(Ab(At == 0 & SOA == s))/length(Ab(At == 0 & SOA == s));
if Fa(s) == 0; Fa(s) = 0.00001; elseif Fa(s) == 1; Fa(s) = 1-0.00001; end
D(s) = dprime(H(s),Fa(s),sum(At==1 & SOA == s),sum(At==0 & SOA == s));
end
% Calculate visbility scores per SOA
nVis = 4;
% get scores
Vr = zeros(nSOAs,nVis);
mVis = zeros(nSOAs,1);
for s = 1:nSOAs
mVis(s) = nanmean(visibility(SOA==s,1),1);
for v = 1:nVis
Vr(s,v) = sum(visibility(SOA==s)==v)/sum(SOA==s);
end
end
% calculate accuracy and visibility per stim
nStim = length(unique(trialMatrix(:,1)));
A_stim = zeros(nStim,nSOAs+1);
V_stim = zeros(nStim,nSOAs+1,4);
for st = 1:nStim
for s = 1:nSOAs
idx = (trialMatrix(:,1)==st) & (trialMatrix(:,2) == s);
if st < 3 % for animate
A_stim(st,s) = sum(animacy(idx,1)==1)/sum(idx);
elseif st > 2
A_stim(st,s) = sum(animacy(idx,1)==2)/sum(idx);
end
for v = 1:4
V_stim(st,s,v) = sum(visibility(idx,1)==v)/sum(idx);
end
end
end
%% Get the data for IM
data = str2fullfile(dataPath,'*_IM_*');
if ~iscell(data); tmp{1} = data; data = tmp; end
animacyIM = []; visibilityIM = []; trialMatrixIM = [];
% load the data
for d = 1:length(data)
load(data{d},'A','V','P');
% if wrong buttons were used
tmp = unique(A(:,1)); tmp(isnan(tmp)) = [];
if tmp == 2 % shifted buttons to the right
fprintf('Wrong buttons were used, adjust them now. \n')
A(A(:,1)==2,1) = 1; % shift them back
A(isnan(A(:,1)),1) = 2;
end
animacyIM = [animacyIM; A];
visibilityIM = [visibilityIM; V];
trialMatrixIM = [trialMatrixIM; P.trialMatrix];
clear A B T P
end
% recode respones
nTrials = length(animacyIM);
imagined = zeros(nTrials,1);
for t = 1:nTrials
imagined(t) = trialMatrixIM(t,trialMatrixIM(t,3));
if animacyIM(t,3)
animacyIM(t,1) = 3-animacyIM(t,1);
end
if visibilityIM(t,3)
visibilityIM(t,1) = 5-visibilityIM(t,1);
end
end
% recode animacy
At = imagined < 3;
Ab = animacyIM(:,1) == 1;
% get accuracy
Ar(nSOAs+1) = sum(Ab==At)/length(At);
% get D prime
H = sum(Ab(At == 1))/length(Ab(At == 1));
if H == 0; H = 0.00001; elseif H == 1; H = 1-0.00001; end % can't handle 0 or 1
Fa = sum(Ab(At == 0))/length(Ab(At == 0));
if Fa == 0; Fa = 0.00001; elseif Fa == 1; Fa = 1-0.00001; end
D(nSOAs+1) = dprime(H,Fa,sum(At==1),sum(At==0));
% get score visibility
for v = 1:nVis
Vr(nSOAs+1,v) = sum(visibilityIM(:,1)==v)/nTrials;
end
mVis(nSOAs+1) = nanmean(visibilityIM(:,1),1);
% calculate accuracy and visibility per stim
for st = 1:nStim
idx = imagined==st;
if st < 3
A_stim(st,nSOAs+1) = sum(animacyIM(idx,1)==1)/sum(idx);
elseif st > 2
A_stim(st,nSOAs+1) = sum(animacyIM(idx,1)==2)/sum(idx);
end
for v = 1:4
V_stim(st,nSOAs+1,v) = sum(visibilityIM(idx,1)==v)/sum(idx);
end
end
%% Save everything
save(fullfile(outputDir,'behaviourData'),'V_stim','A_stim','Vr','D','Ar','mVis')
%% Plot the things
if cfg.plot
subplot(4,3,1:3);
bar(D)
set(gca,'XTicklabel',{'0 ms','67 ms','Imagined'})
xlim([0 4]);
xlabel('Stimulus Onset Asynchrony')
ylabel('D prime')
subplot(4,3,4:6);
bar(Vr);
ylabel('Counts'); legend('Visbility 1','Visbility 2','Visibility 3','Visibility 4')
set(gca,'XTicklabel',{'0 ms','67 ms','Imagined'})
subplot(4,3,7:9);
bar(A_stim'); legend('Stim 1','Stim 2','Stim 3','Stim 4');
set(gca,'XTicklabel',{'0 ms','67 ms','Imagined'})
ylabel('Accuracy'); ylim([0 1])
name = {'0 ms','67 ms','Imagined'};
for s = 1:3
subplot(4,3,s+9)
bar(squeeze(V_stim(:,s,:)));
xlabel('Stimulus'); ylabel('Counts')
ylim([0 1]);
title(name{s}); legend('Visbility 1','Visbility 2','Visibility 3','Visibility 4')
end
end