diff --git a/text/TODO.txt b/text/TODO.txt index e9cd377..a867298 100644 --- a/text/TODO.txt +++ b/text/TODO.txt @@ -24,7 +24,7 @@ correlation zwischen Frequenzband und Koordinaten (r^2) (done) -topgraphisch plotten? (mail, elog-files, suchen) +topgraphisch plotten? (mail, elog-files, suchen) (done) low-frequency features (done) @@ -32,7 +32,7 @@ Titel: Movement Prediction from EEG based on Synergies Evaluation and Comparison to other methods -Prüfer: Rosenstiehl, Benda? (angefragt) +Prüfer: Rosenstiehl, Benda? (Zusage) topoplot_Wrapper (done) diff --git a/usedMcode/callAll.m b/usedMcode/callAll.m index aa54e84..dd68567 100644 --- a/usedMcode/callAll.m +++ b/usedMcode/callAll.m @@ -1,21 +1,22 @@ -addpath('/home/hohlochj/masterarbeit/usedMcode') +addpath('/home/hohlochj/masterarbeitNew/usedMcode') pathToFile='/nfs/wsi/ti/messor/hohlochj/origData/'; %pathToFile='/home/jph/Uni/masterarbeit/origData/'; maxFile=5; threshold=10000; %EMG is classified as movement EMGChannels={'AbdPolLo','Biceps','Triceps','FrontDelt','MidDelt','BackDelt'}; +noSynergies=4; -allSubjects=true; %run all subjects and days or only one random day for one random subject +allSubjects=false; %run all subjects and days or only one random day for one random subject windowEMG=0.2; windowEEG=1; -shift=0.2; +shift=0.05; pburgOrder=50; minEEGFreq=0; maxEEGFreq=200; pause=false; noLFsamples=5; -ridgeParams=[0.1]; +ridgeParams=[10]; k=10; maxExpC=0; maxPerClass=250; @@ -47,23 +48,50 @@ cmScaledEMG=zeros([j,5,5]); cmScaledEEG=zeros([j,5,5]); cmScaledLF=zeros([j,5,5]); - maxRidgeParamIndexEEG=zeros([j,3,k]); - maxRidgeParamIndexEMG=zeros([j,3,k]); - maxRidgeParamIndexLF=zeros([j,3,k]); - correlationEMG=zeros([j,3]); %x,y,angle - correlationEEG=zeros([j,3]); - correlationLF=zeros([j,3]); + maxRidgeParamIndexEEGkin=zeros([j,3,k]); + maxRidgeParamIndexEMGkin=zeros([j,3,k]); + maxRidgeParamIndexLFkin=zeros([j,3,k]); + correlationEMGkin=zeros([j,3]); %x,y,angle + correlationEEGkin=zeros([j,3]); + correlationLFkin=zeros([j,3]); + maxRidgeParamIndexEEGautoenc=zeros([j,noSynergies,k]); + maxRidgeParamIndexEMGautoenc=zeros([j,noSynergies,k]); + maxRidgeParamIndexLFautoenc=zeros([j,noSynergies,k]); + correlationEMGautoenc=zeros([j,noSynergies]); + correlationEEGautoenc=zeros([j,noSynergies]); + correlationLFautoenc=zeros([j,noSynergies]); + maxRidgeParamIndexEEGpca=zeros([j,noSynergies,k]); + maxRidgeParamIndexEMGpca=zeros([j,noSynergies,k]); + maxRidgeParamIndexLFpca=zeros([j,noSynergies,k]); + correlationEMGpca=zeros([j,noSynergies]); + correlationEEGpca=zeros([j,noSynergies]); + correlationLFpca=zeros([j,noSynergies]); + maxRidgeParamIndexEEGnnmf=zeros([j,noSynergies,k]); + maxRidgeParamIndexEMGnnmf=zeros([j,noSynergies,k]); + maxRidgeParamIndexLFnnmf=zeros([j,noSynergies,k]); + correlationEMGnnmf=zeros([j,noSynergies]); + correlationEEGnnmf=zeros([j,noSynergies]); + correlationLFnnmf=zeros([j,noSynergies]); parfor j=1:size(numbersMat,2) number=numbersMat(j); subject=subjectsForNumbers{j}; savePath=readAll(pathToFile,subject,number,windowEMG,windowEEG,shift,maxFile,threshold,pburgOrder,minEEGFreq,maxEEGFreq,pause,noLFsamples); - [meanAccurancysEMG(j),maxCEMG(j,:),cmScaledEMG(j,:,:)]=svmEciton(savePath,'EMG',k,maxExpC,maxPerClass); - [meanAccurancysEEG(j),maxCEEG(j,:),cmScaledEEG(j,:,:)]=svmEciton(savePath,'EEG',k,maxExpC,maxPerClass); - [meanAccurancysLF(j),maxCLF(j,:),cmScaledLF(j,:,:)]=svmEciton(savePath,'LF',k,maxExpC,maxPerClass); - [correlationEMG(j,:),maxRidgeParamIndexEMG(j,:,:)]=ridgeCV(savePath,'EMG',k,ridgeParams); - [correlationEEG(j,:),maxRidgeParamIndexEEG(j,:,:)]=ridgeCV(savePath,'EEG',k,ridgeParams); - [correlationLF(j,:),maxRidgeParamIndexLF(j,:,:)]=ridgeCV(savePath,'LF',k,ridgeParams); +% [meanAccurancysEMG(j),maxCEMG(j,:),cmScaledEMG(j,:,:)]=svmEciton(savePath,'EMG',k,maxExpC,maxPerClass); +% [meanAccurancysEEG(j),maxCEEG(j,:),cmScaledEEG(j,:,:)]=svmEciton(savePath,'EEG',k,maxExpC,maxPerClass); +% [meanAccurancysLF(j),maxCLF(j,:),cmScaledLF(j,:,:)]=svmEciton(savePath,'LF',k,maxExpC,maxPerClass); + [correlationEMGkin(j,:),maxRidgeParamIndexEMGkin(j,:,:)]=ridgeCV(savePath,'EMG','kin',k,ridgeParams); + [correlationEEGkin(j,:),maxRidgeParamIndexEEGkin(j,:,:)]=ridgeCV(savePath,'EEG','kin',k,ridgeParams); + [correlationLFkin(j,:),maxRidgeParamIndexLFkin(j,:,:)]=ridgeCV(savePath,'LF','kin',k,ridgeParams); + [correlationEMGautoenc(j,:),maxRidgeParamIndexEMGautoenc(j,:,:)]=ridgeCV(savePath,'EMG','Autoenc',k,ridgeParams); + [correlationEEGautoenc(j,:),maxRidgeParamIndexEEGautoenc(j,:,:)]=ridgeCV(savePath,'EEG','Autoenc',k,ridgeParams); + [correlationLFautoenc(j,:),maxRidgeParamIndexLFautoenc(j,:,:)]=ridgeCV(savePath,'LF','Autoenc',k,ridgeParams); + [correlationEMGpca(j,:),maxRidgeParamIndexEMGpca(j,:,:)]=ridgeCV(savePath,'EMG','PCA',k,ridgeParams); + [correlationEEGpca(j,:),maxRidgeParamIndexEEGpca(j,:,:)]=ridgeCV(savePath,'EEG','PCA',k,ridgeParams); + [correlationLFpca(j,:),maxRidgeParamIndexLFpca(j,:,:)]=ridgeCV(savePath,'LF','PCA',k,ridgeParams); + [correlationEMGnnmf(j,:),maxRidgeParamIndexEMGnnmf(j,:,:)]=ridgeCV(savePath,'EMG','NNMF',k,ridgeParams); + [correlationEEGnnmf(j,:),maxRidgeParamIndexEEGnnmf(j,:,:)]=ridgeCV(savePath,'EEG','NNMF',k,ridgeParams); + [correlationLFnnmf(j,:),maxRidgeParamIndexLFnnmf(j,:,:)]=ridgeCV(savePath,'LF','NNMF',k,ridgeParams); fprintf('%s%i finished %s\n',subject,number,datestr(datetime('now'))) end @@ -73,13 +101,22 @@ number=numbersMat(j); subject=subjectsForNumbers{j}; - savePath=readEEG(pathToFile,subject,number,windowEMG,windowEEG,shift,maxFile,threshold,pburgOrder,minEEGFreq,maxEEGFreq,pause,noLFsamples,EMGChannels); + savePath=readAll(pathToFile,subject,number,windowEMG,windowEEG,shift,maxFile,threshold,pburgOrder,minEEGFreq,maxEEGFreq,pause,noLFsamples,EMGChannels,noSynergiesergies); [meanAccurancysEMG,maxCEMG,cmScaledEMG(:,:)]=svmEciton(savePath,'EMG',k,maxExpC,maxPerClass); [meanAccurancysEEG,maxCEEG,cmScaledEEG(:,:)]=svmEciton(savePath,'EEG',k,maxExpC,maxPerClass); [meanAccurancysLF,maxCLF,cmScaledLF(:,:)]=svmEciton(savePath,'LF',k,maxExpC,maxPerClass); - [correlationEMG,maxRidgeParamIndexEMG(:,:)]=ridgeCV(savePath,'EMG',k,ridgeParams); - [correlationEEG,maxRidgeParamIndexEEG(:,:)]=ridgeCV(savePath,'EEG',k,ridgeParams); - [correlationLF,maxRidgeParamIndexLF(:,:)]=ridgeCV(savePath,'LF',k,ridgeParams); + [correlationEMGkin,maxRidgeParamIndexEMGkin(:,:)]=ridgeCV(savePath,'EMG','kin',k,ridgeParams); + [correlationEEGkin,maxRidgeParamIndexEEGkin(:,:)]=ridgeCV(savePath,'EEG','kin',k,ridgeParams); + [correlationLFkin,maxRidgeParamIndexLFkin(:,:)]=ridgeCV(savePath,'LF','kin',k,ridgeParams); + [correlationEMGautoenc,maxRidgeParamIndexEMGautoenc(:,:)]=ridgeCV(savePath,'EMG','Autoenc',k,ridgeParams); + [correlationEEGautoenc,maxRidgeParamIndexEEGautoenc(:,:)]=ridgeCV(savePath,'EEG','Autoenc',k,ridgeParams); + [correlationLFautoenc,maxRidgeParamIndexLFautoenc(:,:)]=ridgeCV(savePath,'LF','Autoenc',k,ridgeParams); + [correlationEMGpca,maxRidgeParamIndexEMGpca(:,:)]=ridgeCV(savePath,'EMG','PCA',k,ridgeParams); + [correlationEEGpca,maxRidgeParamIndexEEGpca(:,:)]=ridgeCV(savePath,'EEG','PCA',k,ridgeParams); + [correlationLFpca,maxRidgeParamIndexLFpca(:,:)]=ridgeCV(savePath,'LF','PCA',k,ridgeParams); + [correlationEMGnnmf,maxRidgeParamIndexEMGnnmf(:,:)]=ridgeCV(savePath,'EMG','NNMF',k,ridgeParams); + [correlationEEGnnmf,maxRidgeParamIndexEEGnnmf(:,:)]=ridgeCV(savePath,'EEG','NNMF',k,ridgeParams); + [correlationLFnnmf,maxRidgeParamIndexLFnnmf(:,:)]=ridgeCV(savePath,'LF','NNMF',k,ridgeParams); fprintf('%s%i finished %s\n',subject,number,datestr(datetime('now'))) save(strcat(pathToFile,sprintf('../matlabData/%s_call%s%i.mat',datestr(datetime('now')),subject,number))); diff --git a/usedMcode/noOfSynergies.m b/usedMcode/noOfSynergies.m index 16cd0fa..933f4b5 100644 --- a/usedMcode/noOfSynergies.m +++ b/usedMcode/noOfSynergies.m @@ -41,9 +41,10 @@ r2Autoenc=zeros([maxSize,size(trainingData,2)]); +ae=cell([maxSize,1]); parfor i=1:maxSize - ae=trainAutoencoder(trainingData',i,'ShowProgressWindow',false); - predicted=predict(ae,trainingData'); + ae{i}=trainAutoencoder(trainingData',i,'ShowProgressWindow',false); + predicted=predict(ae{i},trainingData'); r2Autoenc(i,:)=correlation2(trainingData,predicted'); end diff --git a/usedMcode/readAll.m b/usedMcode/readAll.m index c382509..6cc4fe8 100644 --- a/usedMcode/readAll.m +++ b/usedMcode/readAll.m @@ -1,7 +1,10 @@ -function [savePath]=readAll(pathToFile, subject,number,windowEMG,windowEEG,shift,maxFile,threshold,pburgOrder,minEEGFreq,maxEEGFreq,pause,noLFsamples,EMGChannels) +function [savePath]=readAll(pathToFile, subject,number,windowEMG,windowEEG,shift,maxFile,... + threshold,pburgOrder,minEEGFreq,maxEEGFreq,pause,noLFsamples,EMGChannels,noSynergies) %fprintf('start read %s%i %s\n',subject,number,datestr(datetime('now'))); - savePath=strcat(pathToFile,sprintf('../matlabData/%s%i%imsWindowEMG%isWindowEEG%imsShiftFreq%ito%iPause%ipBurg%i.mat',subject,number,windowEMG*1000,windowEEG,shift*1000,minEEGFreq,maxEEGFreq,pause,pburgOrder)); + savePath=strcat(pathToFile,... + sprintf('../matlabData/%s%i%imsWindowEMG%isWindowEEG%imsShiftFreq%ito%iPause%ipBurg%i.mat',... + subject,number,windowEMG*1000,windowEEG,shift*1000,minEEGFreq,maxEEGFreq,pause,pburgOrder)); %only create file if it doesn't exist yet if exist(savePath, 'file') ~= 2 @@ -13,9 +16,15 @@ kin=cell([maxFile,1]); for i=1:maxFile - [sig, stat, params] = load_bcidat(strcat(pathToFile,sprintf('%s/%s_B100%i/%s_B1S00%iR0%i.dat',subject,subject,number,subject,number,i))); - tmpKin=csvread(strcat(pathToFile,sprintf('kin/%s_B1S00%iR0%i.txt',subject,number,i)),1,0); - [trainingDataEEGcell{i},trainingDataEEGlfCell{i},trainingDataEMGcell{i},kin{i}]=generateTrainingData(sig,tmpKin(:,1:4),windowEMG,windowEEG,shift,params,pburgOrder,minEEGFreq,maxEEGFreq,noLFsamples,EMGChannels); + [sig, stat, params] = load_bcidat(strcat(pathToFile,... + sprintf('%s/%s_B100%i/%s_B1S00%iR0%i.dat',subject,subject,... + number,subject,number,i))); + tmpKin=csvread(strcat(pathToFile,... + sprintf('kin/%s_B1S00%iR0%i.txt',subject,number,i)),1,0); + [trainingDataEEGcell{i},trainingDataEEGlfCell{i},... + trainingDataEMGcell{i},kin{i}]=generateTrainingData(sig,... + tmpKin(:,1:4),windowEMG,windowEEG,shift,params,pburgOrder,... + minEEGFreq,maxEEGFreq,noLFsamples,EMGChannels); classesCell{i}=stat.StimulusCode; %fprintf('%ith file processed\n',i) end @@ -30,12 +39,15 @@ kinMat=cell2mat(kin); clear trainingDataEEGcell trainingDataEMGcell classesCell kin - classificationWithPause=classifyAccordingToEMG(size(trainingDataEEG,1), trainingDataEMG,classesMat,shift,bci_sf,threshold,pause); + classificationWithPause=classifyAccordingToEMG(size(... + trainingDataEEG,1), trainingDataEMG,classesMat,shift,... + bci_sf,threshold,pause); clear classesMat smoothClassification=zeros(size(classificationWithPause)); for i=1:size(classificationWithPause,1) - smoothClassification(i)=round(mode(classificationWithPause(max(i-fix(bci_sf/1000),1):min(i+fix(bci_sf/1000),end)))); + smoothClassification(i)=round(mode(classificationWithPause(... + max(i-fix(bci_sf/1000),1):min(i+fix(bci_sf/1000),end)))); end clear classificationWithPause @@ -45,9 +57,17 @@ trainingDataEMG=trainingDataEMG(smoothClassification~=-1,:); classification=smoothClassification(smoothClassification~=-1); kinematics=kinMat(smoothClassification~=-1,:); + ae=trainAutoencoder(trainingDataEMG',noSynergies,'ShowProgressWindow',false); + synergiesAutoenc=encode(ae,trainingDataEMG')'; + COEFF=pca(trainingDataEMG,'Centered',false); + synergiesPCA=trainingDataEMG/COEFF(:,1:4)'; + [~,H]=nnmf(trainingDataEMG,noSynergies); + synergiesNNMF=trainingDataEMG*H'; clear smoothClassification i - save(savePath,'trainingDataEEG','trainingDataEEGlf','trainingDataEMG','classification','kinematics','-v7.3'); + save(savePath,'trainingDataEEG','trainingDataEEGlf','trainingDataEMG',... + 'classification','kinematics','synergiesAutoenc','synergiesPCA',... + 'synergiesNNMF','-v7.3'); %fprintf('finished reading %s%i %s\n',subject,number,datestr(datetime('now'))); diff --git a/usedMcode/ridgeCV.m b/usedMcode/ridgeCV.m index d1fccca..d9af8f9 100644 --- a/usedMcode/ridgeCV.m +++ b/usedMcode/ridgeCV.m @@ -1,26 +1,40 @@ -function [correlation,maxRidgeParamIndex]=ridgeCV(savePath,EEG,k,ridgeParams) +function [correlation,maxRidgeParamIndex]=ridgeCV(savePath,data,goal,k,ridgeParams) load(savePath); clear classification; - if strcmp(EEG,'EEG') + if strcmp(data,'EEG') trainingData=trainingDataEEG; - elseif strcmp(EEG,'EMG') + elseif strcmp(data,'EMG') trainingData=trainingDataEMG; - elseif strcmp(EEG,'LF') + elseif strcmp(data,'LF') trainingData=trainingDataEEGlf; else - error('only EEG,EMG and LF are valid inputs'); + error('only EEG, EMG and LF are valid inputs for data'); end clear trainingDataEEG; + clear trainingDataEEGlf; clear trainingDataEMG; - correlation=zeros(size(kinematics,2),1); - maxRidgeParamIndex=zeros(size(kinematics,2),k); + if strcmp(goal,'Autoenc') + predicted=synergiesAutoenc; + elseif strcmp(goal,'PCA') + predicted=synergiesPCA; + elseif strcmp(goal,'NNMF') + predicted=synergiesNNMF; + elseif strcmp(goal,'kin') + predicted=kinematics; + else + error('only kin, Autoenc, PCA and NNMF are valid inputs for goal'); + end - for j=1:size(kinematics,2) + + correlation=zeros(size(predicted,2),1); + maxRidgeParamIndex=zeros(size(predicted,2),k); + + for j=1:size(predicted,2) randMap=randperm(size(trainingData,1)); - kin=kinematics(:,j); + kin=predicted(:,j); correlations=zeros([k,1]); for i=1:k diff --git a/usedMcode/svmEciton.m b/usedMcode/svmEciton.m index 857d546..a4b40d0 100644 --- a/usedMcode/svmEciton.m +++ b/usedMcode/svmEciton.m @@ -11,7 +11,7 @@ elseif strcmp(EEG,'LF') trainingData=trainingDataEEGlf; else - error('only EEG,EMG and LF are valid inputs'); + error('only EEG, EMG and LF are valid inputs'); end clear trainingDataEEG;