clear all
i=1;
while true
[file,path]=uigetfile('*.mat');
cd(path)
load(file)
HF_PC_compilation{1,i}=SourceFile;
HF_PC_compilation{2,i}=compilation;
i=i+1
cd ..
end

save('HF_DCNLFP_compilation.mat','HF_DCNLFP_compilation')

for i=1:length(HF_PC_compilation)
    hf_on_compilation{1,i}=HF_PC_compilation{1,i};
    hf_on_compilation{2,i}{2,1}="data";
    hf_on_compilation{2,i}{3,1}="shuffled";
    id=find(ismember(HF_PC_compilation{2,i}(3,:),"move"));
    for j=1:length(id)
      hf_on_compilation{2,i}{1,j+1}=mat2str(HF_PC_compilation{2,i}{2,id(j)});
      hf_on_compilation{2,i}{2,j+1}=HF_PC_compilation{2,i}{4,id(j)};
      hf_on_compilation{2,i}{3,j+1}=HF_PC_compilation{2,i}{7,id(j)};
    end
end
%save('hf_on_compilation.mat','hf_on_compilation')


for i=1:length(HF_PC_compilation)
    hf_off_compilation{1,i}=HF_PC_compilation{1,i};
    hf_off_compilation{2,i}{2,1}="data";
    hf_off_compilation{2,i}{3,1}="shuffled";
    id=find(ismember(HF_PC_compilation{2,i}(3,:),"rest"));
    for j=1:length(id)
      hf_off_compilation{2,i}{1,j+1}=mat2str(HF_PC_compilation{2,i}{2,id(j)});
      hf_off_compilation{2,i}{2,j+1}=HF_PC_compilation{2,i}{4,id(j)};
      hf_off_compilation{2,i}{3,j+1}=HF_PC_compilation{2,i}{7,id(j)};
    end
end
%save('hf_off_compilation.mat','hf_off_compilation')

HF_PC_compilation_All.raw=HF_PC_compilation;
HF_PC_compilation_All.move.all=hf_on_compilation;
HF_PC_compilation_All.rest.all=hf_off_compilation;

%% light on
HF_on_PI=[];
for i=1:length(hf_on_compilation)
    [~,l]=size(hf_on_compilation{2,i});
    for j=1:l-1
       id=find(hf_on_compilation{2,i}{2,j+1}(3,:)=="1"); 
       HF_on_PI=[HF_on_PI hf_on_compilation{2,i}{2,j+1}(5,id)];
    end
end
HF_on_PI_LFP1=HF_on_PI;

HF_on_PI=[];
for i=1:length(hf_on_compilation)
    [~,l]=size(hf_on_compilation{2,i});
    for j=1:l-1
       id=find(hf_on_compilation{2,i}{2,j+1}(3,:)=="9"); 
       HF_on_PI=[HF_on_PI hf_on_compilation{2,i}{2,j+1}(5,id)];
    end
end
HF_on_PI_LFP9=HF_on_PI;

HF_on_PI=[];
for i=1:length(hf_on_compilation)
    [~,l]=size(hf_on_compilation{2,i});
    for j=1:l-1
       id=find(hf_on_compilation{2,i}{2,j+1}(3,:)=="129"); 
       HF_on_PI=[HF_on_PI hf_on_compilation{2,i}{2,j+1}(5,id)];
    end
end
HF_on_PI_LFP129=HF_on_PI;

HF_on_PI=[];
for i=1:length(hf_on_compilation)
    [~,l]=size(hf_on_compilation{2,i});
    for j=1:l-1
       id=find(hf_on_compilation{2,i}{2,j+1}(3,:)=="201"); 
       HF_on_PI=[HF_on_PI hf_on_compilation{2,i}{2,j+1}(5,id)];
    end
end
HF_on_PI_LFP201=HF_on_PI;

s_HF_on_PI=[];
for i=1:length(hf_on_compilation)
    [~,l]=size(hf_on_compilation{2,i});
    for j=1:l-1
       id=find(hf_on_compilation{2,i}{3,j+1}(3,:)=="1"); 
       s_HF_on_PI=[s_HF_on_PI hf_on_compilation{2,i}{3,j+1}(5,id)];
    end
end
s_HF_on_PI_LFP1=s_HF_on_PI;

s_HF_on_PI=[];
for i=1:length(hf_on_compilation)
    [~,l]=size(hf_on_compilation{2,i});
    for j=1:l-1
       id=find(hf_on_compilation{2,i}{3,j+1}(3,:)=="9"); 
       s_HF_on_PI=[s_HF_on_PI hf_on_compilation{2,i}{3,j+1}(5,id)];
    end
end
s_HF_on_PI_LFP9=s_HF_on_PI;

s_HF_on_PI=[];
for i=1:length(hf_on_compilation)
    [~,l]=size(hf_on_compilation{2,i});
    for j=1:l-1
       id=find(hf_on_compilation{2,i}{3,j+1}(3,:)=="129"); 
       s_HF_on_PI=[s_HF_on_PI hf_on_compilation{2,i}{3,j+1}(5,id)];
    end
end
s_HF_on_PI_LFP129=s_HF_on_PI;

s_HF_on_PI=[];
for i=1:length(hf_on_compilation)
    [~,l]=size(hf_on_compilation{2,i});
    for j=1:l-1
       id=find(hf_on_compilation{2,i}{3,j+1}(3,:)=="201"); 
       s_HF_on_PI=[s_HF_on_PI hf_on_compilation{2,i}{3,j+1}(5,id)];
    end
end
s_HF_on_PI_LFP201=s_HF_on_PI;


HF_PC_compilation_All.move.LFP1=HF_on_PI_LFP1;
HF_PC_compilation_All.move.LFP9=HF_on_PI_LFP9;
HF_PC_compilation_All.move.LFP129=HF_on_PI_LFP129;
HF_PC_compilation_All.move.LFP201=HF_on_PI_LFP201;
HF_PC_compilation_All.move.s_LFP1=s_HF_on_PI_LFP1;
HF_PC_compilation_All.move.s_LFP9=s_HF_on_PI_LFP9;
HF_PC_compilation_All.move.s_LFP129=s_HF_on_PI_LFP129;
HF_PC_compilation_All.move.s_LFP201=s_HF_on_PI_LFP201;

%% light off 
HF_off_PI=[];
for i=1:length(hf_off_compilation)
    [~,l]=size(hf_off_compilation{2,i});
    for j=1:l-1
       id=find(hf_off_compilation{2,i}{2,j+1}(3,:)=="1"); 
       HF_off_PI=[HF_off_PI hf_off_compilation{2,i}{2,j+1}(5,id)];
    end
end
HF_off_PI_LFP1=HF_off_PI;

HF_off_PI=[];
for i=1:length(hf_off_compilation)
    [~,l]=size(hf_off_compilation{2,i});
    for j=1:l-1
       id=find(hf_off_compilation{2,i}{2,j+1}(3,:)=="9"); 
       HF_off_PI=[HF_off_PI hf_off_compilation{2,i}{2,j+1}(5,id)];
    end
end
HF_off_PI_LFP9=HF_off_PI;

HF_off_PI=[];
for i=1:length(hf_off_compilation)
    [~,l]=size(hf_off_compilation{2,i});
    for j=1:l-1
       id=find(hf_off_compilation{2,i}{2,j+1}(3,:)=="129"); 
       HF_off_PI=[HF_off_PI hf_off_compilation{2,i}{2,j+1}(5,id)];
    end
end
HF_off_PI_LFP129=HF_off_PI;

HF_off_PI=[];
for i=1:length(hf_off_compilation)
    [~,l]=size(hf_off_compilation{2,i});
    for j=1:l-1
       id=find(hf_off_compilation{2,i}{2,j+1}(3,:)=="201"); 
       HF_off_PI=[HF_off_PI hf_off_compilation{2,i}{2,j+1}(5,id)];
    end
end
HF_off_PI_LFP201=HF_off_PI;

s_HF_off_PI=[];
for i=1:length(hf_off_compilation)
    [~,l]=size(hf_off_compilation{2,i});
    for j=1:l-1
       id=find(hf_off_compilation{2,i}{3,j+1}(3,:)=="1"); 
       s_HF_off_PI=[s_HF_off_PI hf_off_compilation{2,i}{3,j+1}(5,id)];
    end
end
s_HF_off_PI_LFP1=s_HF_off_PI;

s_HF_off_PI=[];
for i=1:length(hf_off_compilation)
    [~,l]=size(hf_off_compilation{2,i});
    for j=1:l-1
       id=find(hf_off_compilation{2,i}{3,j+1}(3,:)=="9"); 
       s_HF_off_PI=[s_HF_off_PI hf_off_compilation{2,i}{3,j+1}(5,id)];
    end
end
s_HF_off_PI_LFP9=s_HF_off_PI;

s_HF_off_PI=[];
for i=1:length(hf_off_compilation)
    [~,l]=size(hf_off_compilation{2,i});
    for j=1:l-1
       id=find(hf_off_compilation{2,i}{3,j+1}(3,:)=="129"); 
       s_HF_off_PI=[s_HF_off_PI hf_off_compilation{2,i}{3,j+1}(5,id)];
    end
end
s_HF_off_PI_LFP129=s_HF_off_PI;

s_HF_off_PI=[];
for i=1:length(hf_off_compilation)
    [~,l]=size(hf_off_compilation{2,i});
    for j=1:l-1
       id=find(hf_off_compilation{2,i}{3,j+1}(3,:)=="201"); 
       s_HF_off_PI=[s_HF_off_PI hf_off_compilation{2,i}{3,j+1}(5,id)];
    end
end
s_HF_off_PI_LFP201=s_HF_off_PI;


HF_PC_compilation_All.rest.LFP1=HF_off_PI_LFP1;
HF_PC_compilation_All.rest.LFP9=HF_off_PI_LFP9;
HF_PC_compilation_All.rest.LFP129=HF_off_PI_LFP129;
HF_PC_compilation_All.rest.LFP201=HF_off_PI_LFP201;
HF_PC_compilation_All.rest.s_LFP1=s_HF_off_PI_LFP1;
HF_PC_compilation_All.rest.s_LFP9=s_HF_off_PI_LFP9;
HF_PC_compilation_All.rest.s_LFP129=s_HF_off_PI_LFP129;
HF_PC_compilation_All.rest.s_LFP201=s_HF_off_PI_LFP201;

save HF_PC_compilation_All.mat HF_PC_compilation_All


%% cumsum fig
PI_LFP1=str2double(HF_PC_compilation_All.move.LFP1);
PI_LFP9=str2double(HF_PC_compilation_All.move.LFP9);
PI_LFP129=str2double(HF_PC_compilation_All.move.LFP129);
PI_LFP201=str2double(HF_PC_compilation_All.move.LFP201);
s_PI_LFP1=str2double(HF_PC_compilation_All.move.s_LFP1);
s_PI_LFP9=str2double(HF_PC_compilation_All.move.s_LFP9);
s_PI_LFP129=str2double(HF_PC_compilation_All.move.s_LFP129);
s_PI_LFP201=str2double(HF_PC_compilation_All.move.s_LFP201);

%% 1
edge1=linspace(0,max(PI_LFP1),100);
figure()
h1=histogram(PI_LFP1,100,'Normalization','probability','BinEdges',edge1);
histdata{1,2}="LFP1";
histdata{2,1}="bin edges";
histdata{3,1}="y values";
histdata{2,2}=h1.BinEdges;
histdata{3,2}=h1.Values;
% title('Histogram WT Ch10')
xlabel('Polarity Index')
ylabel('Probability')
ylim([0,1])
xlim([0,0.5])
% saveas(gcf,'Histogram_WT_Ch10.fig')
% saveas(gcf,'Histogram_WT_Ch10.jpg')

figure()
h2=histogram(s_PI_LFP1,100,'Normalization','probability','BinEdges',edge1);
histdata{1,3}="sh_LFP1";
histdata{2,3}=h2.BinEdges;
histdata{3,3}=h2.Values;
%title('Histogram shuffled WT Ch10')
xlabel('Polarity Index')
ylabel('Probability')
ylim([0,1])
xlim([0,0.5])
% saveas(gcf,'Histogram_WT_shuffled_Ch10.fig')
% saveas(gcf,'Histogram_WT_shuffled_Ch10.jpg')

%% 2
edge2=linspace(0,max(PI_LFP9),100);
figure()
h3=histogram(PI_LFP9,100,'Normalization','probability','BinEdges',edge2);
histdata{1,4}="LFP9";
histdata{2,4}=h3.BinEdges;
histdata{3,4}=h3.Values;
%title('Histogram WT Ch11')
xlabel('Polarity Index')
ylabel('Probability')
ylim([0,1])
xlim([0,0.5])
% saveas(gcf,'Histogram_WT_Ch11.fig')
% saveas(gcf,'Histogram_WT_Ch11.jpg')

figure()
h4=histogram(s_PI_LFP9,100,'Normalization','probability','BinEdges',edge2);
histdata{1,5}="sh_LFP9";
histdata{2,5}=h4.BinEdges;
histdata{3,5}=h4.Values;
%title('Histogram shuffled WT Ch11')
xlabel('Polarity Index')
ylabel('Probability')
ylim([0,1])
xlim([0,0.5])
% saveas(gcf,'Histogram_WT_shuffled_Ch11.fig')
% saveas(gcf,'Histogram_WT_shuffled_Ch11.jpg')

%% 3
edge3=linspace(0,max(PI_LFP129),100);
figure()
h5=histogram(PI_LFP129,100,'Normalization','probability','BinEdges',edge3);
histdata{1,6}="LFP129";
histdata{2,6}=h5.BinEdges;
histdata{3,6}=h5.Values;
%title('Histogram WT Ch129')
xlabel('Polarity Index')
ylabel('Probability')
ylim([0,1])
xlim([0,0.8])
% saveas(gcf,'Histogram_WT_Ch129.fig')
% saveas(gcf,'Histogram_WT_Ch129.jpg')

figure()
h6=histogram(s_PI_LFP129,100,'Normalization','probability','BinEdges',edge3);
histdata{1,7}="sh_LFP129";
histdata{2,7}=h6.BinEdges;
histdata{3,7}=h6.Values;
%title('Histogram shuffled WT Ch129')
xlabel('Polarity Index')
ylabel('Probability')
ylim([0,1])
xlim([0,0.8])
% saveas(gcf,'Histogram_WT_shuffled_Ch129.fig')
% saveas(gcf,'Histogram_WT_shuffled_Ch129.jpg')

%% 4
edge4=linspace(0,max(PI_LFP201),100);
figure()
h7=histogram(PI_LFP201,100,'Normalization','probability','BinEdges',edge4);
histdata{1,8}="LFP201";
histdata{2,8}=h7.BinEdges;
histdata{3,8}=h7.Values;
%title('Histogram WT Ch129')
xlabel('Polarity Index')
ylabel('Probability')
ylim([0,1])
xlim([0,0.9])
% saveas(gcf,'Histogram_WT_Ch129.fig')
% saveas(gcf,'Histogram_WT_Ch129.jpg')

figure()
h8=histogram(s_PI_LFP201,100,'Normalization','probability','BinEdges',edge4);
histdata{1,9}="sh_LFP201";
histdata{2,9}=h8.BinEdges;
histdata{3,9}=h8.Values;
%title('Histogram shuffled WT Ch129')
xlabel('Polarity Index')
ylabel('Probability')
ylim([0,1])
xlim([0,0.9])
% saveas(gcf,'Histogram_WT_shuffled_Ch129.fig')
% saveas(gcf,'Histogram_WT_shuffled_Ch129.jpg')
%%
histdata{4,1}="CumSum";
for i=1:length(histdata)-1
    histdata{4,i+1}=cumsum(histdata{3,i+1});
end
save('HF_PC_histdata_edge.mat','histdata')

%%
ax1=(histdata{2,2}(1:end-1)+histdata{2,2}(2:end))/2;
ax9=(histdata{2,4}(1:end-1)+histdata{2,4}(2:end))/2;
ax129=(histdata{2,6}(1:end-1)+histdata{2,6}(2:end))/2;
ax201=(histdata{2,8}(1:end-1)+histdata{2,8}(2:end))/2;

% WT_ax10=(WT_histdata{2,2}(1:end-1)+WT_histdata{2,2}(2:end))/2;
% WT_ax11=(WT_histdata{2,4}(1:end-1)+WT_histdata{2,4}(2:end))/2;
% WT_ax129=(WT_histdata{2,6}(1:end-1)+WT_histdata{2,6}(2:end))/2;

figure()
hold on
plot(ax1,histdata{4,2},'Color',[0 0.4470 0.7410])
plot(ax1,histdata{4,3},'Color',[0.8500 0.3250 0.0980])
% plot(ax10,histdata{4,2},'Color',[0.9290 0.6940 0.1250])
% plot(ax10,histdata{4,3},'Color',[0.4660 0.6740 0.1880])
legend('Original 17J Data','Shuffled Data')
xlabel("Polarity Index")
ylabel("Cumulative Probability")
ylim([0 1])
xlim([0 1])
title(sprintf("LFP Ch1 (n=117)"))
hold off
saveas(gcf,sprintf("17J_PC_LFP_Ch1_(n=117)_PI_hist_cumsum.fig"))
saveas(gcf,sprintf("17J_PC_LFP_Ch1_(n=117)_PI_hist_cumsum.jpg"))

figure()
hold on
plot(ax9,histdata{4,4},'Color',[0 0.4470 0.7410])
plot(ax9,histdata{4,5},'Color',[0.8500 0.3250 0.0980])
% plot(ax10,histdata{4,2},'Color',[0.9290 0.6940 0.1250])
% plot(ax10,histdata{4,3},'Color',[0.4660 0.6740 0.1880])
legend('Original 17J Data','Shuffled Data')
xlabel("Polarity Index")
ylabel("Cumulative Probability")
ylim([0 1])
xlim([0 1])
title(sprintf("LFP Ch9 (n=117)"))
hold off
saveas(gcf,sprintf("17J_IO_LFP_Ch9_(n=117)_PI_hist_cumsum.fig"))
saveas(gcf,sprintf("17J_IO_LFP_Ch9_(n=117)_PI_hist_cumsum.jpg"))

figure()
hold on
plot(ax129,histdata{4,6},'Color',[0 0.4470 0.7410])
plot(ax129,histdata{4,7},'Color',[0.8500 0.3250 0.0980])
% plot(ax10,histdata{4,2},'Color',[0.9290 0.6940 0.1250])
% plot(ax10,histdata{4,3},'Color',[0.4660 0.6740 0.1880])
legend('Original 17J Data','Shuffled Data')
xlabel("Polarity Index")
ylabel("Cumulative Probability")
ylim([0 1])
xlim([0 1])
title(sprintf("LFP Ch129 (n=117)"))
hold off
saveas(gcf,sprintf("17J_IO_LFP_Ch129_(n=117)_PI_hist_cumsum.fig"))
saveas(gcf,sprintf("17J_IO_LFP_Ch129_(n=117)_PI_hist_cumsum.jpg"))

figure()
hold on
plot(ax201,histdata{4,8},'Color',[0 0.4470 0.7410])
plot(ax201,histdata{4,9},'Color',[0.8500 0.3250 0.0980])
% plot(ax10,histdata{4,2},'Color',[0.9290 0.6940 0.1250])
% plot(ax10,histdata{4,3},'Color',[0.4660 0.6740 0.1880])
legend('Original 17J Data','Shuffled Data')
xlabel("Polarity Index")
ylabel("Cumulative Probability")
ylim([0 1])
xlim([0 1])
title(sprintf("LFP Ch201 (n=117)"))
hold off
saveas(gcf,sprintf("17J_IO_LFP_Ch201_(n=117)_PI_hist_cumsum.fig"))
saveas(gcf,sprintf("17J_IO_LFP_Ch201_(n=117)_PI_hist_cumsum.jpg"))

%% 
L=117;
figure()
hold on
scatter(ones(1,L),str2double(HF_PC_compilation_All.move.LFP1))
scatter(ones(1,L)+0.5,str2double(HF_PC_compilation_All.move.s_LFP1))
legend("Original","Shuffled")
title("Ch1")
xlim([0,2.5])
ylabel("Polarity Index")
saveas(gcf,"17J_PC_PI_scatter_Ch1.fig")
saveas(gcf,"17J_PC_PI_scatter_Ch1.jpg")

figure()
hold on
scatter(ones(1,L),str2double(HF_PC_compilation_All.move.LFP9))
scatter(ones(1,L)+0.5,str2double(HF_PC_compilation_All.move.s_LFP9))
legend("Original","Shuffled")
title("Ch9")
xlim([0,2.5])
ylabel("Polarity Index")
saveas(gcf,"17J_PC_PI_scatter_Ch9.fig")
saveas(gcf,"17J_PC_PI_scatter_Ch9.jpg")

figure()
hold on
scatter(ones(1,L),str2double(HF_PC_compilation_All.move.LFP129))
scatter(ones(1,L)+0.5,str2double(HF_PC_compilation_All.move.s_LFP129))
legend("Original","Shuffled")
title("Ch129")
xlim([0,2.5])
ylabel("Polarity Index")
saveas(gcf,"17J_PC_PI_scatter_Ch129.fig")
saveas(gcf,"17J_PC_PI_scatter_Ch129.jpg")

figure()
hold on
scatter(ones(1,L),str2double(HF_PC_compilation_All.move.LFP201))
scatter(ones(1,L)+0.5,str2double(HF_PC_compilation_All.move.s_LFP201))
legend("Original","Shuffled")
title("Ch201")
xlim([0,2.5])
ylabel("Polarity Index")
saveas(gcf,"17J_PC_PI_scatter_Ch201.fig")
saveas(gcf,"17J_PC_PI_scatter_Ch201.jpg")