%% restructuring model predictions for figure 8:

clearvars
OVERWRITE = 1;                                                              % redo the BIC Calculation
currentFolder = pwd;
addpath(genpath('Matlab_functions')) 
path = currentFolder(1:end-8); 
data_folder = 'data\processed\';
fits_folder = 'data\fits\';
%%
NS = [1:10,12:24];
CW = [-1,-1,-1,1,1,1,-1,1,1,-1,-1,1,-1,1,-1,1,1,1,-1,-1,-1,1,1];            % -1 no/CCW-CW; 1 yes/CW-CCW
Apos = [360     5    10    15    35    60   300   325   345   350   355];  % possible target angles
if exist(sprintf('%s%s%',path,'data\restructured_Fig_8.mat'),'file')~=2 || OVERWRITE
    %% loading in the restructured AI data:
    load(sprintf('%s%s%',path,'data\restructured_Fig_4_5_8.mat'),'FF1','SR','FF2')
    %% restructuring:
    %                pp b MD Tr (data/ ma/ m0)
    SR_d_mb_pb = NaN(24,5,11,44,3);
    FF1_d_mb_pb = NaN(24,5,11,4,3);
    FF2_d_mb_pb = NaN(24,5,1,3,3);
    
    %% adding the participant data:
    SR_d_mb_pb(:,:,:,:,1) = SR;
    FF1_d_mb_pb(:,:,:,:,1) = FF1;
    FF2_d_mb_pb(:,:,:,:,1) = FF2;
    %% adding the model predictions
    
    for s = NS                                                      % loop over pp.
        file = sprintf('%s%ssim%02d.MAT',path,'data\simulations\',s);
        load(file,'learningMB','learningPB')
        file = sprintf('%s%sfits_%s_%02d.MAT',path,fits_folder,'motion_based',s);
        load(file,'T')
        
        
        
        for a = 1:11
            for b =1:5
                indSR  = find(T.trialtype == 4 & T.block == b& T.theta_planned == Apos(a));  %index of the Spontaneous recovery trials
                I = indSR -  find(T.trialtype == 4 & T.block == b,1)+1;                % index relative to first spontaneous recovery trial trial of that block
                indB1  = find(T.trialtype == 3 & T.block == b& T.theta_planned == Apos(a)); % index of phase A trials for that angle
                indB2  = find(T.trialtype == 6 & T.block == b& T.theta_planned == Apos(a)); % index of phase A trials for that angle
                
                SR_d_mb_pb(s,b,a,I,2:3) = [learningMB.total(indSR),learningPB.total(indSR)]; %  AI for spontaneous recovery
                FF1_d_mb_pb(s,b,a,:,2:3) = [learningMB.total(indB1),learningPB.total(indB1)];% AI for phase A;
                if Apos(a) == 360
                    FF2_d_mb_pb(s,b,1,:,2:3) = [learningMB.total(indB2(end-2:end)),learningPB.total(indB2(end-2:end))];% AI for phase B;
                end
            end
        end
    end



    save(sprintf('%s%s%',path,'data\restructured_Fig_8.mat'),'FF2_d_mb_pb','FF1_d_mb_pb','SR_d_mb_pb','Apos')
end