-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy patheeg_eval.m
192 lines (176 loc) · 7.09 KB
/
eeg_eval.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
% EEG_EVAL - apply eeglab function to a collection of input datasets
%
% Usage:
% >> OUTEEG = eeg_eval(funcname, INEEG, 'key1', value1, 'key2', value2 ...);
%
% Inputs:
% funcname - [string] name of the function
% INEEG - EEGLAB input dataset(s)
%
% Optional inputs
% 'params' - [cell array] funcname parameters.
% 'warning' - ['on'|'off'] warning pop-up window if several dataset
% stored on disk that will be automatically overwritten.
% Default is 'on'.
%
% Outputs:
% OUTEEG - output dataset(s)
%
% Author: Arnaud Delorme, SCCN, INC, UCSD, 2005
%
% see also: EEGLAB
% Copyright (C) 2005 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] = eeg_eval( funcname, EEG, varargin)
com = '';
if nargin < 2
help eeg_eval;
return;
end
% check input parameters
% ----------------------
g = finputcheck( varargin, { 'params' 'cell' {} {};
'warning' 'string' { 'on','off' } 'off' }, 'eeg_eval');
if ischar(g), error(g); end
% warning pop up
% --------------
eeglab_options;
if strcmpi(g.warning, 'on')
if ~option_storedisk
res = questdlg2(strvcat( 'When processing multiple datasets, it is not', ...
'possible to enter new names for the newly created', ...
'datasets and old datasets are overwritten.', ...
'You may still cancel this operation though.'), ...
'Multiple dataset warning', 'Cancel', 'Proceed', 'Proceed');
else
res = questdlg2( [ 'Data files on disk will be automatically overwritten.' 10 ...
'Are you sure you want to proceed with this operation?' ], ...
'Confirmation', 'Cancel', 'Proceed', 'Proceed');
end
switch lower(res)
case 'cancel', return;
case 'proceed'
end
vPar = ver('parallel');
if option_parallel && ~isempty(vPar)
res = questdlg2(strvcat( 'You have selected to use the parallel toolbox,', ...
'to process multiple datasets. If you saturate the ', ...
'memory, this could cause your computer to become.', ...
'unresponsive or even crash.'), ...
'Multiple dataset warning', 'Cancel', 'Proceed', 'Proceed');
end
switch lower(res)
case 'cancel', return;
case 'proceed'
end
end
% execute function
% ----------------
v = version;
if str2num(v(1)) == '5' % Matlab 5
command = [ 'TMPEEG = ' funcname '( TMPEEG, ' vararg2str(g.params) ');' ];
elseif isstr(funcname)
eval( [ 'func = @' funcname ';' ] );
else
func = funcname;
end
% notCompatibleFunc = {
% @clean_artifacts
% };
NEWEEG = EEG;
parstatus_changed = 0;
if option_parallel
if exist('gcp')
disp('Using the parallel toolbox to process multiple datasets (change in File > Preferences)');
ps = parallel.Settings;
parstatus = ps.Pool.AutoCreate;
ps.Pool.AutoCreate = true;
parstatus_changed = 1;
else
disp('Parallel toolbox not found - nothing to worry about (except slower computation in some cases)');
end
tmpoptions_store = option_storedisk;
parfor i = 1:length(EEG)
fprintf('Processing group dataset %d of %d named: %s ****************\n', i, length(EEG), EEG(i).setname);
TMPEEG = eeg_retrieve(EEG, i);
TMPEEG = feval(func, TMPEEG, g.params{:});
TMPEEG = eeg_checkset(TMPEEG);
TMPEEG.saved = 'no';
if tmpoptions_store
TMPEEG = pop_saveset(TMPEEG, 'savemode', 'resave');
TMPEEG = update_datafield(TMPEEG);
NEWEEG(i) = TMPEEG;
NEWEEG(i).saved = 'yes'; % eeg_store by default set it to no
else
NEWEEG(i) = TMPEEG;
NEWEEG(i).saved = 'yes'; % eeg_store by default set it to no
end
end
else
% check if parallel toolbox active
if exist('gcp')
delete(gcp('nocreate'));
ps = parallel.Settings;
parstatus = ps.Pool.AutoCreate;
ps.Pool.AutoCreate = false;
parstatus_changed = 1;
end
for i = 1:length(EEG)
fprintf('Processing group dataset %d of %d named: %s ****************\n', i, length(EEG), EEG(i).setname);
TMPEEG = eeg_retrieve(EEG, i);
TMPEEG = feval(func, TMPEEG, g.params{:});
TMPEEG = eeg_checkset(TMPEEG);
TMPEEG.saved = 'no';
if option_storedisk
TMPEEG = pop_saveset(TMPEEG, 'savemode', 'resave');
TMPEEG = update_datafield(TMPEEG);
end
NEWEEG = eeg_store(NEWEEG, TMPEEG, i);
if option_storedisk
NEWEEG(i).saved = 'yes'; % eeg_store by default set it to no
end
end
end
EEG = NEWEEG;
% set back default parallelization
% ----------------------
if parstatus_changed
ps.Pool.AutoCreate = parstatus;
end
% history
% -------
if nargout > 1
com = sprintf('%s = %s( %s,%s);', inputname(2), char(funcname), inputname(2), vararg2str(g.params));
end
function EEG = update_datafield(EEG)
if ~isfield(EEG, 'datfile'), EEG.datfile = ''; end
if ~isempty(EEG.datfile)
EEG.data = EEG.datfile;
else
EEG.data = 'in set file';
end
EEG.icaact = [];