[Eeglablist] Using the 'command' field of eeg_plot to reject trials
Matthew Moore
matthew.moore at otago.ac.nz
Wed Jul 2 03:53:18 PDT 2014
Thanks for your help Mikolaj and Makoto!
I am very aware of the dangers of global variables, it is unfortunate that they seem so difficult to avoid in Matlab!
I have created a version using evalin , here it is in case it comes in handy for someone. It is not fully tested, so user beware :)
function cleanedEEGData = detectArtefacts(eegData,threshold,slope,jp,dist,spec)
%DETECTARTEFACTS Detects artefactual data using EEGLAB's functions, then
% displays the data in a plot for the user to check.
% Returns the eegData with artefactual trials removed.
%Absolute limit of electrode values in uV
if nargin<2
threshold=[];
end
lowerLimit=-150;
upperLimit=150;
%Maximum slope over an epoch in uV/epoch
if nargin<3
slope=[];
end
maxSlope=60;
rVal=0.3;
%Reject unlikely epochs, by number of SDs
if nargin<4
jp=[];
end
sdJPSingleChan=4;
sdJPAllChans=4;
%Reject abnormal distributions, by number of SDs
if nargin<5
dist=[];
end
sdKurtSingleChan=4;
sdKurtAllChans=4;
%Reject abnormal spectra
if nargin<6
spec=[];
end
thresholds=[-50 50;-100 25];
frequencies=[0 2;20 40];
epochLength=eegData.xmax;
epochTimeFrames=eegData.pnts;
sampleRate=eegData.srate;
events=eegData.event;
nElec=eegData.nbchan;
%Do the error detecting and get the format for marking trials in
%eegplot
if ~isempty(threshold)
eegData=pop_eegthresh(eegData,1,1:nElec,lowerLimit,upperLimit,0,epochLength,1,0,0);
plotRejThr=trial2eegplot(eegData.reject.rejthresh,eegData.reject.rejthreshE,epochTimeFrames,eegData.reject.rejthreshcol);
eegData.comments = pop_comments(eegData.comments,'',['Filtered by pop_eegthresh, lowerLimit = ' num2str(lowerLimit) ',upperLimit = ' num2str(upperLimit)],1);
else
plotRejThr=[];
end
if ~isempty(slope)
eegData=pop_rejtrend(eegData,1,1:nElec,epochTimeFrames,maxSlope,rVal,1,0,0);
plotRejTre=trial2eegplot(eegData.reject.rejconst,eegData.reject.rejconstE,epochTimeFrames,eegData.reject.rejconstcol);
eegData.comments = pop_comments(eegData.comments,'',['Filtered by pop_rejtrend, maxSlope = ' num2str(maxSlope)],1);
else
plotRejTre=[];
end
if ~isempty(jp)
eegData=pop_jointprob(eegData,1,1:nElec,sdJPSingleChan,sdJPAllChans,1,0);
plotRejJp =trial2eegplot(eegData.reject.rejjp,eegData.reject.rejjpE,epochTimeFrames,eegData.reject.rejjpcol);
eegData.comments = pop_comments(eegData.comments,'',['Filtered by pop_jointprob, sdJPSingleChan = ' num2str(sdJPSingleChan) ',sdJPAllChans = ' num2str(sdJPAllChans)],1);
else
plotRejJp=[];
end
if ~isempty(dist)
eegData=pop_rejkurt(eegData,1,1:nElec,sdKurtSingleChan,sdKurtAllChans,1,0,1);
plotRejKur=trial2eegplot(eegData.reject.rejkurt,eegData.reject.rejkurtE,epochTimeFrames,eegData.reject.rejkurtcol);
eegData.comments = pop_comments(eegData.comments,'',['Filtered by pop_rejkurt, sdKurtSingleChan = ' num2str(sdKurtSingleChan) ',sdKurtAllChans = ' num2str(sdKurtAllChans)],1);
else
plotRejKur=[];
end
if ~isempty(spec)
eegData=pop_rejspec( eegData, 1,'elecrange',1:30,'threshold',thresholds,'freqlimits',frequencies,'eegplotcom','','eegplotplotallrej',1,'eegplotreject',0,'specdata',eegData.specdata);
plotRejSpe=trial2eegplot(eegData.reject.rejfreq,eegData.reject.rejfreqE,epochTimeFrames,eegData.reject.rejfreqcol);
eegData.comments = pop_comments(eegData.comments,'',['Filtered by pop_rejkurt, threshold = ' num2str(thresholds) ',freqlimits = ' num2str(frequencies)],1);
else
plotRejSpe=[];
end
assignin('base', 'EEG', eegData);
evalin('base','detectArtefactsFinished=0;');
%command string for reject marked trials... all marked epochs...
%adapted from http://sccn.ucsd.edu/pipermail/eeglablist/2011/004085.html
cmd = [ ...
'[tmprej tmprejE] = eegplot2trialMod( TMPREJ,' num2str(eegData.pnts) ',' num2str(eegData.trials) ');' ...
'EEG = pop_rejepoch(EEG, tmprej, 1);' ...
'detectArtefactsFinished=1;' ...
] ;
rejE=[plotRejThr;plotRejTre;plotRejJp,plotRejKur,plotRejSpe];
%Draw the data.
eegplot(eegData.data,...
'srate',sampleRate,...
'events',events,...
'winrej',rejE,...
'command',cmd,...
'butlabel','Reject');
%Wait until the user has finished reviewing.
reviewFinished=0;
while ~reviewFinished
reviewFinished=evalin('base','detectArtefactsFinished');
pause(0.01);
end
evalin('base','clear detectArtefactsFinished');
cleanedEEGData=evalin('base','EEG');
end
________________________________
From: Makoto Miyakoshi [mmiyakoshi at ucsd.edu]
Sent: Tuesday, July 01, 2014 6:49 AM
To: Mikołaj Magnuski
Cc: EEGLAB List; Matthew Moore
Subject: Re: [Eeglablist] Using the 'command' field of eeg_plot to reject trials
Very good to know, thanks!
Makoto
On Mon, Jun 30, 2014 at 11:23 AM, Mikołaj Magnuski <imponderabilion at gmail.com<mailto:imponderabilion at gmail.com>> wrote:
It depends on a number of things. For example some of the eeglab functions that you call may be declaring EEG as global or evaluating it in workspace (but in such case this should happen whatever name you gave to the variable passed as EEG).
In some cases it may be safer to declare EEG as global / evalin yourself. I remember once having a function that kept working on the same file instead of looping through multiple files. The global declaration in one of the eeglab functions was overwriting the EEG that it got passed with the previously global EEG (the first EEG processed).
To avoid these perils one can declare EEG as global at the relevant level, use evalin('base', 'EEG', EEG) or clearvars -global EEG :)
30 cze 2014 19:01 "Makoto Miyakoshi" <mmiyakoshi at ucsd.edu<mailto:mmiyakoshi at ucsd.edu>> napisał(a):
Thanks MIkolaj. Actually I did not know it. It seems it has been automatically taken care of in my case probably because I have launched EEGLAB before using any of eeglab functions...
Makoto
On Sat, Jun 28, 2014 at 12:15 AM, Mikołaj Magnuski <imponderabilion at gmail.com<mailto:imponderabilion at gmail.com>> wrote:
Dear Matthew and Makoto,
synchronizing with base workspace eeglab GUI usually requires a bit more (unless you want to change your function into a script).
The easiest way is to declare relevant eeglab variables as globals at the beginning of the function (EEG, ALLEEG, etc. - whatever eeglab variables you are using and want to sync with workspace gui). For example:
global EEG
Unfortunately, using globals is usually frowned upon by programmers (for good reasons) but I don't think you should worry too much when using globals in small applications.
Another way however would be to assign (see assignin docummentation) relevant eeglab variables in the base workspace and evaluate (see evalin) eeglab GUI commands in the base workspace too.
Try globals first. EEGlab declares most (if not all) of the all-caps base workspace variables as globals and synchronizes the gui functions (at least some of them) with the workspace this way - so you should be fine with global variables (from my experience - using globals is the easiest and most efficient way of creating and synchronizing GUI data in matlab without blocking the command window with uiwait( ) etc. ).
28 cze 2014 05:35 "Makoto Miyakoshi" <mmiyakoshi at ucsd.edu<mailto:mmiyakoshi at ucsd.edu>> napisał(a):
Dear Matthew,
Looks like you want to automate the epoch rejection process.
> My problem may be that I can't seem to find how to load the eeglab workspace variables, other than through the gui.
I recommend you replace all eegData in your code to EEG for the sake of simplicity. As long as they are 'EEG' by typing 'eeglab redraw' you can interact with it through EEGLAB main GUI.
To load data, use EEG = pop_loadset()... of course you can do eegData = pop_loadset()... but again using 'EEG' allows you to interact it via GUI.
Makoto
On Tue, Jun 24, 2014 at 9:44 PM, Matthew Moore <matthew.moore at otago.ac.nz<mailto:matthew.moore at otago.ac.nz>> wrote:
Hi,
I am having a bit of trouble getting eegplot to allow inspection and editing of candidates for rejection.
I have looked at this thread: http://sccn.ucsd.edu/pipermail/eeglablist/2011/004085.html
I tried to implement the command listed there, but am having trouble. My problem may be that I can't seem to find how to load the eeglab workspace variables, other than through the gui.
I would really appreciate if someone could have a look at the code and give me some pointers.
Matt
function cleanedEEGData = detectArtefacts(eegData,locFile)
%DETECTARTEFACTS Detects artefactual data using EEGLAB's functions, then
% displays the data in a plot for the user to check.
% Returns the eegData with artefactual trials removed.
% locFile is the file containing electrode locations.
epochLength=eegData.xmax;
epochTimeFrames=eegData.pnts;
sampleRate=eegData.srate;
events=eegData.event;
nElec=eegData.nbchan;
%Absolute limit of electrode values in uV
lowerLimit=-150;
upperLimit=150;
%Maximum slope over an epoch in uV/epoch
maxSlope=60;
rVal=0.3;
%Reject unlikely epochs, by number of SDs
sdJPSingleChan=4;
sdJPAllChans=4;
%Reject abnormal distributions, by number of SDs
sdKurtSingleChan=4;
sdKurtAllChans=4;
%Reject abnormal spectra
thresholds=[-50 50;-100 25];
frequencies=[0 2;20 40];
%Do the error detecting.
eegData=pop_eegthresh(eegData,1,1:nElec,lowerLimit,upperLimit,0,epochLength,1,0,0);
eegData=pop_rejtrend(eegData,1,1:nElec,epochTimeFrames,maxSlope,rVal,1,0,0);
eegData=pop_jointprob(eegData,1,1:nElec,sdJPSingleChan,sdJPAllChans,1,0);
eegData=pop_rejkurt(eegData,1,1:nElec,sdKurtSingleChan,sdKurtAllChans,1,0);
% eegData=pop_rejspec( eegData, 1,'elecrange',1:30,'threshold',thresholds,'freqlimits',frequencies,'eegplotcom','','eegplotplotallrej',1,'eegplotreject',0,'specdata',eegData.specdata);
%Get the format for marking trials in an eegplot
plotRejThr=trial2eegplot(eegData.reject.rejthresh,eegData.reject.rejthreshE,epochTimeFrames,eegData.reject.rejthreshcol);
plotRejTre=trial2eegplot(eegData.reject.rejconst,eegData.reject.rejconstE,epochTimeFrames,eegData.reject.rejconstcol);
plotRejJp =trial2eegplot(eegData.reject.rejjp,eegData.reject.rejjpE,epochTimeFrames,eegData.reject.rejjpcol);
plotRejKur=trial2eegplot(eegData.reject.rejkurt,eegData.reject.rejkurtE,epochTimeFrames,eegData.reject.rejkurtcol);
% rejSpe=trial2eegplot(eegData.reject.rejfreq,eegData.reject.rejfreqE,epochTimeFrames,eegData.reject.rejfreqcol);
%Put the reject candidates in one array.
rejE=[plotRejThr;plotRejTre;plotRejJp;plotRejKur];%;rejSpe];
%command string for reject marked trials... all marked epochs...
%from http://sccn.ucsd.edu/pipermail/eeglablist/2011/004085.html
cmd = [ ...
'[tmprej tmprejE] = eegplot2trial( TMPREJ,EEG.pnts,EEG.trials);' ...
'[EEGTMP LASTCOM] = pop_rejepoch(EEG, tmprej, 1);' ...
'if ~isempty(LASTCOM),'...
' [ALLEEG EEG CURRENTSET tmpcom] = pop_newset(ALLEEG, EEGTMP, CURRENTSET);' ...
' if ~isempty(tmpcom),' ...
' EEG = eegh(LASTCOM, EEG);' ...
' eegh(tmpcom);' ...
' eeglab(''redraw'');' ...
' end;' ...
'end;' ...
'clear EEGTMP tmpcom;' ...
] ;
%Draw the data.
eegplot(eegData.data,...
'eloc_file',locFile,...
'srate',sampleRate,...
'events',events,...
'winrej',rejE,...
'command',cmd,...
'butlabel','Reject');
end
_______________________________________________
Eeglablist page: http://sccn.ucsd.edu/eeglab/eeglabmail.html
To unsubscribe, send an empty email to eeglablist-unsubscribe at sccn.ucsd.edu<mailto:eeglablist-unsubscribe at sccn.ucsd.edu>
For digest mode, send an email with the subject "set digest mime" to eeglablist-request at sccn.ucsd.edu<mailto:eeglablist-request at sccn.ucsd.edu>
--
Makoto Miyakoshi
Swartz Center for Computational Neuroscience
Institute for Neural Computation, University of California San Diego
_______________________________________________
Eeglablist page: http://sccn.ucsd.edu/eeglab/eeglabmail.html
To unsubscribe, send an empty email to eeglablist-unsubscribe at sccn.ucsd.edu<mailto:eeglablist-unsubscribe at sccn.ucsd.edu>
For digest mode, send an email with the subject "set digest mime" to eeglablist-request at sccn.ucsd.edu<mailto:eeglablist-request at sccn.ucsd.edu>
--
Makoto Miyakoshi
Swartz Center for Computational Neuroscience
Institute for Neural Computation, University of California San Diego
--
Makoto Miyakoshi
Swartz Center for Computational Neuroscience
Institute for Neural Computation, University of California San Diego
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://sccn.ucsd.edu/pipermail/eeglablist/attachments/20140702/1189e84a/attachment.html>
More information about the eeglablist
mailing list