% Pipeline including task practice/threshold estimation, MAT pre, checkerboard and MAT post assessment

%%%%%%%%%%%%%%%%%%%%%%
%% enter subject ID %%
%%%%%%%%%%%%%%%%%%%%%%

subj = 'TEST_v1';

%% add required paths

pn.root = '/Volumes/LNDG-1/Julian/Projects/DYNSAT/C_Paradigm/';
pn.CB = [pn.root, 'checker/'];
pn.MAT = [pn.root, 'dotsx/'];

addpath(genpath([pn.root, 'functions/']));
addpath(pn.CB);
addpath(pn.MAT);

addpath(genpath('/Users/Shared/Psychtoolbox/')); % PTB 3.0.13 (160606)

%% create results directory

subjectsPath = [pn.root, '/data/', subj '_' datestr(now, 'yymmdd_HHMM') '/']; mkdir(subjectsPath);

%% set up PTB

Screen('Preference', 'SkipSyncTests', 1);
oldLevel = Screen('Preference', 'Verbosity', 4); % output debugging info
PsychDebugWindowConfiguration(0,0.3)

%% practice MAT task

cd(pn.root);

% staircase parameters
P.PM.priorAlphaRange = .5:.02:.8;
P.PM.stimRange = .5:.01:.8;         % only allow values in range from 50 to 80%
P.PM.numtrials = 32;                % (numtrials/2) needs to be round
P.PM.gamma = 0.5;                   % assumed chance level
P.PM.priorLambdaRange = .2;

dotInfo = createDotInfo_JQK_170704;
dotInfo.dataDir = subjectsPath;

cursubj = [subj, '_practice'];
DEBUG = 1; opacity = 1;
thresholds = MAT_practice_170704(dotInfo, cursubj, DEBUG, opacity, P);

disp(thresholds); disp('Please check whether they are reasonable.')

%% run MAT Task (Pre-CB) 

dotInfo = createDotInfo_JQK_170704(thresholds);
dotInfo.dataDir = [pn.root, 'data/'];

cursubj = [subj, '_PreMAT'];
DEBUG = 1; opacity = 1;
MAT_experiment_170704(dotInfo, cursubj, DEBUG, opacity)

%% practice checkerboard task

subjectsPath = [pn.root, '/data/', subj '_' datestr(now, 'yymmdd_HHMM') '/']; mkdir(subjectsPath);

setup.subj          = subj;
setup.design        = 1;
setup.ET            = 0;
setup.in_scanner    = 0;
setup.dummyScans    = 0;
setup.add_display   = 0; % if not in scanner: additional display connected?
setup.test_trigger  = 0; % test scanner trigger (1) or not (0)
setup.DEBUG         = 1; % debug mode?
setup.transparency  = .99; % only relevant if DEBUG == 1
setup.verbosity     = 4; % magnitude of PTB output
setup.EPI           = 0;
setup.BGmode        = 'black'; % black/grey mode
setup.mode          = 'task';

[checkerInfo_practice, setup] = PureCheckerParameters_Practice_170403(setup);
setup.catchPractice = 0;
checkerBoard_Practice_Threshold_170705(checkerInfo_practice, subjectsPath, setup);

%% set up and run checkerboard

subjectsPath = [pn.root, '/data/', subj '_' datestr(now, 'yymmdd_HHMM') '/']; mkdir(subjectsPath);

setup.subj          = subj;
setup.design        = 1;
setup.ET            = 0;
setup.in_scanner    = 0;
setup.dummyScans    = 0;
setup.add_display   = 0; % if not in scanner: additional display connected?
setup.test_trigger  = 0; % test scanner trigger (1) or not (0)
setup.DEBUG         = 1; % debug mode?
setup.transparency  = .99; % only relevant if DEBUG == 1
setup.verbosity     = 4; % magnitude of PTB output
setup.EPI           = 0;
setup.BGmode        = 'black'; % black/grey mode
setup.mode          = 'task';

[checkerInfo, setup] = PureCheckerParameters_170403(setup);
checkerContrastComp_170705(checkerInfo, [subjectsPath, 'CB_Task_'], setup)

%% run MAT Task (post-CB)

dotInfo = createDotInfo_JQK_170704(thresholds);
dotInfo.dataDir = [pn.root, 'data/'];

cursubj = [subj, '_PostMAT'];
DEBUG = 1; opacity = 1;
MAT_experiment_170704(dotInfo, cursubj, DEBUG, opacity)
