clear;



%% ASDR full plot %%
figure('units','normalized','outerposition',[0 0 1 1]);
h_tight = tight_subplot(1,4,[.04 .05],[.15 .08],[.1 .01]);
%subplot(3,1,1);

for i = 1:4
axes(h_tight(i));

[evt_times evt_text] = get_events('yoda359')
load('yoda359/yoda359_nowaveform.SS','-mat')

hold off;
SS.ASDR(1,0,0);
asdr_times = SS.asdr(:,1);
asdr_value = SS.asdr(:,2);
asdr_times_shifted = asdr_times-evt_times(1);

for m = 1:5
    plot([evt_times(m)-evt_times(1) evt_times(m)-evt_times(1)],[0 max(asdr_value+100)],...
        '--','Color',[0.7 0.7 0.7],'LineWidth',3)
    hold on;
end

plot(asdr_times_shifted,asdr_value,'-k','LineWidth',1.5)
xlim([min(asdr_times_shifted) max(asdr_times_shifted)]);
ylim([0 max(asdr_value)]);

%set(gca,'XTick',3600*ceil(min(asdr_times_shifted)/3600):3600*4:3600*floor(max(asdr_times_shifted)/3600))
%set(gca,'XTickLabel',[ceil(min(asdr_times_shifted)/3600):1*4:floor(max(asdr_times_shifted)/3600)])

set(gca,'XTick',3600*[-20:.25:80],'XTickLabel',[-20:.25:80],'TickDir','out','TickLength',[0.002 0.025],'FontSize',16);
%xlabel('Time (hours)');
%ylabel(['Array-wide Spike Detection Rate (spikes/second)']);

xlim([-20.5*3600 48.5*3600]); ylim([0 2000]);

[evt_times evt_text] = get_events('yoda363')
load('yoda363/yoda363_nowaveform.SS','-mat')
SS.ASDR(1,0,0);
asdr_times = SS.asdr(:,1);
asdr_value = SS.asdr(:,2);
asdr_times_shifted = asdr_times-evt_times(1);
plot(asdr_times_shifted,asdr_value,'-b','LineWidth',2.5)

end

%% Burstiness full plot %%

%subplot(3,1,2);
axes(h_tight(2));

[evt_times evt_text] = get_events('yoda359');
load('yoda359/yoda359_BIfilt');
asdr_times_shifted = asdr_times-evt_times(1);

for m = 1:5
    plot([evt_times(m)-evt_times(1) evt_times(m)-evt_times(1)],[0 1],...
        '--','Color',[0.7 0.7 0.7],'LineWidth',3);
    hold on;
end
plot(asdr_times_shifted,BI,'-k');
xlim([min(asdr_times_shifted) max(asdr_times_shifted)]);
ylim([0 1]);

[evt_times evt_text] = get_events('yoda363');
load('yoda363/yoda363_BIfilt')
asdr_times_shifted = asdr_times-evt_times(1);
plot(asdr_times_shifted,BI,'-b')

set(gca,'XTick',3600*[-20:4:80],'XTickLabel',[-20:4:80],'TickDir','out','TickLength',[0.002 0.025],'FontSize',16);
%xlabel('Time (hours)');
ylabel(['Burstiness Index']);

xlim([-20.5*3600 48.5*3600]); ylim([0 1]);

%% ASDR Median Filter %%
%figure;
%subplot(3,1,3);
axes(h_tight(3));

[evt_times evt_text] = get_events('yoda359');
load('yoda359/yoda359_asdr_medfilt');
asdr_times_shifted = asdr_times-evt_times(1);
hold off;

for m = 1:5
    plot([evt_times(m)-evt_times(1) evt_times(m)-evt_times(1)],[0 25],...
        '--','Color',[0.7 0.7 0.7],'LineWidth',3);
    hold on;
end
plot(asdr_times_shifted,smooth(asdr_med_1min,60*5),'-k');
xlim([min(asdr_times_shifted) max(asdr_times_shifted)]);
ylim([0 max(asdr_med_1min)]);

[evt_times evt_text] = get_events('yoda363');
load('yoda363/yoda363_asdr_medfilt')
asdr_times_shifted = asdr_times-evt_times(1);
plot(asdr_times_shifted,smooth(asdr_med_1min,60*5),'-b')

set(gca,'XTick',3600*[-20:4:80],'XTickLabel',[-20:4:80],'TickDir','out','TickLength',[0.002 0.025],'FontSize',16);
xlabel('Time (hours)'); ylabel(['Median ASDR',10,'(spikes/second)']);

xlim([-20.5*3600 48.5*3600]);
ylim([0 10]);

%% ASDR: Before & After %%


load('milton360/milton360_asdr_medfilt');
[evt_times evt_text] = get_events('milton360');

asdr_times_shifted = asdr_times-evt_times(3);
%structure: -12 to 0 hours, 0 to 12 hours, 0 to 24 hours, 0 to 48 hours
muASDR_ctrl = [mean(asdr_1sec(find(asdr_times_shifted>=-12*3600 & asdr_times_shifted<0))),...
    mean(asdr_1sec(find(asdr_times_shifted>=0 & asdr_times_shifted<12*3600))),...
    mean(asdr_1sec(find(asdr_times_shifted>=0 & asdr_times_shifted<24*3600))),...
    mean(asdr_1sec(find(asdr_times_shifted>=0 & asdr_times_shifted<48*3600)))];

load('kelso361/kelso361_asdr_medfilt');
[evt_times evt_text] = get_events('kelso361');
asdr_times_shifted = asdr_times-evt_times(3);
%structure: -12 to 0 hours, 0 to 12 hours, 0 to 24 hours, 0 to 48 hours
muASDR_cnqx = [mean(asdr_1sec(find(asdr_times_shifted>=-12*3600 & asdr_times_shifted<0))),...
    mean(asdr_1sec(find(asdr_times_shifted>=0 & asdr_times_shifted<12*3600))),...
    mean(asdr_1sec(find(asdr_times_shifted>=0 & asdr_times_shifted<24*3600))),...
    mean(asdr_1sec(find(asdr_times_shifted>=0 & asdr_times_shifted<48*3600)))];

subplot(1,3,1);
for i = 1:3
    subplot(1,3,i);
    plot(muASDR_ctrl(1),muASDR_ctrl(i+1),'.k','MarkerSize',15);
    hold all;
end
for i = 1:3
    subplot(1,3,i);
    plot(muASDR_cnqx(1),muASDR_cnqx(i+1),'ob','MarkerSize',15);
    hold all;
end

%% save
save muASDR_kelso muASDR*

%% plot before and after mean ASDR

fpath = 'C:\Users\USER\Documents\mingfaifong\muASDR\';
fpath_info = dir(fpath);      %get info about contents of directory
isfile = ~[fpath_info.isdir]; %get index for files only
fidlist = {fpath_info(isfile).name}; %create list of files

figure;

for j = 1:length(fidlist)
fid = fidlist{j};
load([fpath fid]);

muASDR_ctrl_all(j,:) = muASDR_ctrl;
muASDR_cnqx_all(j,:) = muASDR_cnqx;

end

subplot(1,3,1)
for k = 1:length(muASDR_ctrl_all)
    plot(muASDR_ctrl(k,1),muASDR_ctrl(k,2),'.','MarkerSize',15);
    hold on;
    plot(muASDR_cnqx(k,1),muASDR_cnqx(k,2),'.','MarkerSize',15);
    hold all;
end
    
for i = 1:3
    subplot(1,3,i);
    plot(muASDR_ctrl(1),muASDR_ctrl(i+1),'.','MarkerSize',15);
end
for i = 1:3
    subplot(1,3,i);
    plot(muASDR_cnqx(1),muASDR_cnqx(i+1),'o','MarkerSize',5);
    hold on;
end

for i = 1:3
    subplot(1,3,i);
    axis([0 45 0 45]);
    plot([0 100],[0 100],'--','Color',[0.7 0.7 0.7])
end