-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrepMEG.m
202 lines (160 loc) · 7.27 KB
/
PrepMEG.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
%% add fieldtrip to your path
% addpath /Users/liusirui/Documents/MATLAB/fieldtrip-20150506
% ft_defaults
% remember, these channel numbers use one indexing
megChannels = 1:157;
refChannels = 158:160;
triggerChannels = 161:168;
eyeChannels = 177:178;
photodiodeChannel = 192;
%% setup paths
exptDir = pathToTA2('MEG');
% exptDir = '/Local/Users/denison/Data/TA2/MEG';
% exptDir = '/Local/Users/denison/Data/TANoise/MEG';
% exptDir = '/Local/Users/denison/Data/TAContrast/MEG';
% exptDir = '/Volumes/DRIVE1/DATA/rachel/MEG/TADetectDiscrim/MEG';
sessionDir = 'R0817_20190625';
filename = 'R0817_TA2_6.25.19_ebi';
dataDir = sprintf('%s/%s/', exptDir, sessionDir);
prepDir = sprintf('%s/prep/', dataDir);
% rootDir = '/Users/liusirui/Documents/MATLAB/MEG/';
% dataDir = [rootDir,'data/TAPilot_meg/data/'];
% prepDir = [rootDir,'data/TAPilot_meg/prep/'];
% filename = 'R0817_TaDeDi_5.26.15_ebi_part1';
sqdfile = [dataDir,filename,'.sqd'];
%% load dataset
dat = ft_read_data(sqdfile);
hdr = ft_read_header(sqdfile);
%% trigger search and preprocessing
% SSVEP trials: prestim = 0.5 (before trigger), poststim = 3.1
% epoched data will be saved in PrepSSVEP
cfg = [];
cfg.dataset = sqdfile;
cfg.trialdef.prestim = 0.2; %0; %0.5; % sec
cfg.trialdef.poststim = 2.3; %2.3; %3.1;
cfg.trialdef.trig = [168,167]; %[168,167]; %[161:164,167]; %168 = precue, 167=blank
threshold = 2.5;
[trl,Events] = mytrialfun_all(cfg,threshold,[]);
prep_data = ft_preprocessing(struct('dataset',sqdfile,...
'channel','MEG','continuous','yes','trl',trl));
% show sample number and trigger channel for each trial
triggers = [Events.trigger]';
type = {Events.channel};
type = [cellfun(@str2num, type)]';
trig_ind = [1:numel(triggers)]';
trigger_info = [trig_ind,triggers,trl,type];
%[trial number, trigger sample, start sample, end sample, offset, trigger channel]
if ~exist(prepDir,'dir')
mkdir(prepDir)
end
save ([prepDir,filename,'_prep.mat'],'prep_data', '-v7.3')
%% ft_rejectvisual (summary mode): visual check of outliers
cfg = [];
cfg.method = 'summary';
cfg.alim = 1e-12; % scaling (10 fT/cm)
cfg.megscale = 1;
cfg.channel = 'MEG';
% cfg.keepchannel = 'yes';
dummy = ft_rejectvisual(cfg,prep_data);
[~,bc,~] = intersect(prep_data.label,setdiff(prep_data.label,dummy.label));
%% TRIALS: databrowser
% first: browse through all trials in data browser, vertical channel mode,
% half of the channels at a time (no rejection, just get a sense of the
% data)
% second: go back through these trials (data browser, vertical mode,
% half channels at a time) and reject bad portions of the time series with
% selection box
cfg = [];
cfg.channel = [143 153 9 12 62 44 80 122 95 61 75 10 137 109 138 30 41,...
60 14 46 13 43 66 57 77 93 120 145 152 35 36 139 146 64 59 16 151,...
149 49 38 6 4 51 50 7 55 98 104 54 69 86 88 99 72 71 117 102 147 85,...
134 32 126 155 67 142 133 113 31 73 53 156 116 91 33 74 37 129 63 47 5 ];
cfg.viewmode = 'vertical';
artf = ft_databrowser(cfg,prep_data); % saved artifact info in artf.artfctdef.visual.artifact
%% Reject trials (those selected from databrowser)
% remove trials that overlap with the segment selected and save the
% remaining data in clean_data1
% % select only the MEG channels
% cfg = [];
% cfg.channel = 'MEG';
% data = ft_preprocessing(cfg,prep_data);
% remove the trials selected
artf.artfctdef.reject = 'complete';
clean_data1 = ft_rejectartifact(artf,prep_data);
%% CHANNELS: ft_rejectvisual (channel mode)
% browse through each channel for all trials and identify candidate
% bad channels.
cfg = [];
cfg.method = 'channel'; % 'trial'
cfg.alim = 1e-12;
% cfg.channel = 'MEG';
dummy1 = ft_rejectvisual(cfg,clean_data1);
%% CHANNELS :databrowser
% inspect candidate bad channels in data browser, vertical mode and
% reject bad channels, save data in clean_data2
[~,badChanls,~] = intersect(prep_data.label,setdiff(prep_data.label,dummy1.label));
cfg = [];
cfg.channel = badChanls;
cfg.viewmode = 'vertical';
artf2 = ft_databrowser(cfg,clean_data1); % saved artifact info in artf2.artfctdef.visual.artifact
%% CHANNELS: reject bad channels
cfg = [];
cfg.method = 'channel';
cfg.alim = 1e-12;
% cfg.channel = 'MEG';
clean_data2 = ft_rejectvisual(cfg,clean_data1);
%% ft_rejectvisual (summary mode) again
% Look at summary again just to check that all the significant outliers
% were removed
cfg = [];
cfg.method = 'summary';
cfg.alim = 1e-12;
% cfg.megscale = 1;
% cfg.channel = 'MEG';
% cfg.keepchannel = 'yes';
dummy2 = ft_rejectvisual(cfg,clean_data2);
cleanPrepData = clean_data2;
%% save clean data
% show rejected trials and the corresponding trigger channels
[~,cleanPrepData.trials_rejected] = setdiff(trl,clean_data1.cfg.trl,'rows');
cleanPrepData.trials_rejected = [cleanPrepData.trials_rejected,trigger_info(cleanPrepData.trials_rejected,6)];
% show rejected channels
cleanPrepData.channels_rejected = setdiff(prep_data.label,clean_data2.label);
% show remaining trials and corresponding trigger channel
[C,ia,ib] = intersect(trl,clean_data1.cfg.trl,'rows');
cleanPrepData.trial_info = [C,trigger_info(ia,6)];
save([prepDir,filename,'_prepCleanData.mat'],'cleanPrepData', '-v7.3')
%% save trials_rejected and channels_rejected separately
trials_rejected = cleanPrepData.trials_rejected(:,1);
save([prepDir '/trials_rejected.mat'], 'trials_rejected')
channels_rejected = cleanPrepData.channels_rejected;
save([prepDir '/channels_rejected.mat'], 'channels_rejected')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% data broswer viewing continuous raw data:
% scroll through the data and select those segments containing artifacts
% (artf will store a list of start and end samples for every data segment
% selected)
% preprocess data as one trial
cfg = [];
cfg.dataset = sqdfile;
cfg.trialdef.triallength = Inf;
cfg = ft_definetrial(cfg);
data = ft_preprocessing(cfg);
% view data by time segments specified in cfg.blocksize (in seconds)
cfg = [];
cfg.channel = [143 153 9 12 62 44 80 122 95 61 75 10 137 109 138 30 41,...
60 14 46 13 43 66 57 77 93 120 145 152 35 36 139 146 64 59 16 151,...
149 49 38 6 4 51 50 7 55 98 104 54 69 86 88 99 72 71 117 102 147 85,...
134 32 126 155 67 142 133 113 31 73 53 156 116 91 33 74 37 129 63 47 5 ];
cfg.viewmode = 'vertical';
cfg.continuous = 'yes';
cfg.blocksize = 5;
artf = ft_databrowser(cfg, data);
%% data browser viewing continuous raw data: reject artifacts
% remove trials that overlap with segments selected and save the remaining
% data in clean_data1
artf.artfctdef.reject = 'complete';
cfg = [];
cfg.channel = 'MEG';
data = ft_preprocessing(cfg,prep_target);
clean_data1 = ft_rejectartifact(artf,data);