
clear;
fid1 = '2012_03_11_Lollipop-10803_02_optoBB_pre-drug';
fid2 = '2012_03_11_Lollipop-10803_02_optoBB_40uM-CNQX';
fid3 = '2012_03_11_Lollipop-10803_02_optoBB_wash1-2';
fid4 = '2012_03_11_Lollipop-10803_02_optoBB_wash3-5';

t_end = loadspk([fid1 '.spk'],'last');
x1 = loadspk([fid1 '.spk'],[0 t_end],0);

t_end = loadspk([fid2 '.spk'],'last');
x2 = loadspk([fid2 '.spk'],[0 t_end],0);

t_end = loadspk([fid3 '.spk'],'last');
x3 = loadspk([fid3 '.spk'],[0 t_end],0);

t_end = loadspk([fid4 '.spk'],'last');
x4 = loadspk([fid4 '.spk'],[0 t_end],0);

time = [x1.time-max(x1.time); x2.time; x3.time+max(x2.time); x4.time+max(x2.time)+max(x3.time)];
unit = [x1.unit; x2.unit; x3.unit; x4.unit];
starttimes = [-max(x1.time),0,max(x2.time),max(x3.time)+max(x2.time),max(x3.time)+max(x2.time)+max(x4.time)];

save 2012_03_11_Lollipop-10803_optoBB_40uM-CNQX time unit starttimes;

% load 2012_03_08_Ollie-363_optoBB_50uM-APV.spk;

%% ASDR plot

figure(1); hold off;

binsize = 60; %seconds
targetFR = 2.3; %Hz/unit

bins = min(time(unit~=0)):binsize:max(time(unit~=0));
asdr = histc(time(unit~=0),bins);

plot([5*60 24*3600+5*60],[targetFR targetFR],'-r','LineWidth',2);
hold on;
plot(bins,asdr/max(unit)/binsize,'-k');
ymax = ceil(max(asdr/max(unit)/binsize));
plot([starttimes(2) starttimes(2)],[0 ymax],':','Color',[0.7 0.7 0.7]);
plot([starttimes(3) starttimes(3)],[0 ymax],':','Color',[0.7 0.7 0.7]);
plot([starttimes(4) starttimes(4)],[0 ymax],':','Color',[0.7 0.7 0.7]);

set(gca,'XTick',[-12*3600:3600:40*3600],'XTickLabel',[-12:1:40],'TickLength',[0.0025 0.025]);
xlim([-3.5*3600 bins(end-1)]);
ylim([0 ymax]);
xlabel('Time (hours)','FontSize',18);
ylabel('Normalized ASDR (Hz/unit)','FontSize',18);

%%

timepoints = [-1*3600,... %an hour before drug
    0,...               %right after drug is added but before stim turns on
    5.5*60,...            %after stim turns on
    12*3600+5.5*60,...    %12 hours after stim begins
    24*3600,...         %5min before stim turns off
    24*3600+5.5*60,...    %after stim turns off but before wash
    starttimes(4)+10*60,...   %10min after 2nd set of washes
    starttimes(4)+3*3600];   %12hr after 2nd set of washes

snip_length = 5*60; %length of time snippet in seconds

figure(2);

for i = 1:length(timepoints)
    subplot(length(timepoints),1,i); hold off;
    snip_ind = find(time>=timepoints(i) & time<timepoints(i)+snip_length & unit~=0);
    plot(time(snip_ind),unit(snip_ind)+0.1*randn(size(unit(snip_ind))),'.','MarkerSize',1);
    ylim([0 max(unit)+1]);
    xlim([time(min(snip_ind)) time(max(snip_ind))]);
    set(gca,'YTick',[0 max(unit)+1],'XTick',[]);
    %text(max(unit(snip_in
    ylabel('Unit','FontSize',14);
end;
xlabel('Time (5-minute snippet)','FontSize',14);