addpath('/home/hohlochj/masterarbeit/usedMcode')
pathToFile='/nfs/wsi/ti/messor/hohlochj/origData/';
%pathToFile='/home/jph/Uni/masterarbeit/Block1_ReachingMovements/';
maxFile=5;
threshold=10000; %EMG is classified as movement

allSubjects=true; %run all subjects and days or only one random day for one random subject

windowEMG=0.2; %try 1s
windowEEG=1;
shift=0.2;
pburgOrder=50;
minEEGFreq=0; %try 8-30
maxEEGFreq=200;
pause=false;
ridgeParams=[0.1];
k=10;
maxExpC=0;
maxPerClass=250;
EEG=true;
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;

meanAccurancysEMG=zeros([j,1]);
meanAccurancysEEG=zeros([j,1]);
maxCEMG=zeros([j,k,1]);
maxCEEG=zeros([j,k,1]);
cmScaledEMG=zeros([j,5,5]);
cmScaledEEG=zeros([j,5,5]);
correlationEMG=zeros([j,3]); %x,y,angle
correlationEEG=zeros([j,3]);

if allSubjects
    parfor j=1:size(numbersMat,2)
        number=numbersMat(j);
        subject=subjectsForNumbers{j};
        savePath=readEEG(pathToFile,subject,number,windowEMG,windowEEG,shift,maxFile,threshold,pburgOrder,minEEGFreq,maxEEGFreq,pause);
        [meanAccurancysEMG(j),maxCEMG(j,:),cmScaledEMG(j,:,:)]=svmEciton(savePath,~EEG,k,maxExpC,maxPerClass);
        [meanAccurancysEEG(j),maxCEEG(j,:),cmScaledEEG(j,:,:)]=svmEciton(savePath,EEG,k,maxExpC,maxPerClass);
        [correlationEMG(j,:)]=ridgeCV(savePath,false,k,ridgeParams);
        [correlationEEG(j,:)]=ridgeCV(savePath,EEG,k,ridgeParams);
        fprintf('%s%i finished %s\n',subject,number,datestr(datetime('now')))
    end

    save(strcat(pathToFile,sprintf('../matlabData/%s_callAll.mat',datestr(datetime('now')))));
else
    j=rand()*size(numbersMat,2)+1;
    
    number=numbersMat(j);
    subject=subjectsForNumbers{j};
    savePath=readEEG(pathToFile,subject,number,windowEMG,windowEEG,shift,maxFile,threshold,pburgOrder,minEEGFreq,maxEEGFreq,pause);
    [meanAccurancysEMG(j),maxCEMG(j,:),cmScaledEMG(j,:,:)]=svmEciton(savePath,~EEG,k,maxExpC,maxPerClass);
    [meanAccurancysEEG(j),maxCEEG(j,:),cmScaledEEG(j,:,:)]=svmEciton(savePath,EEG,k,maxExpC,maxPerClass);
    [correlationEMG(j,:)]=ridgeCV(savePath,false,k,ridgeParams);
    [correlationEEG(j,:)]=ridgeCV(savePath,EEG,k,ridgeParams);
    fprintf('%s%i finished %s\n',subject,number,datestr(datetime('now')))
    
    save(strcat(pathToFile,sprintf('../matlabData/%s_call%s%i.mat',datestr(datetime('now')),subject,number)));
end
delete(poolObj)


