function plot_fig_1FGH

% plot_fig_1FGH
% Tang et al, Recurrent computations for visual pattern completion. 2018

% Internal note: data_occlusion_klab325v2.mat --> behavioral_data_1FGH.mat
x = load('behavioral_data_1FGH.mat');
data = x.data;
data = data(data.pres < 301,:);
clear x;

figure(1); clf; set(gcf,'Color','w');
set(gcf,'Position',[-1343 900 158 165]);
[cr, crw, cr_sem, crw_sem] = plotPerformanceCurveSubject(data);
ylim([0 100]); hold on;
plot([0 150],[20 20],'k--');
xlabel('SOA (ms)');
ylabel('Performance');

figure(2); clf; set(gcf,'Color','w');
set(gcf,'Position',[-1167 897 450 165]);
subplot(1,2,1);
[cr, cr_sem, N1] = plotPerformanceByPercentOcc(data,0); hold on; plot([0 50],[20 20],'k--');
set(gca,'XTick',0:10:40);

subplot(1,2,2);
[cr, cr_sem, N2] = plotPerformanceByPercentOcc(data,1); hold on; plot([0 50],[20 20],'k--');
set(gca,'XTick',0:10:40);

end

function [cr, crw, cr_sem, crw_sem] = plotPerformanceCurveSubject(data)

msize = 10;


chance = 100/length(unique(data.truth));
nsubjects = length(unique(data.subject));

subjs = unique(data.subject);

soas = unique(data.soa);

cr = NaN(length(soas),2);
cr_sem = NaN(length(soas),2);

crw = NaN(nsubjects,length(soas),2);
crw_sem = NaN(nsubjects,length(soas),2);
for i = 1:length(subjs)
    for k = 1:length(soas)
        sel = data.masked == 1 & data.soa == soas(k) & data.occluded & data.subject == subjs(i);
        cr(i,k,1) = mean(data.correct(sel));
        cr_sem(i,k,1) = std(data.correct(sel))/sqrt(sum(sel));
        
        sel = data.masked == 0 & data.soa == soas(k) & data.occluded & data.subject == subjs(i);
        cr(i,k,2) = mean(data.correct(sel));
        cr_sem(i,k,2) = std(data.correct(sel))/sqrt(sum(sel));
        
        sel = data.masked == 1 & data.soa == soas(k) & data.occluded == 0 & data.subject == subjs(i);
        crw(i,k,1) = mean(data.correct(sel));
        crw_sem(i,k,1) = std(data.correct(sel))/sqrt(sum(sel));
        
        sel = data.masked == 0 & data.soa == soas(k) & data.occluded == 0 & data.subject == subjs(i);
        crw(i,k,2) = mean(data.correct(sel));
        crw_sem(i,k,2) = std(data.correct(sel))/sqrt(sum(sel));
        
    end
end


%figure(1); clf; set(gcf,'Color','w');


cr_sem = squeeze(std(cr))/sqrt(size(cr,1));
crw_sem = squeeze(std(crw))/sqrt(size(crw,1));

cr = squeeze(mean(cr));
crw = squeeze(mean(crw));

plot(1000*soas,100*cr(:,1),'.-','Color',[0.7 0.7 0.7],'MarkerSize',msize); hold on;
plot(1000*soas,100*cr(:,2),'.--','Color',[0.7 0.7 0.7],'MarkerSize',msize);
plot(1000*soas,100*crw(:,1),'.-','Color','k','MarkerSize',msize);
plot(1000*soas,100*crw(:,2),'.--','Color','k','MarkerSize',msize);

errorlines(1000*soas,100*cr(:,1),100*cr_sem(:,1),'Color',[0.7 0.7 0.7]);
errorlines(1000*soas,100*cr(:,2),100*cr_sem(:,2),'Color',[0.7 0.7 0.7]);
errorlines(1000*soas,100*crw(:,1),100*crw_sem(:,1),'Color','k');
errorlines(1000*soas,100*crw(:,2),100*crw_sem(:,2),'Color','k');


ylim([20 100]); box off;
xlim([0 150]);
xlabel('SOA (ms)');
ylabel('Performance');
set(gca,'YTick',0:20:100);
text(150, 25, sprintf('n=%d',nsubjects),'HorizontalAlignment','right');

set(gca,'XTick',soas*1000);
set(gca,'TickDir','out');
set(gca,'TickLength',[0.03 0.03]);
end

function [cr, cr_sem, N, hline, herr] = plotPerformanceByPercentOcc(data,mask)
%% graphics
msize = 10;
colors{1} = [239 38 34]/255; % red
colors{2} = [0 173 239]/255; % blue
colors{3} = [0 169 73]/255; % green
colors{4} = [250 180 0]/255; % gold
colors{5} = [163 37 86]/255; % purple


% chance = 100/length(unique(data.truth));
% nsubjects = length(unique(data.subject));

soas = unique(data.soa);
soas = [0.025 0.05 0.075 0.1 0.15];

X = [65:5:95 99];
[~,bin_index] = histc(data.black,X);

cr = NaN(length(soas),length(X));
cr_sem = NaN(length(soas),length(X));
N = NaN(length(soas),length(X));

%soas = unique(data.soa);
for s = 1:length(soas)
    for b = 1:(length(X)-1)
        sel = data.soa == soas(s) & data.black >= X(b) & data.black < X(b+1) & data.occluded == 1;
        
        N(s,b) = sum(sel & data.masked == mask);
        cr(s,b) = mean(data.correct(sel & data.masked == mask));
        cr_sem(s,b) = std(data.correct(sel & data.masked == mask))/sqrt(N(s,b));
    end
    
    sel = data.soa == soas(s) & data.black >= X(end) & data.occluded == 1;
    cr(s,length(X)) = sum(sel & data.masked == mask);
    cr(s,length(X)) = mean(data.correct(sel & data.masked == mask));
    cr_sem(s,length(X)) = std(data.correct(sel & data.masked == mask))/sqrt(N(s,length(X)));
    N(s,length(X)) = sum(sel);
end

hold all;
nbins = size(cr,1);

hline = NaN(nbins,1);
for k = 1:nbins
    if(any(~isnan(cr(k,:))))
        hline(k) = plot(100-X,100*cr(k,:),'.-','Color',colors{k},'MarkerSize',msize);
    end
end

herr = NaN(nbins,length(X));
for k = 1:nbins
    if(any(~isnan(cr(k,:))))
        herr(k,:) = errorlines(100-X,100*cr(k,:),100*cr_sem(k,:),'Color',colors{k});
    end
end

box off;

xlabel('Percent Visible');
ylabel('Performance');
set(gca,'YTick',0:20:100);

xlim([0 50]);

set(gca,'TickDir','out');
set(gca,'TickLength',[0.025 0.025]);
end




function herr = errorlines(x,y,yerror, varargin)
% usage:
% errorlines(X,Y,E,'Color',colors{k});

for i = 1:length(x)
    herr(i) = line('XData',[x(i) x(i)],'YData',[y(i)-yerror(i), y(i)+yerror(i)],varargin{:});
end


end






