
set(0, 'DefaultLineLineWidth', 1);
set(0,'defaultTextFontSize', 12);
set(0,'defaultAxesFontSize',12);
set(0,'DefaultFigureColormap',jet)
set(0,'defaultfigurecolor',[1 1 1])
set(0,'DefaultAxesTitleFontWeight', 'normal')
set(0,'DefaulttextInterpreter','none')
%%
clear all;clc;

%% Load data
filename = dir('*_PETHSpikes.mat')

Pre_PyN_all = [];
Sti_PyN_all = [];
Post_PyN_all = [];
Peth_all = [];
for ii= 1:length(filename)
    load(filename(ii).name)
    disp(filename(ii).name)

    Pre_PyN  = PETHSpikes.PyN.counts(:,1:5000,:);
    Sti_PyN  = PETHSpikes.PyN.counts(:,7001:10000,:);
    Post_PyN = PETHSpikes.PyN.counts(:,10001:15000,:);
    Peth_all =  [Peth_all,squeeze(mean(PETHSpikes.PyN.counts,1))];
    Pre_PyN  = squeeze(sum(mean(Pre_PyN,1),2))/5;
    Sti_PyN  = squeeze(sum(mean(Sti_PyN,1),2))/3;
    Post_PyN = squeeze(sum(mean(Post_PyN,1),2))/5;
    
    Pre_PyN_all = [Pre_PyN_all, Pre_PyN'];
    Sti_PyN_all= [Sti_PyN_all, Sti_PyN'];
    Post_PyN_all= [Post_PyN_all, Post_PyN'];


end

Pre_PyN_all_ses = Pre_PyN_all;
Sti_PyN_all_ses = Sti_PyN_all;
Post_PyN_all_ses = Post_PyN_all;
%%

binEdges = min([Pre_PyN, Sti_PyN, Post_PyN]):0.4:max([Pre_PyN, Sti_PyN, Post_PyN]);
    
[countsPre, edgesPre] = histcounts(Pre_PyN_all, binEdges, 'Normalization', 'probability');
[countsSti, edgesSti] = histcounts(Sti_PyN_all, binEdges, 'Normalization', 'probability');
[countsPost, edgesPost] = histcounts(Post_PyN_all, binEdges, 'Normalization', 'probability');

figure;
subplot(3,1,1);
plot(edgesPre(1:end-1), countsPre);
title('Pre_PyN');

subplot(3,1,2);
plot(edgesSti(1:end-1), countsSti);
title('Sti_PyN');

subplot(3,1,3);
plot(edgesPost(1:end-1), countsPost);
title('Post_PyN');

%%
figure;

% Plot the histograms with 50% transparency
subplot(3,1,1);histogram(Pre_PyN_all, 'FaceColor', 'r', 'FaceAlpha', 0.5);xlim([-9 13]);ylim([0 600])
subplot(3,1,2);histogram(Sti_PyN_all, 'FaceColor', 'g', 'FaceAlpha', 0.5);xlim([-9 13]);ylim([0 600])
subplot(3,1,3);histogram(Post_PyN_all, 'FaceColor', 'b', 'FaceAlpha', 0.5);xlim([-9 13]);ylim([0 600])

hold off;  % Release the hold

% Add a legend
legend('Pre_PyN', 'Sti_PyN', 'Post_PyN');
box off


%%
run colors_definitions.m
col_map  = brewermap(3,'Set2');
idx = Pre_PyN_all_ses<1;
generatePDF(Pre_PyN_all_ses(idx) ,'color_plot',col_map(1,:),'PlotType','curve','bins',50,'histmax_style','else','normalized',false)
hold on
generatePDF(Sti_PyN_all_ses(idx) ,'color_plot',col_map(2,:),'PlotType','curve','bins',50,'histmax_style','else','normalized',false)
generatePDF(Post_PyN_all_ses(idx) ,'color_plot',col_map(3,:),'PlotType','curve','bins',50,'histmax_style','else','normalized',false)
hold off
legend('Pre-PyN', 'Sti-PyN', 'Post-PyN');
box off
% make yscale as log
set(gca,'Xscale','log')
exportgraphics(gcf,['ACh_act_popDistri.pdf'],'BackgroundColor','none','ContentType','vector');
