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='MoveRest'; %suffix for the output, has to be valid name for file (no space, /, ...)

% window and shift given in seconds
windowEMG=0.2; 
windowEEG=1;
shiftEMG=0.05;
shiftEEG=0.2;

eegOffset=0; %predict actions x*shiftEEG after EEG measurement
pburgOrder=250;
minEEGFreq=2; %[Hz]
maxEEGFreq=49;
pause=false; % is all data 1s befor movement dropped?
noLFsamples=5;

% Parameters tried for ridge regression
ridgeParams=100;
k=10; % k-fold CV (usually 10)
% Parameters tried for SVM: 2^-x to 2^x in steps of 1 in x
maxExpC=0;
maxPerClass=250;
poolObj=parpool(32);

% structure data of runs
[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

% run for all 51 runs
if allSubjects
    
%      meanAccuracysEMG=zeros([j,1]);
%      meanAccuracysEEG=zeros([j,1]);
%      meanAccuracysLF=zeros([j,1]);
%      maxCEMG=zeros([j,k,1]);
%      maxCEEG=zeros([j,k,1]);
%      maxCLF=zeros([j,k,1]);
%      meanAccuracysEMGmovements=zeros([j,1]);
%      meanAccuracysEEGmovements=zeros([j,1]);
%      meanAccuracysLFmovements=zeros([j,1]);
%      maxCEMGmovements=zeros([j,k,1]);
%      maxCEEGmovements=zeros([j,k,1]);
%      maxCLFmovements=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,size(EMGChannels,2),k]);
%     correlationEEGemg=zeros([j,size(EMGChannels,2)]);
%     maxRidgeParamIndexAutoencKin=zeros([j,noSynergies,k]);
     maxRidgeParamIndexPCAKin=zeros([j,noSynergies,k]);
     maxRidgeParamIndexNNMFKin=zeros([j,noSynergies,k]);
%     correlationAutoencKin=zeros([j,3]);
     correlationPCAKin=zeros([j,3]);
     correlationNNMFKin=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);
%          [meanAccuracysEMG(j),maxCEMG(j,:),~]=svmEciton(savePath,'EMG',k,maxExpC,maxPerClass,eegOffset,true);
%          [meanAccuracysEEG(j),maxCEEG(j,:),~]=svmEciton(savePath,'EEG',k,maxExpC,maxPerClass,eegOffset,true);
%          [meanAccuracysLF(j),maxCLF(j,:),~]=svmEciton(savePath,'LF',k,maxExpC,maxPerClass,eegOffset,true);
%         [meanAccuracysEMGmovements(j),maxCEMGmovements(j,:),cmScaledEMG(j,:,:)]=svmEciton(savePath,'EMG',k,maxExpC,maxPerClass,eegOffset,false);
%         [meanAccuracysEEGmovements(j),maxCEEGmovements(j,:),cmScaledEEG(j,:,:)]=svmEciton(savePath,'EEG',k,maxExpC,maxPerClass,eegOffset,false);
%         [meanAccuracysLFmovements(j),maxCLFmovements(j,:),cmScaledLF(j,:,:)]=svmEciton(savePath,'LF',k,maxExpC,maxPerClass,eegOffset,false);
%         [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);
        [correlationPCAKin(j,:),maxRidgeParamIndexPCAKin(j,:,:)]=ridgeCV(savePath,'PCA','kin',k,ridgeParams,eegOffset);
        [correlationNNMFKin(j,:),maxRidgeParamIndexNNMFKin(j,:,:)]=ridgeCV(savePath,'NNMF','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
    % run only for one random run
    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);
%     [meanAccuracysEMG,maxCEMG,cmScaledEMG(:,:)]=svmEciton(savePath,'EMG',k,maxExpC,maxPerClass,eegOffset,false);
%     [meanAccuracysEEG,maxCEEG,cmScaledEEG(:,:)]=svmEciton(savePath,'EEG',k,maxExpC,maxPerClass,eegOffset,false);
%     [meanAccuracysLF,maxCLF,cmScaledLF(:,:)]=svmEciton(savePath,'LF',k,maxExpC,maxPerClass,eegOffset,false);
%     [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)
