%% CNQX plot: usain22493 %%
load C:\Users\USER\Documents\mingfaifong\2013-01-24_usain22493_12-14DIV_MEA_CNQX\usain22493_12-14DIV.mat;

%% CNQX+stim plot: trouble22468 %%
load C:\Users\USER\Documents\mingfaifong\2013-01-21_trouble22468_12-14DIV_MEA_CNQX-ChR2-stim-CL\trouble22468_12-14DIV.mat;

%% adjust plot features %%

f1 = figure(1);
set(f1,'Position',[806 604 1752 343]);
hold off;
plotcolors = {[0 0 0],[0 0.2 0.7],[0.9 0 0],[0.1 0.7 0.1],[1 0.5 0],[0.6 0 0.7]}; %black, blue, red, green, orange, purple


dt = 3600; ymax = 40; lw = 3;
% dt = 300; ymax = 80; lw = 1.5;
% dt = 60; ymax = 200; lw = 1;
% dt = 1; ymax = 6000; lw = 1;
% dt = 0.01; ymax = 20000; lw = 1;

bin = [ceil(min(time)/dt)*dt:dt:floor(max(time)/dt)*dt];
asdr = histc(time,bin);

% plot([300/3600 24+300/60],[28.5293 28.5293],'-','Color',[0.75 0.75 0.75],'LineWidth',6); hold on;
stairs(bin/3600,asdr/dt,'-k','LineWidth',lw);
hold on;
plot([0 24.25],[ymax*.98 ymax*.98],'-k','LineWidth',8,'Color',plotcolors{3});
plot([300/3600 24.25],[ymax*.9 ymax*.9],'-k','LineWidth',8,'Color',plotcolors{6});

xlim([-6 24.5]);
ylim([0 ymax]);
set(gca,'XTick',[-6:2:24],'TickDir','out','TickLength',[0.005 0.025],'FontSize',16,'LineWidth',2);
box off;

ylabel('Array-wide firing rate (Hz)','FontSize',18);
xlabel('Time (hours)','FontSize',18);

%% zoom to spots
dt = 0.01; ymax = 20000; lw = 1.5;
inc = 600;

bin_pre4hr = [-4*3600:dt:-4*3600+inc];
bin_pre2hr = [-2.1*3600:dt:-2.1*3600+inc];
bin_post1hr = [1*3600:dt:1*3600+inc];
bin_post6hr = [6*3600:dt:6*3600+inc];
bin_post12hr = [12.1*3600:dt:12.1*3600+inc];
bin_post24hr = [24.01*3600:dt:24.01*3600+inc];

asdr_pre4hr = histc(time,bin_pre4hr);
asdr_pre2hr = histc(time,bin_pre2hr);
asdr_post1hr = histc(time,bin_post1hr);
asdr_post6hr = histc(time,bin_post6hr);
asdr_post12hr = histc(time,bin_post12hr);
asdr_post24hr = histc(time,bin_post24hr);

close all;
f3 = figure(3);
set(f3,'Position',[1360 438 830 495]);
subplot(3,2,1);
stairs(bin_pre4hr-min(bin_pre4hr),asdr_pre4hr/dt,'-k','LineWidth',lw);
subplot(3,2,2);
stairs(bin_pre2hr-min(bin_pre2hr),asdr_pre2hr/dt,'-k','LineWidth',lw);
subplot(3,2,3);
stairs(bin_post1hr-min(bin_post1hr),asdr_post1hr/dt,'-k','LineWidth',lw);
subplot(3,2,4);
stairs(bin_post6hr-min(bin_post6hr),asdr_post6hr/dt,'-k','LineWidth',lw);
subplot(3,2,5);
stairs(bin_post12hr-min(bin_post12hr),asdr_post12hr/dt,'-k','LineWidth',lw);
subplot(3,2,6);
stairs(bin_post24hr-min(bin_post24hr),asdr_post24hr/dt,'-k','LineWidth',lw);

for i = 1:6
    subplot(3,2,i);
    box off;
    axis off;
    ylim([0 30000]);
%     ylim([0 4000]);
end
% subplot(3,2,1); hold on;
% plot([10 130],[1000 1000],'-k','LineWidth',3);
% plot([10 10],[1000 3000],'-k','LineWidth',3);


subplot(3,2,1); xlim([145.17-0.25 145.17+1.25]);
subplot(3,2,2); xlim([13.76-0.25 13.76+1.25]);
subplot(3,2,3); xlim([194.85-0.25 194.85+1.25]); hold on; plot([194.74 194.74+0.01],[29000 29000],'-b','LineWidth',10);
subplot(3,2,4); xlim([26.76-0.25 26.76+1.25]); hold on; plot([26.67 26.67+0.01],[29000 29000],'-b','LineWidth',10);
subplot(3,2,5); xlim([83.67-0.25 83.67+1.25]); hold on; plot([83.6 83.6+0.01],[29000 29000],'-b','LineWidth',10);
subplot(3,2,6); xlim([88.82-0.25 88.82+1.25]); hold on; plot([88.74 88.74+0.01],[29000 29000],'-b','LineWidth',10);
plot([89.5 89.75],[10000 10000],'-k','LineWidth',3)
plot([89.5 89.5],[10000 20000],'-k','LineWidth',3)


%% TTX plot: venus20629 %%
load C:\Users\USER\Documents\mingfaifong\2012-04-20_venus20628_8-11DIV_MEA_TTX\venus20628_8-10DIV.mat;
load C:\Users\USER\Documents\mingfaifong\2013-01-30_vivy_MEA-analysis\vivy21367_10-12DIV.mat
w = [36 37 44 45 52 53 60 61 62];
time = time(ismember(channel,w)==1);
time = [time; repmat(time(time<0),[10,1])];

%% TTX+CTZ plot: venus20628 %%
%load C:\Users\USER\Documents\mingfaifong\2012-04-20_venus20629_8-11DIV_MEA_TTX-CTZ\venus20629_8-10DIV.mat;
load C:\Users\USER\Documents\mingfaifong\2013-01-30_vivy_MEA-analysis\vivy21367_10-12DIV.mat
w = [7 14 15 16 22 23 24 30 31];
time = time(ismember(channel,w)==1);
time = [time; repmat(time(time<0),[13,1])];

%% control plot: vivy21367A %%
%load C:\Users\USER\Documents\mingfaifong\2012-04-20_venus20629_8-11DIV_MEA_TTX-CTZ\venus20629_8-10DIV.mat;
load C:\Users\USER\Documents\mingfaifong\2013-01-30_vivy_MEA-analysis\vivy21367_10-12DIV.mat
w = [3 4 5 12 13 20 21 28 29];
time = time(ismember(channel,w)==1);
time = [time; repmat(time,[8,1])];

%% adjust plot features %%

f1 = figure(1);
set(f1,'Position',[806 604 1752 343]);
hold off;
plotcolors = {[0 0 0],[0 0.2 0.7],[0.9 0 0],[0.1 0.7 0.1],[1 0.5 0],[0.6 0 0.7]}; %black, blue, red, green, orange, purple


dt = 3600; ymax = 40; lw = 3;
% dt = 300; ymax = 80; lw = 1.5;
% dt = 60; ymax = 200; lw = 1;
% dt = 1; ymax = 6000; lw = 1;
%dt = 0.1; ymax = 20000; lw = 1;

bin = [ceil(min(time)/dt)*dt:dt:floor(max(time)/dt)*dt];
asdr = histc(time,bin);

stairs(bin/3600,asdr/dt,'-k','LineWidth',lw);
hold on;
% plot([0 24.25],[ymax*.98 ymax*.98],'-k','LineWidth',8,'Color',plotcolors{2});
% plot([0 24.25],[ymax*.9 ymax*.9],'-k','LineWidth',8,'Color',plotcolors{4});

xlim([-6 24.5]);
ylim([0 ymax]);
set(gca,'XTick',[-6:2:24],'TickDir','out','TickLength',[0.005 0.025],'FontSize',16,'LineWidth',2);
box off;

ylabel('Array-wide firing rate (Hz)','FontSize',18);
xlabel('Time (hours)','FontSize',18);

%% control plots

% load('C:\Users\USER\Documents\mingfaifong\pre-2012 (need to sort)\2011-06-04_orion359_15-18DIV_MEA_ChR2-stim-OL\orion359_nowaveform.mat')
% load('C:\Users\USER\Documents\mingfaifong\pre-2012 (need to sort)\2011-07-30_yoda359_8-11DIV_MEA_vehicle\yoda359_nowaveform.mat');
% time = spktimes(cleanspk==1)*2;
% channel = channels (cleanspk==1);
load C:\Users\USER\Documents\mingfaifong\2013-01-21_trouble22468_12-14DIV_MEA_CNQX-ChR2-stim-CL\trouble22468_12-14DIV.mat;
channel = channel(time<=1*3600 & time>=-13*3600);
unit = unit(time<=1*3600 & time>=-13*3600);
time = time(time<=1*3600 & time>=-13*3600);

f1 = figure(1);
set(f1,'Position',[1625 501 691 343]);
hold off;
plotcolors = {[0 0 0],[0 0.2 0.7],[0.9 0 0],[0.1 0.7 0.1],[1 0.5 0],[0.6 0 0.7]}; %black, blue, red, green, orange, purple

dt = 3600; ymax = 40; lw = 3;
dt = 300; ymax = 50; lw = 1.5;
dt = 60; ymax = 100; lw = 1;
dt = 1; ymax = 4000; lw = 1;
% dt = 0.01; ymax = 20000; lw = 1;

bin = [ceil(min(time)/dt)*dt:dt:floor(max(time)/dt)*dt];
asdr = histc(time,bin);

stairs(bin/3600+12,asdr/dt,'-k','LineWidth',lw);
hold on;
% plot([0 24.25],[ymax*.98 ymax*.98],'-k','LineWidth',8,'Color',plotcolors{3});
% plot([300/3600 24.25],[ymax*.9 ymax*.9],'-k','LineWidth',8,'Color',plotcolors{6});
% 
xlim([0 12]);
ylim([0 ymax]);
set(gca,'XTick',[-6:2:24],'TickDir','out','TickLength',[0.005 0.025],'FontSize',16,'LineWidth',2);
box off;

ylabel('Array-wide firing rate (Hz)','FontSize',18);
xlabel('Time (hours)','FontSize',18);

%%
f3 = figure(3);
set(gcf,'Position',[1173 720 1380 227]);
hold off;
plotcolors = {[0 0 0],[0 0.2 0.7],[0.9 0 0],[0.1 0.7 0.1],[1 0.5 0],[0.6 0 0.7]}; %black, blue, red, green, orange, purple

hold off;
h1 = plot(time/3600+12,channel+0.1*randn(size(unit)),'.k','MarkerSize',1);
xlim([0 12]);
ylim([1 64]);
set(gca,'XTick',[-6:2:24],'YTick',[1 64],'TickDir','out','TickLength',[0.005 0.025],'FontSize',16,'LineWidth',2);
box off;
ylabel('Electrode Number','FontSize',18);
xlabel('Time (hours)','FontSize',18);
%%
xlim([1 2]);
set(h1,'MarkerSize',2);
set(gca,'XTick',[1:1/6:2],'XTickLabel',[0:10:60]);
xlabel('Time (minutes)');

%%
xlim([1.42 1.42+300/3600]);
set(h1,'MarkerSize',3);
set(gca,'XTick',[1.42:300/3600/5:1.42+300/3600],'XTickLabel',[0:1:5]);
xlabel('Time (minutes)');


%%
xlim([1.4294 1.4294+10/3600]);
set(h1,'MarkerSize',5);
set(gca,'XTick',[1.4294:10/3600/10:1.4294+10/3600],'XTickLabel',[0:1:10]);
xlabel('Time (seconds)');

%%
xlim([1.4295 1.4295+1/3600]);
set(h1,'MarkerSize',5);
set(gca,'XTick',[1.4295:1/3600/5:1.4295+1/3600],'XTickLabel',[0:200:1000]);
xlabel('Time (ms)');