%% function to plot subj-avg ERSitem scores for remembered and forgotten items
% over all timeXtime points

function posthoc_cluster_barscatter(subjects,atlasType,atlasIdx,rsa_tag,roi_tag,stats_tag)
    
    fs = '\';
    dataDir = '..\Data';
    load('..\subject_list.mat'); %subNames for easier folder accessing

    cmap = cbrewer('div','RdBu',60); cmap(cmap>1) = 1; cmap(cmap<0) = 0; cmap = flipud(cmap);
    
    if strcmp(atlasType,'BA')
        roi_tag = [atlasType char(replace(join(string(atlasIdx)),' ','_'))];
    elseif strcmp(atlasType,'HO')
        %load dir_labels and not numbers for HO naming
%         load(['F:\iEEG_PFC\ECoG_MemDev\ATLAS\HarvardOxford\labels_both.mat']);
%         fileTag_labels = labels(atlasIdx,3);
%         roi_tag = [atlasType '_' char(replace(join(fileTag_labels),' ','_'))];
    end
    
    %loop over subjects, load raw ERSitem scores per trial, 
    % avg remembered and forgotten @ each timepoint
    % plot average ERSitem 
    rem = zeros(length(subjects),25,25);
    forg = zeros(size(rem));

    for num = 1:length(subjects)
        fileName = subNames{subjects(num)};

        ERSinfo = load([dataDir fs fileName fs 'stats' fs 'ERS' fs rsa_tag fs roi_tag fs 'ERSinfo.mat']);
        ERSinfo = ERSinfo.info;

        load([dataDir fs fileName fs 'stats' fs 'ERS' fs rsa_tag fs roi_tag fs stats_tag fs 'itemspecific_reinstatement.mat']);

        rem_ix = find(ERSinfo.trialinfo(:,2) == 1); 
        forg_ix = find(ERSinfo.trialinfo(:,2) == 2);

        rem_avg = squeeze(nanmean(Reinstatement.ITEM(:,:,rem_ix),3));
        forg_avg = squeeze(nanmean(Reinstatement.ITEM(:,:,forg_ix),3));

        rem(num,:,:) = rem_avg;
        forg(num,:,:) = forg_avg;
    end

    %% avg over subjects and plot
    %plot subjavg
    figure('renderer','painters','position',[450 450 1300 550])
    set(gcf,'color','white')
    %remembered
    subplot(1,2,1); imagesc(squeeze(nanmean(rem,1))); set(gca,'Ydir','normal'); 
    c = colorbar(); c.Label.String = 'ERSitem rho (same - diff)'; caxis([-0.03 0.03]); colormap(cmap); 
    title('remembered');
    ylabel('encoding time'); xlabel('recognition time');
    xticklabels({'350','600','850','1100','1350'}); yticklabels({'350','600','850','1100','1350'});
    a = gca; a.FontSize = 15; hold on;
    
    %look for adjacent timepoints with p<0.05
    [h,p,ci,stats] = ttest(rem);
    cluster_edges = bwperimtrace(squeeze(h));
    for i = 1:1:length(cluster_edges)
        plot(cluster_edges{i}(:,1),cluster_edges{i}(:,2),'-','Color',[0.5 0.5 0.5],'LineWidth',3)
        hold on;
    end
    
    %forgotten
    subplot(1,2,2); imagesc(squeeze(nanmean(forg,1))); set(gca,'Ydir','normal'); 
    c = colorbar();  c.Label.String = 'ERSitem rho (same - diff)'; caxis([-0.03 0.03]); colormap(cmap); 
    title('forgotten');
    ylabel('encoding time'); xlabel('recognition time');
    xticklabels({'350','600','850','1100','1350'}); yticklabels({'350','600','850','1100','1350'});
    sgtitle(['ERS item rho - ' roi_tag])
    a = gca; a.FontSize = 15; hold on;
    
    %look for adjacent timepoints with p<0.05
    [h,p,ci,stats] = ttest(forg);
    cluster_edges = bwperimtrace(squeeze(h));
    for i = 1:1:length(cluster_edges)
        plot(cluster_edges{i}(:,1),cluster_edges{i}(:,2),'-','Color',[0.5 0.5 0.5],'LineWidth',3)
        hold on;
    end
end