/
pop_rmdat.m
212 lines (195 loc) · 8.2 KB
/
pop_rmdat.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
203
204
205
206
207
208
209
210
211
212
% POP_RMDAT - Keep or remove continuous data around specific events
%
% Usage:
% >> OUTEEG = pop_rmdat( EEG); % pop-up a data entry window
% >> OUTEEG = pop_rmdat( EEG, typerange, timelimits, invertselection);
%
% Graphic interface:
% "Time-locking event type(s)" - [edit box] Select 'Edit > Event values'
% to see a list of event.type values; else use the push button.
% To use event types containing spaces, enter in single-quotes.
% "..." - [push button] scroll event types.
% "Time limits" - [edit box] epoch latency range [start, end] in seconds relative
% to the event type latency.
%
% Inputs:
% EEG - Input dataset. Data may already be epoched; in this case,
% extract (shorter) subepochs time locked to epoch events.
% typerange - Cell array of event types to time lock to.
% (Note: An event field called 'type' must be defined in
% the 'EEG.event' structure).
% timelimits - Epoch latency limits [start end] in seconds relative to the time-locking event
% {default: [-1 2]}%
% invertselection - [0|1] Invert selection. 0 is to keep data around events
% and 1 is to remove them {default:1 to remove them}
%
% Outputs:
% OUTEEG - output dataset
%
% Authors: Arnaud Delorme, SCCN, INC, UCSD, 2009-
% Copyright (C) 2001 Arnaud Delorme, Salk Institute, arno@salk.edu
%
% This file is part of EEGLAB, see http://www.eeglab.org
% for the documentation and details.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
% THE POSSIBILITY OF SUCH DAMAGE.
function [EEG, com] = pop_rmdat( EEG, events, timelims, invertsel )
if nargin < 1
help pop_rmdat;
return;
end
com = '';
if nargin < 4
invertsel = 1;
warning('This function behavior has been fixed and data is removed by default')
end
if isempty(EEG(1).event)
error( [ 'No event. This function removes data' 10 'based on event latencies' ]);
end
if isempty(EEG(1).trials)
error( 'This function only works with continuous data');
end
if ~isfield(EEG(1).event, 'latency')
error( 'Absent latency field in event array/structure: must name one of the fields ''latency''');
end
if nargin < 3
% popup window parameters
% -----------------------
promptstr = { strvcat('Time-locking event type(s) ([]=all):', ...
'Select ''Edit > Event values'' to see type values.'), ...
'Epoch limits [start, end] in seconds:', ...
'Name for the new dataset:', ...
'Out-of-bounds EEG rejection limits ([min max], []=none):' };
cbevent = ['tmpEEG = get(gcbf, ''userdata'');' ...
'if ~isfield(tmpEEG.event, ''type'')' ...
' errordlg2(''No type field'');' ...
'else' ...
' tmpevent = tmpEEG.event;' ...
' if isnumeric(tmpEEG.event(1).type),' ...
' [tmps,tmpstr] = pop_chansel(unique([ tmpevent.type ]));' ...
' else,' ...
' [tmps,tmpstr] = pop_chansel(unique({ tmpevent.type }));' ...
' end;' ...
' if ~isempty(tmps)' ...
' set(findobj(''parent'', gcbf, ''tag'', ''events''), ''string'', tmpstr);' ...
' end;' ...
'end;' ...
'clear tmpEEG tmps tmpv tmpstr tmpevent tmpfieldnames;' ];
geometry = { [2 1 1.2] [2 1 1.2] };
uilist = { { 'style' 'text' 'string' 'Event type(s) ([]=all)' } ...
{ 'style' 'edit' 'string' '' 'tag' 'events' } ...
{ 'style' 'pushbutton' 'string' '...' 'callback' cbevent } ...
{ 'style' 'text' 'string' 'Time limits [start, end] in sec.' } ...
{ 'style' 'edit' 'string' '-1 1' } ...
{ 'style' 'popupmenu' 'string' 'Keep selected|Remove selected' } };
result = inputgui( 'geometry', geometry, 'uilist', uilist, 'helpcom', 'pophelp(''pop_rmdat'')', 'title', 'Remove data portions around events - pop_rmdat()', 'userdata', EEG(1));
if isempty(result)
return;
end
if strcmpi(result{1}, '[]'), result{1} = ''; end
if ~isempty(result{1})
if strcmpi(result{1}(1),'''') % If event type appears to be in single-quotes, use comma
% and single-quote as delimiter between event types. toby 2.24.2006
% fixed Arnaud May 2006
events = eval( [ '{' result{1} '}' ] );
else events = parsetxt( result{1});
end
else
events = {};
end
timelims = eval( [ '[' result{2} ']' ] );
invertsel = result{3}-1;
end
% process multiple datasets
% -------------------------
if length(EEG) > 1
if nargin < 3
[ EEG, com ] = eeg_eval( 'pop_rmdat', EEG, 'warning', 'on', 'params', { events, timelims, invertsel } );
else
[ EEG, com ] = eeg_eval( 'pop_rmdat', EEG, 'params', { events, timelims, invertsel } );
end
return;
end
tmpevent = EEG.event;
% Checking for numeric values in fieldnames and changing them to string
for i = 1: length(tmpevent)
checknum(i) = isnumeric(tmpevent(i).type);
end
if sum(checknum)~=0
tmpindx = find(checknum == 1);
for i = 1: length(tmpindx)
tmpevent(tmpindx(i)).type = num2str(tmpevent(tmpindx(i)).type);
end
end
% compute event indices
% ---------------------
allinds = [];
for index = 1:length(events)
inds = strmatch(events{index},{ tmpevent.type }, 'exact');
allinds = [allinds(:); inds(:) ]';
end
allinds = sort(allinds);
if isempty(allinds)
disp('No event found');
return;
end
% compute time limits
% -------------------
array = [];
bnd = strmatch('boundary', lower({tmpevent.type }));
bndlat = [ tmpevent(bnd).latency ];
for bind = 1:length(allinds)
evtlat = EEG.event(allinds(bind)).latency;
evtbeg = evtlat+EEG.srate*timelims(1);
evtend = evtlat+EEG.srate*timelims(2);
if any(bndlat > evtbeg & bndlat < evtend)
% find the closer upper and lower boundaries
bndlattmp = bndlat(bndlat > evtbeg & bndlat < evtend);
diffbound = bndlattmp-evtlat;
allneginds = find(diffbound < 0);
allposinds = find(diffbound > 0);
if ~isempty(allneginds), evtbeg = bndlattmp(allneginds(1)); end
if ~isempty(allposinds), evtend = bndlattmp(allposinds(1)); end;
fprintf('Boundary found: time limits for event %d reduced from %3.2f to %3.2f\n', allinds(bind), ...
(evtbeg-evtlat)/EEG.srate, (evtend-evtlat)/EEG.srate);
end
if ~isempty(array) && evtbeg < array(end)
array(end) = evtend;
else
array = [ array; evtbeg evtend];
end
end
if ~isempty(array) && array(1) < 1, array(1) = 1; end
if ~isempty(array) && array(end) > EEG.pnts, array(end) = EEG.pnts; end
if isempty(array)
disp('No event found');
return;
end
if invertsel
EEG = pop_select(EEG, 'notime', (array-1)/EEG.srate);
else
EEG = pop_select(EEG, 'time', (array-1)/EEG.srate);
end
% generate output command
% -----------------------
com = sprintf('EEG = pop_rmdat( EEG, %s);', vararg2str( { events timelims invertsel } ));