Skip to content

Commit 6de8f98

Browse files
committed
Updated ERP code
1 parent 4f366d7 commit 6de8f98

File tree

1 file changed

+146
-4
lines changed

1 file changed

+146
-4
lines changed

code/preprocessing-eeg/create_mat.m

Lines changed: 146 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
% dataset_dir = '/Users/fzaki001/thrive-theta-ddm';
3333
dataset_dir = '/home/data/NDClab/datasets/thrive-dataset/derivatives/preprocessed/';
3434
% summary_csv_path = '/Users/fzaki001/thrive-theta-ddm/derivatives/behavior/summary.csv';
35-
summary_csv_path = '/home/data/NDClab/analyses/thrive-theta-ddm/derivatives/behavior/summary-eeg.csv';
35+
summary_csv_path = '/home/data/NDClab/analyses/thrive-theta-ddm/derivatives/behavior/summary.csv';
3636

3737
% Setting up other things
3838

@@ -74,8 +74,6 @@
7474
end
7575

7676
%% Count trials
77-
subjects = [50, 53, 60, 62, 64, 66] % these are just ids to put to the resulting csv, they are not used for computations
78-
% n_subjects = length(subjects) % this number will be used for loops because the initial subjects list may be changed based on num of trials
7977
% switch to output directory
8078
cd(output_location);
8179

@@ -158,4 +156,148 @@
158156
%write/append table to disk
159157
writetable(counts_table, [output_location filesep 'thrive_trialCounts_RespAndStim_', date, '.csv'], "WriteMode", "append");
160158

161-
end
159+
end
160+
161+
%% pull resp-locked erp mat file
162+
163+
%read in behavioral data for participants
164+
behavior_info = readtable(summary_csv_path);
165+
166+
%specify min number of trials per condition (if file contains less than
167+
%this number for ANY condition, then they will be skipped for ALL conditions
168+
minTrials = 8;
169+
170+
%specify min accuracy per condition (if file contains less than
171+
%this number for ANY condition, then they will be skipped for ALL conditions
172+
acc_cutoff = .6;
173+
174+
%initialize participant counter variable (used for indexing into large mat
175+
%file that data is saved into)
176+
pIdx = 1;
177+
178+
%initialize matrices to hold erp data and corresponding sub ids
179+
erpDat_data = [];
180+
erpDat_subIds = [];
181+
182+
% loop through each participant in the study
183+
for subject = 1:length(datafile_names)
184+
185+
%initialize numTrials for this participant/file
186+
numTrials = [];
187+
188+
% extract participant number
189+
subNumText = datafile_names{subject}(5:11);
190+
191+
%find row in behavior file corresponding to this participant
192+
behavior_id_match_idxs = find(behavior_info{:,'sub'} == str2num(subNumText));
193+
194+
%if participant has low accuracy in either condition, skip that
195+
%participant for ALL conditions
196+
% if (behavior_info{behavior_id_match_idxs,'acc_nonsoc'} < acc_cutoff || behavior_info{behavior_id_match_idxs,'acc_soc'} < acc_cutoff)
197+
% continue
198+
% end
199+
%
200+
% if (behavior_info{behavior_id_match_idxs,'x6_or_more_err_nonsoc'} < 6 || behavior_info{behavior_id_match_idxs,'x6_or_more_err_soc'} < 6)
201+
% continue
202+
% end
203+
%load the original data set
204+
EEG = pop_loadset( 'filename', datafile_names{subject}, 'filepath', datafile_paths{subject});
205+
EEG = eeg_checkset( EEG );
206+
207+
%remove all the non-stim-locking markers (should have done already...)
208+
EEG = pop_selectevent( EEG, 'latency','-.1 <= .1','deleteevents','on');
209+
EEG = eeg_checkset( EEG );
210+
211+
% NOTE %
212+
%the logic of checking conditions and then looping over conditions
213+
%below is fairly hard-coded and could be be substantially improved to
214+
%allow for easier reuse when number of conditions or number of
215+
%variables per condition changes.
216+
%
217+
%before pulling trials of interest, for any conditions, check to make
218+
%sure this file/participant has more than minTrials for EACH condition.
219+
%If the file/participant is below minTrials for even one of the
220+
%conditions that will be pulled, then the file/participant is skipped
221+
%entirely and no condition data at all will be pulled for this specific
222+
%file (but the participant can still have data pulled for another one
223+
%of their files from another visit).
224+
%
225+
%count trials for each condition of interest and store in numTrials vector
226+
numTrials(1) = length(find( (strcmp({EEG.event.observation}, "s")) & (strcmp({EEG.event.eventType}, "resp")) & (strcmp({EEG.event.congruency}, "i")) & ([EEG.event.accuracy] == 0) & ([EEG.event.responded] == 1) & ([EEG.event.validRt] == 1) ));
227+
numTrials(2) = length(find( (strcmp({EEG.event.observation}, "s")) & (strcmp({EEG.event.eventType}, "resp")) & (strcmp({EEG.event.congruency}, "i")) & ([EEG.event.accuracy] == 1) & ([EEG.event.responded] == 1) & ([EEG.event.validRt] == 1) ));
228+
numTrials(3) = length(find( (strcmp({EEG.event.observation}, "ns")) & (strcmp({EEG.event.eventType}, "resp")) & (strcmp({EEG.event.congruency}, "i")) & ([EEG.event.accuracy] == 0) & ([EEG.event.responded] == 1) & ([EEG.event.validRt] == 1) ));
229+
numTrials(4) = length(find( (strcmp({EEG.event.observation}, "ns")) & (strcmp({EEG.event.eventType}, "resp")) & (strcmp({EEG.event.congruency}, "i")) & ([EEG.event.accuracy] == 1) & ([EEG.event.responded] == 1) & ([EEG.event.validRt] == 1) ));
230+
231+
%logical test if the number of trials for each condition (numTrials vector)
232+
%are NOTE all >= minTrials. If statement is true, then participant/file
233+
%is skipped and for loop over files continues to next file
234+
% if ~(sum(numTrials >= minTrials) == length(numTrials))
235+
% continue
236+
% end
237+
238+
% loop through conditions of interest for this file (combo of event types)
239+
%
240+
% specify number of conditions using a seperate conditionNums var, so
241+
% that it can be referenced below when iterating idx counters (to only
242+
%iterate when c == length(conditionNums);
243+
conditionNums = 1:4;
244+
%
245+
for c = conditionNums
246+
247+
if (c==1) % social error
248+
observation = 's';
249+
eventType = 'resp';
250+
congruency = 'i';
251+
accuracy = 0;
252+
responded = 1;
253+
validRt = 1;
254+
elseif (c==2) % social correct
255+
observation = 's';
256+
eventType = 'resp';
257+
congruency = 'i';
258+
accuracy = 1;
259+
responded = 1;
260+
validRt = 1;
261+
elseif (c==3) % nonsocial error
262+
observation = 'ns';
263+
eventType = 'resp';
264+
congruency = 'i';
265+
accuracy = 0;
266+
responded = 1;
267+
validRt = 1;
268+
elseif (c==4) % nonsocial correct
269+
observation = 'ns';
270+
eventType = 'resp';
271+
congruency = 'i';
272+
accuracy = 1;
273+
responded = 1;
274+
validRt = 1;
275+
end
276+
277+
%select combintion of event types of interest based on vars above
278+
EEG1 = pop_selectevent( EEG, 'latency','-1<=1','observation',observation,'eventType',eventType,'congruency',congruency,'accuracy',accuracy,'responded',responded,'validRt',validRt,'deleteevents','on','deleteepochs','on','invertepochs','off');
279+
EEG1 = eeg_checkset( EEG1 );
280+
281+
% Average across epoch dimension
282+
% this all Channel ERP only needs to be computed once
283+
% per condition
284+
meanEpochs = mean(EEG1.data, 3);
285+
286+
%store data for this condition in array
287+
erpDat_data(pIdx,c,:,:)= meanEpochs;
288+
289+
%store participant number for corresponding row in erpdat
290+
erpDat_subIds{pIdx,1} = datafile_names{subject}(5:11);
291+
292+
%iterate idx counter IMPORTANT: ONLY ITERATE COUNTER WHEN
293+
%ON LAST CONDITION
294+
if c == length(conditionNums)%if this is the last condition of condition loop
295+
pIdx = pIdx + 1;
296+
end
297+
%end loop through conditions
298+
end
299+
%end loop through participants
300+
end
301+
302+
%save the erps and subject list
303+
save('thrive_Resp_erps_min_8t_60acc.mat','erpDat_data', 'erpDat_subIds')

0 commit comments

Comments
 (0)