0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
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
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
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
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
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
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