generateCellMeans;

clearvars -except avg f* n_cutoff

%compute averages for each treatment condition, where n = cells
avg.byCell.ctrl2.mean = nanmean(avg.byCell.ctrl2.value);
avg.byCell.ctrl2.SE = std(avg.byCell.ctrl2.value)/sqrt(length(avg.byCell.ctrl2.value));
avg.byCell.ctrl2.n = length(avg.byCell.ctrl2.value);

avg.byCell.CNQX.mean = nanmean(avg.byCell.CNQX.value);
avg.byCell.CNQX.SE = std(avg.byCell.CNQX.value)/sqrt(length(avg.byCell.CNQX.value));
avg.byCell.CNQX.n = length(avg.byCell.CNQX.value);

avg.byCell.CNQXstim1x.mean = nanmean(avg.byCell.CNQXstim1x.value);
avg.byCell.CNQXstim1x.SE = std(avg.byCell.CNQXstim1x.value)/sqrt(length(avg.byCell.CNQXstim1x.value));
avg.byCell.CNQXstim1x.n = length(avg.byCell.CNQXstim1x.value);

%compute averages for each culture
avg.byCulture.mean(1,1) = nanmean(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'juju')));
avg.byCulture.SE(1,1) = nanstd(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'juju')))/sqrt(length(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'juju'))));
avg.byCulture.n(1,1) = length(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'juju')));
avg.byCulture.mean(2,1) = nanmean(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'trouble')));
avg.byCulture.SE(2,1) = nanstd(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'trouble')))/sqrt(length(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'trouble'))));
avg.byCulture.n(2,1) = length(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'trouble')));
avg.byCulture.mean(3,1) = nanmean(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'usain')));
avg.byCulture.SE(3,1) = nanstd(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'usain')))/sqrt(length(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'usain'))));
avg.byCulture.n(3,1) = length(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'usain')));
avg.byCulture.mean(4,1) = nanmean(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'wingnut')));
avg.byCulture.SE(4,1) = nanstd(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'wingnut')))/sqrt(length(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'wingnut'))));
avg.byCulture.n(4,1) = length(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'wingnut')));
avg.byCulture.mean(5,1) = nanmean(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'xinhai')));
avg.byCulture.SE(5,1) = nanstd(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'xinhai')))/sqrt(length(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'xinhai'))));
avg.byCulture.n(5,1) = length(avg.byCell.ctrl2.value(strcmp(avg.byCell.ctrl2.cultureSeries,'xinhai')));

avg.byCulture.mean(1,2) = nanmean(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'juju')));
avg.byCulture.SE(1,2) = nanstd(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'juju')))/sqrt(length(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'juju'))));
avg.byCulture.n(1,2) = length(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'juju')));
avg.byCulture.mean(2,2) = nanmean(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'trouble')));
avg.byCulture.SE(2,2) = nanstd(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'trouble')))/sqrt(length(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'trouble'))));
avg.byCulture.n(2,2) = length(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'trouble')));
avg.byCulture.mean(3,2) = nanmean(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'usain')));
avg.byCulture.SE(3,2) = nanstd(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'usain')))/sqrt(length(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'usain'))));
avg.byCulture.n(3,2) = length(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'usain')));
avg.byCulture.mean(4,2) = nanmean(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'wingnut')));
avg.byCulture.SE(4,2) = nanstd(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'wingnut')))/sqrt(length(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'wingnut'))));
avg.byCulture.n(4,2) = length(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'wingnut')));
avg.byCulture.mean(5,2) = nanmean(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'xinhai')));
avg.byCulture.SE(5,2) = nanstd(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'xinhai')))/sqrt(length(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'xinhai'))));
avg.byCulture.n(5,2) = length(avg.byCell.CNQX.value(strcmp(avg.byCell.CNQX.cultureSeries,'xinhai')));

avg.byCulture.mean(1,3) = nanmean(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'juju')));
avg.byCulture.SE(1,3) = nanstd(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'juju')))/sqrt(length(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'juju'))));
avg.byCulture.n(1,3) = length(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'juju')));
avg.byCulture.mean(2,3) = nanmean(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'trouble')));
avg.byCulture.SE(2,3) = nanstd(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'trouble')))/sqrt(length(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'trouble'))));
avg.byCulture.n(2,3) = length(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'trouble')));
avg.byCulture.mean(3,3) = nanmean(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'usain')));
avg.byCulture.SE(3,3) = nanstd(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'usain')))/sqrt(length(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'usain'))));
avg.byCulture.n(3,3) = length(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'usain')));
avg.byCulture.mean(4,3) = nanmean(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'wingnut')));
avg.byCulture.SE(4,3) = nanstd(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'wingnut')))/sqrt(length(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'wingnut'))));
avg.byCulture.n(4,3) = length(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'wingnut')));
avg.byCulture.mean(5,3) = nanmean(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'xinhai')));
avg.byCulture.SE(5,3) = nanstd(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'xinhai')))/sqrt(length(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'xinhai'))));
avg.byCulture.n(5,3) = length(avg.byCell.CNQXstim1x.value(strcmp(avg.byCell.CNQXstim1x.cultureSeries,'xinhai')));

%compute averages for each treatment condition, where n = cultures
avg.byCulture.ctrl2.mean = nanmean(avg.byCulture.mean(:,1));
avg.byCulture.ctrl2.SE = std(avg.byCulture.mean(:,1))/sqrt(length(avg.byCulture.mean(:,1)));
avg.byCulture.ctrl2.n = length(avg.byCulture.mean(:,1));

avg.byCulture.CNQX.mean = nanmean(avg.byCulture.mean(:,2));
avg.byCulture.CNQX.SE = std(avg.byCulture.mean(:,2))/sqrt(length(avg.byCulture.mean(:,2)));
avg.byCulture.CNQX.n = length(avg.byCulture.mean(:,2));

avg.byCulture.CNQXstim1x.mean = nanmean(avg.byCulture.mean(:,3));
avg.byCulture.CNQXstim1x.SE = std(avg.byCulture.mean(:,3))/sqrt(length(avg.byCulture.mean(:,3)));
avg.byCulture.CNQXstim1x.n = length(avg.byCulture.mean(:,3));

%% plots

f1 = figure(1);
set(f1,'Position',[24    39   868   778]);

subplot(2,2,1);
hold off;
bar(1,avg.byCell.ctrl2.mean,'FaceColor','k','LineWidth',1); hold on;
errorbar(1,avg.byCell.ctrl2.mean,avg.byCell.ctrl2.SE,'-k','LineWidth',2);
text(1,avg.byCell.ctrl2.mean-0.5,['n=',num2str(avg.byCell.ctrl2.n)],'HorizontalAlignment','center','VerticalAlignment','top','Color',[1 1 1],'FontSize',14);
bar(2,avg.byCell.CNQX.mean,'FaceColor','k','LineWidth',1); hold on;
errorbar(2,avg.byCell.CNQX.mean,avg.byCell.CNQX.SE,'-k','LineWidth',2);
text(2,avg.byCell.CNQX.mean-0.5,['n=',num2str(avg.byCell.CNQX.n)],'HorizontalAlignment','center','VerticalAlignment','top','Color',[1 1 1],'FontSize',14);
bar(3,avg.byCell.CNQXstim1x.mean,'FaceColor','k','LineWidth',1); hold on;
errorbar(3,avg.byCell.CNQXstim1x.mean,avg.byCell.CNQXstim1x.SE,'-k','LineWidth',2);
text(3,avg.byCell.CNQXstim1x.mean-0.5,['n=',num2str(avg.byCell.CNQXstim1x.n)],'HorizontalAlignment','center','VerticalAlignment','top','Color',[1 1 1],'FontSize',14);

set(gca,'XTick',[1,2,3],'XTickLabel',{'control','CNQX','CNQX+stim'},'FontSize',14);
% ylabel('Frequency (Hz)','FontSize',16)
% % ylabel('Amplitude (pA)','FontSize',16)
xlim([0.25 3.75]);
% ylim([0 4.5]);
box off;
title(['Mean+SEM for each treatment type',10,'(where n is number of cells)']);

%figure(2);
subplot(2,2,2);
hold off;
bar(1,avg.byCulture.ctrl2.mean,'FaceColor','k','LineWidth',1); hold on;
errorbar(1,avg.byCulture.ctrl2.mean,avg.byCulture.ctrl2.SE,'-k','LineWidth',2);
text(1,avg.byCulture.ctrl2.mean-0.5,['n=',num2str(avg.byCulture.ctrl2.n)],'HorizontalAlignment','center','VerticalAlignment','top','Color',[1 1 1],'FontSize',14);
bar(2,avg.byCulture.CNQX.mean,'FaceColor','k','LineWidth',1); hold on;
errorbar(2,avg.byCulture.CNQX.mean,avg.byCulture.CNQX.SE,'-k','LineWidth',2);
text(2,avg.byCulture.CNQX.mean-0.5,['n=',num2str(avg.byCulture.CNQX.n)],'HorizontalAlignment','center','VerticalAlignment','top','Color',[1 1 1],'FontSize',14);
bar(3,avg.byCulture.CNQXstim1x.mean,'FaceColor','k','LineWidth',1); hold on;
errorbar(3,avg.byCulture.CNQXstim1x.mean,avg.byCulture.CNQXstim1x.SE,'-k','LineWidth',2);
text(3,avg.byCulture.CNQXstim1x.mean-0.5,['n=',num2str(avg.byCulture.CNQXstim1x.n)],'HorizontalAlignment','center','VerticalAlignment','top','Color',[1 1 1],'FontSize',14);

set(gca,'XTick',[1,2,3],'XTickLabel',{'control','CNQX','CNQX+stim'},'FontSize',14);
% ylabel('Amplitude (pA)','FontSize',16)
% ylabel('Frequency (Hz)','FontSize',16)
xlim([0.25 3.75]);
% ylim([0 4.5]);
box off;
title(['Mean+SEM for each treatment type',10,'(where n is number of cultures)']);


%figure(3);
subplot(2,2,3);
plotcolors = {[0 0 0],[0 0.2 0.7],[0.9 0 0],[0.1 0.7 0.1],[1 0.5 0],[0.6 0 0.7]}; %black, blue, red, green, orange, purple
x_offset = -0.125:0.05:0.125;
hold off;
for i = 1:5
    plot([1 2 3]+x_offset(i),avg.byCulture.mean(i,:),'o-','LineWidth',2.5,'Color',plotcolors{i}); hold on;
    errorbar([1 2 3]+x_offset(i),avg.byCulture.mean(i,:),avg.byCulture.SE(i,:),'LineWidth',2.5,'Color',plotcolors{i});
end
set(gca,'XTick',[1,2,3],'XTickLabel',{'control','CNQX','CNQX+stim'},'FontSize',14);
% ylabel('Amplitude (pA)','FontSize',16)
% ylabel('Frequency (Hz)','FontSize',16)
xlim([0.25 3.75]);
% ylim([0 7.5]);
box off;
%legend('juju','','trouble','','usain','','wingnut','','xinhai','','Location','NorthWest','FontSize',12);
%legend('juju','trouble','usain','wingnut','xinhai','Location','NorthWest','FontSize',12);
title('Mean+SEM for each culture');

%figure(4);
subplot(2,2,4);
markersize = 15;
x_inc = repmat([1 1.1 0.9 1.2 0.8 1.3 0.7]',20,1);
hold off;
y_tmp = sortrows(avg.byCell.ctrl2.value);
x_tmp = x_inc(1:length(y_tmp));
plot(x_tmp,y_tmp,'.k','MarkerSize',markersize);
hold on;
y_tmp = sortrows(avg.byCell.CNQX.value);
x_tmp = x_inc(1:length(y_tmp));
plot(x_tmp+1,y_tmp,'.k','MarkerSize',markersize);
y_tmp = sortrows(avg.byCell.CNQXstim1x.value);
x_tmp = x_inc(1:length(y_tmp));
plot(x_tmp+2,y_tmp,'.k','MarkerSize',markersize);


set(gca,'XTick',[1,2,3],'XTickLabel',{'control','CNQX','CNQX+stim'},'FontSize',14);
% ylabel('Amplitude (pA)','FontSize',16)
% ylabel('Frequency (Hz)','FontSize',16)
xlim([0.25 3.75]);
% ylim([0 16]);
box off;
title('Mean amplitude for each cell');

%% quick sig test
[h p] = ttest2(avg.byCell.ctrl2.value,avg.byCell.CNQX.value)
[h p] = ttest2(avg.byCell.ctrl2.value,avg.byCell.CNQXstim1x.value)
[h p] = ttest2(avg.byCell.CNQX.value,avg.byCell.CNQXstim1x.value)

[h p] = ttest2(avg.byCulture.mean(:,1),avg.byCulture.mean(:,2))
[h p] = ttest2(avg.byCulture.mean(:,1),avg.byCulture.mean(:,3))
[h p] = ttest2(avg.byCulture.mean(:,2),avg.byCulture.mean(:,3))

%% real stats
stats.treatment = [repmat({'control'},[avg.byCell.ctrl2.n,1]); repmat({'CNQX'},[avg.byCell.CNQX.n,1]); repmat({'CNQX+stim'},[avg.byCell.CNQXstim1x.n,1])];
stats.cellMeans = [avg.byCell.ctrl2.value;avg.byCell.CNQX.value;avg.byCell.CNQXstim1x.value];
[p a s] = anova1(stats.cellMeans,stats.treatment);
[c m h nms] = multcompare(s,'ctype','bonferroni');

[p a s] = anova1(avg.byCulture.mean,{'ctrl2','CNQX','CNQX+stim'});
[c m h nms] = multcompare(s,'ctype','bonferroni');

%% no correction
stats.treatment = [repmat({'control'},[avg.byCell.ctrl2.n,1]); repmat({'CNQX'},[avg.byCell.CNQX.n,1]); repmat({'CNQX+stim'},[avg.byCell.CNQXstim1x.n,1])];
stats.cellMeans = [avg.byCell.ctrl2.value;avg.byCell.CNQX.value;avg.byCell.CNQXstim1x.value];
[p a s] = anova1(stats.cellMeans,stats.treatment);
[c m h nms] = multcompare(s);

[p a s] = anova1(avg.byCulture.mean,{'ctrl2','CNQX','CNQX+stim'});
[c m h nms] = multcompare(s);


%% cumhists & rankorder

generateRandSamp;
samp.ctrl2 = reshape(sample.mEPSC.ctrl2,1,size(sample.mEPSC.ctrl2,1)*size(sample.mEPSC.ctrl2,2));
samp.CNQX = reshape(sample.mEPSC.CNQX,1,size(sample.mEPSC.CNQX,1)*size(sample.mEPSC.CNQX,2));
samp.CNQXstim1x = reshape(sample.mEPSC.CNQXstim1x,1,size(sample.mEPSC.CNQXstim1x,1)*size(sample.mEPSC.CNQXstim1x,2));

maxmini = max([samp.ctrl2, samp.CNQX, samp.CNQXstim1x]);

dt = 1; %pA
bin = 0:dt:ceil(maxmini/dt)*dt;
histdata.ctrl2 = histc(samp.ctrl2,bin);
histdata.CNQX = histc(samp.CNQX,bin);
histdata.CNQXstim1x = histc(samp.CNQXstim1x,bin);

cumhistdata.ctrl2 = cumsum(histdata.ctrl2);
cumhistdata.CNQX = cumsum(histdata.CNQX);
cumhistdata.CNQXstim1x = cumsum(histdata.CNQXstim1x);

n_resize = min([length(samp.ctrl2),length(samp.CNQX),length(samp.CNQXstim1x)]);
rankdata.ctrl2 = sort(randsample(samp.ctrl2,n_resize));
rankdata.CNQX = sort(randsample(samp.CNQX,n_resize));
rankdata.CNQXstim1x = sort(randsample(samp.CNQXstim1x,n_resize));
stats.ctrl2_CNQX = regstats(rankdata.CNQX,rankdata.ctrl2);
stats.CNQX_CNQXstim1x = regstats(rankdata.CNQXstim1x,rankdata.CNQX);
stats.ctrl2_CNQXstim1x = regstats(rankdata.CNQXstim1x,rankdata.ctrl2);

plotcolors = {[0 0 0],[0 0.2 0.7],[0.9 0 0],[0.1 0.7 0.1],[1 0.5 0],[0.6 0 0.7]}; %black, blue, red, green, orange, purple

% figure;
subplot(2,3,[1 2]);
hold off;
plot(bin+0.5,smooth(histdata.ctrl2,3),'-','LineWidth',3,'Color',plotcolors{1});
hold on;
plot(bin+0.5,smooth(histdata.CNQX,3),'-','LineWidth',3,'Color',plotcolors{2});
plot(bin+0.5,smooth(histdata.CNQXstim1x,3),'-','LineWidth',3,'Color',plotcolors{3});
legend('control','CNQX','CNQX+stim','Location','NorthEast','FontSize',12);
xlim([0 50]);
ylim([0 1.02*max([histdata.ctrl2 histdata.CNQX histdata.CNQXstim1x])]);
set(gca,'XTick',[0:5:50],'FontSize',14);
xlabel('Amplitude (pA)','FontSize',16)
ylabel('Counts');
box off;

subplot(2,3,3);
hold off;
plot(bin,cumhistdata.ctrl2/max(cumhistdata.ctrl2),'-','LineWidth',3,'Color',plotcolors{1});
hold on;
plot(bin,cumhistdata.CNQX/max(cumhistdata.CNQX),'-','LineWidth',3,'Color',plotcolors{2});
plot(bin,cumhistdata.CNQXstim1x/max(cumhistdata.CNQXstim1x),'-','LineWidth',3,'Color',plotcolors{3});
legend('control','CNQX','CNQX+stim','Location','SouthEast','FontSize',12);
set(gca,'YTick',[0:0.2:100],'XTick',[0:10:50],'FontSize',14);
xlabel('Amplitude (pA)','FontSize',16)
ylabel('Fraction of Events');
box off;
xlim([0 50]);
ylim([0 1.02]);
axis square;


subplot(2,3,4);
hold off;
plot(rankdata.ctrl2,rankdata.CNQX,'ko');
hold on;
plot([0 maxmini],[0 maxmini],'--','LineWidth',1,'Color',[0.5 0.5 0.5]);
plot([0 maxmini],[0+stats.ctrl2_CNQX.beta(1) maxmini*stats.ctrl2_CNQX.beta(2)+stats.ctrl2_CNQX.beta(1)],'--','LineWidth',1,'Color',[1 0 0]);
text(maxmini*0.98,maxmini*0.02,['R^2=',num2str(stats.ctrl2_CNQX.rsquare)],'Color',[1 0 0],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment','right');
%axis([0 maxmini 0 maxmini]);
axis([0 100 0 100]);
set(gca,'XTick',[0:50:200],'YTick',[0:50:200],'FontSize',14);
title('Ranked Amplitude (pA)');
xlabel('Control','FontSize',16)
ylabel('CNQX','FontSize',16)
box off;
axis square;

subplot(2,3,5);
hold off;
plot(rankdata.CNQX,rankdata.CNQXstim1x,'ko');
hold on;
plot([0 maxmini],[0 maxmini],'--','LineWidth',1,'Color',[0.5 0.5 0.5]);
plot([0 maxmini],[0+stats.CNQX_CNQXstim1x.beta(1) maxmini*stats.CNQX_CNQXstim1x.beta(2)+stats.CNQX_CNQXstim1x.beta(1)],'--','LineWidth',1,'Color',[1 0 0]);
text(maxmini*0.98,maxmini*0.02,['R^2=',num2str(stats.CNQX_CNQXstim1x.rsquare)],'Color',[1 0 0],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment','right');
axis([0 maxmini 0 maxmini]);
axis([0 100 0 100]);
set(gca,'XTick',[0:50:200],'YTick',[0:50:200],'FontSize',14);
title('Ranked Amplitude (pA)');
xlabel('CNQX','FontSize',16)
ylabel('CNQX+stim','FontSize',16)
box off;
axis square;

subplot(2,3,6);
hold off;
plot(rankdata.ctrl2,rankdata.CNQXstim1x,'ko');
hold on;
plot([0 maxmini],[0 maxmini],'--','LineWidth',1,'Color',[0.5 0.5 0.5]);
plot([0 maxmini],[0+stats.ctrl2_CNQXstim1x.beta(1) maxmini*stats.ctrl2_CNQXstim1x.beta(2)+stats.ctrl2_CNQXstim1x.beta(1)],'--','LineWidth',1,'Color',[1 0 0]);
text(maxmini*0.98,maxmini*0.02,['R^2=',num2str(stats.ctrl2_CNQXstim1x.rsquare)],'Color',[1 0 0],'FontSize',12,'VerticalAlignment','bottom','HorizontalAlignment','right');
axis([0 maxmini 0 maxmini]);
axis([0 100 0 100]);
set(gca,'XTick',[0:50:200],'YTick',[0:50:200],'FontSize',14);
title('Ranked Amplitude (pA)');
xlabel('Control','FontSize',16)
ylabel('CNQX+stim','FontSize',16)
box off;
axis square;



%% test scaling (traditional)
plotcolors = {[0 0 0],[0 0.2 0.7],[0.9 0 0],[0.1 0.7 0.1],[1 0.5 0],[0.6 0 0.7]}; %black, blue, red, green, orange, purple

%rankdata.scaledCNQX = (rankdata.CNQX-stats.ctrl2_CNQX.beta(1))/stats.ctrl2_CNQX.beta(2);
rankdata.scaledCNQX = rankdata.CNQX/stats.ctrl2_CNQX.beta(2);
figure(3);
plot(rankdata.ctrl2,rankdata.CNQX,'ko');
hold on;
plot(rankdata.ctrl2,rankdata.scaledCNQX,'ro');


%samples.scaledCNQX = reshape((CNQX.sample.mEPSC-stats.ctrl2_CNQX.beta(1))/stats.ctrl2_CNQX.beta(2),1,size(CNQX.sample.mEPSC,1)*size(CNQX.sample.mEPSC,2));
samp.scaledCNQX = reshape((sample.mEPSC.CNQX-stats.ctrl2_CNQX.beta(1))/stats.ctrl2_CNQX.beta(2),1,size(sample.mEPSC.CNQX,1)*size(sample.mEPSC.CNQX,2));
%samples.scaledCNQX = reshape(CNQX.sample.mEPSC/stats.ctrl2_CNQX.beta(2),1,size(CNQX.sample.mEPSC,1)*size(CNQX.sample.mEPSC,2));
samp.scaledCNQXstim1x = reshape((sample.mEPSC.CNQXstim1x-stats.ctrl2_CNQXstim1x.beta(1))/stats.ctrl2_CNQXstim1x.beta(2),1,size(sample.mEPSC.CNQXstim1x,1)*size(sample.mEPSC.CNQXstim1x,2));
%samples.CNQXstim1x = reshape(CNQXstim1x.sample.mEPSC,1,size(CNQXstim1x.sample.mEPSC,1)*size(CNQXstim1x.sample.mEPSC,2));
%maxmini = max([samples.ctrl2, samples.CNQX, samples.CNQXstim1x]);

dt = 1; %pA
bin = 0:dt:ceil(maxmini/dt)*dt;
histdata.scaledCNQX = histc(samp.scaledCNQX,bin);
histdata.scaledCNQXstim1x = histc(samp.scaledCNQXstim1x,bin);

cumhistdata.scaledCNQX = cumsum(histdata.scaledCNQX);
cumhistdata.scaledCNQXstim1x = cumsum(histdata.scaledCNQXstim1x);


figure(4);
hold off;
plot(bin,cumhistdata.ctrl2/max(cumhistdata.ctrl2),'-','LineWidth',3,'Color',plotcolors{1});
hold on;
plot(bin,cumhistdata.scaledCNQX/max(cumhistdata.scaledCNQX),'-','LineWidth',3,'Color',plotcolors{2});
plot(bin,cumhistdata.scaledCNQXstim1x/max(cumhistdata.scaledCNQXstim1x),'-','LineWidth',3,'Color',plotcolors{3});
%legend('control','CNQX','CNQX+stim','Location','SouthEast','FontSize',12);
%set(gca,'YTick',[0:0.2:100],'XTick',[0:10:50],'FontSize',14);
xlabel('Amplitude (pA)','FontSize',16)
ylabel('Fraction of Events');
box off;
xlim([0 50]);
ylim([0 1.02]);
axis square;
