% Jon Newman
% Multi-parameter activity assay for cultures transfected with
% optical exciters and silencers.
%
% This script combines data from both cultures on which this experiment was
% conducted
%   - venus-20708, 2012.05.03, 21 div, 17 dpt
%   - venus-20709, 2012.04.16, 18 div, 17 dpt

% File
% base = '../2012_04_16_Tommy-MultiClamp/tommy-20709/';
% fid = 'multi-u_dead-time_2012-04-16_hoss-r3_tommy_20709_01';
% load('tommy_20709_bad_unit.mat')

base = '../2012_05_03_Venus-MultiClamp/venus-20708/';
fid ='multi-u_dead-time_2012-05-03_hoss-r3_venus_20708_01';
load('venus_20708_bad_unit.mat')

% Plot parameters
h = 7; % mm
w = 32*3+2*3; % mm
s = 1.5;
ht = h+s;

% close all
figure('Units','centimeters','Position',[30 1 30 10])

ap = {};

for f = 1:5
    ap{f} = axes('Units','centimeters','Position',[5 7-(f-1)*ht/10 w/10 h/10]);
    set(gca,'Fontsize',10,'TickDir','Out','TickLength',[0.015 0.025]);
    if f == 5
        set(gca,'XTick',-10:10:70);
    else
        set(gca,'XTick',[]);
    end
    xlim([-10 40]);
    box on
    hold on
    
    as{f} = axes('Units','centimeters','Position',[18 7-(f-1)*ht/10 w/10 h/10]);
    set(gca,'Fontsize',10,'TickDir','Out','TickLength',[0.015 0.025],'YTick',[0.01,0.1,1.0,10],'YScale','Log');
    if f == 5
        set(gca,'XTick',-10:10:70);
    else
        set(gca,'XTick',[]);
    end
    xlim([-10 40]);
    ylim([0.001 50])
    box on
    hold on
    
end

% Load
% s = loadspk([base fid '.spk']);
% d = loaddig([base fid '.dig']);
% ss = SqueakySpk(fid,s.meta.fs_Hz,1,s);
% ss.RemoveChannel([1 8 33 57 64 35 24 60]);
% ss.RemoveUnit([0 bu]);
% ss.UnitFR();

% Calculate unit FR
[x nu] = sort(ss.unitfr(:,2),1);
ou = ss.unitfr(nu,1);

% Rearrange units in decreasing FR
spk = ss.ReturnClean();
cop = spk.unit;
for i = 1:length(ou)
    spk.unit(cop == ou(i)) = i;
end

% Decode dig file
b = dec2bin(d.state,32);
u = bin2dec(b(:,1:29))/10e6;
c = bin2dec(b(:,30:31));
t = bin2dec(b(:,32));

% ChR2 (u1)
uu = unique(u(c == 0));
uu = uu(uu~=0);

% Find start and end time for all u
tu_on = [];
tu_off = [];
for i = 1:length(uu)
    tu_on = [tu_on d.time(find(u == uu(i),1, 'first'))];
    tu_off = [tu_off d.time(find(u == uu(i),1, 'last'))];
    
end
ts = tu_off;
tu_off = tu_off-30;

du = 0.1;
db = 5;
k = 1;


idx = [25 18];%24-31 10-21

for ut = [0.5 0.25]
    %     idx = find(uu >= ut-du & uu <= ut+du);
    %     r = randperm(length(idx));
    %     i = idx(r(1));
    i = idx(k);
    
    b = spk.time >= tu_on(i)-10 & spk.time <= tu_off(i);
    st = spk.time(b)-tu_on(i);
    su = spk.unit(b);
    b = spk.time >= ts(i) & spk.time <= ts(i)+10;
    st = [st;spk.time(b)-ts(i)+30];
    su = [su;spk.unit(b)];
    
%     plot(ap{k},st,su,'k.','MarkerSize',2)
    set(ap{k},'Ylim',[1 max(spk.unit)],'YTick',[1 max(spk.unit)])
    hold on
    
    bin = -10:db:40;
    cnt = histc(st,bin);
    stairs(as{k},bin,cnt/db/max(spk.unit),'r-')
    
    k = k+1;
    
end

%% Spont
t1 = 210;
t2 = t1+30;
b = spk.time >= t1-10 & spk.time <= t2+10;
st = spk.time(b)-t1;
su = spk.unit(b);
% plot(ap{k},st,su,'k.','MarkerSize',2)
set(ap{k},'Ylim',[1 max(spk.unit)],'YTick',[1 max(spk.unit)])

bin = -10:db:40;
cnt = histc(st,bin);
stairs(as{k},bin,cnt/db/max(spk.unit),'r-')
k = k+1;

%% Halo (u2)

uu = u(u~=0 & c == 1);
uu = sort(uu);

% Find start and end time for all u
tu_on = d.time(ismember(u,uu));
tu_off = tu_on + 60;
ts = tu_off;
tu_off = tu_off-30;

idx = [13 28];%11-17 21-31
j = 1;
for ut = [0.25 0.5]
    %     idx = find(uu >= ut-du & uu <= ut+du);
    %     r = randperm(length(idx));
    %     i = idx(r(1));
    i = idx(j);
    j = j+1;
    b = spk.time >= tu_on(i)-10 & spk.time <= tu_off(i);
    st = spk.time(b)-tu_on(i);
    su = spk.unit(b);
    b = spk.time >= ts(i) & spk.time <= ts(i)+10;
    st = [st;spk.time(b)-ts(i)+30];
    su = [su;spk.unit(b)];
    
%     plot(ap{k},st,su,'k.','MarkerSize',2)
    set(ap{k},'Ylim',[1 max(spk.unit)],'YTick',[1 max(spk.unit)])
    
    
    bin = -10:db:40;
    cnt = histc(st,bin);
    stairs(as{k},bin,cnt/db/max(spk.unit),'r-')
    
    
    k = k+1;
    
end

%%
export_fig('-pdf','-transparent','raster-frame_ex-b_30sec_00')
% export_fig('-r700','-transparent','raster-only_ex-b_30sec_00')