% function [decodePerformance rawTimecourse ] = calculateDecodePerformance(des,timeLineStart, timeLineEnd, decodeDuration, svmargs, conditionList, sessionList, voxelList, classList, labelMap,normalize)
function outputStruct = calculateDecodePerformance(inputStruct,SubjectID)

addpath 'libsvm-mat-2.88-1';

METHOD = 'single subject SVM';
% METHOD = 'cross subject SVM';
% METHOD = 'SOM';

outputStruct = struct;

namehelper      = strcat('s',SubjectID);
des             = inputStruct.(namehelper).des;
timeLineStart   = inputStruct.frameShiftStart;
timeLineEnd     = inputStruct.frameShiftEnd;
decodeDuration  = inputStruct.decodeDuration;
svmargs         = inputStruct.svmargs;
sessionList     = inputStruct.sessionList;
voxelList       = inputStruct.(namehelper).voxelList;
% classList       = inputStruct.classList;
% labelMap        = inputStruct.labelMap;
smoothed        = inputStruct.smoothed;
globalStart     = inputStruct.psthStart;
globalEnd       = inputStruct.psthEnd;
baselineStart   = inputStruct.baselineStart;
baselineEnd     = inputStruct.baselineEnd;
eventList       = inputStruct.eventList;

minPerformance = inf;
maxPerformance = -inf;
        
        %% ERSETZEN DURCH ROI-IMAGE!
        for voxel = 1:size(voxelList,1)  % [[x;x],[y;y],[z;z]]
                extr        = calculateImageData(voxelList(voxel,:),des,smoothed); 
                rawdata     = cell2mat({extr.mean}); % Raw Data
                pst{voxel}  = calculatePST(des,globalStart,baselineStart,baselineEnd,globalEnd,eventList,rawdata,sessionList);
        end
        
        timePointArgs.pst = pst;
        timePointArgs.timeLineStart = timeLineStart;
        timePointArgs.timeLineEnd   = timeLineEnd;
        timePointArgs.globalStart   = globalStart;
        timePointArgs.globalEnd     = globalEnd;
        timePointArgs.decodeDuration= decodeDuration;
        timePointArgs.labelMap      = inputStruct.labelMap;
        timePointArgs.eventList     = eventList;
        
        timePointMatrix = buildTimePointMatrix(timePointArgs);

        decodePerformance = [];
        for index = 1:timeLineEnd-timeLineStart+1
            RANDOMIZE_DATAPOINTS = 0;
            svmdata      = timePointMatrix{index}(:,2:size(timePointMatrix{index},2));
            svmlabel     = timePointMatrix{index}(:,1);
            
            if RANDOMIZE_DATAPOINTS
                rndindex  = randperm(length(svmlabel));
                svmdata   = svmdata(rndindex,:);
                svmlabel  = svmlabel(rndindex);
            end

            SVM_METHOD = 'som training'
            switch SVM_METHOD;
                case 'libsvm crossval'
                    performance  = svmtrain(svmlabel, svmdata, svmargs);

                    minPerformance = min(minPerformance,performance);
                    maxPerformance = max(maxPerformance,performance);

                    decodePerformance = [decodePerformance; performance];
                case 'class performance'
                    newsvmopt = killCrossvalOpt(svmargs);
                    
                    model = svmtrain(svmlabel,svmdata,newsvmopt);
                    classperformance = [];
                    for class = unique(svmlabel)';

                        filterindex = find(class == svmlabel);
                        testing_label = svmlabel(filterindex);
                        testing_data  = svmdata(filterindex);
                        [plabel accuracy dvalue] = svmpredict(testing_label,testing_data,model,'');

                        classperformance = [classperformance accuracy(1)];
                    end
                    decodePerformance = [decodePerformance; classperformance];
                    
                case 'som training'
                    display('SOM TRAINING');
                    addpath 'somtoolbox2';
                    sD = som_data_struct(svmdata,'label',num2str(svmlabel));
                    assignin('base','sD',sD);
                    sM = som_make(sD,'msize', [3 4],'lattice', 'rect');
                    
                    assignin('base','sD',sD);
                    assignin('base','sM',sM);
                    
            end
            
        end
        
        outputStruct.decodePerformance  = decodePerformance;
        outputStruct.svmdata            = svmdata;
        outputStruct.svmlabel           = svmlabel;
        outputStruct.rawTimeCourse      = pst;
        outputStruct.minPerformance     = minPerformance;
        outputStruct.maxPerformance     = maxPerformance;
end

function opts = killCrossvalOpt(svmopt)
opts = '';
idx1 = 1;
for idx2=strfind(svmopt,' -')
    if idx1 ~= strfind(svmopt,' -v')
        opts = strcat(opts,svmopt(idx1:idx2));
    end
    idx1=idx2;
    if idx2==max(strfind(svmopt,' -'))
        opts = strcat(opts,svmopt(idx2:end));
    end
end
end