-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathstd_interp.m
187 lines (169 loc) · 7.15 KB
/
std_interp.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
% STD_INTERP - interpolate, if needed, a list of named data channels
% for all datasets included in a STUDY. Currently assumes
% that all channels have uniform locations across datasets.
%
% Usage: >> [STUDY ALLEEG] = std_interp(STUDY, ALLEEG, chans, method);
%
% Inputs:
% STUDY - EEGLAB STUDY structure
% ALLEEG - EEGLAB vector containing all EEG dataset structures in the STUDY.
% chans - [Cell array] cell array of channel names (labels) to interpolate
% into the data if they are missing from one of the datasets.
% method - [string] GRIDDATA method to use for interpolation.
% See >> help EEG_INTERP {default:'spherical'}
%
% Important limitation:
% This function currently presuposes that all datasets have the same channel
% locations (with various channels from a standard set possibly missing).
% If this is not the case, the interpolation will not be performed.
%
% Output:
% STUDY - study structure.
% ALLEEG - updated datasets.
%
% Author: Arnaud Delorme, CERCO, CNRS, August 2006-
%
% See also: EEG_INTERP
% Copyright (C) Arnaud Delorme, CERCO, 2006, 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.
% Deprecated:
% - [chanlocs structure] channel location structure containing
% a full channel structure (missing channels in the current
% dataset are interpolated).
function [STUDY, ALLEEG] = std_interp(STUDY, ALLEEG, chans, method);
if nargin < 2
help std_interp;
return;
end
if nargin < 3
chans = [];
end
if nargin < 4
method = 'spherical';
end
% union of all channel structures
% -------------------------------
alllocs = eeg_mergelocs(ALLEEG.chanlocs);
% check electrode names to interpolate
% ------------------------------------
if iscell(chans)
alllabs = lower({ alllocs.labels });
for index = 1:length(chans)
tmpind = strmatch(lower(chans{index}), alllabs, 'exact');
if isempty(tmpind)
error( sprintf('Channel named ''%s'' not found in any dataset', chans{index}));
end
end
end
% read all STUDY datasets and interpolate electrodes
% ---------------------------------------------------
for index = 1:length(STUDY.datasetinfo)
tmpind = STUDY.datasetinfo(index).index;
tmplocs = ALLEEG(tmpind).chanlocs;
% build electrode location structure for interpolation
% ----------------------------------------------------
[tmp tmp2 id1] = intersect_bc({tmplocs.labels}, {alllocs.labels});
if isempty(chans)
interplocs = alllocs;
elseif iscell(chans)
[tmp tmp2 id2] = intersect_bc( chans, {alllocs.labels});
interplocs = alllocs(union(id1, id2));
else
interplocs = chans;
end
if length(interplocs) ~= length(tmplocs)
% search for position of electrode in removed channels
% ----------------------------------------------
extrachans = [];
if isfield(ALLEEG(tmpind).chaninfo, 'removedchans')
if isfield(ALLEEG(tmpind).chaninfo.removedchans, 'labels')
extrachans = ALLEEG(tmpind).chaninfo.removedchans;
end
end
tmplabels = { tmplocs.labels };
for i=1:length(interplocs)
ind = strmatch( interplocs(i).labels, tmplabels, 'exact');
if ~isempty(ind)
interplocs(i) = tmplocs(ind); % this is necessary for polhemus
elseif ~isempty(extrachans)
ind = strmatch( lower(interplocs(i).labels), lower({ extrachans.labels }), 'exact');
if ~isempty(ind)
fprintf('Found position of %s in chaninfo structure\n', interplocs(i).labels);
interplocs(i) = extrachans(ind);
end
end
end
% perform interpolation
% ---------------------
EEG = eeg_retrieve(ALLEEG, index);
EEG = eeg_checkset(EEG);
EEG = eeg_interp(EEG, interplocs, method);
EEG.saved = 'no';
EEG = pop_saveset(EEG, 'savemode', 'resave');
% update dataset in EEGLAB
% ------------------------
if ischar(ALLEEG(tmpind).data)
tmpdata = ALLEEG(tmpind).data;
[ ALLEEG EEG ] = eeg_store(ALLEEG, EEG, tmpind);
ALLEEG(tmpind).data = tmpdata;
ALLEEG(tmpind).saved = 'yes';
clear EEG;
else
[ ALLEEG EEG ] = eeg_store(ALLEEG, EEG, tmpind);
ALLEEG(tmpind).saved = 'yes';
end
else
fprintf('No need for interpolation for dataset %d\n', tmpind);
end
end
function checkchans(STUDY, ALLEEG)
% Check to see if all the channels have the same coordinates
% (check only the theta field)
% ----------------------------------------------------------
for index = 1:length(STUDY.datasetinfo)
tmpind = STUDY.datasetinfo(index).index;
tmplocs = ALLEEG(tmpind).chanlocs;
[tmp id1 id2] = intersect_bc({tmplocs.labels}, {alllocs.labels});
for ind = 1:length(id1)
if tmplocs(id1(ind)).theta ~= alllocs(id2(ind)).theta
% find datasets with different coordinates
% ----------------------------------------
for ind2 = 1:length(STUDY.datasetinfo)
tmplocs2 = ALLEEG(ind2).chanlocs;
tmpmatch = strmatch(alllocs(id2(ind)).labels, { tmplocs2.labels }, 'exact');
if ~isempty(tmpmatch)
if alllocs(id2(ind)).theta == tmplocs2(tmpmatch).theta
datind = ind2;
break;
end
end
end
error(sprintf( [ 'Dataset %d and %d do not have the same channel location\n' ...
'for electrode ''%s''' ], datind, tmpind, tmplocs(id1(ind)).labels));
end
end
end