
dirName = '/media/sam/SM4/SeqGen/PV5/20160307';
basename = '20160307';

pre_post = [11 9;20 15];



CCGxtime = [];
%find correct xml directory, some variability in folder structure
filename = [dirName '/' basename '.xml'];

SetCurrentSession(filename)

%load  and states
fils  = getAllFiles(dirName);

kp = fils(cellfun(@(a) any(regexp(a,'-states.mat')),fils));

v = load(kp{1});
transitions = [];
for i = [1 3 5]
    temp = [find(diff([0 v.states==i])>0)' find(diff([0 v.states==i])<0)'];
    temp = [i*ones(size(temp,1),1) temp];
    transitions = [transitions;temp];
end
transitions = sortrows(transitions,2);


%get ripples times

fils  = getAllExtFiles(dirName,'rip',1);
ripples = LoadEvents(fils{1});


%find out ripple

%if sam file




rip_on =  ripples.time(cellfun(@(a) any(regexp(a,'Ripple start')), ...
    ripples.description));

rip_off =  ripples.time(cellfun(@(a) any(regexp(a,'Ripple stop')), ...
    ripples.description));


rip_eps = [rip_on rip_off];
%get stimulations


fils  = getAllExtFiles(dirName,'ait',1);
stims = LoadEvents(fils{1});

%get spikes


units = GetUnits;
spikes = GetSpikeTimes(units,'output','full');
spikestimes = spikes(:,1);
[uniqueCells,~,unitID] = unique(spikes(:,2:3),'rows');
spikesIDs = [spikes(:,2:3) unitID];
uniqueCells = sortrows(uniqueCells);
[spikestimes,b] = sort(spikestimes);
spikesIDs = spikesIDs(b,:);
CCGxtime.uniqueCells = uniqueCells;


%get onset offsets

st=[];

for j = 1:3
    
    st{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
st = cell2mat(st');
kp = spikestimes<min(st(:));
cch = CCG(spikestimes(kp),spikesIDs(kp,3),'binSize',.0004,'duration',.1);

for ii = 1:size(pre_post,1)
       ax = tight_subplot(1,2)
u1 = pre_post(ii,1);
u2 = pre_post(ii,2);
times = [spikestimes(spikesIDs(:,3) ==u1);spikestimes(spikesIDs(:,3) ==u2)];
groups = [ones(sum(spikesIDs(:,3) ==u1),1);2*ones(sum(spikesIDs(:,3) ==u2),1)];

[times,b ] = sort(times);
groups = groups(b);

[status,~,~] = InIntervals(times,MergeEpochs2([st(:,1)-1 st(:,2)+1]));


[ccg,n,t1] = CCGTime([times(status);max(times)+1],[groups(status);3],'win',500,'win_inc',1,'binSize',.0008,'duration',.1);
ccg = squeeze(ccg(:,1,2,:));
n = squeeze(n(:,1,2,:));
prob1 = ccg./n;

[ccg,n,t1] = CCGTime([times(~status);max(times)+1],[groups(~status);3],'win',500,'win_inc',1,'binSize',.0008,'duration',.1);
ccg = squeeze(ccg(:,1,2,:));
n = squeeze(n(:,1,2,:));
prob2 = ccg./n;

ts = 1:size(prob1,2);
tim = nan(length(ts),1);
for i = floor(min(st(:,1))):length(ts)
    t = i-st(:,2);
    if any(t(t>0))
    tim(i) = min(t(t>0));
    end
end
axes(ax(1))
imagesc(t1{2},t1{1},prob2)
hold on
plot([st(:,1) st(:,1)]',[-.05*ones(length(st(:,1) ),1) -.04*ones(length(st(:,1) ),1)]')
axes(ax(2))
imagesc(t1{2},t1{1},prob1)
hold on
plot([st(:,1) st(:,1)]',[-.05*ones(length(st(:,1) ),1) -.04*ones(length(st(:,1) ),1)]')
%mtit([num2str(uniqueCells(ii,:)) ' : ' num2str(uniqueCells(jj,:))]) 
uiwait
end


