
set(0, 'DefaultLineLineWidth', 2);
set(0,'defaultTextFontSize', 12);
set(0,'defaultAxesFontSize',12);
set(0,'DefaultFigureColormap',jet)
set(0,'defaultfigurecolor',[1 1 1])
set(0,'DefaultAxesTitleFontWeight', 'normal')
set(0, 'DefaultAxesFontName', 'Arial');

%%
clear all
close all
clc
fname = 'OXYT011_005_220224_160927';
load([fname,'_Oxt_ripples.mat'],'Oxt_ripples')
%
fs = 20e3;
Tstart = Oxt_ripples.alignTim(1)/fs;
Tend = Oxt_ripples.alignTim(2)/fs;

fs_dff = 100;

N_dFF = length(Oxt_ripples.dFoverF);
taxis = (0:1:N_dFF-1)/fs_dff;
ripple_times = Oxt_ripples.ripples.peaks -Tstart;
%%
figure();
set(gcf,'Position',[100 100 600 250])
plot(taxis,100*Oxt_ripples.dFoverF,'r');
hold on
xline(ripple_times,'-.','Color',[0 0.4470 0.7410])
xlim([7400 7900])
box off
exportgraphics(gcf,[fname,'_Oxt_ripples.pdf'],'BackgroundColor','none','ContentType','vector')
xlim([7750 7800])
exportgraphics(gcf,[fname,'_Oxt_ripples_zoomed.pdf'],'BackgroundColor','none','ContentType','vector')

%%
dFoverF = zscore(Oxt_ripples.dFoverF);
dFF_event_mean = [];
count_ = 0;
time_window = 20;
for i = 1:length(ripple_times)
    idx = round(ripple_times(i)*fs_dff);
    if (idx-time_window*fs_dff >0) && (idx+time_window*fs_dff<N_dFF)
        a = dFoverF(idx-time_window*fs_dff:idx+time_window*fs_dff);
        dFF_event_mean = [dFF_event_mean a];
    end
end
%%
t_plot = -time_window:1/fs_dff:time_window;
figure();
subplot(2,1,1)
set(gcf,'Position',[100 100 400 700])
h = pcolor(t_plot,1:size(dFF_event_mean,2),dFF_event_mean');
set(h, 'EdgeColor', 'none');
shading interp;
set(gca,'YDir','reverse')
clim([-3,3])
colormap('parula')
%colormap(brewermap([],'-RdBu'));
colorbar
subplot(2,1,2)
plot(t_plot,(mean(dFF_event_mean,2)),'Color','r')
box off

%%
dt = 1;
t_edges = [0:dt:(N_dFF/fs_dff)];
[N,edges,bin] = histcounts(ripple_times,t_edges);
[~,id]=find(N>0);
dFF_event_mean2 = [];
count_ = 0;
time_window = 20;
for i = 1:length(id)
    idx = round((edges(id(i))+dt/2)*fs_dff);
    if (idx-time_window*fs_dff >0) && (idx+time_window*fs_dff<N_dFF)
        a = dFoverF(idx-time_window*fs_dff:idx+time_window*fs_dff);
        dFF_event_mean2 = [dFF_event_mean2 a];
    end
end

t_plot = -time_window:1/fs_dff:time_window;
figure();
subplot(2,1,1)
set(gcf,'Position',[100 100 400 700])
h = pcolor(t_plot,1:size(dFF_event_mean2,2),dFF_event_mean2');
set(h, 'EdgeColor', 'none');
shading interp;
set(gca,'YDir','reverse')
clim([-3,3])
colormap('parula')
%colormap(brewermap([],'-RdBu'));
colorbar
subplot(2,1,2)
plot(t_plot,(mean(dFF_event_mean2,2)),'Color','r');
hold on
plot(t_plot,(mean(dFF_event_mean,2)),'Color','m');

box off



%%
time_window = 20;
burtsWindow = 1;
[dFF_event_mean] = GetRate(ripple_times,zscore(Oxt_ripples.dFoverF),burtsWindow,1,20);

t_plot = -time_window:1/fs_dff:time_window;
figure();
set(gcf,'Position',[100 100 400 300])
plot(t_plot,(mean(dFF_event_mean,2)),'Color',[1, 0, 0, 0.4]);
hold on
[dFF_event_mean] = GetRate(ripple_times,zscore(Oxt_ripples.dFoverF),burtsWindow,2,20);
plot(t_plot,(mean(dFF_event_mean,2)),'Color',[1, 0, 0, 0.6]);
[dFF_event_mean] = GetRate(ripple_times,zscore(Oxt_ripples.dFoverF),burtsWindow,3,20);
plot(t_plot,(mean(dFF_event_mean,2)),'Color',[1, 0, 0, 1.0]);
box off


%% To calculate the band power:
burtsWindow = 1;
[dFF_event_mean1,ripplePower1] = GetRate(ripple_times,zscore(Oxt_ripples.dFoverF),Oxt_ripples.lfpData.data,burtsWindow,1,20,Tstart);
[dFF_event_mean2,ripplePower2] = GetRate(ripple_times,zscore(Oxt_ripples.dFoverF),Oxt_ripples.lfpData.data,burtsWindow,2,20,Tstart);
[dFF_event_mean3,ripplePower3] = GetRate(ripple_times,zscore(Oxt_ripples.dFoverF),Oxt_ripples.lfpData.data,burtsWindow,3,20,Tstart);

t_plot = -time_window:1/fs_dff:time_window;
figure();
set(gcf,'Position',[100 100 600 600])
subplot(2,1,1)
plot(t_plot,(mean(dFF_event_mean1,2)),'Color',[1, 0, 0, 0.4]);
hold on
plot(t_plot,(mean(dFF_event_mean2,2)),'Color',[1, 0, 0, 0.7]);
plot(t_plot,(mean(dFF_event_mean3,2)),'Color',[1, 0, 0, 1.0]);
box off
legend('Low Rate','High Rate')
tplot2 = linspace(-1*time_window,time_window, length(ripplePower1));
subplot(2,1,2)
plot(tplot2,ripplePower1,'Color',[0 0.4470 0.7410,0.4]);
hold on
plot(tplot2,ripplePower2,'Color',[0 0.4470 0.7410,0.7]);
plot(tplot2,ripplePower3,'Color',[0 0.4470 0.7410,1.0]);
box off
legend('Low Rate','High Rate')


%%
clear all
clc

fnames = {'OXYT010_004_220126_112539';
         'OXYT011_005_220224_160927';
         'OXYT012_001_220323_130702';
         'OXYT014_001_220429_155806'};
    %
fs = 20e3;
fs_dff = 100;
time_window = 20;
dFF_event_all = zeros([2*time_window*fs_dff+1,3,length(fnames) ]);
Ripple_event_all = zeros([81,3,length(fnames)]);
for i = 1:length(fnames)

    load([fnames{i},'_Oxt_ripples.mat'],'Oxt_ripples');
    Tstart = Oxt_ripples.alignTim(1)/fs;
    Tend = Oxt_ripples.alignTim(2)/fs;
    ripple_times = Oxt_ripples.ripples.peaks -Tstart;
    
    LFPs = Oxt_ripples.lfpData.data(Oxt_ripples.lfpData.timestamps>Tstart);
    Oxt_ripples.lfpData.data = LFPs;
    times_ = Oxt_ripples.lfpData.timestamps(Oxt_ripples.lfpData.timestamps>Tstart)-Tstart;
    Oxt_ripples.lfpData.timestamps = times_;
    burtsWindow = 1;
    [dFF_event_mean1,ripplePower1] = GetRate(ripple_times,zscore(Oxt_ripples.dFoverF),Oxt_ripples.lfpData.data,burtsWindow,1,20);
    [dFF_event_mean2,ripplePower2] = GetRate(ripple_times,zscore(Oxt_ripples.dFoverF),Oxt_ripples.lfpData.data,burtsWindow,2,20);
    [dFF_event_mean3,ripplePower3] = GetRate(ripple_times,zscore(Oxt_ripples.dFoverF),Oxt_ripples.lfpData.data,burtsWindow,3,20);
    
    dFF_event_all (:,1,i) = mean(dFF_event_mean1,2);
    dFF_event_all (:,2,i) = mean(dFF_event_mean2,2);
    dFF_event_all (:,3,i) = mean(dFF_event_mean3,2);
    Ripple_event_all (:,1,i) = ripplePower1;
    Ripple_event_all (:,2,i) = ripplePower2;
    Ripple_event_all (:,3,i) = ripplePower3;
    i
end


%%
t_plot = -time_window:1/fs_dff:time_window;
figure();
set(gcf,'Position',[100 100 400 600])
[Nt,Nc,Na] = size(dFF_event_all);
meanBaseLines = squeeze(mean(dFF_event_all(1:5*fs_dff,1,:),1));
dFF_plot = squeeze(dFF_event_all(:,1,:))-meanBaseLines';
options.handle     = figure(1);
subplot(2,1,1)
options.color_area = [0, 0.4,  0];   
options.color_line = [0, 0.4,  0, 0.4];
options.alpha      = 0.1;
options.line_width = 2;
options.error      = 'sem';
options.x_axis = t_plot;
plot_areaerrorbar(dFF_plot', options)
%plot(t_plot,mean(dFF_plot,2),'Color',[1, 0, 0, 0.4]);
hold on
meanBaseLines = squeeze(mean(dFF_event_all(1:5*fs_dff,2,:),1));
dFF_plot = squeeze(dFF_event_all(:,2,:))-meanBaseLines';
options.color_area = [0, 0.4,  0];   
options.color_line = [0, 0.4,  0, 0.7];
options.alpha      = 0.2;
plot_areaerrorbar(dFF_plot', options)
%plot(t_plot,mean(dFF_plot,2),'Color',[1, 0, 0, 0.7]);

meanBaseLines = squeeze(mean(dFF_event_all(1:5*fs_dff,3,:),1));
dFF_plot = squeeze(dFF_event_all(:,3,:))-meanBaseLines';
options.color_area = [0, 0.4,  0];   
options.color_line = [0, 0.4,  0, 1.0];
options.alpha      = 0.3;
plot_areaerrorbar(dFF_plot', options)
%plot(t_plot,mean(dFF_plot,2),'Color',[1, 0, 0, 1.0]);
box off
legend('Low Rate','Mid','High Rate')

tplot2 = linspace(-1*time_window,time_window, size(Ripple_event_all,1));
options.handle     = figure(1);
subplot(2,1,2)
options.color_area = [0 0 0];   
options.color_line = [0 0.0 0.0,0.3];
options.alpha      = 0.1;
options.line_width = 2;
options.error      = 'sem';
options.x_axis = tplot2;
plot_areaerrorbar(squeeze(Ripple_event_all(:,1,:))', options)
%plot(tplot2,squeeze(mean(Ripple_event_all(:,1,:),3)),'Color',[0 0.4470 0.7410,0.4]);
hold on
options.color_line = [0 0.0 0.0,0.6];
options.alpha      = 0.2;
plot_areaerrorbar(squeeze(Ripple_event_all(:,2,:))', options)
options.color_line = [0 0.0 0.0,1];
options.alpha      = 0.3;
plot_areaerrorbar(squeeze(Ripple_event_all(:,3,:))', options)
%plot(tplot2,squeeze(mean(Ripple_event_all(:,2,:),3)),'Color',[0 0.4470 0.7410,0.7]);
%plot(tplot2,squeeze(mean(Ripple_event_all(:,3,:),3)),'Color',[0 0.4470 0.7410,1.0]);
box off
legend('Low Rate','Mid','High Rate')
xlabel('Time (s)')
exportgraphics(gcf,'All_Oxt_ripples.pdf','BackgroundColor','none','ContentType','vector')

%% ALL RIPPLE
%%
clear all
clc
fnames = {'OXYT010_004_220126_112539';
         'OXYT011_005_220224_160927';
         'OXYT012_001_220323_130702';
         'OXYT014_001_220429_155806'};
    %
    %
fs = 20e3;
fs_dff = 100;
time_window = 20;
dFF_event_all = zeros([2*time_window*fs_dff+1,length(fnames) ]);
Ripple_event_all = zeros([81,length(fnames)]);
for i = 1:length(fnames)

    load([fnames{i},'_Oxt_ripples.mat'],'Oxt_ripples');
    Tstart = Oxt_ripples.alignTim(1)/fs;
    Tend = Oxt_ripples.alignTim(2)/fs;
    ripple_times = Oxt_ripples.ripples.peaks -Tstart;
    
    LFPs = Oxt_ripples.lfpData.data(Oxt_ripples.lfpData.timestamps>Tstart);
    Oxt_ripples.lfpData.data = LFPs;
    times_ = Oxt_ripples.lfpData.timestamps(Oxt_ripples.lfpData.timestamps>Tstart)-Tstart;
    Oxt_ripples.lfpData.timestamps = times_;
    burtsWindow = 1;
    [dFF_event_mean,ripplePower] = GetRate_allRipple(ripple_times,zscore(Oxt_ripples.dFoverF),Oxt_ripples.lfpData.data,burtsWindow,0,20);
    
    dFF_event_all (:,i) = mean(dFF_event_mean,2);
    Ripple_event_all (:,i) = ripplePower;
    i
end

%%
t_plot = -time_window:1/fs_dff:time_window;
figure();
set(gcf,'Position',[100 100 400 400])
tbaseline = 10;
%subplot(2,1,1)
[Nt,Na] = size(dFF_event_all);
meanBaseLines = squeeze(mean(dFF_event_all(1:tbaseline*fs_dff,:),1));
dFF_plot_Oxt= dFF_event_all-meanBaseLines;
yyaxis right
%plot(t_plot,mean(dFF_plot_Oxt,2),'Color',[0,0.4, 0, 1.0]);
ax =gca;
ax.YAxis(2).Color = [0, 0.4,  0];
box off
options.handle     = figure(1);
tbaseline = 10;
options.color_area = [0, 0.4,  0];   
options.color_line = [0, 0.4,  0, 1.0];
options.alpha      = 0.1;
options.line_width = 2;
options.error      = 'sem';
options.x_axis = t_plot;
plot_areaerrorbar(dFF_plot_Oxt', options)
% tplot2 = linspace(-1*time_window,time_window, size(Ripple_event_all,1));
% subplot(2,1,2)
% plot(tplot2,squeeze(Ripple_event_all(:,:)),'Color',[0 0.4470 0.7410,0.4]);
exportgraphics(gcf,'All_Oxt_ripples.pdf','BackgroundColor','none','ContentType','vector')


