-
Notifications
You must be signed in to change notification settings - Fork 4
/
make_dataframe.m
233 lines (195 loc) · 10.4 KB
/
make_dataframe.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
function make_dataframe(datasets)
% Code to fit the history-dependent drift diffusion models as described in
% Urai AE, de Gee JW, Tsetsos K, Donner TH (2019) Choice history biases subsequent evidence accumulation. eLife, in press.
%
% MIT License
% Copyright (c) Anne Urai, 2019
close all; clc;
addpath(genpath('~/code/Tools'));
warning off; global mypath
% ============================================ %
% SUMMARIZE EACH DATASET
% ============================================ %
set(groot, 'defaultaxesfontsize', 7, 'defaultaxestitlefontsizemultiplier', 1, ...
'defaultaxestitlefontweight', 'bold', ...
'defaultfigurerenderermode', 'manual', 'defaultfigurerenderer', 'painters');
ds = 1:length(datasets);
for d = ds,
disp(datasets{d});
% load data
csvfile = dir(sprintf('%s/%s/*.csv', mypath, datasets{d}));
csvfile = csvfile(arrayfun(@(x) ~strcmp(x.name(1),'.'), csvfile)); % remove hidden files
alldata = readtable(sprintf('%s/%s/%s', mypath, datasets{d}, csvfile.name));
% recode Anke's stimulus into stim and coh
if ~isempty(strfind(datasets{d}, 'Anke')) | ~isempty(strfind(datasets{d}, 'NatComm')) | ~isempty(strfind(datasets{d}, 'Bharath')),
alldata.coherence = abs(alldata.stimulus);
alldata.stimulus2 = sign(alldata.stimulus);
try; alldata.stimulus2(alldata.coherence == 0) = sign(alldata.motionenergy(alldata.coherence == 0)); end
alldata.stimulus = alldata.stimulus2;
end
% SAVE IN A NICE CSV
switch d
case 1
writetable(alldata, sprintf('%s/summary/visual_motion_2afc_rt.csv', mypath));
case 2
writetable(alldata, sprintf('%s/summary/visual_motion_2afc_fd.csv', mypath));
case 3
writetable(alldata, sprintf('%s/summary/visual_motion_2ifc_fd_1.csv', mypath));
case 4
writetable(alldata, sprintf('%s/summary/visual_motion_2ifc_fd_2.csv', mypath));
case 5
writetable(alldata, sprintf('%s/summary/visual_contrast_yesno.csv', mypath));
case 6
writetable(alldata, sprintf('%s/summary/auditory_yesno.csv', mypath));
end
% compute a bunch of basic things from Matlab
% ===================================== %
results = define_behavioral_metrics(alldata);
% ===================================== %
results = [results; array2table(nan(size(results.Properties.VariableNames)), ...
'variablenames', results.Properties.VariableNames)];
results.session(isnan(results.subjnr)) = 0;
results.repetition(isnan(results.subjnr)) = 0;
results.subjnr(isnan(results.subjnr)) = 0;
% add personality scores and drug conditions
switch datasets{d}
case {'MEG', 'MEG_MEGsessions', 'MEG_MEGdata'},
disp('adding in personality questionnaires');
results.drug = repmat({'NaN'}, length(results.dprime), 1);
results.BIS = nan(size(results.dprime));
results.BAS = nan(size(results.dprime));
results.AQ = nan(size(results.dprime));
results.PSWQ = nan(size(results.dprime));
sjs = unique(results.subjnr)';
sjs(sjs == 0) = []; % exclude group average
for sj = sjs,
subjectdata = subjectspecifics(sj);
results.drug(results.subjnr == sj) = {subjectdata.drug};
results.BIS(results.subjnr == sj) = subjectdata.BIS;
results.BAS(results.subjnr == sj) = subjectdata.BAS;
results.AQ(results.subjnr == sj) = subjectdata.AQ;
results.PSWQ(results.subjnr == sj) = subjectdata.PSWQ;
end
end
for whichFit = 1:2,
switch whichFit
case 1
% get the summary results from HDDM
hddmresults = readtable(sprintf('%s/summary/%s/individualresults.csv', mypath, datasets{d}));
case 2
if exist(sprintf('%s/summary/%s/individualresults_Gsq.csv', mypath, datasets{d}), 'file'),
hddmresults = readtable(sprintf('%s/summary/%s/individualresults_Gsq.csv', mypath, datasets{d}));
else
continue;
end
end
% most parameters will go under session 0
hddmresults.session = zeros(size(hddmresults.subjnr));
% will only keep session 0 stuff
allresults = innerjoin(results, hddmresults, 'keys', {'subjnr', 'session'});
% now add back all the stuff from the different sessions
allresults2 = tableAppend(allresults, results);
% remove duplicate rows, save only those with HDDM info
% http://stackoverflow.com/questions/27547463/matlab-delete-duplicate-table-entries-on-multiple-columns
[~, ind] = unique(allresults2(:, [1 2]), 'rows');
tab = allresults2(ind,:);
assert(any(ismember(tab.subjnr, 0)), 'group average missing');
% ============================================ %
% RECODE SESSION-SPECIFIC PARAMETERS
% ============================================ %
% manually recode the drift rate parameters to match the specific session
switch datasets{d}
case 'RT_RDK'
sessions = 1:5;
case {'MEG', 'MEG_MEGsessions'};
case 'NatComm'
sessions = 1:5;
end
varidx = find(~cellfun(@isempty, strfind(tab.Properties.VariableNames, sprintf('_s%d_', 1))));
vars = tab.Properties.VariableNames(varidx);
for v = 1:length(vars),
for s = sessions,
% if this is the first session, make a new column for
% the overall drift rate (which will then be repopulated per
% session)
if s == min(sessions),
newvar = regexprep(vars{v}, sprintf('_s%d__', s), '__');
tab.(newvar) = nan(size(tab.(vars{v})));
thisvar = vars{v};
else
thisvar = regexprep(vars{v}, '_s1_', sprintf('_s%d_', s));
end
% then, move the values over
try
tab.(newvar)(tab.session == s) = tab.(thisvar)(tab.session == 0);
% can happen that there is no session 2-4 (MEG pupil)
end
end
end
% remove sessions where no data was recorded
skippedSession = (isnan(nanmean(tab{:, 3:11}, 2)));
tab(skippedSession, :) = [];
% group-level HDDM estimates - remove sjnr and session nr
tab.session(tab.subjnr == 0) = NaN;
tab.subjnr(tab.subjnr == 0) = NaN;
% ============================================ %
% SAVE TO FIGSHARE FOR CLEARER OVERVIEW
% ============================================ %
switch whichFit
case 1
writetable(tab, sprintf('%s/summary/%s/allindividualresults.csv', mypath, datasets{d}));
%% ALSO SAVE FOR FIGSHARE
tab2 = tab(tab.session == 0 | isnan(tab.session), :);
tab2(isnan(tab2.subjnr), :) = [];
tab2(:, cat(2, {'repetition2', 'repetition3', 'dic', 'criterionshift'}, ...
tab2.Properties.VariableNames(endsWith(tab2.Properties.VariableNames, 'groupsplit')), ...
tab2.Properties.VariableNames(endsWith(tab2.Properties.VariableNames, 'svgroup')), ...
tab2.Properties.VariableNames(endsWith(tab2.Properties.VariableNames, 'multiplicative')), ...
tab2.Properties.VariableNames(endsWith(tab2.Properties.VariableNames, 'congruency')), ...
tab2.Properties.VariableNames(startsWith(tab2.Properties.VariableNames, 'cum')), ...
tab2.Properties.VariableNames(startsWith(tab2.Properties.VariableNames, 'alt')), ...
tab2.Properties.VariableNames(startsWith(tab2.Properties.VariableNames, 'sv')), ...
tab2.Properties.VariableNames(startsWith(tab2.Properties.VariableNames, 'sz')))) = [];
switch d
case 1
writetable(tab2, sprintf('%s/summary/visual_motion_2afc_rt_hddmfits.csv', mypath));
case 2
writetable(tab2, sprintf('%s/summary/visual_motion_2afc_fd_hddmfits.csv', mypath));
case 3
writetable(tab2, sprintf('%s/summary/visual_motion_2ifc_fd_1_hddmfits.csv', mypath));
case 4
writetable(tab2, sprintf('%s/summary/visual_motion_2ifc_fd_2_hddmfits.csv', mypath));
case 5
writetable(tab2, sprintf('%s/summary/visual_contrast_yesno_hddmfits.csv', mypath));
case 6
writetable(tab2, sprintf('%s/summary/auditory_yesno_hddmfits.csv', mypath));
end
case 2
writetable(tab, sprintf('%s/summary/%s/allindividualresults_Gsq.csv', mypath, datasets{d}));
%% ALSO SAVE FOR FIGSHARE
tab2 = tab(tab.session == 0, :);
tab2(isnan(tab2.subjnr), :) = [];
tab2(:, cat(2, {'repetition2', 'repetition3', 'criterionshift'}, ...
tab2.Properties.VariableNames(startsWith(tab2.Properties.VariableNames, 'cum')), ...
tab2.Properties.VariableNames(startsWith(tab2.Properties.VariableNames, 'alt')), ...
tab2.Properties.VariableNames(startsWith(tab2.Properties.VariableNames, 'sv')), ...
tab2.Properties.VariableNames(startsWith(tab2.Properties.VariableNames, 'sz')))) = [];
switch d
case 1
writetable(tab2, sprintf('%s/summary/visual_motion_2afc_rt_gsquarefits.csv', mypath));
case 2
writetable(tab2, sprintf('%s/summary/visual_motion_2afc_fd_gsquarefits.csv', mypath));
case 3
writetable(tab2, sprintf('%s/summary/visual_motion_2ifc_fd_1_gsquarefits.csv', mypath));
case 4
writetable(tab2, sprintf('%s/summary/visual_motion_2ifc_fd_2_gsquarefits.csv', mypath));
case 5
writetable(tab2, sprintf('%s/summary/visual_contrast_yesno_gsquarefits.csv', mypath));
case 6
writetable(tab2, sprintf('%s/summary/auditory_yesno_gsquarefits.csv', mypath));
end
end
fprintf('%s/summary/%s/allindividualresults.csv \n', mypath, datasets{d});
end
end