analyse_01_preprocess_localiser.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. %% Setup
  2. % paths to data
  3. eeg_path = fullfile('raw_data', 'eeg-pc', 'localiser');
  4. beh_path = fullfile('raw_data', 'stim-pc', 'data', 'localiser');
  5. % import eeglab (assumes eeglab has been added to path), e.g.
  6. addpath('C:/EEGLAB/eeglab2020_0')
  7. [ALLEEG, EEG, CURRENTSET, ALLCOM] = eeglab;
  8. % As this uses fastica algorithm for ICA, FastICA needs to be on the path, e.g.
  9. addpath('C:/EEGLAB/FastICA_25')
  10. % region of interest for finding maximal electrodes
  11. roi = {'TP7', 'CP5', 'P7', 'P5', 'P9', 'PO7', 'PO3', 'O1'};
  12. % cutoff probability for identifying eye and muscle related ICA components with ICLabel
  13. icl_cutoff = 0.85;
  14. % sigma parameter for ASR
  15. asr_sigma = 20;
  16. %% Clear Output Folders
  17. delete(fullfile('localiser_sample_data', '*.csv'))
  18. %% Import lab book
  19. % handle commas in vectors
  20. lab_book_file = fullfile('raw_data', 'stim-pc', 'participants.csv');
  21. lab_book_raw_dat = fileread(lab_book_file);
  22. [regstart, regend] = regexp(lab_book_raw_dat, '\[.*?\]');
  23. for regmatch_i = 1:numel(regstart)
  24. str_i = lab_book_raw_dat(regstart(regmatch_i):regend(regmatch_i));
  25. str_i(str_i==',') = '.';
  26. lab_book_raw_dat(regstart(regmatch_i):regend(regmatch_i)) = str_i;
  27. end
  28. lab_book_fixed_file = fullfile('raw_data', 'stim-pc', 'participants_tmp.csv');
  29. lab_book_fixed_conn = fopen(lab_book_fixed_file, 'w');
  30. fprintf(lab_book_fixed_conn, lab_book_raw_dat);
  31. fclose(lab_book_fixed_conn);
  32. lab_book_readopts = detectImportOptions(lab_book_fixed_file, 'VariableNamesLine', 1, 'Delimiter', ',');
  33. % read subject ids as class character
  34. lab_book_readopts.VariableTypes{strcmp(lab_book_readopts.SelectedVariableNames, 'subj_id')} = 'char';
  35. lab_book = readtable(lab_book_fixed_file, lab_book_readopts);
  36. delete(lab_book_fixed_file)
  37. %% Count the total number of excluded electrodes
  38. n_bads = 0;
  39. n_bads_per_s = zeros(size(lab_book, 1), 0);
  40. for subject_nr = 1:size(lab_book, 1)
  41. bad_channels = eval(strrep(strrep(strrep(lab_book.loc_bad_channels{subject_nr}, '[', '{'), ']', '}'), '.', ','));
  42. n_bads_per_s(subject_nr) = numel(bad_channels);
  43. n_bads = n_bads + numel(bad_channels);
  44. end
  45. perc_bads = n_bads / (64 * size(lab_book, 1)) * 100;
  46. %% Set up results table
  47. max_elec_columns = {'subject_id',...
  48. 'max_elec_bacs', 'max_time_bacs', 'max_diff_bacs',...
  49. 'max_elec_noise', 'max_time_noise', 'max_diff_noise'};
  50. empty_tablecells = cell(size(lab_book, 1), numel(max_elec_columns));
  51. max_elecs = cell2table(empty_tablecells);
  52. max_elecs.Properties.VariableNames = max_elec_columns;
  53. %% Iterate over subjects
  54. % record trial exclusions
  55. total_excl_trials_incorr = zeros(1, size(lab_book, 1));
  56. total_excl_trials_rt = zeros(1, size(lab_book, 1));
  57. n_bad_ica = zeros(size(lab_book, 1), 0);
  58. for subject_nr = 1:size(lab_book, 1)
  59. subject_id = lab_book.subj_id{subject_nr};
  60. fprintf('\n\n Subject Iteration %g/%g, ID: %s\n', subject_nr, size(lab_book, 1), subject_id)
  61. %% get subject-specific info from lab book
  62. exclude = lab_book.exclude(subject_nr);
  63. bad_channels = eval(strrep(strrep(strrep(lab_book.loc_bad_channels{subject_nr}, '[', '{'), ']', '}'), '.', ','));
  64. bad_channels_pictureword = eval(strrep(strrep(strrep(lab_book.pw_bad_channels{subject_nr}, '[', '{'), ']', '}'), '.', ','));
  65. bad_trigger_indices = eval(strrep(lab_book.loc_bad_trigger_indices{subject_nr}, '.', ','));
  66. % add PO4 to bad channels, which seems to be consistently noisy, even when not marked as bad
  67. if sum(strcmp('PO4', bad_channels))==0
  68. bad_channels(numel(bad_channels)+1) = {'PO4'};
  69. end
  70. %% abort if excluded
  71. if exclude
  72. % this is not planned to be used, but will be an easy way for other
  73. % researchers to see the effect of excluding specific participants
  74. % by editing the participants.csv file
  75. fprintf('Subject %s excluded. Preprocessing aborted.\n', subject_id)
  76. fprintf('Lab book note: %s\n', lab_book.note{subject_nr})
  77. continue
  78. end
  79. if (numel(bad_channels) >= 10) || (numel(bad_channels_pictureword) >= 10)
  80. fprintf('Subject %s excluded as >=10 electrodes marked as bad in either task. Preprocessing aborted.\n', subject_id)
  81. fprintf('Lab book note: %s\n', lab_book.note{subject_nr})
  82. continue
  83. end
  84. %% load participant's data
  85. % load raw eeg
  86. raw_datapath = fullfile(eeg_path, append(subject_id, '.bdf'));
  87. % abort if no EEG data collected yet
  88. if ~isfile(raw_datapath)
  89. fprintf('Subject %s skipped: no EEG data found\n', subject_id)
  90. continue
  91. end
  92. EEG = pop_biosig(raw_datapath, 'importevent', 'on', 'rmeventchan', 'off');
  93. % load behavioural
  94. all_beh_files = dir(beh_path);
  95. beh_regex_matches = regexpi({all_beh_files.name}, append('^', subject_id, '_.+\.csv$'), 'match');
  96. regex_emptymask = cellfun('isempty', beh_regex_matches);
  97. beh_regex_matches(regex_emptymask) = [];
  98. subj_beh_files = cellfun(@(x) x{:}, beh_regex_matches, 'UniformOutput', false);
  99. if numel(subj_beh_files)>1
  100. fprintf('%g behavioural files found?\n', size(subj_beh_files))
  101. break
  102. end
  103. beh_datapath = fullfile(beh_path, subj_beh_files{1});
  104. beh = readtable(beh_datapath);
  105. %% Set data features
  106. % set channel locations
  107. orig_locs = EEG.chanlocs;
  108. EEG.chanlocs = pop_chanedit(EEG.chanlocs, 'load', {'BioSemi64.loc', 'filetype', 'loc'}); % doesn't match order for the data
  109. % set channel types
  110. for ch_nr = 1:64
  111. EEG.chanlocs(ch_nr).type = 'EEG';
  112. end
  113. for ch_nr = 65:72
  114. EEG.chanlocs(ch_nr).type = 'EOG';
  115. end
  116. for ch_nr = 73:79
  117. EEG.chanlocs(ch_nr).type = 'MISC';
  118. end
  119. for ch_nr = 65:79
  120. EEG.chanlocs(ch_nr).theta = [];
  121. EEG.chanlocs(ch_nr).radius = [];
  122. EEG.chanlocs(ch_nr).sph_theta = [];
  123. EEG.chanlocs(ch_nr).sph_phi = [];
  124. EEG.chanlocs(ch_nr).X = [];
  125. EEG.chanlocs(ch_nr).Y = [];
  126. EEG.chanlocs(ch_nr).Z = [];
  127. end
  128. % change the order of channels in EEG.data to match the new order in chanlocs
  129. data_reordered = EEG.data;
  130. for ch_nr = 1:64
  131. % make sure the new eeg data array matches the listed order
  132. ch_lab = EEG.chanlocs(ch_nr).labels;
  133. orig_locs_idx = find(strcmp(lower({orig_locs.labels}), lower(ch_lab)));
  134. data_reordered(ch_nr, :) = EEG.data(orig_locs_idx, :);
  135. end
  136. EEG.data = data_reordered;
  137. % remove unused channels
  138. EEG = pop_select(EEG, 'nochannel', 69:79);
  139. % plot the ROI for the paper
  140. if strcmp(subject_id, '1')
  141. roi_fig = figure;
  142. roi_idx = find(ismember({EEG.chanlocs.labels}, roi));
  143. hold on;
  144. topoplot(zeros(64, 0), EEG.chanlocs, 'electrodes', 'off');
  145. % set line width
  146. set(findall(gca, 'Type', 'Line'), 'LineWidth', 1);
  147. for i = 1:64
  148. if ismember(i, roi_idx)
  149. markcol = [1, 0, 0];
  150. else
  151. markcol = [0.75, 0.75, 0.75];
  152. end
  153. topoplot(zeros(64, 0), EEG.chanlocs, 'colormap', [0,0,0], 'emarker', {'.', markcol, 15, 1}, 'plotchans', i, 'headrad', 0);
  154. end
  155. hold off
  156. set(roi_fig, 'Units', 'Inches', 'Position', [0, 0, 1.5, 1.5], 'PaperUnits', 'Inches', 'PaperSize', [1.5, 1.5])
  157. exportgraphics(roi_fig, 'figs/roi_channels.pdf', 'BackgroundColor','none')
  158. close all
  159. end
  160. % remove bad channels
  161. ur_chanlocs = EEG.chanlocs; % store a copy of the full channel locations before removing (for later interpolation)
  162. bad_channels_indices = find(ismember(lower({EEG.chanlocs.labels}), lower(bad_channels)));
  163. EEG = pop_select(EEG, 'nochannel', bad_channels_indices);
  164. %% Identify events (trials)
  165. % make the sopen function happy
  166. x = fileparts( which('sopen') );
  167. rmpath(x);
  168. addpath(x,'-begin');
  169. % build the events manually from the raw eeg file (pop_biosig removes event offsets)
  170. % NB: this assumes no resampling between reading the BDF file and now
  171. bdf_dat = sopen(raw_datapath, 'r', [0, Inf], 'OVERFLOWDETECTION:OFF');
  172. event_types = bdf_dat.BDF.Trigger.TYP;
  173. event_pos = bdf_dat.BDF.Trigger.POS;
  174. event_time = EEG.times(event_pos);
  175. sclose(bdf_dat);
  176. clear bdf_dat;
  177. triggers = struct(...
  178. 'off', 0,...
  179. 'word', 101,...
  180. 'bacs', 102,...
  181. 'noise', 103,...
  182. 'practice', 25);
  183. % add 61440 to each trigger value (because of number of bits in pp)
  184. trigger_labels = fieldnames(triggers);
  185. for field_nr = 1:numel(trigger_labels)
  186. triggers.(trigger_labels{field_nr}) = triggers.(trigger_labels{field_nr}) + 61440;
  187. end
  188. % remove the first trigger if it is at time 0 and has a value which isn't a recognised trigger
  189. if (event_time(1)==0 && ~ismember(event_types(1), [triggers.off, triggers.word, triggers.bacs, triggers.noise, triggers.practice]))
  190. event_types(1) = [];
  191. event_pos(1) = [];
  192. event_time(1) = [];
  193. end
  194. % remove the new first trigger if it has a value of off
  195. if (event_types(1)==triggers.off)
  196. event_types(1) = [];
  197. event_pos(1) = [];
  198. event_time(1) = [];
  199. end
  200. % check every second trigger is an offset
  201. offset_locs = find(event_types==triggers.off);
  202. if any(offset_locs' ~= 2:2:numel(event_types))
  203. fprintf('Expected each second trigger to be an off?')
  204. break
  205. end
  206. % check every first trigger is non-zero
  207. onset_locs = find(event_types~=triggers.off);
  208. if any(onset_locs' ~= 1:2:numel(event_types))
  209. fprintf('Expected each first trigger to be an event?')
  210. break
  211. end
  212. % create the events struct manually
  213. events_onset_types = event_types(onset_locs);
  214. events_onsets = event_pos(onset_locs);
  215. events_offsets = event_pos(offset_locs);
  216. events_durations = events_offsets - events_onsets;
  217. EEG.event = struct();
  218. for event_nr = 1:numel(events_onsets)
  219. EEG.event(event_nr).type = events_onset_types(event_nr);
  220. EEG.event(event_nr).latency = events_onsets(event_nr);
  221. EEG.event(event_nr).offset = events_offsets(event_nr);
  222. EEG.event(event_nr).duration = events_durations(event_nr);
  223. end
  224. % copy the details over to urevent
  225. EEG.urevent = EEG.event;
  226. % record the urevent in event, for reference if they change
  227. for event_nr = 1:numel(events_onsets)
  228. EEG.event(event_nr).urevent = event_nr;
  229. end
  230. % remove bad events recorded in lab book (misfired triggers)
  231. EEG = pop_editeventvals(EEG, 'delete', find(ismember([EEG.event.urevent], bad_trigger_indices)));
  232. % remove practice trials
  233. EEG = pop_editeventvals(EEG, 'delete', find(ismember([EEG.event.type], triggers.practice)));
  234. % check the events make sense
  235. if sum(~ismember([EEG.event.type], [triggers.word, triggers.bacs, triggers.noise])) > 0
  236. fprintf('Unexpected trial types?\n')
  237. break
  238. end
  239. if numel({EEG.event.type})~=300
  240. fprintf('%g trial triggers detected (expected 300)?\n', numel({EEG.event.type}))
  241. break
  242. end
  243. if sum(ismember([EEG.event.type], [triggers.word])) ~= sum(ismember([EEG.event.type], [triggers.bacs]))
  244. fprintf('Unequal number of word and BACS trials?\n')
  245. break
  246. end
  247. if sum(ismember([EEG.event.type], [triggers.word])) ~= sum(ismember([EEG.event.type], [triggers.noise]))
  248. fprintf('Unequal number of word and noise trials?\n')
  249. break
  250. end
  251. % add the trials' onsets, offsets, durations, and triggers to the behavioural data
  252. beh.event = zeros(size(beh, 1), 1);
  253. beh.latency = zeros(size(beh, 1), 1);
  254. for row_nr = 1:size(beh, 1)
  255. cond_i = beh.condition(row_nr);
  256. beh.event(row_nr) = triggers.(cond_i{:}); % look up the trial's expected trigger
  257. beh.latency(row_nr) = EEG.event(row_nr).latency;
  258. beh.offset(row_nr) = EEG.event(row_nr).offset;
  259. beh.duration(row_nr) = EEG.event(row_nr).duration;
  260. beh.duration_ms(row_nr) = (EEG.event(row_nr).duration * 1000/EEG.srate) - 500; % minus 500 as event timer starts at word presentation, but rt timer starts once word turns green
  261. end
  262. % check events expected in beh are same as those in the events struct
  263. if any(beh.event' ~= [EEG.event.type])
  264. fprintf('%g mismatches between behavioural data and triggers?\n', sum(beh.event' ~= [EEG.event.type]))
  265. break
  266. end
  267. % check the difference between the durations and the response times (should be very small)
  268. % hist(beh.rt - beh.duration_ms, 100)
  269. % record trial numbers in EEG.event
  270. for row_nr = 1:size(beh, 1)
  271. EEG.event(row_nr).trl_nr = beh.trl_nr(row_nr);
  272. end
  273. %% Remove segments of data that fall outside of blocks
  274. % record block starts
  275. beh.is_block_start(1) = 1;
  276. for row_nr = 2:size(beh, 1)
  277. beh.is_block_start(row_nr) = beh.block_nr(row_nr) - beh.block_nr(row_nr-1) == 1;
  278. end
  279. % record block ends
  280. beh.is_block_end(size(beh, 1)) = 1;
  281. for row_nr = 1:(size(beh, 1)-1)
  282. beh.is_block_end(row_nr) = beh.block_nr(row_nr+1) - beh.block_nr(row_nr) == 1;
  283. end
  284. % record block boundaries (first start and last end point of each block, with 0.75 seconds buffer)
  285. beh.block_boundary = zeros(size(beh, 1), 1);
  286. for row_nr = 1:size(beh, 1)
  287. if beh.is_block_start(row_nr)
  288. beh.block_boundary(row_nr) = beh.latency(row_nr) - (EEG.srate * 0.75);
  289. elseif beh.is_block_end(row_nr)
  290. beh.block_boundary(row_nr) = beh.offset(row_nr) + (EEG.srate * 0.75);
  291. end
  292. end
  293. % get the boundary indices in required format (start1, end1; start2, end2; start3, end3)
  294. block_boundaries = reshape(beh.block_boundary(beh.block_boundary~=0), 2, [])';
  295. % remove anything outside of blocks
  296. EEG = pop_select(EEG, 'time', (block_boundaries / EEG.srate));
  297. %% Trial selection
  298. % include only correct responses
  299. beh_filt_acc_only = beh(beh.acc==1, :);
  300. excl_trials_incorr = size(beh, 1)-size(beh_filt_acc_only, 1);
  301. total_excl_trials_incorr(subject_nr) = excl_trials_incorr;
  302. fprintf('Lost %g trials to incorrect responses\n', excl_trials_incorr)
  303. % include only responses faster than 1500 ms
  304. beh_filt = beh_filt_acc_only(beh_filt_acc_only.rt<=1500, :);
  305. excl_trials_rt = size(beh_filt_acc_only, 1)-size(beh_filt, 1);
  306. total_excl_trials_rt(subject_nr) = excl_trials_rt;
  307. fprintf('Lost %g trials to RTs above 1500\n', excl_trials_rt)
  308. fprintf('Lost %g trials in total to behavioural data\n', size(beh, 1)-size(beh_filt, 1))
  309. % filter the events structure
  310. discarded_trls = beh.trl_nr(~ismember(beh.trl_nr, beh_filt.trl_nr));
  311. discarded_events_indices = []; % (collect in a for loop, as [EEG.event.trl_nr] would remove missing data)
  312. for event_nr = 1:size(EEG.event, 2)
  313. if ismember(EEG.event(event_nr).trl_nr, discarded_trls)
  314. discarded_events_indices = [discarded_events_indices, event_nr];
  315. end
  316. end
  317. EEG = pop_editeventvals(EEG, 'delete', discarded_events_indices);
  318. % check the discarded trials are the expected length
  319. if numel(discarded_trls) ~= size(beh, 1)-size(beh_filt, 1)
  320. fprintf('Mismatch between behavioural data and EEG events in the number of trials to discard?')
  321. break
  322. end
  323. % check the sizes match
  324. if numel([EEG.event.trl_nr]) ~= size(beh_filt, 1)
  325. fprintf('Inconsistent numbers of trials between events structure and behavioural data after discarding trials?')
  326. break
  327. end
  328. % check the trl numbers match
  329. if any([EEG.event.trl_nr]' ~= beh_filt.trl_nr)
  330. fprintf('Trial IDs mmismatch between events structure and behavioural data after discarding trials?')
  331. break
  332. end
  333. %% Rereference, downsample, and filter
  334. % rereference
  335. EEG = pop_reref(EEG, []);
  336. % downsample if necessary
  337. if EEG.srate ~= 512
  338. EEG = pop_resample(EEG, 512);
  339. end
  340. % filter
  341. % EEG = eeglab_butterworth(EEG, 0.5, 40, 4, 1:size(EEG.chanlocs, 2)); % preregistered filter
  342. EEG = eeglab_butterworth(EEG, 0.1, 40, 4, 1:size(EEG.chanlocs, 2)); % filter with lower highpass
  343. %% ICA
  344. % apply ASR
  345. %EEG_no_asr = EEG;
  346. EEG = clean_asr(EEG, asr_sigma, [], [], [], [], [], [], [], [], 1024); % The last number is available memory in mb, needed for reproducibility
  347. rng(3101) % set seed for reproducibility
  348. EEG = pop_runica(EEG, 'icatype', 'fastica', 'approach', 'symm');
  349. % classify components with ICLabel
  350. EEG = iclabel(EEG);
  351. % store results for easy indexing
  352. icl_res = EEG.etc.ic_classification.ICLabel.classifications;
  353. icl_classes = EEG.etc.ic_classification.ICLabel.classes;
  354. % identify and remove artefact components
  355. artefact_comps = find(icl_res(:, strcmp(icl_classes, 'Eye')) >= icl_cutoff | icl_res(:, strcmp(icl_classes, 'Muscle')) >= icl_cutoff);
  356. fprintf('Removing %g artefact-related ICA components\n', numel(artefact_comps))
  357. n_bad_ica(subject_nr) = numel(artefact_comps);
  358. %EEG_no_iclabel = EEG;
  359. EEG = pop_subcomp(EEG, artefact_comps);
  360. %% Interpolate bad channels
  361. % give the original chanlocs structure so EEGLAB interpolates the missing electrode(s)
  362. if numel(bad_channels)>0
  363. EEG = pop_interp(EEG, ur_chanlocs);
  364. end
  365. %% Epoch the data
  366. % identify and separate into epochs
  367. EEG_epo = struct();
  368. EEG_epo.word = pop_epoch(EEG, {triggers.word}, [-0.25, 1]);
  369. EEG_epo.bacs = pop_epoch(EEG, {triggers.bacs}, [-0.25, 1]);
  370. EEG_epo.noise = pop_epoch(EEG, {triggers.noise}, [-0.25, 1]);
  371. % remove baseline
  372. EEG_epo.word = pop_rmbase(EEG_epo.word, [-200, 0]);
  373. EEG_epo.bacs = pop_rmbase(EEG_epo.bacs, [-200, 0]);
  374. EEG_epo.noise = pop_rmbase(EEG_epo.noise, [-200, 0]);
  375. % check times vectors are identical
  376. if ~isequal(EEG_epo.word.times, EEG_epo.bacs.times, EEG_epo.noise.times)
  377. fprintf('The times vectors in the epoch structures are not identical!')
  378. break
  379. end
  380. %% Get the maximal electrode
  381. % (word Vs. BACS for main analysis, but word Vs. noise also found)
  382. fprintf('Getting maximal electrodes...\n')
  383. % get channel means for each condition
  384. ch_avg = struct();
  385. ch_avg.word = mean(EEG_epo.word.data, 3);
  386. ch_avg.bacs = mean(EEG_epo.bacs.data, 3);
  387. ch_avg.noise = mean(EEG_epo.noise.data, 3);
  388. % get index of time window
  389. targ_window = [120, 200];
  390. targ_window_idx = EEG_epo.word.times >= targ_window(1) & EEG_epo.word.times <= targ_window(2);
  391. % get index of roi channels
  392. eeg_chan_idx = ismember({EEG.chanlocs.labels}, roi); % EEG chanlocs same as chanlocs in ch_avg structs as they're copied over
  393. % store vectors of times and channels in ch_avg
  394. ch_avg.times = EEG_epo.word.times(targ_window_idx); % taken from word condition but identical across conditions
  395. ch_avg.chanlocs = EEG.chanlocs(eeg_chan_idx);
  396. % get only roi electrode data in target window
  397. ch_avg.word = ch_avg.word(eeg_chan_idx, targ_window_idx);
  398. ch_avg.bacs = ch_avg.bacs(eeg_chan_idx, targ_window_idx);
  399. ch_avg.noise = ch_avg.noise(eeg_chan_idx, targ_window_idx);
  400. % get differences of interest
  401. % - directional, so find max of these
  402. ch_avg.diff_word_bacs = ch_avg.bacs - ch_avg.word;
  403. ch_avg.diff_word_noise = ch_avg.noise - ch_avg.word;
  404. % find the maximum difference indices
  405. mean_bacs_diff_perchan = mean(ch_avg.diff_word_bacs, 2);
  406. max_bacs_ch_idx = mean_bacs_diff_perchan == max(mean_bacs_diff_perchan);
  407. mean_noise_diff_perchan = mean(ch_avg.diff_word_noise, 2);
  408. max_noise_ch_idx = mean_noise_diff_perchan == max(mean_noise_diff_perchan);
  409. % if multiple channels have an equal mean difference, select one randomly (but reprocubily)
  410. if sum(max_bacs_ch_idx) > 1
  411. rng(42 + subject_nr)
  412. perm_idx = randperm(sum(max_bacs_ch_idx));
  413. maxes_idx = find(max_bacs_ch_idx);
  414. max_bacs_ch_idx = maxes_idx(perm_idx(numel(maxes_idx)));
  415. end
  416. if sum(max_noise_ch_idx) > 1
  417. rng(42 + subject_nr)
  418. perm_idx = randperm(sum(max_noise_ch_idx));
  419. maxes_idx = find(max_noise_ch_idx);
  420. max_noise_ch_idx = maxes_idx(perm_idx(numel(maxes_idx)));
  421. end
  422. % get the channel names
  423. chan_names = {ch_avg.chanlocs.labels};
  424. max_chan_bacs = chan_names{max_bacs_ch_idx};
  425. max_chan_noise = chan_names{max_noise_ch_idx};
  426. % get the timepoint and value of the maximum difference for the max channels
  427. [max_chan_bacs_peak_diff, max_chan_bacs_peak_diff_idx] = max(ch_avg.diff_word_bacs(max_bacs_ch_idx, :));
  428. max_chan_bacs_peak_diff_signed = ch_avg.diff_word_bacs(max_bacs_ch_idx, max_chan_bacs_peak_diff_idx);
  429. max_chan_bacs_peak_time = ch_avg.times(max_chan_bacs_peak_diff_idx);
  430. [max_chan_noise_peak_diff, max_chan_noise_peak_diff_idx] = max(ch_avg.diff_word_noise(max_noise_ch_idx, :));
  431. max_chan_noise_peak_diff_signed = ch_avg.diff_word_noise(max_noise_ch_idx, max_chan_noise_peak_diff_idx);
  432. max_chan_noise_peak_time = ch_avg.times(max_chan_noise_peak_diff_idx);
  433. % store the values in the table
  434. max_elecs.subject_id(subject_nr) = {subject_id};
  435. max_elecs.max_elec_bacs(subject_nr) = {max_chan_bacs};
  436. max_elecs.max_time_bacs(subject_nr) = {max_chan_bacs_peak_time};
  437. max_elecs.max_diff_bacs(subject_nr) = {max_chan_bacs_peak_diff_signed};
  438. max_elecs.max_elec_noise(subject_nr) = {max_chan_noise};
  439. max_elecs.max_time_noise(subject_nr) = {max_chan_noise_peak_time};
  440. max_elecs.max_diff_noise(subject_nr) = {max_chan_noise_peak_diff_signed};
  441. %% Save sample-level data for all electrodes
  442. disp('Getting sample-level localiser results...')
  443. % resample to 256 Hz
  444. EEG_256 = pop_resample(EEG, 256);
  445. % get epochs of low-srate data
  446. EEG_epo_256 = pop_epoch(EEG_256, {triggers.word, triggers.bacs, triggers.noise}, [-0.25, 0.5]);
  447. % remove baseline
  448. EEG_epo_256 = pop_rmbase(EEG_epo_256, [-200, 0]);
  449. % pre-allocate the table
  450. var_names = {'subj_id', 'stim_grp', 'resp_grp', 'trl_nr', 'ch_name', 'time', 'uV'};
  451. var_types = {'string', 'string', 'string', 'double', 'string', 'double', 'double'};
  452. nrows = 64 * size(EEG_epo_256.times, 2) * size(beh_filt, 1);
  453. sample_res = table('Size',[nrows, numel(var_names)], 'VariableTypes',var_types, 'VariableNames',var_names);
  454. sample_res.subj_id = repmat(beh_filt.subj_id, 64*size(EEG_epo_256.times, 2), 1);
  455. sample_res.stim_grp = repmat(beh_filt.stim_grp, 64*size(EEG_epo_256.times, 2), 1);
  456. sample_res.resp_grp = repmat(beh_filt.resp_grp, 64*size(EEG_epo_256.times, 2), 1);
  457. % get the 64 channel eeg data as an array
  458. eeg_arr = EEG_epo_256.data(1:64, :, :);
  459. % a vector of all eeg data
  460. eeg_vec = squeeze(reshape(eeg_arr, 1, 1, []));
  461. % array and vector of the channel labels for each value in EEG.data
  462. channel_labels_arr = cell(size(eeg_arr));
  463. channel_label_lookup = {EEG_epo_256.chanlocs.labels};
  464. for chan_nr = 1:size(eeg_arr, 1)
  465. channel_labels_arr(chan_nr, :, :) = repmat(channel_label_lookup(chan_nr), size(channel_labels_arr, 2), size(channel_labels_arr, 3));
  466. end
  467. channel_labels_vec = squeeze(reshape(channel_labels_arr, 1, 1, []));
  468. % array and vector of the item numbers for each value in EEG.data
  469. times_arr = zeros(size(eeg_arr));
  470. times_lookup = EEG_epo_256.times;
  471. for time_idx = 1:size(eeg_arr, 2)
  472. times_arr(:, time_idx, :) = repmat(times_lookup(time_idx), size(times_arr, 1), size(times_arr, 3));
  473. end
  474. times_vec = squeeze(reshape(times_arr, 1, 1, []));
  475. % array and vector of the trial numbers
  476. trials_arr = zeros(size(eeg_arr));
  477. trials_lookup = beh_filt.trl_nr;
  478. for trl_idx = 1:size(eeg_arr, 3)
  479. trials_arr(:, :, trl_idx) = repmat(trials_lookup(trl_idx), size(trials_arr, 1), size(trials_arr, 2));
  480. end
  481. trials_vec = squeeze(reshape(trials_arr, 1, 1, []));
  482. % store sample-level results in the table
  483. sample_res.ch_name = channel_labels_vec;
  484. sample_res.trl_nr = trials_vec;
  485. sample_res.time = times_vec;
  486. sample_res.uV = eeg_vec;
  487. % look up and store some info about the trials
  488. trial_info_lookup = beh_filt(:, {'trl_nr', 'condition', 'string', 'item_nr'});
  489. sample_res = outerjoin(sample_res, trial_info_lookup, 'MergeKeys', true);
  490. % sort by time, channel, item_nr
  491. sample_res = sortrows(sample_res, {'time', 'ch_name', 'trl_nr'});
  492. % Save the sample-level results
  493. disp('Saving sample-level localiser results...')
  494. writetable(sample_res, fullfile('localiser_sample_data', [subject_id, '.csv']));
  495. end
  496. %% save the results
  497. fprintf('\nSaving results...\n')
  498. writetable(max_elecs, 'max_elecs.csv');
  499. fprintf('Finished preprocessing localiser data!\n')
  500. %% Functions
  501. % custom function for applying a Butterworth filter to EEGLAB data
  502. function EEG = eeglab_butterworth(EEG, low, high, order, chanind)
  503. fprintf('Applying Butterworth filter between %g and %g Hz (order of %g)\n', low, high, order)
  504. % create filter
  505. [b, a] = butter(order, [low, high]/(EEG.srate/2));
  506. % apply to data (requires transposition for filtfilt)
  507. data_trans = single(filtfilt(b, a, double(EEG.data(chanind, :)')));
  508. EEG.data(chanind, :) = data_trans';
  509. end
  510. % custom function for finding the closest timepoint in an EEG dataset
  511. function [idx, closesttime] = eeglab_closest_time(EEG, time)
  512. dists = abs(EEG.times - time);
  513. idx = find(dists == min(dists));
  514. % in the unlikely case there are two equidistant times, select one randomly
  515. if numel(idx) > 1
  516. fprintf('Two equidistant times! Selecting one randomly.')
  517. idx = idx(randperm(numel(idx)));
  518. idx = idx(1);
  519. end
  520. closesttime = EEG.times(idx);
  521. end