Home > matlabmk > remove_artifact_ics.m

remove_artifact_ics

PURPOSE ^

remove_artifact_ics() - Removes the independent components (ICs) of an

SYNOPSIS ^

function art_ics=remove_artifact_ics(bsln_wind,verblevel)

DESCRIPTION ^

 remove_artifact_ics() - Removes the independent components (ICs) of an
                         EEGLAB "EEG" variable that have been labeled as an
                         artifact in EEG.ic_labels or EEG.reject.gcompreject.
                         The artifacts are removed from EEG.data and
                         EEG.data is optionally baselined.  Artifact
                         status of labels in EEG.ic_labels is based on
                         is_art.m.  Note that EEG.ic_labels is a field
                         unique to Kutaslab data.


 Usage:
  >> art_ics=remove_artifact_ics(bsln_wind,verblevel);

 Required Global Variable:
  EEG - EEGLAB EEG variable.  EEG.data field will be modified by this
        function.

 Optional Inputs:
  bsln_wind - [start_time stop_time or NaN] Two element vector specifying 
              baseline window in milliseconds or NaN.  If NaN, data are 
              not baselined. Otherwise, the mean EEG at each channel and
              epoch in this window will be removed from each epoch.
  verblevel - an integer specifiying the amount of information you want
              functions to provide about what they are doing during runtime.
                Options are:
                  0 - quiet, only show errors, warnings, and EEGLAB reports
                  1 - stuff anyone should probably know
                  2 - stuff you should know the first time you start working
                      with a data set {default value if not already globally
                      specified}
                  3 - stuff that might help you debug (show all
                      reports)

 Outputs:
  art_ics - [integer vector] Indices of ICs labeled as artifacts


 Author:
 David Groppe
 Kutaslab, 9/2010

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 % remove_artifact_ics() - Removes the independent components (ICs) of an
0002 %                         EEGLAB "EEG" variable that have been labeled as an
0003 %                         artifact in EEG.ic_labels or EEG.reject.gcompreject.
0004 %                         The artifacts are removed from EEG.data and
0005 %                         EEG.data is optionally baselined.  Artifact
0006 %                         status of labels in EEG.ic_labels is based on
0007 %                         is_art.m.  Note that EEG.ic_labels is a field
0008 %                         unique to Kutaslab data.
0009 %
0010 %
0011 % Usage:
0012 %  >> art_ics=remove_artifact_ics(bsln_wind,verblevel);
0013 %
0014 % Required Global Variable:
0015 %  EEG - EEGLAB EEG variable.  EEG.data field will be modified by this
0016 %        function.
0017 %
0018 % Optional Inputs:
0019 %  bsln_wind - [start_time stop_time or NaN] Two element vector specifying
0020 %              baseline window in milliseconds or NaN.  If NaN, data are
0021 %              not baselined. Otherwise, the mean EEG at each channel and
0022 %              epoch in this window will be removed from each epoch.
0023 %  verblevel - an integer specifiying the amount of information you want
0024 %              functions to provide about what they are doing during runtime.
0025 %                Options are:
0026 %                  0 - quiet, only show errors, warnings, and EEGLAB reports
0027 %                  1 - stuff anyone should probably know
0028 %                  2 - stuff you should know the first time you start working
0029 %                      with a data set {default value if not already globally
0030 %                      specified}
0031 %                  3 - stuff that might help you debug (show all
0032 %                      reports)
0033 %
0034 % Outputs:
0035 %  art_ics - [integer vector] Indices of ICs labeled as artifacts
0036 %
0037 %
0038 % Author:
0039 % David Groppe
0040 % Kutaslab, 9/2010
0041 
0042 % Changes
0043 % 8/23/2012 - NaN now a possible value for bsln_wind to avoid any
0044 % baselining
0045 
0046 function art_ics=remove_artifact_ics(bsln_wind,verblevel)
0047 
0048 global EEG;
0049 global VERBLEVEL;
0050 
0051 if isempty(EEG.icawinv),
0052     %ICA hasn't been applied to these data
0053     art_ics=[];
0054     return
0055 else
0056     
0057     if nargin<1
0058         bsln_wind=[];
0059     end
0060     
0061     if nargin<2,
0062         if ~isnan(bsln_wind)
0063             fprintf('NOT baselining data.\n')
0064         elseif (length(bsln_wind)~=2) || ~isnumeric(bsln_wind),
0065             if ~isempty(bsln_wind)
0066                 error('Baseline window needs to be a two element vector composed of start and stop times (in ms).');
0067             end
0068         end
0069         
0070         if isempty(VERBLEVEL)
0071             VERBLEVEL=2;
0072         end
0073     else
0074         VERBLEVEL=verblevel;
0075     end
0076     
0077     if nargin>2,
0078         error('remove_artifact_ics.m accepts only two arguments.');
0079     end
0080     
0081     
0082     [n_chans, n_pts, n_epochs]=size(EEG.data);
0083     
0084     if ~isempty(bsln_wind),
0085         bsln_pts(1)=find_tpt(bsln_wind(1),EEG.times);
0086         bsln_pts(2)=find_tpt(bsln_wind(2),EEG.times);
0087     end
0088     
0089     fldnames=fieldnames(EEG);
0090     iclabels_used=0;
0091     for fn=1:length(fldnames),
0092         if strcmpi(fldnames{fn},'iclabels')
0093             iclabels_used=1;
0094         end
0095     end
0096     
0097     n_ics=size(EEG.icawinv,2);
0098     if iclabels_used,
0099         n=length(EEG.iclabels);
0100     else
0101         n=0;
0102     end
0103     art_ics=[];
0104     if VERBLEVEL>=2,
0105         fprintf('Removing artifact ICs from EEG set file: %s\n',EEG.setname);
0106         if n
0107             %n is only non-zero if there are IC labels
0108             disp('Artifact ICs:');
0109         end
0110     end
0111     for dg=1:n,
0112         if is_art(EEG.iclabels{dg}),
0113             art_ics=[art_ics dg];
0114             if VERBLEVEL>=2,
0115                 fprintf('IC %d: %s\n',dg,EEG.iclabels{dg});
0116             end
0117         end
0118     end
0119     
0120     %Check EEG.iclabels artifact ICs against EEG.reject
0121     eeglab_rej=find(EEG.reject.gcompreject==1);
0122     if iclabels_used,
0123         dif1=setdiff(eeglab_rej,art_ics);
0124         dif2=setdiff(art_ics,eeglab_rej);
0125         if ~isempty(dif1) || ~isempty(dif2)
0126             if isempty(eeglab_rej),
0127                 rej_ic_str='None';
0128             else
0129                 rej_ic_str=int2str(eeglab_rej);
0130             end
0131             msg=['The ICs marked for rejection by EEGLAB in EEG.reject.gcompreject differ from those labeled as artifacts in EEG.iclabels.' ...);
0132                 10 'Only the artifact labels in EEG.iclabels will be used.' 10 ...
0133                 'The ICs labeled by artifacts by EEG.reject.gcompreject are: ' rej_ic_str];
0134             watchit(msg);
0135         end
0136     else
0137         art_ics=eeglab_rej;
0138     end
0139     
0140     if VERBLEVEL>=2,
0141         fprintf('%d total artifact ICs will be removed.\n',length(art_ics));
0142     end
0143     
0144     nonart_ics=setdiff(1:n_ics,art_ics);
0145     if ~isempty(nonart_ics),
0146         %zero IC artifact ICS
0147         unmix=EEG.icaweights*EEG.icasphere;
0148         fltr=EEG.icawinv(:,nonart_ics)*unmix(nonart_ics,:);
0149         EEG.data=fltr*reshape(EEG.data,n_chans,n_pts*n_epochs);
0150         
0151         if isempty(bsln_wind) || sum(isnan(bsln_wind)),
0152             EEG.data=reshape(EEG.data,n_chans,n_pts,n_epochs);
0153         else
0154             %baseline data
0155             if VERBLEVEL>=2,
0156                 fprintf('Baselining data by removing mean EEG between %d and %d ms (time points %d and %d).\n', ...
0157                     bsln_wind(1),bsln_wind(2),bsln_pts(1),bsln_pts(2));
0158             end
0159             
0160             EEG.data=rmbase(EEG.data,n_pts,bsln_pts(1):bsln_pts(2));
0161             EEG.data=reshape(EEG.data,n_chans,n_pts,n_epochs);
0162         end
0163     else
0164         error('All ICs have been labeled as artifacts.');
0165     end
0166     
0167 end

Generated on Tue 10-May-2016 16:37:45 by m2html © 2005