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='ViaToPos'; %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
    maxRidgeParamIndexEEGpos=zeros([j,3,k]);
    maxRidgeParamIndexEMGpos=zeros([j,3,k]);
    maxRidgeParamIndexLFpos=zeros([j,3,k]);
    correlationEMGpos=zeros([j,3]); %x,y,angle
    correlationEEGpos=zeros([j,3]);
    correlationLFpos=zeros([j,3]);
    
    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);
    
    parfor j=1:size(numbersMat,2)
        number=numbersMat(j);
        subject=subjectsForNumbers{j};
        savePath=readAllPos(pathToFile, subject,number,windowEMG,windowEEG,shiftEMG,shiftEEG,maxFile,...
                                threshold,pburgOrder,minEEGFreq,maxEEGFreq,pause,noLFsamples,EMGChannels);
%        [correlationEMGpos(j,:),maxRidgeParamIndex(j,:,:)]=ridgeCV(savePath,'EMG','kin',k,ridgeParams,eegOffset);
%        [correlationEEGpos(j,:),maxRidgeParamIndex(j,:,:)]=ridgeCV(savePath,'EEG','kin',k,ridgeParams,eegOffset);
%        [correlationLFpos(j,:),maxRidgeParamIndex(j,:,:)]=ridgeCV(savePath,'LF','kin',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);
       
        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=readAllPos(pathToFile, subject,number,windowEMG,windowEEG,shiftEMG,shiftEEG,maxFile,...
                            threshold,pburgOrder,minEEGFreq,maxEEGFreq,pause,noLFsamples,EMGChannels);
    [correlationEMGpos,maxRidgeParamIndex(:,:)]=ridgeCV(savePath,'EMG','kin',k,ridgeParams,eegOffset);
    [correlationEEGpos,maxRidgeParamIndex(:,:)]=ridgeCV(savePath,'EEG','kin',k,ridgeParams,eegOffset);
    [correlationLFpos,maxRidgeParamIndex(:,:)]=ridgeCV(savePath,'LF','kin',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);
   
    save(strcat(pathToFile,sprintf('../matlabData/%s_call%s%i-%s.mat',datestr(datetime('now')),subject,number,name)));
end
delete(poolObj)


