Switch to unified view

a b/functions/adminfunc/eeg_eval.m
1
% EEG_EVAL - apply eeglab function to a collection of input datasets
2
%
3
% Usage:
4
%   >> OUTEEG = eeg_eval(funcname, INEEG, 'key1', value1, 'key2', value2 ...);
5
%
6
% Inputs:
7
%   funcname      - [string] name of the function
8
%   INEEG         - EEGLAB input dataset(s)
9
%
10
% Optional inputs
11
%   'params'      - [cell array] funcname parameters.
12
%   'warning'     - ['on'|'off'] warning pop-up window if several dataset
13
%                   stored on disk that will be automatically overwritten.
14
%                   Default is 'on'.
15
%
16
% Outputs:
17
%   OUTEEG        - output dataset(s)
18
%
19
% Author: Arnaud Delorme, SCCN, INC, UCSD, 2005
20
% 
21
% see also: EEGLAB
22
23
% Copyright (C) 2005 Arnaud Delorme, Salk Institute, arno@salk.edu
24
%
25
% This file is part of EEGLAB, see http://www.eeglab.org
26
% for the documentation and details.
27
%
28
% Redistribution and use in source and binary forms, with or without
29
% modification, are permitted provided that the following conditions are met:
30
%
31
% 1. Redistributions of source code must retain the above copyright notice,
32
% this list of conditions and the following disclaimer.
33
%
34
% 2. Redistributions in binary form must reproduce the above copyright notice,
35
% this list of conditions and the following disclaimer in the documentation
36
% and/or other materials provided with the distribution.
37
%
38
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
39
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
42
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
43
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
44
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
46
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
48
% THE POSSIBILITY OF SUCH DAMAGE.
49
50
function [EEG, com] = eeg_eval( funcname, EEG, varargin)
51
    
52
    com = '';
53
    if nargin < 2
54
        help eeg_eval;
55
        return;
56
    end
57
    
58
    % check input parameters
59
    % ----------------------
60
    g = finputcheck( varargin, { 'params'   'cell'    {}               {};
61
                                 'warning'  'string'  { 'on','off' }   'off' }, 'eeg_eval');
62
    if ischar(g), error(g); end
63
    
64
    % warning pop up
65
    % --------------
66
    eeglab_options;
67
    if strcmpi(g.warning, 'on')
68
        if ~option_storedisk
69
            res = questdlg2(strvcat( 'When processing multiple datasets, it is not', ...
70
                                 'possible to enter new names for the newly created', ...
71
                                 'datasets and old datasets are overwritten.', ...
72
                                 'You may still cancel this operation though.'), ...
73
                 'Multiple dataset warning', 'Cancel', 'Proceed', 'Proceed');
74
        else
75
            res = questdlg2( [ 'Data files on disk will be automatically overwritten.' 10 ...
76
                                'Are you sure you want to proceed with this operation?' ], ...
77
                            'Confirmation', 'Cancel', 'Proceed', 'Proceed');
78
        end
79
        switch lower(res)
80
            case 'cancel', return;
81
            case 'proceed'
82
        end
83
        vPar = ver('parallel');
84
        if option_parallel && ~isempty(vPar)
85
            res = questdlg2(strvcat( 'You have selected to use the parallel toolbox,', ...
86
                                 'to process multiple datasets. If you saturate the ', ...
87
                                 'memory, this could cause your computer to become.', ...
88
                                 'unresponsive or even crash.'), ...
89
                 'Multiple dataset warning', 'Cancel', 'Proceed', 'Proceed');
90
        end
91
        switch lower(res)
92
            case 'cancel', return;
93
            case 'proceed'
94
        end
95
    end
96
 
97
    % execute function
98
    % ----------------
99
    v = version;
100
    if str2num(v(1)) == '5' % Matlab 5
101
        command = [ 'TMPEEG = ' funcname '( TMPEEG, ' vararg2str(g.params) ');' ];
102
    elseif isstr(funcname)
103
        eval( [ 'func = @' funcname ';' ] );
104
    else
105
        func = funcname;
106
    end
107
    
108
%     notCompatibleFunc = { 
109
%         @clean_artifacts
110
%     };
111
    
112
    NEWEEG = EEG;
113
    parstatus_changed = 0;
114
115
    if option_parallel
116
        if exist('gcp')
117
            disp('Using the parallel toolbox to process multiple datasets (change in File > Preferences)');
118
            ps = parallel.Settings;
119
            parstatus = ps.Pool.AutoCreate;
120
            ps.Pool.AutoCreate = true;
121
            parstatus_changed = 1;
122
        else
123
            disp('Parallel toolbox not found - nothing to worry about (except slower computation in some cases)');
124
        end
125
        
126
        tmpoptions_store = option_storedisk;
127
        parfor i = 1:length(EEG)
128
            fprintf('Processing group dataset %d of %d named: %s ****************\n', i, length(EEG), EEG(i).setname);
129
            TMPEEG    = eeg_retrieve(EEG, i);
130
            TMPEEG = feval(func, TMPEEG, g.params{:});
131
            TMPEEG = eeg_checkset(TMPEEG);
132
            TMPEEG.saved = 'no';
133
            if tmpoptions_store
134
                TMPEEG = pop_saveset(TMPEEG, 'savemode', 'resave');
135
                TMPEEG = update_datafield(TMPEEG);
136
                NEWEEG(i) = TMPEEG;
137
                NEWEEG(i).saved = 'yes'; % eeg_store by default set it to no
138
            else
139
                NEWEEG(i) = TMPEEG;
140
                NEWEEG(i).saved = 'yes'; % eeg_store by default set it to no
141
            end
142
        end
143
144
    else
145
146
        % check if parallel toolbox active
147
        if exist('gcp')
148
            delete(gcp('nocreate'));
149
            ps = parallel.Settings;
150
            parstatus = ps.Pool.AutoCreate;
151
            ps.Pool.AutoCreate = false;
152
            parstatus_changed = 1;
153
        end
154
155
        for i = 1:length(EEG)
156
                fprintf('Processing group dataset %d of %d named: %s ****************\n', i, length(EEG), EEG(i).setname);
157
            TMPEEG    = eeg_retrieve(EEG, i);
158
            TMPEEG = feval(func, TMPEEG, g.params{:});
159
            TMPEEG = eeg_checkset(TMPEEG);
160
            TMPEEG.saved = 'no';
161
            if option_storedisk
162
                TMPEEG = pop_saveset(TMPEEG, 'savemode', 'resave');
163
                TMPEEG = update_datafield(TMPEEG);
164
            end
165
            NEWEEG = eeg_store(NEWEEG, TMPEEG, i);
166
            if option_storedisk
167
                NEWEEG(i).saved = 'yes'; % eeg_store by default set it to no
168
            end
169
        end
170
    end
171
    EEG = NEWEEG;
172
    
173
    % set back default parallelization
174
    % ----------------------
175
    if parstatus_changed
176
        ps.Pool.AutoCreate = parstatus;
177
    end
178
179
    % history
180
    % -------
181
    if nargout > 1
182
        com = sprintf('%s = %s( %s,%s);', inputname(2), char(funcname), inputname(2), vararg2str(g.params));
183
    end
184
185
function EEG = update_datafield(EEG)
186
    if ~isfield(EEG, 'datfile'), EEG.datfile = ''; end
187
    if ~isempty(EEG.datfile)
188
        EEG.data = EEG.datfile;
189
    else 
190
        EEG.data = 'in set file';
191
    end
192
    EEG.icaact = [];