[422372]: / functions / studyfunc / std_checkset.m

Download this file

418 lines (387 with data), 18.1 kB

  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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
% STD_CHECKSET - check STUDY set consistency
%
% Usage: >> [STUDY, ALLEEG] = std_checkset(STUDY, ALLEEG);
%
% Input:
% STUDY - EEGLAB STUDY set
% ALLEEG - vector of EEG datasets included in the STUDY structure
%
% Output:
% STUDY - a new STUDY set containing some or all of the datasets in ALLEEG,
% plus additional information from the optional inputs above.
% ALLEEG - an EEGLAB vector of EEG sets included in the STUDY structure
%
% Authors: Arnaud Delorme & Hilit Serby, SCCN, INC, UCSD, November 2005
% Copyright (C) Arnaud Delorme, SCCN, INC, UCSD, arno@sccn.ucsd.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 [STUDY, ALLEEG, command] = std_checkset(STUDY, ALLEEG, options)
if nargin < 2
help std_checkset;
return;
end
command = '';
if isempty(STUDY), return; end
studywasempty = 0;
modif = 0;
if ~isfield(STUDY, 'name'), STUDY.name = ''; modif = 1; end
if ~isfield(STUDY, 'task'), STUDY.task = ''; modif = 1; end
if ~isfield(STUDY, 'notes'), STUDY.notes = ''; modif = 1; end
if ~isfield(STUDY, 'filename'), STUDY.filename = ''; modif = 1; end
if ~isfield(STUDY, 'filepath'), STUDY.filepath = ''; modif = 1; end
if ~isfield(STUDY, 'history'), STUDY.history = ''; modif = 1; end
if ~isfield(STUDY, 'subject'), STUDY.subject = {}; modif = 1; end
if ~isfield(STUDY, 'group'), STUDY.group = {}; modif = 1; end
if ~isfield(STUDY, 'session'), STUDY.session = {}; modif = 1; end
if ~isfield(STUDY, 'run'), STUDY.run = {}; modif = 1; end
if ~isfield(STUDY, 'condition'), STUDY.condition = {}; modif = 1; end
if ~isfield(STUDY, 'etc'), STUDY.etc = []; modif = 1; end
if ~isfield(STUDY, 'cache'), STUDY.cache = []; modif = 1; end
if ~isfield(STUDY, 'etc.warnmemory'), STUDY.etc.warnmemory = 1; modif = 1; end
if ~isfield(STUDY, 'preclust'), STUDY.preclust = []; modif = 1; end
if ~isfield(STUDY, 'datasetinfo'), STUDY.datasetinfo = []; modif = 1; end
if ~isfield(STUDY.etc, 'version'), STUDY.etc.version = []; modif = 1; end
if ~isfield(STUDY.preclust, 'erpclusttimes' ), STUDY.preclust.erpclusttimes = []; modif = 1; end
if ~isfield(STUDY.preclust, 'specclustfreqs' ), STUDY.preclust.specclustfreqs = []; modif = 1; end
if ~isfield(STUDY.preclust, 'erspclustfreqs' ), STUDY.preclust.erspclustfreqs = []; modif = 1; end
if ~isfield(STUDY.preclust, 'erspclusttimes' ), STUDY.preclust.erspclusttimes = []; modif = 1; end
if ~isfield(STUDY.datasetinfo, 'comps') && ~isempty(STUDY.datasetinfo), STUDY.datasetinfo(1).comps = []; modif = 1; end
if ~isfield(STUDY.datasetinfo, 'index') && ~isempty(STUDY.datasetinfo), STUDY.datasetinfo(1).index = []; modif = 1; end
if ~isfield(STUDY.datasetinfo, 'run') && ~isempty(STUDY.datasetinfo), STUDY.datasetinfo(1).run = []; modif = 1; end
% all summary fields
% ------------------
try, subject = unique_bc({ STUDY.datasetinfo.subject });
catch,
subject = '';
disp('Important warning: some datasets do not have subject codes; some functions may crash!');
end
try, group = unique_bc({ STUDY.datasetinfo.group });
catch,
group = {};
% disp('Important warning: some datasets do not have group codes; some functions may crash!');
end
try, condition = unique_bc({ STUDY.datasetinfo.condition });
catch,
condition = {};
disp('Important warning: some datasets do not have condition codes; some functions may crash!');
end
try, session = unique_bc([STUDY.datasetinfo.session]);
catch,
session = '';
% disp('Important warning: some datasets do not have session numbers; some functions may crash!');
end
try, run = unique_bc([STUDY.datasetinfo.run]);
catch,
run = '';
% disp('Important warning: some datasets do not have session numbers; some functions may crash!');
end
if ~isequal(STUDY.subject, subject ), STUDY.subject = subject; modif = 1; end
if ~isequal(STUDY.group, group ), STUDY.group = group; modif = 1; end
if ~isequal(STUDY.condition, condition), STUDY.condition = condition; modif = 1; end
if ~isequal(STUDY.session, session ), STUDY.session = session; modif = 1; end
if ~isequal(STUDY.run, run ), STUDY.run = run; modif = 1; end
% check dataset info consistency
% ------------------------------
for k = 1:length(STUDY.datasetinfo)
if ~strcmpi(STUDY.datasetinfo(k).filename, ALLEEG(k).filename)
STUDY.datasetinfo(k).filename = ALLEEG(k).filename; modif = 1;
fprintf('Warning: file name has changed for dataset %d and the study has been updated\n', k);
fprintf(' to discard this change in the study, reload it from disk\n');
end
end
% recompute setind array (setind is deprecated but we keep it anyway)
% -------------------------------------------------------------------
setind = [];
sameica = std_findsameica(ALLEEG);
for index = 1:length(sameica)
setind(length(sameica{index}):-1:1,index) = sameica{index}';
end
setind(find(setind == 0)) = NaN;
if any(isnan(setind))
warndlg('Warning: non-uniform set of dataset, some function might not work');
end
% check that dipfit is present in all datasets
% --------------------------------------------
for cc = 1:length(sameica)
idat = [];
if length(sameica{cc}) > 1
for tmpi = 1:length(sameica{cc})
if isfield(ALLEEG(sameica{cc}(tmpi)).dipfit, 'model')
idat = sameica{cc}(tmpi);
end
end
if ~isempty(idat)
for tmpi = 1:length(sameica{cc})
if ~isfield(ALLEEG(sameica{cc}(tmpi)).dipfit, 'model')
ALLEEG(sameica{cc}(tmpi)).dipfit = ALLEEG(idat).dipfit;
ALLEEG(sameica{cc}(tmpi)).saved = 'no';
fprintf('Warning: no ICA dipoles for dataset %d, using dipoles from dataset %d (same ICA)\n', sameica{cc}(tmpi), idat);
end
end
end
end
end
% put in fake channels if channel labels are missing
% --------------------------------------------------
chanlabels = { ALLEEG.chanlocs };
if any(cellfun(@isempty, chanlabels))
if any(~cellfun(@isempty, chanlabels))
disp('********************************************************************');
disp(' IMPORTANT WARNING: SOME DATASETS DO NOT HAVE CHANNEL LABELS AND ');
disp(' SOME OTHERs HAVE CHANNEL LABELS. GENERATING CHANNEL LABELS FOR ');
disp(' THE FORMER DATASETS (THIS SHOULD PROBABLY BE FIXED BY THE USER).');
disp('********************************************************************');
end
disp('Generating channel labels for all datasets...');
for currentind = 1:length(ALLEEG)
for ind = 1:ALLEEG(currentind).nbchan
ALLEEG(currentind).chanlocs(ind).labels = int2str(ind);
end
end
ALLEEG(currentind).saved = 'no';
end
if length( unique( [ ALLEEG.srate ] )) > 1
disp('********************************************************************');
disp(' IMPORTANT WARNING: SOME DATASETS DO NOT HAVE THE SAME SAMPLING ');
disp(' RATE AND THIS WILL MAKE MOST OF THE STUDY FUNCTIONS CRASH. THIS');
disp(' SHOULD PROBABLY BE FIXED BY THE USER.');
disp('********************************************************************');
end
% create STUDY design if it is not present
% ----------------------------------------
rebuild_design = 0;
if ~isfield(STUDY, 'cluster'), STUDY.cluster = []; modif = 1; end
if ~isfield(STUDY, 'changrp'), STUDY.changrp = []; modif = 1; end
if ~studywasempty
if isfield(STUDY.datasetinfo, 'trialinfo')
alltrialinfo = { STUDY.datasetinfo.trialinfo };
if any(cellfun(@isempty, alltrialinfo)) && any(~cellfun(@isempty, alltrialinfo))
disp('Rebuilding trial information structure for STUDY');
STUDY = std_maketrialinfo(STUDY, ALLEEG); % some dataset do not have trialinfo and
% some other have it, remake it for everybody
end
elseif ALLEEG(1).trials > 1
disp('Rebuilding trial information structure for STUDY');
STUDY = std_maketrialinfo(STUDY, ALLEEG); % some dataset do not have trialinfo and
end
if ~isfield(STUDY, 'design') || isempty(STUDY.design) || ~isfield(STUDY.design, 'name')
STUDY = std_maketrialinfo(STUDY, ALLEEG);
STUDY = std_makedesign(STUDY, ALLEEG);
STUDY = std_selectdesign(STUDY, ALLEEG,1);
rebuild_design = 0;
else
if isfield(STUDY.design, 'indvar1')
STUDY = std_convertdesign(STUDY, ALLEEG);
end
% check independent variable
setFields = fieldnames(STUDY.datasetinfo);
if isempty(ALLEEG(1).event)
trialFields = [];
else trialFields = fieldnames(ALLEEG(1).event);
end
rmInd = [];
for indDes = 1:length(STUDY.design)
for indVar = 1:length(STUDY.design(indDes).variable)
if isempty(strmatch( STUDY.design(indDes).variable(indVar).label, setFields)) && ...
~isempty(trialFields) && ~isempty(STUDY.design(indDes).variable(indVar).label) && ...
isempty(strmatch( STUDY.design(indDes).variable(indVar).label, trialFields))
% Missing independent variable
if isempty(rmInd)
textVal1 = sprintf('The variable ''%s'' defined in the STUDY design is not found in ', STUDY.design(indDes).variable(indVar).label);
textVal2 = 'some of the EEG datasets. Some STUDY features will not be functional.';
if nargin > 2 && strcmpi(options, 'popup')
textVal = [ textVal1 10 textVal2 10 10 'Do you want to remove all designs with missing variables?' ];
res = questdlg2( textVal, 'Issue with studies', 'No', 'Yes', 'Yes');
if strcmpi(res, 'Yes')
rmInd = [ rmInd indDes ];
end
else
disp([textVal1 textVal2]);
end
else
rmInd = [ rmInd indDes ];
end
end
end
end
if ~isempty(rmInd)
STUDY.design(rmInd) = [];
end
% check cases
for indDes = 1:length(STUDY.design)
if ~isempty(setdiff(STUDY.design(indDes).cases.value, STUDY.subject))
fprintf('Fixing STUDY design to account for removed subjects\n');
STUDY.design(indDes).cases.value = intersect( STUDY.design(indDes).cases.value, STUDY.subject );
end
end
% convert combined independent variable values
% between dash to cell array of strings
% -------------------------------------
for inddes = 1:length(STUDY.design)
rmVar = [];
for indvar = 1:length(STUDY.design(inddes).variable)
% add pairing info in case it is missing
if isempty(STUDY.design(inddes).variable(indvar).label)
rmVar = [ rmVar indvar ];
end
if ~isfield(STUDY.design(inddes).variable, 'pairing') || isempty(STUDY.design(inddes).variable(indvar).pairing)
if strcmpi(STUDY.design(inddes).variable(indvar).label, 'group')
STUDY.design(inddes).variable(indvar).pairing = 'off';
else
STUDY.design(inddes).variable(indvar).pairing = 'on';
end
end
for indval = 1:length(STUDY.design(inddes).variable(indvar).value)
STUDY.design(inddes).variable(indvar).value{indval} = convertindvarval(STUDY.design(inddes).variable(indvar).value{indval});
end
end
STUDY.design(inddes).variable(rmVar) = [];
for indinclude = 1:length(STUDY.design(inddes).include)
if iscell(STUDY.design(inddes).include{indinclude})
for indval = 1:length(STUDY.design(inddes).include{indinclude})
STUDY.design(inddes).include{indinclude}{indval} = convertindvarval(STUDY.design(inddes).include{indinclude}{indval});
end
end
end
end
end
if rebuild_design % in case datasets have been added or removed
STUDY = std_rebuilddesign(STUDY, ALLEEG);
end
% scan design to fix old pairing format
% ------------------------------------
for design = 1:length(STUDY.design)
for var = 1:length(STUDY.design(design).variable)
if ischar(STUDY.design(design).variable(var).pairing)
if strcmpi(STUDY.design(design).variable(var).pairing, 'paired')
STUDY.design(design).variable(var).pairing = 'on';
elseif strcmpi(STUDY.design(design).variable(var).pairing, 'unpaired')
STUDY.design(design).variable(var).pairing = 'off';
end
end
end
end
% add filepath field if absent
for ind = 1:length(STUDY.design)
if ~isfield(STUDY.design, 'filepath') || (isnumeric(STUDY.design(ind).filepath) && isempty(STUDY.design(ind).filepath))
STUDY.design(ind).filepath = '';
STUDY.saved = 'no';
modif = 1;
end
end
% check for level field and add it if it not present
if length(STUDY.design) > 0 && isfield(STUDY.design, 'variable')
if ~isfield(STUDY.design(1).variable, 'level')
STUDY = std_addvarlevel(STUDY);
end
end
% check that ICA is present and if it is update STUDY.datasetinfo
recreateclusters = 0;
for index = 1:length(STUDY.datasetinfo)
if max(STUDY.datasetinfo(index).comps) > size(ALLEEG(index).icaweights,1)
STUDY.datasetinfo(index).comps = [1:size(ALLEEG(index).icaweights,1)];
recreateclusters = 1;
modif = 1;
end
end
if recreateclusters
fprintf('Recreating empty clusters because ICA has changed for some datasets\n');
STUDY.cluster = [];
end
% make channel groups
% -------------------
STUDYTMP = std_changroup(STUDY, ALLEEG);
if length(STUDYTMP.changrp) ~= length(STUDY.changrp)
STUDY.changrp = STUDYTMP.changrp;
modif = 1;
end
clear STUDYTMP;
end
% check cluster array
% -------------------
if isempty(STUDY.changrp) && isempty(STUDY.cluster)
rebuild_design = 1;
end
if isfield(STUDY.cluster, 'sets'),
if max(STUDY.cluster(1).sets(:)) > length(STUDY.datasetinfo)
disp('Warning: Some datasets had been removed from the STUDY, clusters have been reinitialized');
STUDY.cluster = []; modif = 1;
else
if isempty(STUDY.cluster(1).sets)
STUDY.cluster = []; modif = 1;
end
end
end
if ~studywasempty
if isempty(STUDY.cluster)
modif = 1;
[STUDY] = std_createclust(STUDY, ALLEEG, 'parentcluster', 'on');
end
if length(STUDY.cluster(1).child) == length(STUDY.cluster)-1 && length(STUDY.cluster) > 1
newchild = { STUDY.cluster(2:end).name };
if ~isequal(STUDY.cluster(1).child, newchild)
STUDY.cluster(1).child = newchild;
end
end
end
% adapt STUDY design to new framework
% -----------------------------------
if ~isempty(STUDY.design) && ~isfield(STUDY.design(1).variable, 'vartype')
for inddes = 1:length(STUDY.design)
for indvar = 1:length(STUDY.design(inddes).variable)
STUDY.design(inddes).variable(indvar).vartype = 'categorical';
end
end
end
if ~isempty(STUDY.design) && isfield(STUDY.design(1), 'cell')
STUDY.design = rmfield(STUDY.design, 'cell');
end
% determine if there has been any change
% --------------------------------------
if modif
STUDY.saved = 'no';
command = '[STUDY ALLEEG] = std_checkset(STUDY, ALLEEG);';
addToHistory = true;
% check duplicate
if length(STUDY.history) >= length(command) && strcmpi(STUDY.history(end-length(command)+1:end), command)
addToHistory = false;
end
if addToHistory
STUDY.history = sprintf('%s\n%s', STUDY.history, command);
end
end
% convert combined independent variables
% --------------------------------------
function val = convertindvarval(val)
if ischar(val)
inddash = findstr(' - ', val);
if isempty(inddash), return; end
inddash = [ -2 inddash length(val)+1];
for ind = 1:length(inddash)-1
newval{ind} = val(inddash(ind)+3:inddash(ind+1)-1);
end
val = newval;
end