
ft_defaults
% this returns the location where FieldTrip is installed
[ftver, ftdir] = ft_version;


%% Starting source reconstruction
exp_main_path = cfg_fun.exp_main_path;
analysis_Path = [exp_main_path filesep 'Matlab' filesep  'MEG3_scripts' filesep 'source_reconstruction' filesep]; % subjecf MEG dat



subjects =  [47 48 49 50 51 52 53 55 56  57 58 59 60 61 62 63 64 65 66 67 68 69 70 71];
nsubjects =  length(subjects);

for isubj = 1 : nsubjects
    % isubj = 58
    SUBJS = {['0',num2str(subjects(isubj))]}
    curSubj = SUBJS{1}
    
    %addpath(genpath( [exp_main_path filesep 'Matlab' filesep 'MEG3_scripts'])); % general processing scripts in this exp.
    
    G.subjPath = [exp_main_path filesep 'MEG' filesep 'AVE_MEG3' filesep curSubj filesep]; % main subject folder
    G.inputPath = [G.subjPath ,'func', filesep, 'source_based']; % subject saving artifact and preprocessing matrixes
    G.outputPath = [G.subjPath ,'func', filesep, 'source_based']; % subject saving artifact and preprocessing matrixes
    
    load([G.outputPath, filesep, 'decod_ROIS.mat']);
    nrois = length(ROI_univ);
    
    
    for i_roi = 1 : nrois
        for i_comb = 1 :  length(ROI_decod(i_roi).combi)
            cfg = [];
            timelock(i_roi).combi(i_comb).m0{isubj} =  ROI_decod(i_roi).combi(i_comb).m1;
            timelock(i_roi).combi(i_comb).m1{isubj} =  ROI_decod(i_roi).combi(i_comb).m0;
            timelock(i_roi).combi(i_comb).pCorrect{isubj} =  ROI_decod(i_roi).combi(i_comb).pCorrect;
            TGM(i_roi).combi(i_comb).m0(isubj,:,:) =  ROI_decod(i_roi).combi(i_comb).TGM_m0;
            TGM(i_roi).combi(i_comb).m1(isubj,:,:) =  ROI_decod(i_roi).combi(i_comb).TGM_m1;
            TGM(i_roi).combi(i_comb).pCorrect(isubj,:,:) =  ROI_decod(i_roi).combi(i_comb).TGM_pCorrect;
        end
    end
end



% Preparing group level averaged data
for i_roi = 1 : nrois
    for i_comb = 1 :  length(ROI_decod(i_roi).combi)
        cfg = [];
        results.grandAvg(i_roi).combi(i_comb).m0 = ft_timelockgrandaverage(cfg, timelock(i_roi).combi(i_comb).m0{1 : nsubjects});
        results.grandAvg(i_roi).combi(i_comb).m1 = ft_timelockgrandaverage(cfg, timelock(i_roi).combi(i_comb).m1{1 : nsubjects});
        results.grandAvg(i_roi).combi(i_comb).pCorrect = ft_timelockgrandaverage(cfg, timelock(i_roi).combi(i_comb).pCorrect{1 : nsubjects});
    end
end




% Plotting average data in the diagonals

time =  results.grandAvg(i_roi).combi(i_comb).m0.time;

for i_roi = 1 : nrois
    isubplot = 1;
    figure('name', ROI_decod(i_roi).name);
    for i_comb = 1 :  length(ROI_decod(i_roi).combi)
        subplot(1,3,isubplot);
        datplot = results.grandAvg(i_roi).combi(i_comb);
        %subplot(3, 1, 1);
        hold on;
        grid on;
        plot(time, datplot.m0.avg, 'black');
        plot(time, datplot.m1.avg, 'red');
        % subplot(3, 1, 2);
        % plot(time, log10(datplot.p)); grid on;
        % subplot(3, 1, 3);
        % plot(time, datplot.pCorrect); grid on
        
        title([char(datplot.m0.label)]);
        
        isubplot = isubplot + 1;
    end
end


isubplot = 1;
figure(); % 'name', ROI_decod(i_roi).name
for i_roi = 1 : nrois
    subplot(1,4,isubplot);
    
    datplot = results.grandAvg(i_roi);
    %subplot(3, 1, 1);
    hold on;
    grid on;
    plot(time, datplot.combi(1).pCorrect.avg, 'black');
    plot(time, datplot.combi(2).pCorrect.avg, 'red');
    plot(time, datplot.combi(3).pCorrect.avg, 'green');
    legend('SA vs LC','SA vs HC','LC vs HC')
    % subplot(3, 1, 2);
    % plot(time, log10(datplot.p)); grid on;
    % subplot(3, 1, 3);
    % plot(time, datplot.pCorrect); grid on
    
    title(ROI_decod(i_roi).name);
    
    isubplot = isubplot + 1;
end




for i_roi = 1 : nrois
    isubplot = 1;
    figure('name', ROI_decod(i_roi).name,'Renderer', 'painters', 'Position', [10 10 1300 500]);
    
    for i_comb = 1 :  length(ROI_decod(i_roi).combi)
        subplot(1,3, isubplot);
        anal_name = results.grandAvg(i_roi).combi(i_comb).m1.label;
        datplot = squeeze(mean(TGM(i_roi).combi(i_comb).pCorrect,1));
        %subplot(3, 1, 1);
        hold on; %grid on;
        imagesc(time,time, datplot);
        
        H = gca
        H.CLim = [0.4 0.6];
        %H.CLim = [-0.2 0.2];
        habline(0, 'black', 'linewidth', 0.5);
         vabline(0, 'black', 'linewidth', 0.5);
        
        
        diagonal = plot(time, time);
        diagonal.LineWidth = 1;
        diagonal.Color = [0 0 0];
        diagonal.LineStyle = ':';
        
        xlabel('Decoding time (s)')
        ylabel('Training time (s)')
        axis square;
        %axis equal;
        colorbar();
        xlim([-0.2 1.5]);
        ylim([-0.2 1.5]);
        %eqClims(0.2);
        title([char(anal_name) ]);
        isubplot = isubplot + 1;
    end
end

