function main(model,task,subtask)
disp('RUN');
switch task 
    case 'pre'
        preprocess(model,subtask);
    case 'decode'
        decode(model,subtask);
    case 'plot'
        decode_plot(model,subtask);
end
end
% disp('all warnings OFF')
% warn = warning('off','all');


function preprocess(model,task)
% parse the GUI and pass parameters as structure

evalin('base','clear(''preprocessedData'')');

timeLine = getTimeLineParams(model);
subjects = getSubjectCellList(model);
classDef = parseClassDef(model);

mask     = ['^' cell2mat(getImageFileMask(model)) '.*\.img$'];

psthOpts = struct;
psthOpts.colBias       = getColumnBiasRemoveOption(model);
psthOpts.psthNorm      = getPsthNormalizationMethod(model);


switch task
    case 'COORD'
        disp('LUT Approach');
       
        out = struct;
        out.header = struct;
        out.header.type = 'COORD';
        out.header.timeline = timeLine;
        out.header.classDef = classDef;
        
        coordargs = struct;
        coordargs.subjects      = subjects;
        coordargs.timeline      = timeLine;
        coordargs.basedir       = getBaseDir(model);
        coordargs.sessionList   = 1:3;
        coordargs.eventList     = classDef.eventMatrix;
        coordargs.coords        = parseCoordinateTextField(model);
        coordargs.mask          = mask;
        coordargs.psthOpts      = psthOpts;
        coordargs.groupingFkt   = getSpatialGroupingFunction(model);
        
        out.subjectdata = runCoordTable(coordargs);

    case 'ROI'
        disp('ROI Image Approach');
        out = struct;
        out.header = struct;
        out.header.type = 'ROI';
        out.header.timeline = timeLine;
        out.header.classDef = classDef;
        
        roiargs = struct;
        roiargs.subjects        = subjects;
        roiargs.timeline        = timeLine;
        roiargs.classes         = classDef;
        roiargs.mask            = mask;
        roiargs.basedir         = model.baseDir;
        roiargs.sessionList     = 1:3;
        roiargs.eventList       = classDef.eventMatrix;
        roiargs.psthOpts        = psthOpts;
        
        out.subjectdata = runROIImageMaskMode(roiargs);
        
    case 'FBS'
        disp('FSB');
        out = struct;
        out.header = struct;
        out.header.type = 'FBS';
        out.header.timeline = timeLine;
        out.header.classDef = classDef;
        

        out.subjectdata  = fbs_load_mask(model.baseDir,subjects);
        
%         if(size(subjects,2)>1)
%             display(sprintf('No BATCH Support for Searchlight!'));
%             return
%         end
        
        fbsargs = struct;
        fbsargs.timeline        = timeLine;
        fbsargs.classes         = classDef;
        fbsargs.mask            = mask;
        fbsargs.basedir         = model.baseDir;
        fbsargs.sessionList     = 1:3;
        fbsargs.eventList       = classDef.eventMatrix;
        fbsargs.psthOpts        = psthOpts;

        out.header.args         = fbsargs;

        % no more preprocessing in this step!
                
end

assignin('base','preprocessedData',out);


end

function decode(model,task)
preprocessedData = evalin('base','preprocessedData');

if(~(isa(preprocessedData,'struct')))
    display('you need to preprocess some data before this step');
    return
end
warn = warning('off','all');
header            = preprocessedData.header;
header.frameShift = getFrameShiftParams(model);
header.timeframeGroupingfunction = getTemporalGroupingFunction(model);
data              = preprocessedData.subjectdata;

switch task
    case 'SVM'
        disp('SVM');
        svmopts    = getSvmArgs(model,1);
        header.svmrnd = getSvmRnd(model);
        header.nantozero = 1;
        decode = calculateMultiSubjectDecodePerformance(header,data,svmopts);
        decode.header = header;
        assignin('base','decode',decode);
    case 'XSVM'
        disp('XSVM')
        svmopts  = getSvmArgs(model,0);
        header.svmrnd = getSvmRnd(model);
        header.nantozero = 1;
        decode = xsvm_subject_loop(header,data,svmopts);
        decode.header = header;
        assignin('base','decode',decode);
    case 'SOM'
        display('SOM');
        somopts.rnd = 1;
        somopts.nantozero = 1;
        somopts.size = [3 3];
        somopts.lattice = 'rect';
        somopts.nfold = 6;
        decode = som_subject_batch(header,data,somopts);
        decode.header = header;
        assignin('base','decode',decode);
    case 'XSOM'
        display('XSOM');
        somopts.rnd = 1;
        somopts.nantozero = 1;
        somopts.size = [3 3];
        somopts.lattice = 'rect';
        decode = som_xsubject_performance(header,data,somopts);
        decode.header = header;
        assignin('base','decode',decode);
    case 'FBS'
        display('FBS');
        fbsargs.svmopts = getSvmArgs(model,1);
        header.svmrnd   = getSvmRnd(model);
        fbsargs.radius  = getSearchlightRadius(model);
        fbsargs.timeline = getSearchlightTimeline(model);
            runFBSImageMaskMode(header,data,fbsargs);
        
end

disp('warings restored');
warning(warn);
end

function decode_plot(model,type)
decode = evalin('base','decode');
preprocessed = evalin('base','preprocessedData');
plotDecodePerformance(type,decode,preprocessed.subjectdata);
       
end