-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmake_MADE_epochs.m
168 lines (134 loc) · 6.74 KB
/
make_MADE_epochs.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
function [tEEG] = make_MADE_epochs(tEEG,eeg_file_name, json_file_name, task, siteinfo)
%MAKE_MADE_EPOCHS Function that epochs EEG data for MADE pipeline
% The function takes EEG, which is an EEGLAB structure with data
% for one EEG task. The eeg_file_name is the (absolute or relative)
% path to the EEG file and will be used to extract the task name.
% The json_file_name is the path to the json configuration file
% that will be used to guide the extraction of epochs from EEG.
% The json file should have a field corresponding to the task
% name for the file of interest. Within that field, there should
% be an array for marker_names (the markers to extract epochs around),
% pre_latency and post_latency (both positive and in units seconds).
% If there is no intrinsic task structure to the EEG acquisition,
% the additional task related field make_dummy_events, num_dummy_events,
% and dummy_events_spacing can be used to insert dummy markers that
% epochs will be constructed around. In the case where dummy markers are
% used, marker_names should be set as an array containing the first
% marker to occur within the scan. All subsequent dummy markers will then
% be placed following the initial instance of the marker named
% marker_names(1).
% The returned output is a variable tEEG which contains the epoched data.
% TM - added in siteinfo to pull site information for
% check_missing_dins
%Find the task label
s = grab_settings(eeg_file_name, json_file_name);
%Grab task specific settings
marker_names = s.marker_names;
pre_latency = s.pre_latency;
post_latency = s.post_latency;
erp_filter = s.erp_filter;
erp_lowpass = s.erp_lowpass;
%Make copy of EEG file
tEEG = deal(tEEG);
if pre_latency < 0
error('Error: pre_latency should be defined as a positive value.');
end
if iscell(marker_names) == false
error('Error: json value for marker_names should be a list/array');
end
if isfield(s,'make_dummy_events')
if s.make_dummy_events
if length(marker_names) ~= 1
error('Error: there should be exactly one marker name for rest-like files, made to indicate the point to start creating dummy events.');
end
if strcmp(marker_names(1), 'DIN3') %add code to address error where there is an extra DIN3 in RS before the bas+ flag
if numel(find(strcmp({tEEG.event.type}, 'DIN3')))>1
bas_lat = find(strcmp({tEEG.event.type}, 'bas+'));
din_lat = find(strcmp({tEEG.event.type}, 'DIN3'));
if numel(bas_lat) == 1
for i=din_lat
if i < bas_lat
tEEG.event(i).type = 'EXTRA DIN';
end
end
end
end
end
start_index = find(strcmp({tEEG.event.type}, marker_names(1)));
if length(start_index) > 1 % TM add code to address error where there is an extra DIN3 at the end of RS (after TRSP)
tEEG.event(start_index(2)).type = 'EXTRA DIN';
start_index = find(strcmp({tEEG.event.type}, marker_names(1)));
end
% if length(start_index) > 1
% error('Error: there should only be one instance of DIN3 event in EEG file'); %TM patch
% end
if length(start_index) < 1 % TM add code to check for missing RS din3 and add it in
tEEG = check_missing_dins(tEEG, task, siteinfo, s);
start_index = find(strcmp({tEEG.event.type}, marker_names(1)));
end
start_latency = (tEEG.event(start_index).latency)/tEEG.srate;
num_dummy_events = s.num_dummy_events;
dummy_event_spacing = s.dummy_event_spacing;
for i = 1:num_dummy_events
latency = start_latency + dummy_event_spacing*(i-1);
type = 'dummy_marker';
tEEG = pop_editeventvals(tEEG,'insert',{1 [] [] []},'changefield',{1 'type' type},'changefield',{1 'latency' latency});
end
marker_names = {'dummy_marker'};
end
end
%add kira's code here -- TM 8/1/24
tEEG = check_missing_dins(tEEG, task, siteinfo, s);
tEEG = eeg_checkset(tEEG);
epoch_length=[-1*pre_latency post_latency]; % define Epoch Length
tEEG = eeg_checkset( tEEG );
%if ~strcmp(task, 'RS')
% tEEG = pop_selectevent( tEEG, 'Task', task,'deleteevents','on');
%end
% This section ONLY does low pass, NOT high pass
% Remember to change the value of lowpass
if erp_filter == 1
% 7. Initialize the filters
%highpass = s.highpass; % High-pass frequency
%lowpass = s.lowpass; % Low-pass frequency. We recommend low-pass filter at/below line noise frequency (see manuscript for detail)
lowpass = erp_lowpass;
% STEP 6: Filter data
% Calculate filter order using the formula: m = dF / (df / fs), where m = filter order,
% df = transition band width, dF = normalized transition width, fs = sampling rate
% dF is specific for the window type. Hamming window dF = 3.3
%high_transband = highpass; % high pass transition band
low_transband = 10; % low pass transition band
%hp_fl_order = 3.3 / (high_transband / EEG.srate);
lp_fl_order = 3.3 / (low_transband / tEEG.srate);
% Round filter order to next higher even integer. Filter order is always even integer.
% if mod(floor(hp_fl_order),2) == 0
% hp_fl_order=floor(hp_fl_order);
% elseif mod(floor(hp_fl_order),2) == 1
% hp_fl_order=floor(hp_fl_order)+1;
% end
if mod(floor(lp_fl_order),2) == 0
lp_fl_order=floor(lp_fl_order)+2;
elseif mod(floor(lp_fl_order),2) == 1
lp_fl_order=floor(lp_fl_order)+1;
end
% Calculate cutoff frequency
%high_cutoff = highpass/2;
low_cutoff = lowpass + (low_transband/2);
% % Performing high pass filtering
% EEG = eeg_checkset( EEG );
% EEG = pop_firws(EEG, 'fcutoff', high_cutoff, 'ftype', 'highpass', 'wtype', 'hamming', 'forder', hp_fl_order, 'minphase', 0);
% EEG = eeg_checkset( EEG );
% pop_firws() - filter window type hamming ('wtype', 'hamming')
% pop_firws() - applying zero-phase (non-causal) filter ('minphase', 0)
% Performing low pass filtering
tEEG = eeg_checkset( tEEG );
tEEG = pop_firws(tEEG, 'fcutoff', low_cutoff, 'ftype', 'lowpass', 'wtype', 'hamming', 'forder', lp_fl_order, 'minphase', 0);
tEEG = eeg_checkset( tEEG );
% pop_firws() - transition band width: 10 Hz
% pop_firws() - filter window type hamming ('wtype', 'hamming')
% pop_firws() - applying zero-phase (non-causal) filter ('minphase', 0)
end %if erp filter is turned on
%tEEG = pop_selectevent(tEEG, 'type', marker_names, 'deleteevents', 'on'); %LY TEMP
tEEG = pop_epoch( tEEG, marker_names, epoch_length, 'epochinfo', 'yes');
tEEG = pop_selectevent( tEEG, 'latency','-.1 <= .1','deleteevents','on');
end