| function [EEG,bad_chans,bad_epochs,bad_ICAs]=APPLE_ActiveCap_v2(EEG,eeg_chans,ref_chan,Do_ICA,subno,VEOG,session,TASK)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| tic
|
|
|
|
|
| dims=size(EEG.data);
|
|
|
| hasVEOG = 0;
|
| SubjID = subno;
|
|
|
|
|
| for ai=1:dims(1), Z(ai)=EEG.chanlocs(ai).Z; end
|
| Vertex=find(Z==max(Z)); clear Z;
|
|
|
| TEMPPRE = pop_reref( EEG, []);
|
| PreFixERP=eegfilt(squeeze(mean(TEMPPRE.data(Vertex,:,:),3)),TEMPPRE.srate,[],20);
|
| PreFixERP=PreFixERP-repmat(mean(PreFixERP),1,length(PreFixERP));
|
|
|
| T1=find( abs(TEMPPRE.times-300) == min(abs(TEMPPRE.times-300)) ) ;
|
| T2=find( abs(TEMPPRE.times-400) == min(abs(TEMPPRE.times-400)) ) ;
|
| PreFixTopo=squeeze(mean(mean(TEMPPRE.data(:,T1:T2,:),2),3));
|
| clear TEMPPRE;
|
|
|
|
|
|
|
|
|
| tempeeg=EEG;
|
| [EEG, indelec, measure] = pop_rejchan( EEG, 'elec', eeg_chans);
|
| clear EEG; EEG=tempeeg; clear tempeeg;
|
|
|
|
|
| chan = channel_properties(EEG, eeg_chans, ref_chan);
|
| chan_exceeded_threshold = min_z_JFC(chan);
|
| FASTER_bad_chans = find(logical(chan_exceeded_threshold(:,2)+chan_exceeded_threshold(:,3)));
|
|
|
|
|
| TOTAL_bad_chans=unique([FASTER_bad_chans(:);indelec(:)]);
|
|
|
|
|
| if ~isempty(TOTAL_bad_chans)
|
| EEG.data=double(EEG.data);
|
| EEG = pop_interp(EEG,TOTAL_bad_chans,'spherical');
|
| end
|
|
|
| bad_chans{1}=FASTER_bad_chans;
|
| bad_chans{2}=indelec;
|
| bad_chans{3}=TOTAL_bad_chans;
|
|
|
|
|
| EEG = pop_reref( EEG, []);
|
|
|
|
|
|
|
|
|
| tempeeg=EEG;
|
| [EEG, rmepochs] = pop_autorej(EEG,'nogui','on');
|
|
|
| clear EEG; EEG=tempeeg; clear tempeeg;
|
| autorej_bad_epochs=zeros(EEG.trials,1);
|
| autorej_bad_epochs(sort(rmepochs))=1;
|
|
|
|
|
| epoch = epoch_properties(EEG,eeg_chans);
|
| epoch_exceeded_threshold = min_z_JFC(epoch);
|
| FASTER_bad_epochs = logical(epoch_exceeded_threshold(:,1)+epoch_exceeded_threshold(:,2)+epoch_exceeded_threshold(:,3));
|
|
|
|
|
| TOTAL_bad_epochs=logical(FASTER_bad_epochs+autorej_bad_epochs);
|
|
|
|
|
| binarized=zeros(1,EEG.trials);
|
| binarized(FASTER_bad_epochs)=1;
|
| EEG = pop_rejepoch(EEG,binarized,0);
|
| goodepochs=logical(1-binarized);
|
|
|
| EP2REJ=1;
|
| bad_epochs{1}=FASTER_bad_epochs;
|
| bad_epochs{2}=autorej_bad_epochs;
|
| bad_epochs{3}=TOTAL_bad_epochs;
|
|
|
|
|
|
|
| if Do_ICA==1
|
|
|
|
|
| k=25;
|
| C=dims(1)-length(TOTAL_bad_chans);
|
| sizeneeded=C^2*k;
|
| epochsneeded=round(sizeneeded/EEG.srate);
|
|
|
|
|
| EEG = pop_runica(EEG,'icatype','runica');
|
|
|
|
|
| EEG.icaact = eeg_getica(EEG);
|
|
|
|
|
|
|
| bad_ADJUST_ICAs=99999;
|
|
|
|
|
| if hasVEOG==1
|
| for ai=1:size(EEG.icaact,1)
|
| temp=squeeze(EEG.icaact(ai,:,:));
|
| r=corrcoef(temp,VEOG(:,goodepochs));
|
| VEOG_ICA_Corrs(ai)=abs(r(1,2)); clear temp;
|
| end
|
| bad_VEOG_ICAs=find(abs(zscore(VEOG_ICA_Corrs))>3);
|
| if isempty(bad_VEOG_ICAs), bad_VEOG_ICAs=find(VEOG_ICA_Corrs==max(abs(VEOG_ICA_Corrs))); end
|
| else
|
| bad_VEOG_ICAs=0;
|
| end
|
|
|
|
|
|
|
| for ai=1:dims(1), X(ai)=EEG.chanlocs(ai).X; end
|
| FrontoPolars=find(X==max(X)); clear X;
|
|
|
| for fpi=1:length(FrontoPolars)
|
| e2use=FrontoPolars(fpi);
|
| eucdist=zeros(1,size(EEG.icawinv,1)); topocorr=zeros(1,size(EEG.icawinv,1));
|
| for chani=1:size(EEG.icawinv,1)
|
| eucdist(chani)=sqrt( (EEG.chanlocs(chani).X-EEG.chanlocs(e2use).X)^2 + (EEG.chanlocs(chani).Y-EEG.chanlocs(e2use).Y)^2 + (EEG.chanlocs(chani).Z-EEG.chanlocs(e2use).Z)^2 );
|
| end
|
| s=30; template(fpi,:) = exp(- (eucdist.^2)/(2*s^2) );
|
| end
|
| template=mean(template,1);
|
|
|
| for chani=1:size(EEG.icawinv,2)
|
| topocorr(chani) = corr(EEG.icawinv(:,chani),template');
|
| end
|
| % Select the max correlations
|
| bad_TEMPLATE_ICAs=find(abs(zscore(topocorr))>3);
|
| if isempty(bad_TEMPLATE_ICAs), bad_TEMPLATE_ICAs=find(abs(topocorr)==max(abs(topocorr))); end % in case z-scores are too tightly distributed
|
|
|
| % Aggregate all this
|
| bad_ICAs{1}=bad_ADJUST_ICAs;
|
| bad_ICAs{2}=bad_VEOG_ICAs;
|
| bad_ICAs{3}=bad_TEMPLATE_ICAs;
|
| bad_ICAs{4}=[sum(goodepochs),epochsneeded];
|
|
|
| end
|
|
|
| %% Show Stats
|
|
|
| elapsed=toc;
|
| pBAD_CHANS=(length(bad_chans{3})./dims(1))*100;
|
| pBAD_EPOCHS=(sum(bad_epochs{3})./dims(3))*100;
|
|
|
| % Show ERP and Topo after rejecting blink ICA, but don't actually remove that from the real EEG data
|
| tempeeg=EEG;
|
| EEG = pop_subcomp( EEG, bad_TEMPLATE_ICAs, 0);
|
| PostFixERP=eegfilt(squeeze(mean(EEG.data(Vertex,:,:),3)),EEG.srate,[],20);
|
| PostFixERP=PostFixERP-repmat(mean(PostFixERP),1,length(PostFixERP));
|
| PostFixTopo=squeeze(mean(mean(EEG.data(:,T1:T2,:),2),3));
|
| clear EEG; EEG=tempeeg; clear tempeeg;
|
|
|
| figure;
|
| subplot(2,3,1)
|
| pie([dims(1)-length(bad_chans{3}),length(bad_chans{3})],[0 1],{['Good=',num2str(dims(1)-length(bad_chans{3}))],['Bad=',num2str(length(bad_chans{3}))]})
|
| title(['Subj: ',num2str(SubjID), ' Bad Chans']);
|
| subplot(2,3,2)
|
| pie([dims(3)-sum(bad_epochs{EP2REJ}),sum(bad_epochs{EP2REJ})],[0 1],{['Good=',num2str(dims(3)-sum(bad_epochs{EP2REJ}))],['Bad=',num2str(sum(bad_epochs{EP2REJ}))]})
|
| title(['Subj: ',num2str(SubjID), ' Bad Epochs']);
|
| subplot(2,3,3)
|
| if Do_ICA==1
|
| text(.2, .90, ['Bad ADJUST ICAs: ',num2str(bad_ICAs{1})]);
|
| text(.2, .75, ['Bad VEOGcorr ICAs: ',num2str(bad_ICAs{2})]);
|
| text(.2, .60, ['Bad TEMPLATE ICAs: ',num2str(bad_ICAs{3})]);
|
| text(.2, .45, ['Epochs Needed for ICA: ',num2str(bad_ICAs{4}(2))]);
|
| text(.2, .30, ['Epochs in Dataset (good): ',num2str(bad_ICAs{4}(1))]);
|
| text(.2, .15, ['Mins Elapsed: ',num2str(elapsed/60)]);
|
| else
|
| text(.2, .50, bad_ICAs);
|
| text(.2, .05, ['Mins Elapsed: ',num2str(elapsed/60)]);
|
| end
|
| set(gca,'visible','off');
|
|
|
| subplot(2,3,4)
|
| hold on
|
| topoplot(PreFixTopo,EEG.chanlocs);
|
| title('Topo Before Fixes (300-400 ms)');
|
| subplot(2,3,5)
|
| hold on
|
| topoplot(PostFixTopo,EEG.chanlocs);
|
| title('Topo After Fixes (300-400 ms)');
|
| subplot(2,3,6)
|
| hold on
|
| plot(EEG.times,PreFixERP,'r');
|
| plot(EEG.times,PostFixERP,'b--');
|
| legend({'Pre-Fixes','Post-Fixes'},'Location','SouthOutside');
|
| title('ERP at Vertex (20 Hz Filter)');
|
|
|
|
|
| saveas(gcf, [TASK,num2str(SubjID),'_APPLE.png'],'png');
|
| close all;
|
|
|
|
|
| pop_selectcomps(EEG, [1:30] );
|
| saveas(gcf, [TASK,num2str(SubjID),'_APPLE_ICAs.png'],'png');
|
|
|
|
|
| close all;
|
|
|
|
|
| function [lengths] = min_z_JFC(list_properties,rejection_options)
|
| if (~exist('rejection_options','var'))
|
| rejection_options.measure=ones(1,size(list_properties,2));
|
| rejection_options.z=3*ones(1,size(list_properties,2));
|
| end
|
|
|
| rejection_options.measure=logical(rejection_options.measure);
|
| zs=list_properties-repmat(mean(list_properties,1),size(list_properties,1),1);
|
| zs=zs./repmat(std(zs,[],1),size(list_properties,1),1);
|
| zs(isnan(zs))=0;
|
|
|
|
|
|
|
| lengths = abs(zs) > repmat(rejection_options.z,size(list_properties,1),1);
|
|
|
|
|
|
|