
%mouse1_180415, UID = 5,20,25 = axo axo


spikes = bz_GetSpikes('getWaveforms',false,'noprompts',true);
basepath = pwd;
basename = bz_BasenameFromBasepath(basepath);

ripFil = [basepath '/' basename '.evt.rip'];
LFPFil = [basepath '/' basename '.lfp'];
rip_evs = LoadEvents(ripFil);

rip_ts = rip_evs.time(cellfun(@any,regexp(rip_evs.description,'peak')));

rip_st = rip_evs.time(cellfun(@any,regexp(rip_evs.description,'start')));

rip_end = rip_evs.time(cellfun(@any,regexp(rip_evs.description,'stop')));

stims = LoadEvents([basename '.evt.ait']);
on = stims.time(cellfun(@any,regexp(stims.description,'pulse_on')));
off = stims.time(cellfun(@any,regexp(stims.description,'pulse_off')));
clu = LoadClu('mouse1_180414.clu.1');
mono_res = MonoSynClick([],'plot',false);
%%

loner = rip_st(diff([0;rip_st])>.5);
rip_peth = [];
for i = 1:length(spikes.times)
rip_peth(i,:) = CrossCorr(loner,spikes.times{i},.01,100);
end

 rip_peth = (rip_peth/length(loner)*100);
%%

stim_peth = [];
for i = 1:length(spikes.times)
stim_peth(i,:) = CrossCorr(on,spikes.times{i},.01,100);
end


 stim_peth = (stim_peth/length(on)*100);
%%
 
 acg =[];
 for i = 1:length(spikes.times)
 [status, interval] = InIntervals(spikes.times{i},[on off]);

 spks = spikes.times{i}(~status);
 
 ok = CrossCorr(spks,spks,.001,200);
 rate(i) = sum(~status)/spikes.spindices(end,1);
 acg(i,:) = ok/sum(~status);
 acg(i,101) = nan;
 end
 




 %%
 
 
fname = '/mnt/NyuShare/Buzsakilabspace/Datasets/EnglishD/Mouse1_AxAx1_Axoaxonic/mouse1_180414/mouse1_180414.spk.1';
waveforms = LoadSpikeWaveforms(fname,32,48);

%%
%mouse1_180414, UID = 5,20,25 = axo axo

cel = 5;
xx = [2 repmat(1:3,1,10) 2];
yy = [1 upSample(2:11,3)' 12];

figure 
 ax = tight_subplot(1,4);
 
 
 axes(ax(1))
 
 
 for i = 1:32
 plot(100*xx(i)+(1:48), squeeze(nanmean(waveforms(i,:,clu==cel+1),3))-yy(i)*700,'k')
 hold on
 end
 axis off
 
 
 axes(ax(2))
 bar(-100:100,acg(cel,:),'k')
xlim([-100 100])



 
 axes(ax(3))
 bar(-50:50,stim_peth(cel,:),'k')
xlim([-50 50])
ylim([0 50])

 axes(ax(4))
 bar(-50:50,rip_peth(cel,:),'k')
xlim([-50 50])
ylim([0 10])


mtit(num2str([spikes.shankID(cel) spikes.cluID(cel)]))

 %%
 
 
 
%get mean waveform

pos = [];

uclu = setdiff(unique(clu),0)';
ix=1;
for i = uclu
    
wv = nanmean(waveforms(:,:,clu==i),3);
[~,b] = min(min(wv,[],2));

pos(ix,:) = [xx(b) yy(b)];
ix = ix+1;
end


 %%
 close all
 int = [4 5 6 15 18 19 24 ];
 ax = [4 19 24];
 pyr = setdiff(1:length(uclu),[int 8]);
 
 kp = ismember(mono_res.sig_con(:,2),int) & ismember(mono_res.sig_con(:,1),pyr);
 
 sig_con = mono_res.sig_con(kp,:);
 
 figure
 
 for i = 1:length(pos)
 pos1(i,:) = pos(i,:)+rand(1)/2;
 end
 for i = 1:length(sig_con)
     
     if ismember(sig_con(i,2),ax)
          plot([pos1(sig_con(i,1),1) pos1(sig_con(i,2),1)]',[pos1(sig_con(i,1),2) pos1(sig_con(i,2),2)]','color',[.5 .5 1])
     else
         
     plot([pos1(sig_con(i,1),1) pos1(sig_con(i,2),1)]',[pos1(sig_con(i,1),2) pos1(sig_con(i,2),2)]','color',[1 .5 .5])
     end
     hold on
     
 end
 
 for i = 1:length(pyr)
     plot(pos1(pyr(i),1),pos1(pyr(i),2),'^','color','k')
 end
 
 
 
 for i = 1:length(int)
     plot(pos1(int(i),1),pos1(int(i),2),'o','color','r')
 end
 
 
 
 for i = 1:length(ax)
     plot(pos1(ax(i),1),pos1(ax(i),2),'x','color','b')
 end
 
 
 
 axis square
 axis off
 set(gca,'ydir','reverse')
 
 