clear all; close all;

feature = 'amplitude';
n_cutoff = 50;
x_offset = 0;
savedir = 'C:\Users\mingfaifong\Documents\2012-12-06_TTX-CTZ-vehicle_patch-analysis\amplitude-50events_02\';

% VENUS 20628, 20629
cultureID = {'','venus20628','venus20629'};
culturelabels = {'control','TTX','TTX + CTZ'};
dirID = 'C:\Users\mingfaifong\Documents\2012-04-22_venus20628-venus20629_patch-analysis\';
savename = [savedir 'venus20628-20629_', num2str(n_cutoff),'events_',feature];
% 
% % WORM 19308, 19309
% cultureID = {'','worm19308','worm19309'};
% culturelabels = {'control','TTX','TTX + CTZ'};
% dirID = 'C:\Users\mingfaifong\Documents\2012-05-02_worm19308-worm19309_patch-analysis\';
% savename = [savedir 'worm19308-19309_', num2str(n_cutoff),'events_',feature];
% 
% % XEKO 2, 3, 4
% cultureID = {'xeko2','xeko3','xeko4'};
% culturelabels = {'control','TTX','TTX + CTZ'};
% dirID = 'C:\Users\mingfaifong\Documents\2012-05-14_xeko2-xeko3-xeko4_patch-analysis\';
% savename = [savedir 'xeko2-3-4_', num2str(n_cutoff),'events_',feature];
% 
% % ZENO 7, 10, 9
% cultureID = {'zeno7','zeno10','zeno9'};
% culturelabels = {'control','TTX','TTX + CTZ'};
% dirID = 'C:\Users\mingfaifong\Documents\2012-06-13_zeno7-zeno10-zeno9_patch-analysis\';
% savename = [savedir 'zeno7-10-9_', num2str(n_cutoff),'events_',feature];
% 
% % ANCHOVY 1, 3, 2
% cultureID = {'anchovy1','anchovy3','anchovy2'};
% culturelabels = {'control','TTX','TTX + CTZ'};
% dirID = 'C:\Users\mingfaifong\Documents\2012-06-20_anchovy1-anchovy3-anchovy2_patch-analysis\';
% savename = [savedir 'anchovy1-3-2_', num2str(n_cutoff),'events_',feature];
% 
% % ANCHOVY 5, 4, 6
% cultureID = {'anchovy5','anchovy4','anchovy6'};
% culturelabels = {'control','TTX','TTX + CTZ'};
% dirID = 'C:\Users\mingfaifong\Documents\2012-06-21_anchovy5-anchovy4-anchovy6_patch-analysis\';
% savename = [savedir 'anchovy5-4-6_', num2str(n_cutoff),'events_',feature];


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

% -- setting values for feature of interest -- %
switch feature
    case 'amplitude'
        f_label = 'Amplitude';
        f_unit = 'pA';
        f_datcol = 3;
        f_bounds = [5 400];
        f_cumhist = 1;
    case 'frequency'
        f_label = 'Frequency';
        f_unit = 'Hz';
        f_datcol = 19;
        f_bounds = [1/200 1000];
        f_cumhist = 0;
    case 'decay'
        f_label = 'Decay Time Constant';
        f_unit = 'ms';
        f_datcol = 5;
        f_bounds = [0.2 6]; %for AMPAergic minis
        f_cumhist = 0;
    case 'charge'
        f_label = 'Charge Transfer';
        f_unit = 'fC';
        f_datcol = 6;
        f_bounds = [1 inf];
        f_cumhist = 0;
end

% f1 = figure(1);
% set(f1,'Position',[1 1 1000 700])
% f2 = figure(2);
% set(f2,'Position',[100 200 500 400])
% f3 = figure(3);
% set(f3,'Position',[600 200 500 400])
% 

%% plot by cell, display averages %%

byCell_ticklabels = {''};

if isempty(cultureID{1})
    startval = 2;
else
    startval = 1;
end

for i_cultureID = startval:length(cultureID)
    load([dirID cultureID{i_cultureID} '_alldata.mat']);
    plotdata_all = [];
    plotdata_good = [];
    plotdata_bad = [];
    clearvars byCell* -except byCell_ticklabels
    sampdata = [];
   
    % -- generating data for plotting -- %
    for i_dat = 1:length(dat)
        x = dat{i_dat}(:,f_datcol);
        x = x(x>=f_bounds(1) & x<=f_bounds(2));
        tmp_plotdata = [i_dat*ones(size(x)),x]; %first column is cell number, second column is feature data
        plotdata_all = [plotdata_all; tmp_plotdata];
        if length(tmp_plotdata) >= n_cutoff
            plotdata_good = [plotdata_good; tmp_plotdata];
            byCell_good(i_dat) = 1;
            sampdata = [sampdata; randsample(dat{i_dat}(:,f_datcol),n_cutoff)];
        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;
        byCell_ticklabels{end+1} = i_dat;
    end
    
    % -- getting overall average for this culture -- %
    cell_avg{i_cultureID} = byCell_avg(byCell_good==1);
    culture_avg(i_cultureID) = nanmean(byCell_avg(byCell_good==1));
    cell_sampdata{i_cultureID} = sampdata;

end


% %% culture averages and random sample %%
% 
% byCell_ticklabels = {''};
% 
% for i_cultureID = 1:length(cultureID)
%     load([dirID cultureID{i_cultureID} '_alldata.mat']);
%     clearvars byCell* -except byCell_ticklabels
%     sampdata = [];
%     
%     % -- generating cell averages and randsamp for cumhist -- %
%     for i_dat = 1:length(dat)
%         x = dat{i_dat}(:,f_datcol);
%         x = x(x>=f_bounds(1) & x<=f_bounds(2));
%         if length(x) >= n_cutoff
%             byCell_good(i_dat) = 1;
%             sampdata = [sampdata; randsample(dat{i_dat}(:,f_datcol),n_cutoff)];
%         else
%              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;
%         byCell_ticklabels{end+1} = i_dat;
%     end
%     
%     % -- getting overall average for this culture -- %
%     cell_avg{i_cultureID} = byCell_avg(byCell_good==1);
%     culture_avg(i_cultureID) = nanmean(byCell_avg(byCell_good==1));
%     cell_sampdata{i_cultureID} = sampdata;
% end

%% save data

save(savename,'cell_avg','culture_avg','n_cutoff','cultureID','cell_sampdata');

