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
name='eegOffset'; %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=1; %predict actions x*shiftEEG after EEG measurement
pburgOrder=50;
minEEGFreq=0;
maxEEGFreq=200;
pause=false;
noLFsamples=5;
ridgeParams=[10];
k=10;
maxExpC=0;
maxPerClass=250;
poolObj=parpool(18);

[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;


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]);
    
    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);
        %[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,shiftEMG,shiftEEG,eegOffset);
        [correlationEEGkin(j,:),maxRidgeParamIndexEEGkin(j,:,:)]=ridgeCV(savePath,'EEG','kin',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
        [correlationLFkin(j,:),maxRidgeParamIndexLFkin(j,:,:)]=ridgeCV(savePath,'LF','kin',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
        %[correlationEMGautoenc(j,:),maxRidgeParamIndexEMGautoenc(j,:,:)]=ridgeCV(savePath,'EMG','Autoenc',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
        [correlationEEGautoenc(j,:),maxRidgeParamIndexEEGautoenc(j,:,:)]=ridgeCV(savePath,'EEG','Autoenc',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
        [correlationLFautoenc(j,:),maxRidgeParamIndexLFautoenc(j,:,:)]=ridgeCV(savePath,'LF','Autoenc',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
        %[correlationEMGpca(j,:),maxRidgeParamIndexEMGpca(j,:,:)]=ridgeCV(savePath,'EMG','PCA',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
        [correlationEEGpca(j,:),maxRidgeParamIndexEEGpca(j,:,:)]=ridgeCV(savePath,'EEG','PCA',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
        [correlationLFpca(j,:),maxRidgeParamIndexLFpca(j,:,:)]=ridgeCV(savePath,'LF','PCA',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
        %[correlationEMGnnmf(j,:),maxRidgeParamIndexEMGnnmf(j,:,:)]=ridgeCV(savePath,'EMG','NNMF',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
        [correlationEEGnnmf(j,:),maxRidgeParamIndexEEGnnmf(j,:,:)]=ridgeCV(savePath,'EEG','NNMF',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
        [correlationLFnnmf(j,:),maxRidgeParamIndexLFnnmf(j,:,:)]=ridgeCV(savePath,'LF','NNMF',k,ridgeParams,shiftEMG,shiftEEG,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,shiftEMG,shiftEEG,eegOffset);
    [correlationEEGkin,maxRidgeParamIndexEEGkin(:,:)]=ridgeCV(savePath,'EEG','kin',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
    [correlationLFkin,maxRidgeParamIndexLFkin(:,:)]=ridgeCV(savePath,'LF','kin',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
    %[correlationEMGautoenc,maxRidgeParamIndexEMGautoenc(:,:)]=ridgeCV(savePath,'EMG','Autoenc',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
    [correlationEEGautoenc,maxRidgeParamIndexEEGautoenc(:,:)]=ridgeCV(savePath,'EEG','Autoenc',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
    [correlationLFautoenc,maxRidgeParamIndexLFautoenc(:,:)]=ridgeCV(savePath,'LF','Autoenc',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
    %[correlationEMGpca,maxRidgeParamIndexEMGpca(:,:)]=ridgeCV(savePath,'EMG','PCA',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
    [correlationEEGpca,maxRidgeParamIndexEEGpca(:,:)]=ridgeCV(savePath,'EEG','PCA',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
    [correlationLFpca,maxRidgeParamIndexLFpca(:,:)]=ridgeCV(savePath,'LF','PCA',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
    %[correlationEMGnnmf,maxRidgeParamIndexEMGnnmf(:,:)]=ridgeCV(savePath,'EMG','NNMF',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
    [correlationEEGnnmf,maxRidgeParamIndexEEGnnmf(:,:)]=ridgeCV(savePath,'EEG','NNMF',k,ridgeParams,shiftEMG,shiftEEG,eegOffset);
    [correlationLFnnmf,maxRidgeParamIndexLFnnmf(:,:)]=ridgeCV(savePath,'LF','NNMF',k,ridgeParams,shiftEMG,shiftEEG,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)
