Skip to content

Commit

Permalink
Added preprocessing script, minor adjustments in the plots
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusKlug committed Aug 27, 2020
1 parent 51806e5 commit 629de37
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,73 @@
max_threads = 4;
num_models = 1;

%%

removed_chans = cell(length(subjects), 1);


%%% filling in the manual steps
% do this by manually do the cleaning (first check which channels have to be interpolated, then
% once clean for just data segments, then clean again everything out that is an artifact), enter
% 'eegh' in the console and the matrix that has been cleaned out into the matrix here

% 1:
removed_chans{1} = [2 4 9 29 64 81 123 128 129 144 150];

% 2:
removed_chans{2} = [65 70 83 95 125];

% 3:
removed_chans{3} = [64 3 79 80 100 105 113 118 124 128 130 133 134 136 137 142 154];

% 4:
removed_chans{4} = [15 66 77 95 97 120 132 151];

% 5:
removed_chans{5} = [120];

% 6:
removed_chans{6} = [23 29 31 68 97 98 99 100 103 120 124 125 127 128 129 130 134 137 138 141 145 150 151 152];

% 7:
removed_chans{7} = [2 3 5 26 27 29 30 44 58 61 63 68 73 74 75 79 81 82 85 86 89 90 91 92 93 96 97 101 110 124 133 134 138 150 156];

% 9:
removed_chans{9} = [24 43 59 65 66 100 125 129 136 151];

% 10:
removed_chans{10} = [3 9 24 65 69 79 96 100 124 125 127 129 131 132 136 137 138 139];

% 11:
removed_chans{11} = [32 65 66 79 81 95 98 99 100 103 105 107 118 124 125 126 129 130 131 132 134 135 137 138 139 142 144 150];

% 12:
removed_chans{12} = [5 81 83 95 108 123 125 126];

% 13:
removed_chans{13} = [23 79 81 95 101 110 121 123 125 127 129 132 137 139 142 143];

% 14:
removed_chans{14} = [61 70 71 95 97 98 99 100 101 102 105 124 126 129 130 131 132 133 137 138 139];

% 15:
removed_chans{15} = [1 51 56 65 97 98 107 108 115 116 118 124 125 128 129 135 137 138 143 144 145 146 150 151 152];

% 16:
removed_chans{16} = [2 9 23 26 29 30 32 45 81 99 105 123 124 125 126 127 128 129 133 136 137 138 141 146 154];

% 17:
removed_chans{17} = [4 28 65 95 97 99 102 118 123 124 125 127 130 131 137 138 151 152];

% 18:
removed_chans{18} = [79 97 98 99 101 102 116 117 121 122 123 126 127 128 130 131 132 133 134 136 137 138 139 140 141 142 143 145 146 147 148 152 153 154 155];

% 19:
removed_chans{19} = [9 18 19 23 26 27 60 79 81 83 124 126 129 132 134 136];

% 20:
removed_chans{20} = [35 36 60 61 66 79 80 95 107 123 128 132 157];

%% manual rejection values
rejections_segments = cell(length(subjects), 1);

Expand Down
146 changes: 146 additions & 0 deletions compute_1_1_electrode_mixup.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@

switch subject

%%% subject 1
case 1

% r21: adjacent are r18, r20, r22, r26; can be interpolated in
% polar values (polar assumes a spherical head, carthesian XYZ might be a little
% under the scalp surface after interpolation. polar is hence
% theoretically better but it has to be inspected because it's
% sometimes far off!)
% r18 = [51.3796 0.098719];
% r20 = [103.3013 0.076907];
% r22 = [67.626 0.17049];
% r26 = [101.4057 0.15783];
% r21 = mean([r20; r22; r26; r18],1)

EEG=pop_chanedit(EEG, 'changefield',{85 'theta' '80.9281'},'changefield',{85 'radius' '0.1260'},'convert',{'topo2all'});
[ALLEEG EEG] = eeg_store(ALLEEG, EEG, CURRENTSET);
EEG = eeg_checkset( EEG );

EEG.etc.interpolated_locations.location = 'r21';
EEG.etc.interpolated_locations.from = {'r20' 'r22' 'r26' 'r18'};
EEG.etc.interpolated_locations.method = 'topo2all';

%%% subject 3:
case 3

% r30: adjacent are r27, r29, r31, w21; can be interpolated in polar
% r27 = [101.5161 0.2575];
% r29 = [129.2299 0.3031];
% r31 = [96.099 0.32382];
% w21 = [117.8316 0.37519];
% r30 = mean([r27; r29; r31; w21],1)

EEG=pop_chanedit(EEG, 'changefield',{94 'theta' '111.1691'},'changefield',{94 'radius' '0.3149'},'convert',{'topo2all'});
[ALLEEG EEG] = eeg_store(ALLEEG, EEG, CURRENTSET);
EEG = eeg_checkset( EEG );

EEG.etc.interpolated_locations.location = 'r30';
EEG.etc.interpolated_locations.from = {'r27' 'r29' 'r31' 'w21'};
EEG.etc.interpolated_locations.method = 'topo2all';

% data cables had been mixed
[ALLEEG, EEG, CURRENTSET] = bemobil_unmix_electrode_mixups( EEG , ALLEEG, CURRENTSET, [1:64; 33:64 1:32], []);

%%% subject 4:
case 4

% left eye frontal electrodes physically misplaced
[ALLEEG, EEG, CURRENTSET] = bemobil_unmix_electrode_mixups( EEG , ALLEEG, CURRENTSET, [10 12 14 16; 12 14 16 10], [10 12 14 16; 12 14 16 10]);

%%% subject 8:
case 8

% data cables had been mixed
[ALLEEG, EEG, CURRENTSET] = bemobil_unmix_electrode_mixups( EEG , ALLEEG, CURRENTSET, [1:64; 33:64 1:32], []);

%%% subject 9:
case 9

% n19: adjacent are n12, n13, n18, n20, n25, n26; neck has to be
% interpolated in XYZ
% n12 = [-105.607 -4.592 -23.81];
% n13 = [-94.891 -33.141 -29.122];
% n18 = [-110.099 14.004 -31.48];
% n20 = [-94.025 -44.157 -44.663];
% n25 = [-123.142 -4.121 -42.436];
% n26 = [-116.396 -35.635 -48.902];
% n19 = mean([n12; n13; n18; n20; n25; n26],1)

EEG=pop_chanedit(EEG, 'changefield',{147 'X' '-107.3600'},'changefield',{147 'Y' '-17.9403'},'changefield',{147 'Z' '-36.7355'},'convert',{'cart2all'});
[ALLEEG EEG] = eeg_store(ALLEEG, EEG, CURRENTSET);
EEG = eeg_checkset( EEG );

EEG.etc.interpolated_locations.location = 'n19';
EEG.etc.interpolated_locations.from = {'n12' 'n13' 'n18' 'n20' 'n25' 'n26'};
EEG.etc.interpolated_locations.method = 'cart2all';

%%% subject 12:
case 12

% r29 is missing, located at position of 30, which is located at position
% of 31, which is located at 32, double with 32. shit. approach:
% interpolate 31 at location of 29, then interchange locations back to
% correct. Adjacent to (new and incorrect at first) r31 are r26, r28, r29,
% w20; polar interpolation is weird, cartesian is used
% r26 = [-41.338 -74.413 88.987];
% r28 = [-76.935 -51.122 78.529];
% r29 = [-40.196 -80.661 58.764];
% w20 = [-71.558 -63.023 43.157];
% r31 = mean([r26; r28; r29; w20],1);

EEG=pop_chanedit(EEG, 'changefield',{95 'X' '-57.5067'},'changefield',{95 'Y' '-67.3047'}, 'changefield',{95 'Z' '67.3593'},'convert',{'cart2all'});
[ALLEEG EEG] = eeg_store(ALLEEG, EEG, CURRENTSET);
EEG = eeg_checkset( EEG );

EEG.etc.interpolated_locations.location = 'r31';
EEG.etc.interpolated_locations.from = {'r26' 'r28' 'r29' 'w20'};
EEG.etc.interpolated_locations.method = 'cart2all';

% some electrode location digitization mixed up
[ALLEEG, EEG, CURRENTSET] = bemobil_unmix_electrode_mixups( EEG , ALLEEG, CURRENTSET, [], [93 94 95; 95 93 94]);

%%% subject 13:
case 13

% right eye frontal electrodes physically misplaced
[ALLEEG, EEG, CURRENTSET] = bemobil_unmix_electrode_mixups( EEG , ALLEEG, CURRENTSET, [22 32; 32 22], [22 32; 32 22]);

%%% subject 15:
case 15

% w22 is wrong, adjacent are w9 and w23; has to be interpolated in XYZ
% because over the 180 boarder (and is weird then)
% w9 = [-96.002 10.727 25.247];
% w23 = [-94.697 -29.176 23.551];
% w22 = mean([w9; w23],1)

EEG=pop_chanedit(EEG,'changefield',{118 'X' '-95.3495'},'changefield',{118 'Y' '-9.2245'},'changefield',{118 'Z' '24.3990'},'convert',{'cart2all'});
[ALLEEG EEG] = eeg_store(ALLEEG, EEG, CURRENTSET);
EEG = eeg_checkset( EEG );

EEG.etc.interpolated_locations.location = 'w22';
EEG.etc.interpolated_locations.from = {'w9' 'w23'};
EEG.etc.interpolated_locations.method = 'cart2all';

%%% subject 15:
case 17

% r21: adjacent are r20, r22, r18, r26; can be interpolated in polar
r20 = [144.3636 0.1851];
r22 = [103.6495 0.18567];
r18 = [126.4892 0.11861];
r26 = [125.8998 0.24653];
r30 = mean([r20; r22; r18; r26],1)

EEG=pop_chanedit(EEG, 'changefield',{85 'theta' '125.1005'},'changefield',{85 'radius' '0.1840'},'convert',{'topo2all'});
[ALLEEG EEG] = eeg_store(ALLEEG, EEG, CURRENTSET);
EEG = eeg_checkset( EEG );

EEG.etc.interpolated_locations.location = 'r21';
EEG.etc.interpolated_locations.from = {'r20' 'r22' 'r18' 'r26'};
EEG.etc.interpolated_locations.method = 'topo2all';

end
67 changes: 67 additions & 0 deletions compute_1_preprocess.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
subjects = [1:7 9:20];

compute_0_AMICA_investigation_settings

% raw data is on a different server
input_path = 'V:\spot_rotation\raw_EEG_data';
chanloc_path = 'V:\spot_rotation\raw_EEG_data';


% preprocessing
channel_locations_filename = 'channel_locations.elc';
channels_to_remove = {'N29' 'N30' 'N31'};
eog_channels = {'G16' 'G32'};
resample_freq = 250;

if ~exist('ALLEEG','var'); eeglab; end
pop_editoptions( 'option_storedisk', 0, 'option_savetwofiles', 1, 'option_saveversion6', 0, 'option_single', 0, 'option_memmapdata', 0, 'option_eegobject', 0, 'option_computeica', 1, 'option_scaleicarms', 1, 'option_rememberfolder', 1, 'option_donotusetoolboxes', 0, 'option_checkversion', 1, 'option_chat', 1);


filenames = {'control_body' 'control_joy' 'test_body' 'test_joy'};

%%
subject = 1
for subject = subjects
disp(['Subject #' num2str(subject)]);
STUDY = []; CURRENTSTUDY = 0; ALLEEG = []; EEG=[]; CURRENTSET=[];

input_filepath = fullfile(input_path, num2str(subject));

%% load
EEG = pop_loadset('filename', strcat(filenames, '_EEG.set'), 'filepath', input_filepath);
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 0,'study',0);

%% merge
% merges all files currently loaded in EEGLAB into one file and stores
% the original filenames in EEG.etc.appended_files
[ALLEEG EEG CURRENTSET] = bemobil_merge(ALLEEG,EEG,CURRENTSET,[1:length(ALLEEG)]);

%% preprocess
% enter chanlocs, remove unused channels, declare EOG, resample

channel_locations_filepath = fullfile(chanloc_path,num2str(subject),channel_locations_filename);

[ALLEEG EEG CURRENTSET] = bemobil_preprocess(ALLEEG, EEG, CURRENTSET, channel_locations_filepath,...
channels_to_remove,eog_channels,resample_freq);

%% Interpolation of chanlocs and switching of interchanged channels
% This first has to be determined manually by looking at the 3D channel visualization at EEGLAB ->
% Edit -> channel locations -> Plot 3-D (xyz) and checking if this are off somehow (e.g. if the
% same electrode has been measured twice), or by checking the data (e.g. if green and yellow
% electrode bands have been mixed up you see the blinks at the wrong data channel)
compute_1_1_electrode_mixup

%% interpolate bad channels
% the function stores the interpolated channels in EEG.etc and saves a
% data set
[ALLEEG, EEG, CURRENTSET] = bemobil_interp( EEG , ALLEEG, CURRENTSET, removed_chans{subject});

%% Compute average reference
EEG = pop_reref( EEG, []);
EEG = eeg_checkset( EEG );

%% save on disk
EEG = pop_saveset( EEG, 'filename','interpolated_avRef','filepath', input_filepath);
disp('...done');

end
8 changes: 5 additions & 3 deletions compute_2_AMICAs.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
compute_AMICA_investigation_settings;

matlab_instance = 10;

% userpath('reset')
Expand Down Expand Up @@ -59,6 +59,8 @@

filter_freqs = [0 0.5:0.25:1.5 1.75:0.5:3.25 4.25] % for checking all

compute_0_AMICA_investigation_settings;

%% ICA loop

% raw data is on a different server
Expand Down Expand Up @@ -453,8 +455,8 @@
'coord_transform',this_warping_transform ,...
'chansel',[1:EEG.nbchan] );

EEG = pop_multifit(EEG, [1:size(EEG.icaweights,1)] ,'threshold',residualVariance_threshold,...
'dipoles', number_of_dipoles, 'rmout', do_remove_outside_head);
EEG = pop_multifit(EEG, [1:size(EEG.icaweights,1)] ,'threshold',100,...
'dipoles', 1, 'rmout', 'off');

%% run iclabel, store in mat and save data
for i_classifier_version = 1:length(classifier_versions)
Expand Down
Loading

0 comments on commit 629de37

Please sign in to comment.