%% Fig 5 - get control data

clear;
load('unicorn364\unicorn364_nowaveform.mat');
load('yoda359\yoda359_nowaveform.mat');
channel = channels; clean = cleanspk; time = spktimes;
clear channels cleanspk spktimes;




%%
load('cinderella19309\cinderella19309_nowaveform.mat');
u364.time = time(clean==1);
u364.channel = channel(clean==1);
%u364.channel = u364.channel(u364.time>max(time)-60*3600);
%u364.time = u364.time(u364.time>max(time)-60*3600);
u364.bins = min(u364.time):1:max(u364.time);
u364.asdr = hist(u364.time,u364.bins);

hold off;
plot((u364.bins-min(u364.time))/3600-20.03,u364.asdr*1.5,'-','Color',[0.85 0.85 0.85]);
set(gca,'XTick',-12:2:48)
xlim([-12 48])
ylim([0 6000]);

%% Fig 5 TTX ASDR

hold on;
[events.time events.comment] = get_events('donkeylips363');
load('donkeylips363\donkeylips363_nowaveform.mat');
time(time>events.time(3)) = time(time>events.time(3))-events.time(3)+52*3600+events.time(1);
% [events.time events.comment] = get_events('donkeylips363');
% load('donkeylips363\donkeylips363_nowaveform.mat');
%time(time>events.time(3)) = time(time>events.time(3))-events.time(3)+52*3600+events.time(1);

b359.time = (time-events.time(1))/3600;
b359.time = b359.time(clean==1);
b359.channel = channel(clean==1);

b359.bins = min(b359.time):1/3600:max(b359.time);
b359.asdr = hist(b359.time,b359.bins);

plot(b359.bins,b359.asdr*2,'-k');
set(gca,'XTick',-12:2:48)
xlim([-12 48]);
ylim([0 6000]);
legend('control','TTX-treated','Location','NorthWest');

%% identify bursts vs not bursts

b359.ISI = [NaN; diff(b359.time)];
tmp1 = b359.time(b359.ISI<=19.5/3600/1000 & b359.time<0);
%tmp2 = [];
tmp2 = b359.time(b359.ISI<=11.5/3600/1000 & b359.time>=0 & b359.time<50);
%tmp3 = b359.time(b359.ISI<=39.5/3600/1000 & b359.time>=50);
tmp3 = [];
b359.burst = [tmp1; tmp2; tmp3];
clear tmp;
tmp1 = b359.time(b359.ISI>19.5/3600/1000 & b359.time<0);
%tmp2 = [];
tmp2 = b359.time(b359.ISI>11.5/3600/1000 & b359.time>=0 & b359.time<50);
%tmp3 = b359.time(b359.ISI>39.5/3600/1000 & b359.time>=50);
tmp3 = [];
b359.IBI = [tmp1; tmp2; tmp3];

b359.bin1hr = floor(min(b359.time)):1:ceil(max(b359.time));
b359.asdr1hr.all = histc(b359.time,b359.bin1hr)/3600;
b359.asdr1hr.burst = histc(b359.burst,b359.bin1hr)/3600;
b359.asdr1hr.IBI = histc(b359.IBI,b359.bin1hr)/3600;

%% Fig 5B - CNQX firing rate 1-hour bins


hold off;
plot(b359.bin1hr+0.5,b359.asdr1hr.all*2,'-ok','LineWidth',2);
hold on;
plot(b359.bin1hr+0.5,b359.asdr1hr.burst*2,'-or','LineWidth',2);
plot(b359.bin1hr+0.5,b359.asdr1hr.IBI*2,'-ob','LineWidth',2);
plot(b359.bin1hr+0.5,b359.asdr1hr.all*2,'-ok','LineWidth',2);

xlim([-12 48]);
%ylim([0 24]);
%set(gca,'YTick',0:4:24)
grid on;
set(gca,'XTick',-12:2:64);
legend('all spikes','spikes in bursts','spikes in IBI','location','northeast','orientation','horizontal')