Skip to content

Commit

Permalink
handle better removed channels when interpolating
Browse files Browse the repository at this point in the history
  • Loading branch information
arnodelorme committed Oct 10, 2022
1 parent d68648b commit beb207d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
28 changes: 19 additions & 9 deletions functions/popfunc/pop_interp.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@
disp('editor and declare such channels as non-data channels');

enablenondat = 'off';
if isfield(EEG.chaninfo, 'nodatchans')
if ~isempty(EEG.chaninfo.nodatchans)
if isfield(EEG.chaninfo, 'removedchans')
if ~isempty(EEG.chaninfo.removedchans)
enablenondat = 'on';
end
end

uilist = { { 'Style' 'text' 'string' 'What channel(s) do you want to interpolate' 'fontweight' 'bold' } ...
{ 'style' 'text' 'string' 'none selected' 'tag' 'chanlist' } ...
{ 'style' 'pushbutton' 'string' 'Select from removed channels' 'callback' 'pop_interp(''nondatchan'',gcbf);' 'enable' enablenondat } ...
{ 'style' 'pushbutton' 'string' 'Select from removed channels' 'callback' 'pop_interp(''removedchans'',gcbf);' 'enable' enablenondat } ...
{ 'style' 'pushbutton' 'string' 'Select from data channels' 'callback' 'pop_interp(''datchan'',gcbf);' } ...
{ 'style' 'pushbutton' 'string' 'Use specific channels of other dataset' 'callback' 'pop_interp(''selectchan'',gcbf);'} ...
{ 'style' 'pushbutton' 'string' 'Use all channels from other dataset' 'callback' 'pop_interp(''uselist'',gcbf);'} ...
Expand All @@ -87,7 +87,7 @@

geom = { 1 1 1 1 1 1 1 [1.1 1] 1 1 };
geomvert = [ 1 1 1 1 1 1 1 1 0.5 1 ];
[res userdata tmp restag ] = inputgui( 'uilist', uilist, 'title', 'Interpolate channel(s) -- pop_interp()', 'geometry', geom, 'geomvert', geomvert, 'helpcom', 'pophelp(''pop_interp'')');
[res, userdata, ~, restag ] = inputgui( 'uilist', uilist, 'title', 'Interpolate channel(s) -- pop_interp()', 'geometry', geom, 'geomvert', geomvert, 'helpcom', 'pophelp(''pop_interp'')');
if isempty(res) || isempty(userdata), return; end

if restag.method == 1
Expand All @@ -97,7 +97,7 @@
bad_elec = userdata.chans;

com = sprintf('EEG = pop_interp(EEG, %s, ''%s'');', userdata.chanstr, method);
if ~isempty(findstr('nodatchans', userdata.chanstr))
if ~isempty(findstr('removedchans', userdata.chanstr))
eval( [ userdata.chanstr '=[];' ] );
end

Expand All @@ -107,13 +107,13 @@
fig = bad_elec;
userdata = get(fig, 'userdata');

if strcmpi(command, 'nondatchan')
if strcmpi(command, 'removedchans')
global EEG;
tmpchaninfo = EEG.chaninfo;
[chanlisttmp chanliststr] = pop_chansel( { tmpchaninfo.nodatchans.labels } );
[chanlisttmp chanliststr] = pop_chansel( { tmpchaninfo.removedchans.labels } );
if ~isempty(chanlisttmp),
userdata.chans = EEG.chaninfo.nodatchans(chanlisttmp);
userdata.chanstr = [ 'EEG.chaninfo.nodatchans([' num2str(chanlisttmp) '])' ];
userdata.chans = EEG.chaninfo.removedchans(chanlisttmp);
userdata.chanstr = [ 'EEG.chaninfo.removedchans([' num2str(chanlisttmp) '])' ];
set(fig, 'userdata', userdata);
set(findobj(fig, 'tag', 'chanlist'), 'string', chanliststr);
end
Expand Down Expand Up @@ -169,6 +169,16 @@
return;
end

% remove from removedchans if interpolated
if isfield(EEG.chaninfo, 'removedchans') && ~isempty(EEG.chaninfo.removedchans) && isstruct(bad_elec)
for iChan = 1:length(bad_elec)
ind = strmatch(lower(bad_elec(iChan).labels), lower({EEG.chaninfo.removedchans.labels}), 'exact');
if ~isempty(ind)
EEG.chaninfo.removedchans(ind) = [];
end
end
end

EEG = eeg_interp(EEG, bad_elec, method);


34 changes: 17 additions & 17 deletions functions/popfunc/pop_reref.m
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,16 @@
'end;' ];
cb_chansel1 = 'tmpEEG = get(gcbf, ''userdata''); tmpchanlocs = tmpEEG(1).chanlocs; [tmp tmpval] = pop_chansel({tmpchanlocs.labels}, ''withindex'', ''on''); set(findobj(gcbf, ''tag'', ''reref'' ), ''string'',tmpval); clear tmpEEG tmpchanlocs tmp tmpval';
cb_chansel2 = 'tmpEEG = get(gcbf, ''userdata''); tmpchanlocs = tmpEEG(1).chanlocs; [tmp tmpval] = pop_chansel({tmpchanlocs.labels}, ''withindex'', ''on''); set(findobj(gcbf, ''tag'', ''exclude'' ), ''string'',tmpval); clear tmpEEG tmpchanlocs tmp tmpval';
cb_chansel3 = [ 'tmpEEG = get(gcbf, ''userdata''); if ~isfield(tmpEEG(1).chaninfo, ''nodatchans''), ' ...
cb_chansel3 = [ 'tmpEEG = get(gcbf, ''userdata''); if ~isfield(tmpEEG(1).chaninfo, ''removedchans''), ' ...
' warndlg2(''There are no Reference channel defined, add it using the channel location editor'');' ...
'elseif isempty(tmpEEG(1).chaninfo.nodatchans),' ...
'elseif isempty(tmpEEG(1).chaninfo.removedchans),' ...
' warndlg2(''There are no Reference channel defined, add it using the channel location editor'');' ...
'elseif isfield(tmpEEG(1).chaninfo.nodatchans, ''type''),' ...
' fidType = ismember(cellfun(@char, { tmpEEG(1).chaninfo.nodatchans.type}, ''UniformOutput'', false), ''FID'');' ...
'elseif isfield(tmpEEG(1).chaninfo.removedchans, ''type''),' ...
' fidType = ismember(cellfun(@char, { tmpEEG(1).chaninfo.removedchans.type}, ''UniformOutput'', false), ''FID'');' ...
' if sum(fidType == 0) == 0,' ...
' warndlg2(''There are no Reference channel defined, add it using the channel location editor'');' ...
' else,' ...
' tmpchaninfo = tmpEEG(1).chaninfo; [tmp tmpval] = pop_chansel({tmpchaninfo.nodatchans(~fidType).labels}, ''withindex'', ''on'');' ...
' tmpchaninfo = tmpEEG(1).chaninfo; [tmp tmpval] = pop_chansel({tmpchaninfo.removedchans(~fidType).labels}, ''withindex'', ''on'');' ...
' set(findobj(gcbf, ''tag'', ''refloc'' ), ''string'',tmpval);' ...
' end;' ...
'end;' ...
Expand Down Expand Up @@ -185,13 +185,13 @@
if ~isempty(restag.refloc)
try
tmpchaninfo = EEG(1).chaninfo;
tmpallchans = lower({ tmpchaninfo.nodatchans.labels });
tmpallchans = lower({ tmpchaninfo.removedchans.labels });
allelecs = parsetxt(lower(restag.refloc));
chanind = [];
for iElec = 1:length(allelecs)
chanind = [chanind strmatch( allelecs{iElec}, tmpallchans, 'exact') ];
end
options = { options{:} 'refloc' EEG(1).chaninfo.nodatchans(chanind) };
options = { options{:} 'refloc' EEG(1).chaninfo.removedchans(chanind) };
catch, disp('Error with old reference: ignoring it');
end
end
Expand Down Expand Up @@ -336,32 +336,32 @@
% deal with reference
% -------------------
if ~isempty(refchan)
if ~isfield(EEG.chaninfo, 'nodatchans')
EEG.chaninfo.nodatchans = refchan;
elseif isempty(EEG.chaninfo.nodatchans)
EEG.chaninfo.nodatchans = refchan;
if ~isfield(EEG.chaninfo, 'removedchans')
EEG.chaninfo.removedchans = refchan;
elseif isempty(EEG.chaninfo.removedchans)
EEG.chaninfo.removedchans = refchan;
else
allf = fieldnames(refchan);
n = length(EEG.chaninfo.nodatchans);
n = length(EEG.chaninfo.removedchans);
for iRef = 1:length(refchan)
for ind = 1:length(allf)
EEG.chaninfo.nodatchans = setfield(EEG.chaninfo.nodatchans, { n+iRef }, ...
EEG.chaninfo.removedchans = setfield(EEG.chaninfo.removedchans, { n+iRef }, ...
allf{ind}, getfield(refchan(iRef), allf{ind}));
end
end
end
end
if ~isempty(g.refloc)
if isfield(EEG.chaninfo, 'nodatchans') && ~isempty(EEG.chaninfo.nodatchans)
if isfield(EEG.chaninfo, 'removedchans') && ~isempty(EEG.chaninfo.removedchans)
allinds = [];
tmpchaninfo = EEG.chaninfo;
for iElec = 1:length(g.refloc)
if isempty(tmpchaninfo) || isempty(tmpchaninfo.nodatchans)
if isempty(tmpchaninfo) || isempty(tmpchaninfo.removedchans)
error('Missing reference channel information. Edit channels and add reference first.');
end
allinds = [allinds strmatch( g.refloc(iElec).labels, { tmpchaninfo.nodatchans.labels }) ];
allinds = [allinds strmatch( g.refloc(iElec).labels, { tmpchaninfo.removedchans.labels }) ];
end
EEG.chaninfo.nodatchans(allinds) = [];
EEG.chaninfo.removedchans(allinds) = [];
else
error('Missing reference channel information. Edit channels and add reference first.');
end
Expand Down
10 changes: 5 additions & 5 deletions functions/studyfunc/std_interp.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@

if length(interplocs) ~= length(tmplocs)

% search for position of electrode in backup structure
% search for position of electrode in removed channels
% ----------------------------------------------
extrachans = [];
if isfield(ALLEEG(tmpind).chaninfo, 'nodatchans')
if isfield(ALLEEG(tmpind).chaninfo.nodatchans, 'labels')
extrachans = ALLEEG(tmpind).chaninfo.nodatchans;
if isfield(ALLEEG(tmpind).chaninfo, 'removedchans')
if isfield(ALLEEG(tmpind).chaninfo.removedchans, 'labels')
extrachans = ALLEEG(tmpind).chaninfo.removedchans;
end
end
tmplabels = { tmplocs.labels };
Expand All @@ -120,7 +120,7 @@
if ~isempty(ind)
interplocs(i) = tmplocs(ind); % this is necessary for polhemus
elseif ~isempty(extrachans)
ind = strmatch( interplocs(i).labels, { extrachans.labels }, 'exact');
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);
Expand Down

0 comments on commit beb207d

Please sign in to comment.