forked from chassall/continuousfeedback
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis_04_model.m
More file actions
174 lines (140 loc) · 7.89 KB
/
analysis_04_model.m
File metadata and controls
174 lines (140 loc) · 7.89 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
% Compute instantaneous reward signals, etc.
%
% Other m-files required:
% EEGLAB toolbox https://github.com/sccn/eeglab
% /private/getpds
% Author: Cameron Hassall, Department of Psychiatry, University of Oxford
% email address: cameron.hassall@psych.ox.ac.uk
% Website: http://www.cameronhassall.com
% Behavioural file headers
% [t thisGnomeType gnomeImageNumbeinstReward(thisGnomeType) thisTarget fixationTime rtStart rtEnd rexpectancyponseTime rexpectancypX rexpectancypY guexpectancysProp guexpectancysPoints totalPoints preFeedbackTime];
% 1 trial
% 2 gnome type: 1 = low, 2 = high, 3 = low/high, 4 = LOW/high, 5 = low/HIGH, 6 = uniform
% 3 image number
% 4 target level (proportion of bar, 0-1)
% 5 fixation time
% 6 rt start time
% 7 rt end time
% 8 total rexpectancyponse time
% 9 rexpectancyponse x
% 10 rexpectancyponse y
% 11 partipant guexpectancys (proportion of bar, 0-1)
% 12 points this round
% 13 total points
% 14 pre-feedback time (time before bar starts to grow)
close all; clear all; clc;
if ispc
projectFolder = 'E:\OneDrive - Nexus365\Projects\2021_EEG_Gnomes_Hassall';
else
projectFolder = '/Users/chassall/OneDrive - Nexus365/Projects/2021_EEG_Gnomes_Hassall';
end
dataFolder = [projectFolder '/data'];
ps = {'01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21'};
allGuexpectancys = nan(length(ps),6,25);
meanGuexpectancys = nan(length(ps),6);
pbarHeight = getpds(); % Probability density for each gnome 1-6
for p = 1:length(ps)
% Load behavioural data
thisBehFolder = [dataFolder '/sub-' ps{p} '/beh'];
thisBehFile = ['sub-' ps{p} '_task-gnomes_beh.tsv'];
tempData = tdfread(fullfile(thisBehFolder,thisBehFile));
participantData = [tempData.trial tempData.condition tempData.image tempData.target tempData.fixationTime tempData.rtStart tempData.rtEnd tempData.rt tempData.responseX tempData.responseY tempData.responseProp tempData.pointsThisRound tempData.pointsTotal tempData.preFeedbackTime];
% Load EEG
thisEEGFolder = [dataFolder '/derivatives/eegprep/sub-' ps{p}];
thisEEGFile = ['sub-' ps{p} '_task-gnomes_eegprep.mat'];
load(fullfile(thisEEGFolder,thisEEGFile));
timexpectancytep = 1000/EEG.srate;
for g = 1:6
thisData = participantData(participantData(:,2) == g,:);
meanGuexpectancys(p,g) = mean(thisData(:,11));
allGuexpectancys(p,g,:) = thisData(:,11);
end
t = 1; % trial counter
% Initialize regrexpectancysoinstReward
barHeight = zeros(1,EEG.pnts);
instReward = zeros(1,EEG.pnts);
instRewardFull = zeros(1,EEG.pnts); % trials for which the bar reached the guess
expectancy = zeros(1,EEG.pnts);
instPE = zeros(1,EEG.pnts);
aveRew = zeros(1,EEG.pnts);
barHeightSplit = zeros(3,EEG.pnts);
instRewardSplit = zeros(3,EEG.pnts);
barHeightRiseFall = zeros(2,EEG.pnts);
instRewardRiseFall = zeros(2,EEG.pnts);
barHeightRiseFallSplit = zeros(3,2,EEG.pnts);
instRewardRiseFallSplit = zeros(3,2,EEG.pnts);
% Loop over events
for e = 1:length(EEG.event)
thisEvent = EEG.event(e);
if ismember(thisEvent.type,{'S 41','S 42','S 43','S 44','S 45','S 46'})
thisGnomeType = participantData(t,2);
thisPD = pbarHeight{thisGnomeType};
thisTrialTarget = participantData(t,4);
thisTrialGuess = participantData(t,11);
startTime = round(thisEvent.latency);
endTime = round(EEG.event(e+1).latency);
prevData= participantData(1:t,:);
prevData = prevData(prevData(:,2)==thisGnomeType,:);
numEncounters = size(prevData,1);
if size(prevData,1) == 1 % This is the first encounter with this gnome
thisAveRew = 0.5;
else % Compute the average reward for this gnome
thesePoints = 1 - abs(prevData(1:numEncounters-1,11) - prevData(1:numEncounters-1,4));
thisAveRew = mean(thesePoints);
% thisAveRew = mean(prevData(1:numEncounters-1,12))/100;
end
%thisPE = prevData(numEncounters,12) - aveR;
%peByGnome(thisGnomeType,numEncounters) = thisPE;
%plot(peByGnome'); drawnow(); pause();
numTimesteps = endTime-startTime+1;
barInc = thisTrialTarget/numTimesteps;
thisTrialI = startTime:endTime;
barHeight(thisTrialI) = cumsum(barInc*ones(1,numTimesteps));
%if thisTrialGuess > thisTrialTarget
instReward(thisTrialI) = 1 - abs(thisTrialGuess - barHeight(thisTrialI));
instPE(thisTrialI) = instReward(thisTrialI) - thisAveRew;
aveRew(thisTrialI) = thisAveRew;
%end
expectancy(thisTrialI) = pdf(thisPD,barHeight(thisTrialI)');
isRising = barHeight(thisTrialI) <= thisTrialGuess;
isFalling = barHeight(thisTrialI) > thisTrialGuess;
% if any(isRising) && any(isFalling)
% return;
% end
barHeightRiseFall(1,thisTrialI(isRising)) = barHeight(thisTrialI(isRising));
instRewardRiseFall(1,thisTrialI(isRising)) = instReward(thisTrialI(isRising));
barHeightRiseFall(2,thisTrialI(isFalling)) = barHeight(thisTrialI(isFalling));
instRewardRiseFall(2,thisTrialI(isFalling)) = instReward(thisTrialI(isFalling));
if ismember(thisEvent.type,{'S 41','S 42'})
barHeightSplit(1,thisTrialI) = cumsum(barInc*ones(1,numTimesteps));
instRewardSplit(1,thisTrialI) = 1 - abs(thisTrialGuess - barHeight(thisTrialI));
barHeightRiseFallSplit(1,1,thisTrialI(isRising)) = barHeight(thisTrialI(isRising));
instRewardRiseFallSplit(1,1,thisTrialI(isRising)) = instReward(thisTrialI(isRising));
barHeightRiseFallSplit(1,2,thisTrialI(isFalling)) = barHeight(thisTrialI(isFalling));
instRewardRiseFallSplit(1,2,thisTrialI(isFalling)) = instReward(thisTrialI(isFalling));
elseif ismember(thisEvent.type,{'S 44','S 45'})
barHeightSplit(2,thisTrialI) = cumsum(barInc*ones(1,numTimesteps));
instRewardSplit(2,thisTrialI) = 1 - abs(thisTrialGuess - barHeight(thisTrialI));
barHeightRiseFallSplit(2,1,thisTrialI(isRising)) = barHeight(thisTrialI(isRising));
instRewardRiseFallSplit(2,1,thisTrialI(isRising)) = instReward(thisTrialI(isRising));
barHeightRiseFallSplit(2,2,thisTrialI(isFalling)) = barHeight(thisTrialI(isFalling));
instRewardRiseFallSplit(2,2,thisTrialI(isFalling)) = instReward(thisTrialI(isFalling));
elseif ismember(thisEvent.type,{'S 43','S 46'})
barHeightSplit(3,thisTrialI) = cumsum(barInc*ones(1,numTimesteps));
instRewardSplit(3,thisTrialI) = 1 - abs(thisTrialGuess - barHeight(thisTrialI));
barHeightRiseFallSplit(3,1,thisTrialI(isRising)) = barHeight(thisTrialI(isRising));
instRewardRiseFallSplit(3,1,thisTrialI(isRising)) = instReward(thisTrialI(isRising));
barHeightRiseFallSplit(3,2,thisTrialI(isFalling)) = barHeight(thisTrialI(isFalling));
instRewardRiseFallSplit(3,2,thisTrialI(isFalling)) = instReward(thisTrialI(isFalling));
end
t = t+1;
end
end
expectancy = expectancy ./ max(expectancy);
thisRegFolder = [dataFolder '/derivatives/behmod/sub-' ps{p}];
thisRegFile = ['sub-' ps{p} '_task-gnomes_reg.mat'];
if ~isfolder(thisRegFolder)
mkdir(thisRegFolder);
end
save(fullfile(thisRegFolder,thisRegFile),'barHeight','barHeightSplit','barHeightRiseFall','instReward','instPE','aveRew','instRewardRiseFall','instRewardSplit','expectancy','instRewardRiseFallSplit','barHeightRiseFallSplit');
end