%% plot amplitudes

clear all; close all;

cultureID = {'xeko1','xeko20629','xeko20628'};
culturelabels = {'control','20uM CNQX (24hr)','20uM CNQX + ChR2 stim (24hr)'};
plotcolors = {[0 0 0],[0 0.4 0.7],[0.9 0 0]};

dat_col = 3; %amplitude
% dat_col = 5; %decay
% dat_col = 6; %charge
% dat_col = 19; %frequency

n_cutoff = 20;
x_offset = 0;

f1 = figure(1);
set(f1,'Position',[1 1 1000 700])
f2 = figure(2);
set(f2,'Position',[1 1 1000 400])

%% plot by cell %%

for i_cultureID = 1:length(cultureID)
    load(['C:\Documents and Settings\mingfaifong\My Documents\MiniAnalysis Data\for_plotting\' cultureID{i_cultureID} '_alldata.mat']);
    plotdata_all = [];
    plotdata_good = [];
    plotdata_bad = [];
    clear byCell*
    
    % -- generating data for plotting -- %
    for i_dat = 1:length(dat)
        tmp_plotdata = [i_dat*ones(size(dat{i_dat}(:,dat_col))),dat{i_dat}(:,dat_col)];
        plotdata_all = [plotdata_all; tmp_plotdata];
        if length(tmp_plotdata) >= n_cutoff
            plotdata_good = [plotdata_good; tmp_plotdata];
            byCell_good(i_dat) = 1;
        else
            plotdata_bad = [plotdata_bad; tmp_plotdata];
            byCell_good(i_dat) = 0;
        end
        byCell_avg(i_dat) = nanmean(tmp_plotdata(:,2));
        byCell_n(i_dat) = length(tmp_plotdata(:,2));
        byCell_nanstderr(i_dat) = nanstd(tmp_plotdata(:,2))/sqrt(byCell_n(i_dat));
        byCell_cellID(i_dat) = i_dat;
    end

    % -- displaying cell and overall averages -- %
    disp([10,culturelabels{i_cultureID},10,'================================'])
    for i_avg = 1:length(byCell_avg)
        disp(['cell ' num2str(i_avg) ': ' num2str(byCell_avg(i_avg)) ' +/- ' num2str(byCell_nanstderr(i_avg)) ' (n=' num2str(byCell_n(i_avg)) ')']);
    end
    byCulture_avg_all(i_cultureID) = nanmean(byCell_avg);
    byCulture_avg_good(i_cultureID) = nanmean(byCell_avg(byCell_good==1));
    disp(['overall (for cells with at least ' num2str(n_cutoff) ' events): ' num2str(byCulture_avg_good(i_cultureID)),10]);

    % -- plotting data -- %
    figure(1); subplot(2,1,1); hold on;
    plot(x_offset + plotdata_good(:,1)+0.15*randn(size(plotdata_good(:,1))),plotdata_good(:,2),'.','Color',plotcolors{i_cultureID},'MarkerSize',5);
    if ~isempty(plotdata_bad)
        plot(x_offset + plotdata_bad(:,1)+0.15*randn(size(plotdata_bad(:,1))),plotdata_bad(:,2),'.','Color',(plotcolors{i_cultureID}+[1 1 1])/2,'MarkerSize',5);
    end
    
    figure(1); subplot(2,1,2); hold on;
    errorbar(x_offset + byCell_cellID(byCell_good==1),byCell_avg(byCell_good==1),byCell_nanstderr(byCell_good==1),'o','Color',plotcolors{i_cultureID},'LineWidth',2);
    if sum(byCell_good) ~= length(byCell_good)
        errorbar(x_offset + byCell_cellID(byCell_good==0),byCell_avg(byCell_good==0),byCell_nanstderr(byCell_good==0),'o','Color',(plotcolors{i_cultureID}+[1 1 1])/2,'LineWidth',2);
    end
    
    x_offset = 1 + x_offset + max(plotdata_all(:,1));
end

% -- tweet plots -- %
figure(1); subplot(2,1,1)
xlim([0 x_offset]);
ylabel('Amplitude (pA)','FontSize',14);

figure(1); subplot(2,1,2);
xlim([0 x_offset]);
ylabel('Amplitude (pA)','FontSize',14);
xlabel('Cell Number','FontSize',14);

for i_plotmeans = 1:length(cultureID)
    figure(1); subplot(2,1,1)
    plot([0.5 x_offset-0.5],[byCulture_avg_good(i_plotmeans) byCulture_avg_good(i_plotmeans)],'--','Color',plotcolors{i_plotmeans});
    figure(1); subplot(2,1,2);
    plot([0.5 x_offset-0.5],[byCulture_avg_good(i_plotmeans) byCulture_avg_good(i_plotmeans)],'--','Color',plotcolors{i_plotmeans});
end

%% plot by culture %%

for i_cultureID = 1:length(cultureID)
    load(['C:\Documents and Settings\mingfaifong\My Documents\MiniAnalysis Data\for_plotting\' cultureID{i_cultureID} '_alldata.mat']);
    sampdata = [];
    clear byCulture*
    
    % -- generating sample data -- %
    for i_dat = 1:length(dat)
        if length(dat{i_dat}) >= n_cutoff
            sampdata = [sampdata; randsample(dat{i_dat}(:,dat_col),n_cutoff)];
        end
    end
    
    bin = 5:1:150;
    byCulture_nhist = histc(sampdata,bin);
    byCulture_cumhist = cumsum(byCulture_nhist);
    byCulture_avg = nanmean(sampdata);
    byCulture_n = length(sampdata);
    byCulture_stderr = nanstd(sampdata)/sqrt(length(sampdata));
    
figure(2); subplot(1,2,1); hold on;
bar(i_cultureID,byCulture_avg,'FaceColor',plotcolors{i_cultureID},'LineWidth',1)
errorbar(i_cultureID,byCulture_avg,byCulture_stderr,'-k','LineWidth',2);
figure(2); subplot(1,2,2); hold on;
plot(1:length(byCulture_cumhist),byCulture_cumhist/max(byCulture_cumhist),'Color',plotcolors{i_cultureID},'LineWidth',3);

end

figure(2); subplot(1,2,1);
set(gca,'Xtick',[1 2 3],'XTickLabel',{'control','CNQX','CNQX+stim'});
ylabel('mEPSC amplitude (pA)','FontSize',14);

figure(2); subplot(1,2,2);
xlim([0 50]);
ylabel('Cumulative Probability','FontSize',14);
xlabel('mEPSC amplitude (pA)','FontSize',14);
legend(culturelabels,'Location','SouthEast')

