 import CMBHOME.Utils.*
evts = LoadEvents('20161012_161013_112816.evt.ait');
onset1 = evts.time(cellfun(@any,regexp(evts.description,'pulse_on 1')));
offset1 = evts.time(cellfun(@any,regexp(evts.description,'pulse_off 1')));
pulse1 = evts.description(cellfun(@any,regexp(evts.description,'pulse_center 1')));
ok = regexp(pulse1,'\w*\s\d');
pulse1 = cellfun(@(a,b) str2num(a(b(2):end)),pulse1,ok);

SetCurrentSession

units  = GetUnits;
units = [units;[1:10]' ones(10,1)];
spiketimes  =GetSpikeTimes(units,'output','full');
uniqueCells = unique(spiketimes(:,2:3),'rows');
SUB = uniqueCells(uniqueCells(:,1)>4,:);

%%
for i = 1:size(SUB,1)
spks = spiketimes(ismember(spiketimes(:,2:3),SUB(i,:),'rows'),1);



spks_stim{i} = cellfun(@(a) spks(spks>a(1) & spks<a(2)),num2cell([ onset1-.3 onset1+.7],2),'uni',0);
adj_spikes{i} = cellfun(@(a,b) a-b,spks_stim{i},num2cell(onset1),'uni',0);
end
%%


figure
t = 0:100:15000;
ax  =tight_subplot(2,2);idx = 1;
for i = [28 60 115 155]%length(adj_spikes)
    axes(ax(idx))
    
    st1 = t(find(histc(linearize(cell2mat(spks_stim{i})),t)>75,1,'first'));
    st2 = t(find(histc(linearize(cell2mat(spks_stim{i})),t)>75,1,'last'));
   % st1 = 0;st2 = inf;
    kp = onset1>st1 & onset1<st2;
    
    
    hold on
  patch([0 0 .1 .1],[0 20 20 0],[.7 .7 1],'edgecolor','none','facealpha',.3)

%PipeRaster(adj_spikes{i}(1:1200));
bar(-.3:.01:.4,(100*histc(cell2mat(adj_spikes{i}(kp)),-.3:.01:.4))/sum(kp))
ylim([0 20])
set(gca,'ytick',0:10:20,'yticklabel',0:10:20)
    xlim([-.3 .4])
    idx = idx+1;
end

%%


figure
t = 0:100:15000;
idx = 1;
for i = 117:length(adj_spikes)
    figure
    st1 = t(find(histc(linearize(cell2mat(spks_stim{i})),t)>75,1,'first'));
    st2 = t(find(histc(linearize(cell2mat(spks_stim{i})),t)>75,1,'last'));
   % st1 = 0;st2 = inf;
   if any(st1)
    kp = onset1>st1 & onset1<st2;
    
    
    hold on
  patch([0 0 .1 .1],[0 20 20 0],[.7 .7 1],'edgecolor','none','facealpha',.3)

%PipeRaster(adj_spikes{i}(1:1200));
bar(-.3:.01:.4,(100*histc(cell2mat(adj_spikes{i}(kp)),-.3:.01:.4))/sum(kp))
ylim([0 20])
set(gca,'ytick',0:10:20,'yticklabel',0:10:20)
    xlim([-.3 .4])
    idx = idx+1;
    
    uiwait
   end
end



%%
figure
ax  =tight_subplot(3,2);idx = 1;
for sh = 5:9
    
    axes(ax(idx))
sh_spk = adj_spikes(SUB(:,1)==sh);
sh_ts = spks_stim(SUB(:,1)==sh);
spks = [];st = [];
for c = 1:length(sh_spk)
    
    spks = [spks;sh_spk{c}];
    st = [st;sh_ts{c}];
end

spks = cell2mat(spks);
st = cell2mat(st);
xlim([-.3 .4])
spks = spks;
bar(-.31:.005:.4,(1000*histc(spks,-.31:.005:.4))/(5*length(adj_spikes{1})))
idx = idx+1;
end

%%
CA1 = uniqueCells(uniqueCells(:,1)<=4,:);


for i = 11:size(CA1,1)
spks = spiketimes(ismember(spiketimes(:,2:3),CA1(i,:),'rows'),1);



spks_stim1{i} = cellfun(@(a) spks(spks>a(1) & spks<a(2)),num2cell([ onset1-.3 onset1+.7],2),'uni',0);
adj_spikes1{i} = cellfun(@(a,b) a-b,spks_stim1{i},num2cell(onset1),'uni',0);
end



%%


figure
ax  =tight_subplot(1,2);idx = 1;
for sh = 2:3
    
    axes(ax(idx))
sh_spk = adj_spikes1(CA1(:,1)==sh);
sh_ts = spks_stim1(CA1(:,1)==sh);
spks1 = [];st = [];
for c = 1:length(sh_spk)
    
    spks1 = [spks1;sh_spk{c}];
    st = [st;sh_ts{c}];
end

spks1 = cell2mat(spks1);
st = cell2mat(st);

spks1 = spks1;
hold on
plot([0 0],[0 25],'color',[.7 .7 .7])

plot([0.1 0.1],[0 25],'color',[.7 .7 .7])
bar(-.31:.0001:.4,(1000*histc(spks1,-.31:.0001:.4))/(5*length(adj_spikes1{1})))

idx = idx+1;
xlim([-.3 .4])
ylim([0 25])

end


