forked from anne-urai/2019_Urai_choice-history-ddm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_into_Matlab_gSquare.m
96 lines (77 loc) · 3.18 KB
/
read_into_Matlab_gSquare.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
function read_into_Matlab_gSquare(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
addpath(genpath('~/code/Tools'));
close all; clc;
warning off MATLAB:table:ModifiedVarnames % skip this warning
for d = 1:length(datasets),
clear allres;
mypath = '/nfs/aeurai/HDDM';
usepath = sprintf('/nfs/aeurai/HDDM/%s/', datasets{d});
savepath = sprintf('/nfs/aeurai/HDDM/summary/%s', datasets{d});
if ~exist(savepath, 'dir'),
cp = pwd; cd('/nfs/aeurai/HDDM/summary');
mkdir(datasets{d}); cd(cp);
end
disp(usepath);
% GRAB ALL MODELS THAT EXIST!
mdls = dir(usepath);
mdls = mdls(~ismember({mdls.name},{'.','..', '.DS_Store'}));
mdls = mdls([mdls.isdir]); % only return directories
mdls = {mdls(:).name};
% load the csv file to check the existing subject numbers for later
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));
subjects = unique(alldata.subj_idx); clear alldata;
for m = 1:length(mdls),
% skip if this model is empty
stuff = dir(sprintf('%s/%s', usepath, mdls{m}));
stuff = stuff(arrayfun(@(x) ~strcmp(x.name(1),'.'),stuff)); % remove hidden files
if isempty(stuff),
disp(['skipping ' mdls{m}]);
continue;
end
if ~exist(sprintf('%s/%s/BIC.csv', usepath, mdls{m}), 'file'),
continue;
end
% ============================================ %
% GET THE GSQUARE FITS
% ============================================ %
gsq = readtable(sprintf('%s/%s/Gsquare.csv', usepath, mdls{m}));
% ============================================ %
% APPEND BIC VALUES
% ============================================ %
BIC = readtable(sprintf('%s/%s/BIC.csv', usepath, mdls{m}));
% assert(1==0)
gsq.bic = BIC.bic;
gsq.likelihood = BIC.likelihood;
gsq.penalty = BIC.penalty;
% ============================================ %
% rename to indicate the model
% ============================================ %
gsq.Var1 = [];
varnames = gsq.Properties.VariableNames;
varnames(ismember(varnames, 'subj_idx')) = [];
for f = 1:length(varnames),
gsq.Properties.VariableNames{varnames{f}} = regexprep([regexprep(varnames{f}, '__1', '_2'), ...
'__' regexprep(mdls{m}, '_', '')], '___', '__');
end
gsq.Properties.VariableNames{'subj_idx'} = 'subjnr';
if ~exist('allres', 'var')
allres = gsq;
else
allres = join(allres, gsq);
end
end
% remove duplicate columns
if exist('allres', 'var'),
writetable(allres, sprintf('%s/individualresults_Gsq.csv', savepath));
end
allres.bic__stimcodingnohist
end % datasets
end