addpath('/home/hohlochj/masterarbeit/usedMcode')
pathToFile='/nfs/wsi/ti/messor/hohlochj/origData/';
%pathToFile='/home/jph/Uni/masterarbeit/origData/';
maxFile=1;
threshold=10000; %EMG is classified as movement
EMGChannels={'AbdPolLo','Biceps','Triceps','FrontDelt','MidDelt','BackDelt'};
noSynergies=2;

allSubjects=true; %run all subjects and days or only one random day for one random subject
name='Via2Syn'; %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;
maxEEGFreq=49;
pause=false;
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
%     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,noSynergies);
%        [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
    % run only for one random run
    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,noSynergies);
    [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)


