-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpop_grandaverage.m
More file actions
188 lines (165 loc) · 6.52 KB
/
pop_grandaverage.m
File metadata and controls
188 lines (165 loc) · 6.52 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
% pop_grandaverage - Grand average epoched EEGLAB EEG datasets
%
% Usage:
% >> [EEG, com] = pop_grandaverage(); % pop-up window mode
% >> [EEG, com] = pop_grandaverage({'dataset1.set' 'dataset2.set' ...
% 'datasetn.set'}, 'key1', value1, 'key2', ...
% value2, 'keyn', valuen);
% >> [EEG, com] = pop_grandaverage(ALLEEG); % pop-up window mode
% >> [EEG, com] = pop_grandaverage(ALLEEG, 'key1', value1, 'key2', ...
% value2, 'keyn', valuen);
%
% Inputs:
% ALLEEG - vector of EEGLAB EEG structures OR cell array of
% strings with dataset filenames
% 'datasets' - vector datasets to average
%
% Optional inputs:
% 'pathname' - char array path name {default '.'}
% 'eventtype' - new event type {default: first time locking event of
% each dataset}
%
% Outputs:
% EEG - EEGLAB EEG structure
% com - history string
%
% Note:
% Use with separate datasets for each subject and event type. All trials
% are averaged per dataset and the averages are collected in the third
% (trial) dimension of the output dataset. The event type of each average
% is defined by the first time locking event. In file mode all datasets
% should reside in a single directory. Electrode locations are NOT
% averaged. Electrode locations are preserved if all datasets contain the
% same or only a single dataset contains electrode location information.
%
% Author: Andreas Widmann, University of Leipzig, 2005
%123456789012345678901234567890123456789012345678901234567890123456789012
% Copyright (C) 2005 Andreas Widmann, University of Leipzig, widmann@uni-leipzig.de
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
% $Id$
function [EEG, com] = pop_grandaverage(ALLEEG, varargin)
EEG = [];
com = '';
% Convert args to struct
args = struct(varargin{:});
if nargin < 1
% Pop up file selection ui
[args.filenames args.pathname] = uigetfile('*.set', 'Select datasets -- pop_grandaverage', 'Multiselect', 'on');
if isnumeric(args.filenames) && args.filenames == 0, return, end
if ischar(args.filenames)
args.filenames = {args.filenames};
end
elseif iscell(ALLEEG)
args.filenames = ALLEEG;
clear ALLEEG;
else
% Pop up dataset selection ui
if ~isfield(args, 'datasets')
drawnow;
uigeom = {1 1};
uilist = {{'style' 'text' 'string' 'Select datasets:'} ...
{'style' 'listbox' 'string' {ALLEEG(:).setname} 'max' length(ALLEEG)}};
result = inputgui(uigeom, uilist, 'pophelp(''pop_grandaverage'')', 'Grand average loaded datasets -- pop_grandaverage()', [], 'normal', [1 length(ALLEEG)]);
if isempty(result), return, end
args.datasets = result{1};
end
ALLEEG = ALLEEG(args.datasets);
end
EEG = eeg_emptyset;
% Load datasets
if isfield(args, 'filenames')
if ~isfield(args, 'pathname')
args.pathname = '.';
end
for file = 1:length(args.filenames)
ALLEEG(file) = pop_loadset('filename', args.filenames{file}, 'filepath', args.pathname);
end
end
% Grand average data
for dataset = 1:length(ALLEEG)
EEG.data = cat(3, EEG.data, mean(ALLEEG(dataset).data, 3));
% Get event type from first time locking event
if isfield(args, 'eventtype')
EEG.event(dataset).type = args.eventtype;
elseif isfield(ALLEEG(dataset).event, 'type') && isfield(ALLEEG(dataset).event, 'latency')
tleLatency = (0 - ALLEEG(dataset).xmin) * ALLEEG(dataset).srate + 1;
[foo, tleIndex] = min([ALLEEG(dataset).event.latency] - tleLatency);
EEG.event(dataset).type = ALLEEG(dataset).event(tleIndex).type;
else
EEG.event(dataset).type = 'TLE'; % default
end
end
% pnts, nbchan, srate, xmin, xmax
for iField = {'pnts' 'nbchan' 'srate' 'xmin' 'xmax'}
EEG.(iField{:}) = unique([ALLEEG.(iField{:})]);
if length(EEG.(iField{:})) ~= 1
error(['Field EEG.' iField{:} ' not uniform.']);
end
end
% Reference
try
EEG.ref = unique(cat(1, ALLEEG.ref), 'rows');
if size(EEG.ref, 1) > 1
error('Field EEG.ref not uniform.');
end
catch
error('Field EEG.ref not uniform.');
end
% Trials
EEG.trials = size(EEG.data, 3);
% Channel labels and locations
Chanlocs = cat(1, ALLEEG.chanlocs);
fieldArray = fieldnames(Chanlocs);
for iField = 1:length(fieldArray)
for iChan = 1:EEG.nbchan
EEG.chanlocs(iChan).(fieldArray{iField}) = unique(cat(1, Chanlocs(:, iChan).(fieldArray{iField})), 'rows');
if size(EEG.chanlocs(iChan).(fieldArray{iField}), 1) > 1 && strcmpi(fieldArray{iField}, 'labels')
error('Structure EEG.chanlocs.labels not uniform.');
elseif size(EEG.chanlocs(iChan).(fieldArray{iField}), 1) > 1
warning(['Structure EEG.chanlocs not uniform. Grand-averaging of electrode locations not yet implemented. Skipping field ' fieldArray{iField} '.']);
EEG.chanlocs = rmfield(EEG.chanlocs, fieldArray{iField});
break
end
end
end
% Events
tmp = num2cell(round((0 - EEG.xmin) * EEG.srate + 1) + (0:length(EEG.event) - 1) * EEG.pnts);
[EEG.event(:).latency] = deal(tmp{:});
tmp = num2cell(1:length(EEG.event));
[EEG.event(:).epoch] = deal(tmp{:});
[EEG.event(:).trials] = deal(ALLEEG.trials);
[EEG.event(:).setname] = deal(ALLEEG.setname);
% Check dataset
EEG = eeg_checkset(EEG, 'eventconsistency');
% History string
if isfield(args, 'filenames')
com = sprintf('EEG = %s({', mfilename);
for file = 1:length(args.filenames) - 1
com = [com sprintf('''%s'' ', args.filenames{file})];
end
com = [com sprintf('''%s''}', args.filenames{end})];
args = rmfield(args, 'filenames');
else
com = sprintf('EEG = %s(ALLEEG', mfilename);
end
for c = fieldnames(args)'
if ischar(args.(c{:}))
com = [com sprintf(', ''%s'', ''%s''', c{:}, args.(c{:}))];
else
com = [com sprintf(', ''%s'', %s', c{:}, mat2str(args.(c{:})))];
end
end
com = [com ');'];