addpath('/home/hohlochj/masterarbeit/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=3;

allSubjects=true; %run all subjects and days or only one random day for one random subject
name='EEGtoEMGandAutoencToKinandVia'; %suffix for the output, has to be valid name for file (no space, /, ...)
windowEMG=0.2;
windowEEG=1;
shiftEMG=0.05;
shiftEEG=0.2;
eegOffset=0; %predict actions x*shiftEEG after EEG measurement
pburgOrder=250;
minEEGFreq=2;
maxEEGFreq=49;
pause=false;
noLFsamples=5;
ridgeParams=100;
k=10;
maxExpC=0;
maxPerClass=250;
poolObj=parpool(32);

[subjects,numbers]=namesAndNumbers(pathToFile);
numbersMat=cell2mat(numbers);
subjectsForNumbers=cell(size(numbersMat,2),1);
j=1;
for i=1:size(subjects,2)
    subject=subjects{i};
    for number=numbers{i}
        subjectsForNumbers{j}=subject;
        j=j+1;
    end
end
j=j-1; %number of trial-days for all subjects


if allSubjects
    
%     meanAccurancysEMG=zeros([j,1]);
%     meanAccurancysEEG=zeros([j,1]);
%     meanAccurancysLF=zeros([j,1]);
%     maxCEMG=zeros([j,k,1]);
%     maxCEEG=zeros([j,k,1]);
%     maxCLF=zeros([j,k,1]);
%     cmScaledEMG=zeros([j,5,5]);
%     cmScaledEEG=zeros([j,5,5]);
%     cmScaledLF=zeros([j,5,5]);
%     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]);
    maxRidgeParamIndexEEGemg=zeros([j,noSynergies,k]);
    correlationEMGemg=zeros([j,noSynergies]);
    maxRidgeParamIndexAutoencKin=zeros([j,noSynergies,k]);
    correlationAutoencKin=zeros([j,3]);
    correlationViaAutoenc=zeros([j,3]);
    correlationViaPCA=zeros([j,3]);
    correlationViaNNMF=zeros([j,3]);
    viaCorrelationAutoenc=zeros(j,noSynergies);
    viaCorrelationPCA=zeros(j,noSynergies);
    viaCorrelationNNMF=zeros(j,noSynergies);
    correlationEMGViaAutoenc=zeros([j,size(EMGChannels,2)]);
    
    parfor j=1:size(numbersMat,2)
        number=numbersMat(j);
        subject=subjectsForNumbers{j};
        savePath=readAll(pathToFile,subject,number,windowEMG,windowEEG,shiftEMG,shiftEEG,maxFile,threshold,pburgOrder,minEEGFreq,maxEEGFreq,pause,noLFsamples,EMGChannels,noSynergies);
%         [meanAccurancysEMG(j),maxCEMG(j,:),cmScaledEMG(j,:,:)]=svmEciton(savePath,'EMG',k,maxExpC,maxPerClass,eegOffset);
%         [meanAccurancysEEG(j),maxCEEG(j,:),cmScaledEEG(j,:,:)]=svmEciton(savePath,'EEG',k,maxExpC,maxPerClass,eegOffset);
%         [meanAccurancysLF(j),maxCLF(j,:),cmScaledLF(j,:,:)]=svmEciton(savePath,'LF',k,maxExpC,maxPerClass,eegOffset);
%         [correlationEMGkin(j,:),maxRidgeParamIndexEMGkin(j,:,:)]=ridgeCV(savePath,'EMG','kin',k,ridgeParams,eegOffset);
%         [correlationEEGkin(j,:),maxRidgeParamIndexEEGkin(j,:,:)]=ridgeCV(savePath,'EEG','kin',k,ridgeParams,eegOffset);
%         [correlationLFkin(j,:),maxRidgeParamIndexLFkin(j,:,:)]=ridgeCV(savePath,'LF','kin',k,ridgeParams,eegOffset);
%         [correlationEMGautoenc(j,:),maxRidgeParamIndexEMGautoenc(j,:,:)]=ridgeCV(savePath,'EMG','Autoenc',k,ridgeParams,eegOffset);
%         [correlationEEGautoenc(j,:),maxRidgeParamIndexEEGautoenc(j,:,:)]=ridgeCV(savePath,'EEG','Autoenc',k,ridgeParams,eegOffset);
%         [correlationLFautoenc(j,:),maxRidgeParamIndexLFautoenc(j,:,:)]=ridgeCV(savePath,'LF','Autoenc',k,ridgeParams,eegOffset);
%         [correlationEMGpca(j,:),maxRidgeParamIndexEMGpca(j,:,:)]=ridgeCV(savePath,'EMG','PCA',k,ridgeParams,eegOffset);
%         [correlationEEGpca(j,:),maxRidgeParamIndexEEGpca(j,:,:)]=ridgeCV(savePath,'EEG','PCA',k,ridgeParams,eegOffset);
%         [correlationLFpca(j,:),maxRidgeParamIndexLFpca(j,:,:)]=ridgeCV(savePath,'LF','PCA',k,ridgeParams,eegOffset);
%         [correlationEMGnnmf(j,:),maxRidgeParamIndexEMGnnmf(j,:,:)]=ridgeCV(savePath,'EMG','NNMF',k,ridgeParams,eegOffset);
%         [correlationEEGnnmf(j,:),maxRidgeParamIndexEEGnnmf(j,:,:)]=ridgeCV(savePath,'EEG','NNMF',k,ridgeParams,eegOffset);
%         [correlationLFnnmf(j,:),maxRidgeParamIndexLFnnmf(j,:,:)]=ridgeCV(savePath,'LF','NNMF',k,ridgeParams,eegOffset);
        [correlationEEGemg(j,:),maxRidgeParamIndexEEGemg(j,:,:)]=ridgeCV(savePath,'EEG','EMG',k,ridgeParams,eegOffset);
        [correlationAutoencKin(j,:),maxRidgeParamIndexAutoencKin(j,:,:)]=ridgeCV(savePath,'Autoenc','kin',k,ridgeParams,eegOffset);
        [correlationViaAutoenc(j,:),viaCorrelationAutoenc(j,:)]=ridgeCVvia(savePath,'EEG','Autoenc','kin',k,ridgeParams,eegOffset);
        [correlationViaPCA(j,:),viaCorrelationPCA(j,:)]=ridgeCVvia(savePath,'EEG','PCA','kin',k,ridgeParams,eegOffset);
        [correlationViaNNMF(j,:),viaCorrelationNNMF(j,:)]=ridgeCVvia(savePath,'EEG','NNMF','kin',k,ridgeParams,eegOffset);
        [correlationEMGViaAutoenc(j,:),~]=ridgeCVvia(savePath,'EEG','Autoenc','EMG',k,ridgeParams,eegOffset);
        
        fprintf('%s%i finished %s\n',subject,number,datestr(datetime('now')))
    end
    
    save(strcat(pathToFile,sprintf('../matlabData/%s_callAll-%s.mat',datestr(datetime('now')),name)));
else
    j=fix(rand()*size(numbersMat,2)+1);
    
    number=numbersMat(j);
    subject=subjectsForNumbers{j};
    savePath=readAll(pathToFile,subject,number,windowEMG,windowEEG,shiftEMG,shiftEEG,maxFile,threshold,pburgOrder,minEEGFreq,maxEEGFreq,pause,noLFsamples,EMGChannels,noSynergies);
%     [meanAccurancysEMG,maxCEMG,cmScaledEMG(:,:)]=svmEciton(savePath,'EMG',k,maxExpC,maxPerClass,eegOffset);
%     [meanAccurancysEEG,maxCEEG,cmScaledEEG(:,:)]=svmEciton(savePath,'EEG',k,maxExpC,maxPerClass,eegOffset);
%     [meanAccurancysLF,maxCLF,cmScaledLF(:,:)]=svmEciton(savePath,'LF',k,maxExpC,maxPerClass,eegOffset);
%     [correlationEMGkin,maxRidgeParamIndexEMGkin(:,:)]=ridgeCV(savePath,'EMG','kin',k,ridgeParams,eegOffset);
%     [correlationEEGkin,maxRidgeParamIndexEEGkin(:,:)]=ridgeCV(savePath,'EEG','kin',k,ridgeParams,eegOffset);
%     [correlationLFkin,maxRidgeParamIndexLFkin(:,:)]=ridgeCV(savePath,'LF','kin',k,ridgeParams,eegOffset);
%     [correlationEMGautoenc,maxRidgeParamIndexEMGautoenc(:,:)]=ridgeCV(savePath,'EMG','Autoenc',k,ridgeParams,eegOffset);
%     [correlationEEGautoenc,maxRidgeParamIndexEEGautoenc(:,:)]=ridgeCV(savePath,'EEG','Autoenc',k,ridgeParams,eegOffset);
%     [correlationLFautoenc,maxRidgeParamIndexLFautoenc(:,:)]=ridgeCV(savePath,'LF','Autoenc',k,ridgeParams,eegOffset);
%     [correlationEMGpca,maxRidgeParamIndexEMGpca(:,:)]=ridgeCV(savePath,'EMG','PCA',k,ridgeParams,eegOffset);
%     [correlationEEGpca,maxRidgeParamIndexEEGpca(:,:)]=ridgeCV(savePath,'EEG','PCA',k,ridgeParams,eegOffset);
%     [correlationLFpca,maxRidgeParamIndexLFpca(:,:)]=ridgeCV(savePath,'LF','PCA',k,ridgeParams,eegOffset);
%     [correlationEMGnnmf,maxRidgeParamIndexEMGnnmf(:,:)]=ridgeCV(savePath,'EMG','NNMF',k,ridgeParams,eegOffset);
%     [correlationEEGnnmf,maxRidgeParamIndexEEGnnmf(:,:)]=ridgeCV(savePath,'EEG','NNMF',k,ridgeParams,eegOffset);
%     [correlationLFnnmf,maxRidgeParamIndexLFnnmf(:,:)]=ridgeCV(savePath,'LF','NNMF',k,ridgeParams,eegOffset);
    [correlationEEGemg,maxRidgeParamIndexEEGemg(:,:)]=ridgeCV(savePath,'EEG','EMG',k,ridgeParams,eegOffset);
    [correlationAutoencKin,maxRidgeParamIndexAutoencKin(:,:)]=ridgeCV(savePath,'Autoenc','kin',k,ridgeParams,eegOffset);
    [correlationViaAutoenc,viaCorrelationAutoenc]=ridgeCVvia(savePath,'EEG','Autoenc','kin',k,[100],eegOffset);
    [correlationViaPCA,viaCorrelationPCA]=ridgeCVvia(savePath,'EEG','PCA','kin',k,[100],eegOffset);
    [correlationViaNNMF,viaCorrelationNNMF]=ridgeCVvia(savePath,'EEG','NNMF','kin',k,[100],eegOffset);
    [correlationEMGViaAutoenc,~]=ridgeCVvia(savePath,'EEG','Autoenc','EMG',k,ridgeParams,eegOffset);
    fprintf('%s%i finished %s\n',subject,number,datestr(datetime('now')))
    
    save(strcat(pathToFile,sprintf('../matlabData/%s_call%s%i-%s.mat',datestr(datetime('now')),subject,number,name)));
end
delete(poolObj)
