% Jon Newman
% Ga Tech

% 15 Apr 2010

% Plots bar graph of e/ipsc rate for different stimulus parameters

% Specify tank and block and plot parameters
% *************************************************************************
dirID = 'C:\TempData\20100409\20100409-1\';
dirdat = dir(strcat([dirID '*.mat']));
% *************************************************************************

h = figure()

legend1 = {}; c1 = 1;
erate = [];
sderate = [];
irate = [];
sdirate = [];
taus = [];

for j = 1:size(dirdat,1)
    fid = dirdat(j).name;
    load(strcat([dirID fid]));
    
    tau = data.stimpara.tau;
    taus = [taus tau];
    amp = data.stimpara.amp;
    
    if isfield(data,'psc')
        if ~isempty(data.psc.coords)
            
            % PSC counts for this parametric condition
            nume = [];
            numi = [];
            for i = 1:size(data.psc.coords,2)
                coords = data.psc.coords{i};
                if ~isempty(coords)
                    nume = [nume size(coords(coords(:,2)<0,:),1)];
                    numi = [numi size(coords(coords(:,2)>0,:),1)];
                end
            end
            
            % Calculate mena PSC rates and SD's
            erate = [erate mean(data.sampfreq*(nume./size(data.intrarec.imon,1)))];
            sderate = [sderate std(data.sampfreq*(nume./size(data.intrarec.imon,1)))];
            
            irate = [irate mean(data.sampfreq*(numi./size(data.intrarec.imon,1)))];
            sdirate = [sdirate std(data.sampfreq*(numi./size(data.intrarec.imon,1)))];
            
            if amp ~= 0
                legend1{c1} = strcat(['T = ' num2str(tau) ', A = '  num2str(amp)]);
                c1 = c1+1;
            else
                legend1{c1} = strcat('No Stimulus');
                c1 = c1+1;
            end
        end
    else
        error('You have not yet found PSCs for these data. Please do this first.');
    end
end

[temp tauind] = sort(taus);

% Plot labels
subplot(121)
hold on
bar(erate(tauind),'k')
h = errorbar(erate(tauind),sderate(tauind));
hChildren = get(h, 'children');
set(hChildren(1), 'visible','off','color','k')
set(hChildren(2), 'Color',[0 0 0],'linewidth',2)
title('EPSC Rate')
ylabel('EPSC/sec')
xticklabelrotate(1:length(legend1),45,legend1(tauind));


subplot(122)
hold on
bar(irate(tauind),'k')
h = errorbar(irate(tauind),sdirate(tauind));
hChildren = get(h, 'children');
set(hChildren(1), 'visible','off','color','k')
set(hChildren(2), 'Color',[0 0 0],'linewidth',2)
xticklabelrotate(1:length(legend1),45,legend1(tauind));
title('IPSC Rate')
ylabel('IPSC/sec')

findfigs

reply = input('Save Figure? [y/n]: ','s');
if isempty(reply)
    reply = 'y';
end

if reply == 'y'
    figdir = strcat([dirID 'AcrossParameters_' '_FIG']);
    figfid = 'PSC_Rate';
    if ~exist(figdir,'dir')
        mkdir(figdir);
    end
    saveas(h, strcat([figdir '\' figfid '.fig']))
    saveas(h, strcat([figdir '\' figfid '.jpg']))
end
