
dirNames = '/media/sam/SM4/SeqGen/camkii3/20160518';
NWBfil = '/home/sam/Dropbox/buzsaki/data/SeqGen/camkii3_20160518_NWB.mat';
v = load(NWBfil);
NWB = v.NWB;
fils  = getAllExtFiles(dirNames,'ait',1);
stims = LoadEvents(fils{1});
sh = nan(length(stims.time/2),1);
for j = 1:5
    
    sti{j} = unique([stims.time(cellfun(@(a) any(regexp(a,'on')) & ...
        any(regexp(a,num2str(j))),stims.description)) ...
        stims.time(cellfun(@(a) any(regexp(a,'off')) & ...
        any(regexp(a,num2str(j))),stims.description))],'rows');
    
end




%get stimulatoin response





%get spikes times and put in order
xml = '/media/sam/SM4/SeqGen/camkii3/20160518/20160518.xml';
SetCurrentSession(xml)
units = GetUnits;
nCel = size(units,1);
spikes = GetSpikeTimes(units,'output','full');
[~,~,cellID] = unique(spikes(:,2:3),'rows');
spikesIDs = [spikes(:,2:3) cellID];

maxts = max(spikes(:,1));
stimmat = cell2mat(sti');
stimmat = sortrows(stimmat);
stimmat  =MergeEpochs2(stimmat);



stiID = cell2mat(cellfun(@(a,b) a*ones(size(b,1),1),num2cell(1:5),sti,'uni',0)');
stiID = stiID + max(spikesIDs(:,3));

[spiketimes,b] = sort(spikes(:,1));
spikesIDs = spikesIDs(b,:);


[spiketimes1,b]  = sort([spiketimes(:,1);stimmat(:,1)]);
cellID = [spikesIDs(:,3);stiID];
cellID  = cellID(b,:);

%first costim

st1= sti{4}(1);
st2= sti{4}(end);


secbefore = .5;
secafter = 1;
nbins = 300;
onset = sti{1}(sti{1}(:,1)<st1,:);
inbin=nan(size(onset,1),nbins,size(units,1));
tot_duration = secbefore + secafter;
bin_duration = tot_duration/nbins;

bin_times=-secbefore:bin_duration:secafter;
for u=1:size(units,1)
    cel=units(u,:);
    spikes=spiketimes(spikesIDs(:,3)==u);
    [status,interval] = InIntervals(spikes,[onset(:,1)-secbefore onset(:,1)+secafter]);
    n = histoc(interval(interval>0),1:size(onset,1));
     spikes = mat2cell(spikes(status),n);
    adjustedSpikes=cellfun(@(a,b) a-b, spikes, num2cell(onset(:,1)) , 'uni',0);
    spkhist = cell2mat(cellfun(@(a) reshape(histc(a,bin_times),1,[]),adjustedSpikes, 'UniformOutput', false));
   % spkhist=cell2mat(cellfun(@(a) imfilter(reshape(histc(a,bin_times),1,[]),kernel,'same'),adjustedSpikes, 'UniformOutput', false));
   % spkhist = spkhist(:,1:end-1)./bin_duration;%turn into rate
    inbin(:,:,u) = spkhist(:,1:end-1)./bin_duration;
   
   
    %inbin(:,:,u)= (spkhist-nanmean(spkhist(:)))/nanstd(spkhist(:));%turn into z-score
    
end


binnedPop=permute(inbin,[3 2 1]);

zBin1 = nanzscore(nanmean(binnedPop,3),[],2);


onset = sti{1}(sti{1}(:,1)>st1 & sti{1}(:,1)<st2,:);
inbin=nan(size(onset,1),nbins,size(units,1));
tot_duration = secbefore + secafter;
bin_duration = tot_duration/nbins;

bin_times=-secbefore:bin_duration:secafter;
for u=1:size(units,1)
    cel=units(u,:);
    spikes=spiketimes(spikesIDs(:,3)==u);
    [status,interval] = InIntervals(spikes,[onset(:,1)-secbefore onset(:,1)+secafter]);
    n = histoc(interval(interval>0),1:size(onset,1));
     spikes = mat2cell(spikes(status),n);
    adjustedSpikes=cellfun(@(a,b) a-b, spikes, num2cell(onset(:,1)) , 'uni',0);
    spkhist = cell2mat(cellfun(@(a) reshape(histc(a,bin_times),1,[]),adjustedSpikes, 'UniformOutput', false));
   % spkhist=cell2mat(cellfun(@(a) imfilter(reshape(histc(a,bin_times),1,[]),kernel,'same'),adjustedSpikes, 'UniformOutput', false));
   % spkhist = spkhist(:,1:end-1)./bin_duration;%turn into rate
    inbin(:,:,u) = spkhist(:,1:end-1)./bin_duration;
   
   
    %inbin(:,:,u)= (spkhist-nanmean(spkhist(:)))/nanstd(spkhist(:));%turn into z-score
    
end


binnedPop=permute(inbin,[3 2 1]);

zBin2 = nanzscore(nanmean(binnedPop,3),[],2);


onset = sti{1}(sti{1}(:,1)>st2,:);
inbin=nan(size(onset,1),nbins,size(units,1));
tot_duration = secbefore + secafter;
bin_duration = tot_duration/nbins;

bin_times=-secbefore:bin_duration:secafter;
for u=1:size(units,1)
    cel=units(u,:);
    spikes=spiketimes(spikesIDs(:,3)==u);
    [status,interval] = InIntervals(spikes,[onset(:,1)-secbefore onset(:,1)+secafter]);
    n = histoc(interval(interval>0),1:size(onset,1));
     spikes = mat2cell(spikes(status),n);
    adjustedSpikes=cellfun(@(a,b) a-b, spikes, num2cell(onset(:,1)) , 'uni',0);
    spkhist = cell2mat(cellfun(@(a) reshape(histc(a,bin_times),1,[]),adjustedSpikes, 'UniformOutput', false));
   % spkhist=cell2mat(cellfun(@(a) imfilter(reshape(histc(a,bin_times),1,[]),kernel,'same'),adjustedSpikes, 'UniformOutput', false));
   % spkhist = spkhist(:,1:end-1)./bin_duration;%turn into rate
    inbin(:,:,u) = spkhist(:,1:end-1)./bin_duration;
   
   
    %inbin(:,:,u)= (spkhist-nanmean(spkhist(:)))/nanstd(spkhist(:));%turn into z-score
    
end


binnedPop=permute(inbin,[3 2 1]);

zBin3 = nanzscore(nanmean(binnedPop,3),[],2);


zBin1 = nanzscore(nanmean(binnedPop,3),[],2);


onset = sti{1}(sti{1}(:,1)>st1 & sti{1}(:,1)<st2,:);
inbin=nan(size(onset,1),nbins,size(units,1));
tot_duration = secbefore + secafter;
bin_duration = tot_duration/nbins;

bin_times=-secbefore:bin_duration:secafter;
for u=1:size(units,1)
    cel=units(u,:);
    spikes=spiketimes(spikesIDs(:,3)==u);
    [status,interval] = InIntervals(spikes,[onset(:,1)-secbefore onset(:,1)+secafter]);
    n = histoc(interval(interval>0),1:size(onset,1));
     spikes = mat2cell(spikes(status),n);
    adjustedSpikes=cellfun(@(a,b) a-b, spikes, num2cell(onset(:,1)) , 'uni',0);
    spkhist = cell2mat(cellfun(@(a) reshape(histc(a,bin_times),1,[]),adjustedSpikes, 'UniformOutput', false));
   % spkhist=cell2mat(cellfun(@(a) imfilter(reshape(histc(a,bin_times),1,[]),kernel,'same'),adjustedSpikes, 'UniformOutput', false));
   % spkhist = spkhist(:,1:end-1)./bin_duration;%turn into rate
    inbin(:,:,u) = spkhist(:,1:end-1)./bin_duration;
   
   
    %inbin(:,:,u)= (spkhist-nanmean(spkhist(:)))/nanstd(spkhist(:));%turn into z-score
    
end


binnedPop=permute(inbin,[3 2 1]);

zBin2 = nanzscore(nanmean(binnedPop,3),[],2);


onset = sti{1}(sti{1}(:,1)>st2,:);
inbin=nan(size(onset,1),nbins,size(units,1));
tot_duration = secbefore + secafter;
bin_duration = tot_duration/nbins;

bin_times=-secbefore:bin_duration:secafter;
for u=1:size(units,1)
    cel=units(u,:);
    spikes=spiketimes(spikesIDs(:,3)==u);
    [status,interval] = InIntervals(spikes,[onset(:,1)-secbefore onset(:,1)+secafter]);
    n = histoc(interval(interval>0),1:size(onset,1));
     spikes = mat2cell(spikes(status),n);
    adjustedSpikes=cellfun(@(a,b) a-b, spikes, num2cell(onset(:,1)) , 'uni',0);
    spkhist = cell2mat(cellfun(@(a) reshape(histc(a,bin_times),1,[]),adjustedSpikes, 'UniformOutput', false));
   % spkhist=cell2mat(cellfun(@(a) imfilter(reshape(histc(a,bin_times),1,[]),kernel,'same'),adjustedSpikes, 'UniformOutput', false));
   % spkhist = spkhist(:,1:end-1)./bin_duration;%turn into rate
    inbin(:,:,u) = spkhist(:,1:end-1)./bin_duration;
   
   
    %inbin(:,:,u)= (spkhist-nanmean(spkhist(:)))/nanstd(spkhist(:));%turn into z-score
    
end


binnedPop=permute(inbin,[3 2 1]);

zBin3 = nanzscore(nanmean(binnedPop,3),[],2);



zBin1 = nanzscore(nanmean(binnedPop,3),[],2);


onset = sti{1}(sti{1}(:,1)>st1 & sti{1}(:,1)<st2,:);
inbin=nan(size(onset,1),nbins,size(units,1));
tot_duration = secbefore + secafter;
bin_duration = tot_duration/nbins;

bin_times=-secbefore:bin_duration:secafter;
for u=1:size(units,1)
    cel=units(u,:);
    spikes=spiketimes(spikesIDs(:,3)==u);
    [status,interval] = InIntervals(spikes,[onset(:,1)-secbefore onset(:,1)+secafter]);
    n = histoc(interval(interval>0),1:size(onset,1));
     spikes = mat2cell(spikes(status),n);
    adjustedSpikes=cellfun(@(a,b) a-b, spikes, num2cell(onset(:,1)) , 'uni',0);
    spkhist = cell2mat(cellfun(@(a) reshape(histc(a,bin_times),1,[]),adjustedSpikes, 'UniformOutput', false));
   % spkhist=cell2mat(cellfun(@(a) imfilter(reshape(histc(a,bin_times),1,[]),kernel,'same'),adjustedSpikes, 'UniformOutput', false));
   % spkhist = spkhist(:,1:end-1)./bin_duration;%turn into rate
    inbin(:,:,u) = spkhist(:,1:end-1)./bin_duration;
   
   
    %inbin(:,:,u)= (spkhist-nanmean(spkhist(:)))/nanstd(spkhist(:));%turn into z-score
    
end


binnedPop=permute(inbin,[3 2 1]);

zBin2 = nanzscore(nanmean(binnedPop,3),[],2);


onset = sti{1}(sti{1}(:,1)>st2,:);
inbin=nan(size(onset,1),nbins,size(units,1));
tot_duration = secbefore + secafter;
bin_duration = tot_duration/nbins;

bin_times=-secbefore:bin_duration:secafter;
for u=1:size(units,1)
    cel=units(u,:);
    spikes=spiketimes(spikesIDs(:,3)==u);
    [status,interval] = InIntervals(spikes,[onset(:,1)-secbefore onset(:,1)+secafter]);
    n = histoc(interval(interval>0),1:size(onset,1));
     spikes = mat2cell(spikes(status),n);
    adjustedSpikes=cellfun(@(a,b) a-b, spikes, num2cell(onset(:,1)) , 'uni',0);
    spkhist = cell2mat(cellfun(@(a) reshape(histc(a,bin_times),1,[]),adjustedSpikes, 'UniformOutput', false));
   % spkhist=cell2mat(cellfun(@(a) imfilter(reshape(histc(a,bin_times),1,[]),kernel,'same'),adjustedSpikes, 'UniformOutput', false));
   % spkhist = spkhist(:,1:end-1)./bin_duration;%turn into rate
    inbin(:,:,u) = spkhist(:,1:end-1)./bin_duration;
   
   
    %inbin(:,:,u)= (spkhist-nanmean(spkhist(:)))/nanstd(spkhist(:));%turn into z-score
    
end


binnedPop=permute(inbin,[3 2 1]);

zBin3 = nanzscore(nanmean(binnedPop,3),[],2);



%%
kp  =max(zBin2(:,100:130),[],2)>5;

sh = [];
for i = 1:3
    
    sh(i) = find(units(kp,1)==i,1,'last');
end
figure
ax  = tight_subplot(1,3);

axes(ax(1))

imagesc(bin_times,[],zBin1(kp,:),[0 10])
xlim([-.01 .175])
set(gca,'yticklabel',[])
hold on
for i = 1:3
plot([bin_times(i) bin_times(end)],[ sh(i) sh(i)]+.5,'w')
end
colormap('jet')
axes(ax(2))

imagesc(bin_times,[],zBin2(kp,:),[0 10])
xlim([-.01 .175])
set(gca,'yticklabel',[])
colormap('jet')
hold on
for i = 1:3
plot([bin_times(i) bin_times(end)],[ sh(i) sh(i)]+.5,'w')
end
axes(ax(3))

imagesc(bin_times,[],zBin3(kp,:),[0 10])
xlim([-.01 .175])
set(gca,'yticklabel',[])
colormap('jet')
hold on
for i = 1:3
plot([bin_times(i) bin_times(end)],[ sh(i) sh(i)]+.5,'w')
end