
%% compare and make figures
fig=figure(); hold on;
x=categorical({'Sham','Stim'})
prctRes_Stim_p_shuffl_modidx=[];
prctRes_Sham_p_shuffl_modidx=[];
for sesi=1:length(allVars)
    if sesi~=0
    s_numTransient=[];
    for celli=1:length(allVars{sesi}.s)
    s_numTransient(celli)=length(find([allVars{sesi}.s{celli}]> 0));
    end
    criteria=find(s_numTransient>50 & ~ismember(1:size(allVars{sesi}.dff,2),allVars{sesi}.badcells));
    
    percntgResp=length(find(s_numTransient(find(allVars{sesi}.p_shuffl_modidx<0.05 & allVars{sesi}.p_shuffl_modidx>0))>50))/length(criteria)*100;
    if allVars{sesi}.isStim==1
        prctRes_Stim_p_shuffl_modidx=[prctRes_Stim_p_shuffl_modidx percntgResp];
    else
        prctRes_Sham_p_shuffl_modidx=[prctRes_Sham_p_shuffl_modidx percntgResp];
    end
    end
end
% group=[repmat("Sham",[length(prctRes_Sham_p_shuffl_modidx) 1]);repmat("Stim",[length(prctRes_Stim_p_shuffl_modidx) 1])];
% boxplot([prctRes_Sham_p_shuffl_modidx prctRes_Stim_p_shuffl_modidx],group,...
%     'PlotStyle','compact','Colors','br')
groupStats({prctRes_Sham_p_shuffl_modidx prctRes_Stim_p_shuffl_modidx},[],...
    'inAxis',true,'plotData',true,'color',[[0 0.45 0.8];[0.8 0.2 0]],'labelSummary',false)
legend({'Stim','Sham'},'box','off')
%[h,p] = ttest(prctRes_Sham_p_shuffl_modidx,prctRes_Stim_p_shuffl_modidx);
p= ranksum(prctRes_Sham_p_shuffl_modidx,prctRes_Stim_p_shuffl_modidx);
sigstar({[1 2]},[p]);
ylabel('% of responding cells')
title({['shuffl\_modidx\_SpikeEvn '];[ 'p=' num2str(p)]})
%ylim([0 40])
set(gcf,'position',[   680   560   220   420])
set(gca,'fontweight','bold','fontsize',11,'linewidth',1)
set(fig,'Renderer', 'painters')
saveas(gca,fullfile(cd,['shuffl_modidx_SpikeEvn'  '.pdf']))
saveas(gca,fullfile(cd,['shuffl_modidx_SpikeEvn'  '.svg']))
saveas(gca,fullfile(cd,['shuffl_modidx_SpikeEvn'  '.fig']))
close all





All_p_shuffl_modidx_Sham=[];
All_p_shuffl_modidx_Stim=[];

for sesi=1:length(allVars)
    s_numTransient=[];
    for celli=1:length(allVars{sesi}.s)
    s_numTransient(celli)=length(find([allVars{sesi}.s{celli}]> 0));
    end
    criteria=find(s_numTransient>50 & ~ismember(1:size(allVars{sesi}.dff,2),allVars{sesi}.badcells));

    if allVars{sesi}.isStim==1   
        All_p_shuffl_modidx_Stim=[All_p_shuffl_modidx_Stim ...
            allVars{sesi}.p_shuffl_modidx(criteria) ];
        
    else 
        All_p_shuffl_modidx_Sham=[All_p_shuffl_modidx_Sham ...
            allVars{sesi}.p_shuffl_modidx(criteria) ];    end
end

fig=figure(); hold on;
histogram(All_p_shuffl_modidx_Sham(find(All_p_shuffl_modidx_Sham>0)),50,'DisplayStyle','stairs','Normalization','probability','EdgeColor','b')
histogram(All_p_shuffl_modidx_Stim(find(All_p_shuffl_modidx_Stim>0)),50,'DisplayStyle','stairs','Normalization','probability','EdgeColor','r')
p=ranksum(All_p_shuffl_modidx_Sham(find(All_p_shuffl_modidx_Sham>0)),All_p_shuffl_modidx_Stim(find(All_p_shuffl_modidx_Stim>0)));
xlabel('p\_value (a.u.)')
ylabel('count#')
title(['distribution of p\_shuffl\_modidx\_SpikeEvnt for all traces p=' num2str(p)])
legend({'Sham','Stim'},'box','off')
set(gca,'position',[    0.1364    0.1100    0.8    0.8150])
set(gca,'fontweight','bold','fontsize',11,'linewidth',1)
set(fig,'Renderer', 'painters')
saveas(gca,fullfile(cd,['distrib_p_shuffl_modidx'  '.pdf']))
saveas(gca,fullfile(cd,['distrib_p_shuffl_modidx'  '.svg']))
saveas(gca,fullfile(cd,['distrib_p_shuffl_modidx'  '.fig']))
close all




%%%%%%%%%%%
% Spike Events
AllCellsSpikeEventsStimRFON=[];
AllCellsSpikeEventsStimRFOFF=[];
mod_idx_SpikeEventsStim=[];
AllCellsSpikeEventsShamRFON=[];
AllCellsSpikeEventsShamRFOFF=[];
mod_idx_SpikeEventsSham=[];

for sesi=1:length(allVars)
    s_numTransient=[];
    for celli=1:length(allVars{sesi}.s)
    s_numTransient(celli)=length(find([allVars{sesi}.s{celli}]> 0));
    end
    criteria=[];
    criteria=find(s_numTransient>50 & ~ismember(1:size(allVars{sesi}.dff,2),allVars{sesi}.badcells));

    if allVars{sesi}.isStim==1
        AllCellsSpikeEventsStimRFON=[AllCellsSpikeEventsStimRFON; allVars{sesi}.NumEventsTrialStim(criteria)];
        AllCellsSpikeEventsStimRFOFF=[AllCellsSpikeEventsStimRFOFF; allVars{sesi}.NumEventsTrialNoStim(criteria)];
        mod_idx_SpikeEventsStim=[mod_idx_SpikeEventsStim;allVars{sesi}.mod_idx_NumEvents(criteria)];
    else
        AllCellsSpikeEventsShamRFON=[AllCellsSpikeEventsShamRFON; allVars{sesi}.NumEventsTrialStim(criteria)];
        AllCellsSpikeEventsShamRFOFF=[AllCellsSpikeEventsShamRFOFF; allVars{sesi}.NumEventsTrialNoStim(criteria)];
        mod_idx_SpikeEventsSham=[mod_idx_SpikeEventsSham;allVars{sesi}.mod_idx_NumEvents(criteria)];
    end
end

% for Stim sessions
figure();hold on;
scatter(AllCellsSpikeEventsStimRFOFF,AllCellsSpikeEventsStimRFON,20);
scatter(AllCellsSpikeEventsStimRFOFF(find(All_p_shuffl_modidx_Stim>0 & All_p_shuffl_modidx_Stim<0.05)),...
    AllCellsSpikeEventsStimRFON(find(All_p_shuffl_modidx_Stim>0 & All_p_shuffl_modidx_Stim<0.05)),20,'r');
line([0 max(max(xlim),max(ylim))],[0 max(max(xlim),max(ylim))],'linestyle',':', 'color','k')
slope=AllCellsSpikeEventsStimRFOFF\AllCellsSpikeEventsStimRFON;
plot(AllCellsSpikeEventsStimRFOFF,slope*AllCellsSpikeEventsStimRFOFF,'linestyle',':', 'color','r')
xlabel('Sum of Spike Events for RF-OFF (a.u.)')
ylabel('Sum of Spike Events for RF-ON (a.u.)')
title({['total number of traces Stim pardigm=' num2str(length(find(All_p_shuffl_modidx_Stim>0)))];...
    ['total num of responding cells=' num2str(length(find(All_p_shuffl_modidx_Stim>0 & All_p_shuffl_modidx_Stim<0.05)))];...
    ['Lin Reg: y=' num2str(slope) '*x']});
set(gca,'fontweight','bold','fontsize',11,'linewidth',1)
%inset
axes('Position',[.2 .7 .2 .2]); hold on;
box on
scatter(AllCellsSpikeEventsStimRFOFF,AllCellsSpikeEventsStimRFON,10);
scatter(AllCellsSpikeEventsStimRFOFF(find(All_p_shuffl_modidx_Stim>0 & All_p_shuffl_modidx_Stim<0.05)),...
    AllCellsSpikeEventsStimRFON(find(All_p_shuffl_modidx_Stim>0 & All_p_shuffl_modidx_Stim<0.05)),10,'r');
set(gca,'xscale','log','yscale','log')
line([1e1 1e3],[1e1 1e3],'linestyle',':', 'color','k')
xlim([1e1,1e3]);ylim([1e1,1e3]);
set(gcf,'position',[  292    42    950   900])
set(gca,'fontweight','bold','fontsize',11,'linewidth',1)
saveas(gca,fullfile(cd,['SummaryAllCellsSpikeEvents_Stim.pdf']))
saveas(gca,fullfile(cd,['SummaryAllCellsSpikeEvents_Stim.svg']))
saveas(gca,fullfile(cd,['SummaryAllCellsSpikeEvents_Stim.fig']))
close all

% for Sham sessions
figure();hold on;
scatter(AllCellsSpikeEventsShamRFOFF,AllCellsSpikeEventsShamRFON,20);
scatter(AllCellsSpikeEventsShamRFOFF(find(All_p_shuffl_modidx_Sham>0 & All_p_shuffl_modidx_Sham<0.05)),...
    AllCellsSpikeEventsShamRFON(find(All_p_shuffl_modidx_Sham>0 & All_p_shuffl_modidx_Sham<0.05)),20,'r');
line([0 max(max(xlim),max(ylim))],[0 max(max(xlim),max(ylim))],'linestyle',':', 'color','k')
slope=AllCellsSpikeEventsShamRFOFF\AllCellsSpikeEventsShamRFON;
plot(AllCellsSpikeEventsShamRFOFF,slope*AllCellsSpikeEventsShamRFOFF,'linestyle',':', 'color','r')
xlabel('Sum of Spike Events for RF-OFF (a.u.)')
ylabel('Sum of Spike Events for RF-ON (a.u.)')
title({['total number of traces Sham pardigm=' num2str(length(find(All_p_shuffl_modidx_Sham>0)))];...
    ['total num of responding cells=' num2str(length(find(All_p_shuffl_modidx_Sham>0 & All_p_shuffl_modidx_Sham<0.05)))];...
        ['Lin Reg: y=' num2str(slope) '*x']});
set(gca,'fontweight','bold','fontsize',11,'linewidth',1)
%inset
axes('Position',[.2 .7 .2 .2]); hold on;
box on
scatter(AllCellsSpikeEventsShamRFOFF,AllCellsSpikeEventsShamRFON,10);
scatter(AllCellsSpikeEventsShamRFOFF(find(All_p_shuffl_modidx_Sham>0 & All_p_shuffl_modidx_Sham<0.05)),...
    AllCellsSpikeEventsShamRFON(find(All_p_shuffl_modidx_Sham>0 & All_p_shuffl_modidx_Sham<0.05)),10,'r');
set(gca,'xscale','log','yscale','log')
line([1e1 1e3],[1e1 1e3],'linestyle',':', 'color','k')
xlim([1e1,1e3]);ylim([1e1,1e3]);
set(gcf,'position',[  292    42    950   900])
set(gca,'fontweight','bold','fontsize',11,'linewidth',1)
saveas(gca,fullfile(cd,['SummaryAllCellsSpikeEvents_Sham.pdf']))
saveas(gca,fullfile(cd,['SummaryAllCellsSpikeEvents_Sham.svg']))
saveas(gca,fullfile(cd,['SummaryAllCellsSpikeEvents_Sham.fig']))
close all

%compare distribution of mod-idx
p=ranksum(mod_idx_SpikeEventsStim(find(All_p_shuffl_modidx_Stim>0)),mod_idx_SpikeEventsSham(find(All_p_shuffl_modidx_Sham>0)));
fig=figure(); hold on;
histogram(mod_idx_SpikeEventsStim(find(All_p_shuffl_modidx_Stim>0)),60,'DisplayStyle','stairs','Normalization','probability','EdgeColor','r')
histogram(mod_idx_SpikeEventsSham(find(All_p_shuffl_modidx_Sham>0)),60,'DisplayStyle','stairs','Normalization','probability','EdgeColor','b')
% boxplot([mod_idx_SpikeEventsStim(find(All_p_shuffl_modidx_Stim>0));mod_idx_SpikeEventsSham(find(All_p_shuffl_modidx_Sham>0))],...
%     [ones(size(mod_idx_SpikeEventsStim(find(All_p_shuffl_modidx_Stim>0))));zeros(size(mod_idx_SpikeEventsSham(find(All_p_shuffl_modidx_Sham>0))))],...
%     'Colors','rb')
line(0*[1 1],ylim,'linestyle',':','color','k')
xlim([-1 1])
xlabel('Modulation Index (a.u.)')
ylabel('count #')
legend({'Stim','Sham'},'box','off')
title({['SpikeEvents-Stim-vs-Sham'];['ranksum p=' num2str(p)]})
set(gca,'fontweight','bold','fontsize',11,'linewidth',1)
set(fig,'Renderer', 'painters')
saveas(gca,fullfile(cd,['CompareDistModIdx_SpikeEventsStimVsSham.pdf']))
saveas(gca,fullfile(cd,['CompareDistModIdx_SpikeEventsStimVsSham.svg']))
saveas(gca,fullfile(cd,['CompareDistModIdx_SpikeEventsStimVsSham.fig']))
close all

