function [G1, G1c] = plotBehaviorSummary(DATA, LOC)

%% Plot Performance curves
FF  = fieldnames(DATA);

lcol = lines(7);
cmap = table();
cmap.OptoPDGrp = ["0"; "0.02"; "0.3"; "0.6"; "1.2"];
cmap.cmap      = [0 0 0; lcol([5 1 2 4],:)];

G1 = SubaxisGrid(4, numel(FF), 'mb', 0, 'mt', 0.1, 'sh', 0.08);
G1.figtitle(0.08, sprintf('Optostim Behavioral Summary: Gau @ %s', LOC));
G1.coltitles(0.08, FF, 'FontWeight','bold');
% G1.rowtitles(0.13, {'Hits','Correct Rejections','PCMax','Threshold (boot.sd)','Criterion (boot.sd)'}, ...
%   'FontWeight', 'bold');

xx = 0:100;
for ff = 1:numel(FF)
  uPD = DATA.(FF{ff}).(LOC).uPD;
  uPD = innerjoin(uPD,cmap,'Keys','OptoPDGrp','LeftVariables',{'OptoPDGrp'});
  pdlabel = arrayfun(@(x) ['PD',char(x)], uPD.OptoPDGrp', 'UniformOutput', false);
  
  maxN = arrayfun(@(x) x.nhitmiss(:), DATA.(FF{ff}).(LOC).SUMMARY.Behavior_L0.ys, 'UniformOutput', false);
  maxN = max(cat(1,maxN{:}));
  G1.subaxis(1,ff);
    hold on
      dat = DATA.(FF{ff}).(LOC).SUMMARY.Behavior_L0;
%       plot(dat.xs,100*dat.ys(1).hit,'.','Color','k','MarkerSize',15);
      scatter(dat.xs(:)/2,100*dat.ys(1).hit(:),500*sqrt((dat.ys(1).nhitmiss(:)+1)/maxN), ...
        repmat([0 0 0],numel(dat.xs),1),'.');
      plot(xx/2,100*dat.fit_hitmiss{1}(xx),'-','Color','k','LineWidth',1);
      for ii = 1:height(uPD)
%         plot(dat.xs,100*dat.ys(ii+1).hit,'.','Color',uPD.cmap(ii,:),'MarkerSize',15);
        scatter(dat.xs(:)/2,100*dat.ys(ii+1).hit(:),500*sqrt((dat.ys(ii+1).nhitmiss(:)+1)/maxN), ...
          repmat(uPD.cmap(ii,:),numel(dat.xs),1),'.');
        plot(xx/2,100*dat.fit_hitmiss{ii+1}(xx),'-','Color',uPD.cmap(ii,:),'LineWidth',1);
      end      
    hold off
%     grid on
    axis([0 height(uPD)+2 0 10]);
    set(gca,'XTick',1:height(uPD)+1,'XTickLabel',['Vis',pdlabel],'XTickLabelRotation',45);
    set(gca,'YTick',[0 5 10]);
    if ff == 1
      ylabel('threshold (%)');
    end      
    hold off
%     grid on
    axis([0 30 35 105]);
    set(gca,'XTick',[0 10 20 30],'YTick',[40 70 100]);
%     xlabel('Gaussian Amp (%)');
    if (ff == 1)
      ylabel('%hit');
    end
    
  maxN = arrayfun(@(x) x.ncrfa(:), DATA.(FF{ff}).(LOC).SUMMARY.Behavior_L0.ys, 'UniformOutput', false);
  maxN = max(cat(1,maxN{:}));
  G1.subaxis(2,ff);
    hold on
      dat = DATA.(FF{ff}).(LOC).SUMMARY.Behavior_L0;
%       plot(dat.xs,100*dat.ys(1).cr,'.','Color','k','MarkerSize',15);
      scatter(dat.xs(:)/2,100*dat.ys(1).cr(:),500*sqrt((dat.ys(1).ncrfa(:)+1)/maxN), ...
        repmat([0 0 0],numel(dat.xs),1),'.');
      plot(xx/2,100*dat.fit_crfa{1}(xx),'-','Color','k','LineWidth',1);
      for ii = 1:height(uPD)
%         plot(dat.xs,100*dat.ys(ii+1).cr,'.','Color',uPD.cmap(ii,:),'MarkerSize',15);
        scatter(dat.xs(:)/2,100*dat.ys(ii+1).cr(:),500*sqrt((dat.ys(ii+1).ncrfa(:)+1)/maxN), ...
          repmat(uPD.cmap(ii,:),numel(dat.xs),1),'.');
        plot(xx/2,100*dat.fit_crfa{ii+1}(xx),'-','Color',uPD.cmap(ii,:),'LineWidth',1);
      end
    hold off
%     grid on
    axis([0 30 35 105]);
    set(gca,'XTick',[0 10 20 30],'YTick',[40 70 100]);
%     xlabel('G.Amp');
    if ff == 1
      ylabel('%cr');
    end
  
  maxN = arrayfun(@(x) x.nsuccess(:), DATA.(FF{ff}).(LOC).SUMMARY.Behavior_L0.ys, 'UniformOutput', false);
  maxN = max(cat(1,maxN{:}));
  G1.subaxis(3,ff);
    hold on

      % threshold
      barh(40+3*height(uPD),dat.Threshold_unbiased(1)/2,3,'FaceColor','w');
      for ii = 1:height(uPD)
        barh(40+3*(height(uPD)-ii),dat.Threshold_unbiased(ii+1)/2,3,'FaceColor',uPD.cmap(ii,:));
      end
                
      dat = DATA.(FF{ff}).(LOC).SUMMARY.Behavior_L0;
      pcmax = normcdf(dat.ys(1).dprime/2);
%       plot(dat.xs,100*pcmax,'.','Color','k','MarkerSize',15);
      scatter(dat.xs(:)/2,100*pcmax(:),500*sqrt((dat.ys(1).nsuccess(:)+1)/maxN), ...
        repmat([0 0 0],numel(dat.xs),1),'.');
      plot(xx/2,100*dat.fit_unbiased{1}(xx),'-','Color','k','LineWidth',1);
      for ii = 1:height(uPD)
        pcmax = normcdf(dat.ys(ii+1).dprime/2);
%         plot(dat.xs,100*pcmax,'.','Color',uPD.cmap(ii,:),'MarkerSize',15);
        scatter(dat.xs(:)/2,100*pcmax(:),500*sqrt((dat.ys(ii+1).nsuccess(:)+1)/maxN), ...
          repmat(uPD.cmap(ii,:),numel(dat.xs),1),'.');
        plot(xx/2,100*dat.fit_unbiased{ii+1}(xx),'-','Color',uPD.cmap(ii,:),'LineWidth',1);
      end
      
    hold off
%     grid on
    axis([0 30 35 105]);
    set(gca,'XTick',[0 10 20 30],'YTick',[40 70 100]);
    xlabel('target contrast (%)');
    if ff == 1
      ylabel('%correct (bias adj.)');
    end

  G1.subaxis(4,ff);
    hold on
      plot(nan, nan, '.-', 'MarkerSize', 20, 'LineWidth', 2, 'Color', 'k');
      for ii = 1:height(uPD)
        plot(nan, nan, '.-', 'MarkerSize', 20, 'LineWidth', 2, 'Color', uPD.cmap(ii,:));
      end      
    hold off
    axis off
    ncount = arrayfun(@(x) sum(x.nsuccess(:)), DATA.(FF{ff}).(LOC).SUMMARY.Behavior_L0.ys, 'UniformOutput', false);
    lab2   = cellfun(@(x,y) sprintf('%s (n = %d)',x,y), ['Vis',pdlabel], ncount', 'UniformOutput', false);
    legend(lab2,'Location','North')    
    
end


%% Plot thresholds, criterion and slope

combo  = [1 2; 1 3; 1 4];

% % % G1b = SubaxisGrid(1, 2*numel(FF), 'mt', 0.2, 'mb', 0.2, 'sh', 0.05);
% % % G1b.figtitle(0.1, sprintf('Optostim Behavioral Summary: Gau @ %s', LOC));
% % % G1b.coltitles(0.1, [FF,FF], 'FontWeight','bold');
% % % % G1.rowtitles(0.13, {'Hits','Correct Rejections','PCMax','Threshold (boot.sd)','Criterion (boot.sd)'}, ...
% % % %   'FontWeight', 'bold');
% % % 
% % % for ff = 1:numel(FF)
% % %   uPD = DATA.(FF{ff}).(LOC).uPD;
% % %   uPD = innerjoin(uPD,cmap,'Keys','OptoPDGrp','LeftVariables',{'OptoPDGrp'});
% % %   pdlabel = arrayfun(@(x) ['PD',char(x)], uPD.OptoPDGrp', 'UniformOutput', false);
% % %   
% % %   G1b.subaxis(1,ff);
% % %     hold on
% % %       dat = DATA.(FF{ff}).(LOC).SUMMARY.Behavior_L0;
% % %       bar(1,dat.Threshold_unbiased(1)/2,0.7,'FaceColor','w');
% % %       for ii = 1:height(uPD)
% % %         bar(ii+1,dat.Threshold_unbiased(ii+1)/2,0.7,'FaceColor',uPD.cmap(ii,:));
% % %       end
% % %       err = squeeze(dat.fit_sigdec.mcX(:,1,:)) - dat.Threshold_unbiased';
% % %       err = sqrt(sum(err.^2,1)/(size(err,1)-1));
% % %       errorbar( ...
% % %         1:height(uPD)+1, dat.Threshold_unbiased'/2, ...
% % %         err/2, err/2, 'k', 'LineStyle', 'none', 'CapSize', 0 ...
% % %       );
% % %       maxy = max(dat.Threshold_unbiased' + err)/2;
% % %       for ii = 1:size(combo,1)
% % %         if combo(ii,2) <= height(uPD)+1
% % %           dd = dat.fit_sigdec.mcX(:,1,combo(ii,2)) ...
% % %              - dat.fit_sigdec.mcX(:,1,combo(ii,1));
% % %           pp = sum(dd<=0) / numel(dd);
% % %           if combo(ii,1) == 1 && pp < 0.01
% % %             sigline([combo(ii,1) combo(ii,2)],'**', gca, maxy);
% % %             maxy = maxy + 1;
% % %           end
% % %         end
% % %       end    
% % %     hold off
% % % %     grid on
% % %     axis([0 height(uPD)+2 0 10]);
% % %     set(gca,'XTick',1:height(uPD)+1,'XTickLabel',['Vis',pdlabel],'XTickLabelRotation',45);
% % %     set(gca,'YTick',[0 5 10]);
% % %     if ff == 1
% % %       ylabel('threshold (%)');
% % %     end
% % %   
% % %   G1b.subaxis(1,ff+numel(FF));
% % %     hold on
% % %       dat = DATA.(FF{ff}).(LOC).SUMMARY.Behavior_L0;
% % %       bar(1, dat.Criterion_unbiased(1), 0.7, 'FaceColor', 'w');
% % %       for ii = 1:height(uPD)
% % %         bar(ii+1,dat.Criterion_unbiased(ii+1), 0.7, 'FaceColor', uPD.cmap(ii,:));
% % %       end
% % %       err = squeeze(dat.fit_sigdec.mcX(:,4,:)) - dat.Criterion_unbiased';
% % %       err = sqrt(sum(err.^2,1)/(size(err,1)-1));
% % %       errorbar( 1:height(uPD)+1,dat.Criterion_unbiased', ...
% % %         err, err, 'k', 'LineStyle', 'none', 'CapSize', 0  ...
% % %       );
% % %       maxy = max([0.1 dat.Criterion_unbiased' + err]) + 0.1;
% % %       for ii = 1:size(combo,1)
% % %         if combo(ii,2) <= height(uPD)+1          
% % %           dd = dat.fit_sigdec.mcX(:,4,combo(ii,2)) ...
% % %              - dat.fit_sigdec.mcX(:,4,combo(ii,1));
% % %           pp1 = sum(dd<=0) / numel(dd);
% % %           pp2 = sum(dd>=0) / numel(dd);
% % %           pp  = min(pp1,pp2);
% % %           if combo(ii,1) == 1 && pp < 0.01
% % %             sigline([combo(ii,1) combo(ii,2)],'**', gca, maxy);
% % %             maxy = maxy + 0.3;
% % %           end
% % %         end
% % %       end
% % %     hold off
% % % %     grid on
% % %     axis([0 height(uPD)+2 -1 2.5]);
% % %     set(gca,'XTick',1:height(uPD)+1,'XTickLabel',['Vis',pdlabel],'XTickLabelRotation',45);
% % %     set(gca,'YTick',[-1 0 1 2]);
% % %     if ff == 1
% % %       ylabel('criterion (d'')');
% % %     end
% % %         
% % % end


G1c = SubaxisGrid(1, 2*numel(FF), 'mt', 0.2, 'mb', 0.2, 'sh', 0.05);
G1c.figtitle(0.1, sprintf('Optostim Behavioral Summary: Gau @ %s', LOC));
G1c.coltitles(0.1, [FF,FF], 'FontWeight','bold');
% G1.rowtitles(0.13, {'Hits','Correct Rejections','PCMax','Threshold (boot.sd)','Criterion (boot.sd)'}, ...
%   'FontWeight', 'bold');

for ff = 1:numel(FF)
  uPD = DATA.(FF{ff}).(LOC).uPD;
  uPD = innerjoin(uPD,cmap,'Keys','OptoPDGrp','LeftVariables',{'OptoPDGrp'});
  pdlabel = arrayfun(@(x) ['PD',char(x)], uPD.OptoPDGrp', 'UniformOutput', false);
  
  G1c.subaxis(1,ff);
    hold on
      dat = DATA.(FF{ff}).(LOC).SUMMARY.Behavior_L0;
      vth = dat.Threshold_unbiased(1)/2/ 100;
      bar(1,dat.Threshold_unbiased(1)/2 / vth,0.7,'FaceColor','w');
      for ii = 1:height(uPD)
        bar(ii+1,dat.Threshold_unbiased(ii+1)/2 / vth,0.7,'FaceColor',uPD.cmap(ii,:));
      end
      err = squeeze(dat.fit_sigdec.mcX(:,1,:)) - dat.Threshold_unbiased';
      err = sqrt(sum(err.^2,1)/(size(err,1)-1));
      errorbar( ...
        1:height(uPD)+1, dat.Threshold_unbiased'/2 / vth, ...
        err/2/vth, err/2/vth, 'k', 'LineStyle', 'none', 'CapSize', 0 ...
      );
      maxy = max(dat.Threshold_unbiased' + err)/2 / vth - 10;
      for ii = 1:size(combo,1)
        if combo(ii,2) <= height(uPD)+1
          dd = dat.fit_sigdec.mcX(:,1,combo(ii,2)) ...
             - dat.fit_sigdec.mcX(:,1,combo(ii,1));
          pp = sum(dd<=0) / numel(dd);
          if combo(ii,1) == 1 && pp < 0.01
            sigline([combo(ii,1) combo(ii,2)],'**', maxy);
            maxy = maxy + 20;
          end
        end
      end    
    hold off
%     grid on
    axis([0 height(uPD)+2 0 400]);
    set(gca,'XTick',1:height(uPD)+1,'XTickLabel',['Vis',pdlabel],'XTickLabelRotation',45);
    set(gca,'YTick',[0 100 200 300 400]);
    if ff == 1
      ylabel('norm threshold (%vis)');
    end
  
  G1c.subaxis(1,ff+numel(FF));
    hold on
      dat = DATA.(FF{ff}).(LOC).SUMMARY.Behavior_L0;
      bar(1, dat.Criterion_unbiased(1), 0.7, 'FaceColor', 'w');
      for ii = 1:height(uPD)
        bar(ii+1,dat.Criterion_unbiased(ii+1), 0.7, 'FaceColor', uPD.cmap(ii,:));
      end
      err = squeeze(dat.fit_sigdec.mcX(:,4,:)) - dat.Criterion_unbiased';
      err = sqrt(sum(err.^2,1)/(size(err,1)-1));
      errorbar( 1:height(uPD)+1,dat.Criterion_unbiased', ...
        err, err, 'k', 'LineStyle', 'none', 'CapSize', 0  ...
      );
      maxy = max([0.1 dat.Criterion_unbiased' + err]) + 0.1;
      for ii = 1:size(combo,1)
        if combo(ii,2) <= height(uPD)+1          
          dd = dat.fit_sigdec.mcX(:,4,combo(ii,2)) ...
             - dat.fit_sigdec.mcX(:,4,combo(ii,1));
          pp1 = sum(dd<=0) / numel(dd);
          pp2 = sum(dd>=0) / numel(dd);
          pp  = min(pp1,pp2);
          if combo(ii,1) == 1 && pp < 0.01
            sigline([combo(ii,1) combo(ii,2)],'**', gca, maxy);
            maxy = maxy + 0.3;
          end
        end
      end
    hold off
%     grid on
    axis([0 height(uPD)+2 -1 2.5]);
    set(gca,'XTick',1:height(uPD)+1,'XTickLabel',['Vis',pdlabel],'XTickLabelRotation',45);
    set(gca,'YTick',[-1 0 1 2]);
    if ff == 1
      ylabel('criterion (d'')');
    end
        
end
