cd 'D:\ACC Data\HPC\57\day1'
clear all;
mousedata=table2array(readtable('XYtotalspike1.csv'));
mouse_posn=mousedata(:,4);
mouse_dir=mousedata(:,5);

upper=find(mouse_posn==65);
tempupper=diff(upper);
tempupper=find(tempupper<30);
tempupper=tempupper+1;
upper(tempupper)=[];

lower=find(mouse_posn==6);
templower=diff(lower);
templower=find(templower<30);
templower=templower+1;
lower(templower)=[];
all=sort([upper;lower]);

all_corrected=[];

for m=2:length(all);

temprun=mouse_posn(all((m*1)-1):all(m*1));
if abs(diff([max(temprun);min(temprun)]))>53;
    all_corrected=[all_corrected;all((m*1)-1);all(m*1)];
end;
end;

forward_runs=[];

for m=2:2:length(all_corrected);

tempdir=mouse_dir(all_corrected(m-1):all_corrected(m));
if mean(tempdir)>0.7 && mean(tempdir)<1.3;
    forward_runs=[forward_runs;all_corrected(m-1);all_corrected(m)];
end;
end;

backward_runs=[];

for m=2:2:length(all_corrected);

tempdir=mouse_dir(all_corrected(m-1):all_corrected(m));
if mean(tempdir)>1.7 && mean(tempdir)<2.3;
    backward_runs=[backward_runs;all_corrected(m-1);all_corrected(m)];
end;
end;

% %Check result
% figure;
% plot(mouse_posn)
% hold on
% scatter(backward_runs,mouse_posn(backward_runs),'k')
% scatter(forward_runs,mouse_posn(forward_runs),'r')

clearvars -except forward_runs backward_runs mousedata mouse_posn

PCs=table2array(readtable('pPC.csv'));
SCCs=table2array(readtable('SC.csv'));
mousedata=mousedata(:,6:end);
PCs=PCs(:,1);PCs=find(PCs>0);
SCCs=SCCs(:,1);SCCs=find(SCCs>0);

PCratemaps_forward=zeros(59,length(PCs),length(forward_runs)/2);
SCCratemaps_forward=zeros(59,length(SCCs),length(forward_runs)/2);
for mm=2:2:length(forward_runs);

    [N1,edges,binned_forward] = histcounts(mouse_posn(forward_runs(mm-1):forward_runs(mm)),[6:65]);
    occupancy=N1;
    occupancy=occupancy*(1/20);

    clear N1 N2

 trimmed=mousedata(forward_runs(mm-1):forward_runs(mm),:);   
 trimmed=trimmed(:,[PCs;SCCs]);
 tmat = repmat( mouse_posn(forward_runs(mm-1):forward_runs(mm)), 1, size(trimmed,2) );
 tempmousedata = tmat .* trimmed;

norm_ratemap=zeros(59,size(trimmed,2));
for nm=1:size(tempmousedata,2);
       [N1,edges,binned_forward] = histcounts(tempmousedata(:,nm),[6:65]);
    ratemap=N1./occupancy;
    norm_ratemap(:,nm)=ratemap';
    clear N1 N2;
 end;
norm_ratemap(isnan(norm_ratemap))=0;
PCratemaps_forward(:,:,mm/2)=norm_ratemap(:,1:length(PCs));
SCCratemaps_forward(:,:,mm/2)=norm_ratemap(:,length(PCs)+1:end);
end;

PCratemaps_backward=zeros(59,length(PCs),length(backward_runs)/2);
SCCratemaps_backward=zeros(59,length(SCCs),length(backward_runs)/2);
for mm=2:2:length(backward_runs);

    [N1,edges,binned_backward] = histcounts(mouse_posn(backward_runs(mm-1):backward_runs(mm)),[6:65]);
    occupancy=N1;
    occupancy=occupancy*(1/20);

    clear N1 N2

 trimmed=mousedata(backward_runs(mm-1):backward_runs(mm),:);   
 trimmed=trimmed(:,[PCs;SCCs]);
 tmat = repmat( mouse_posn(backward_runs(mm-1):backward_runs(mm)), 1, size(trimmed,2) );
 tempmousedata = tmat .* trimmed;

norm_ratemap=zeros(59,size(trimmed,2));
for nm=1:size(tempmousedata,2);
       [N1,edges,binned_backward] = histcounts(tempmousedata(:,nm),[6:65]);
    ratemap=N1./occupancy;
    norm_ratemap(:,nm)=ratemap';
    clear N1 N2;
 end;
norm_ratemap(isnan(norm_ratemap))=0;
PCratemaps_backward(:,:,mm/2)=norm_ratemap(:,1:length(PCs));
SCCratemaps_backward(:,:,mm/2)=norm_ratemap(:,length(PCs)+1:end);
end;

clearvars -except SCCratemaps_backward PCratemaps_backward PCratemaps_forward SCCratemaps_forward;
save 'Single_lap_ratemaps.mat' SCCratemaps_backward PCratemaps_backward PCratemaps_forward SCCratemaps_forward;

clear all
%  load Single_lap_ratemaps.mat
% 
%  forward_PC_UMAP=zeros(59,3,size(PCratemaps_forward,3));
%  backward_PC_UMAP=zeros(59,3,size(PCratemaps_backward,3));
%  forward_SCC_UMAP=zeros(59,3,size(PCratemaps_forward,3));
%  backward_SCC_UMAP=zeros(59,3,size(PCratemaps_backward,3));
% 
%  rng default
%  for mm=1:size(PCratemaps_forward,3);
% 
%      [reduced_data_PC_f, umap, clusterIdentifiers] = run_umap(PCratemaps_forward(:,:,mm), 'n_components', 3, 'randomize', 'false' );
%      [reduced_data_SCC_f, umap, clusterIdentifiers] = run_umap(SCCratemaps_forward(:,:,mm), 'n_components', 3, 'randomize', 'false' );  
%      close all;
%      forward_PC_UMAP(:,:,mm)=reduced_data_PC_f;
%      forward_SCC_UMAP(:,:,mm)=reduced_data_SCC_f;
%  end;
% 
%  for mm=1:size(PCratemaps_backward,3);
% 
%      [reduced_data_PC_f, umap, clusterIdentifiers] = run_umap(PCratemaps_backward(:,:,mm), 'n_components', 3, 'randomize', 'false' );
%      [reduced_data_SCC_f, umap, clusterIdentifiers] = run_umap(SCCratemaps_backward(:,:,mm), 'n_components', 3, 'randomize', 'false' ); 
%      close all;
%      backward_PC_UMAP(:,:,mm)=reduced_data_PC_f;
%      backward_SCC_UMAP(:,:,mm)=reduced_data_SCC_f;
%  end;
% 
%  clearvars -except forward_PC_UMAP backward_PC_UMAP forward_SCC_UMAP backward_SCC_UMAP;
% save 'UMAP_output.mat' forward_PC_UMAP backward_PC_UMAP forward_SCC_UMAP backward_SCC_UMAP;
% 
% figure
% scatter3(forward_PC_UMAP(:,1,1),forward_PC_UMAP(:,2,1),forward_PC_UMAP(:,3,1))
% hold on
% for mm=2:size(forward_PC_UMAP,3);
% scatter3(forward_PC_UMAP(:,1,mm),forward_PC_UMAP(:,2,mm),forward_PC_UMAP(:,3,mm))
% end;
% 
% figure;
% scatter3(forward_SCC_UMAP(:,1,1),forward_SCC_UMAP(:,2,1),forward_SCC_UMAP(:,3,1))
% hold on
% for mm=2:size(forward_SCC_UMAP,3);
% scatter3(forward_SCC_UMAP(:,1,mm),forward_SCC_UMAP(:,2,mm),forward_SCC_UMAP(:,3,mm))
% end;
% 
% figure
% scatter3(backward_PC_UMAP(:,1,1),backward_PC_UMAP(:,2,1),backward_PC_UMAP(:,3,1),'.')
% hold on
% for mm=2:size(backward_PC_UMAP,3);
% scatter3(backward_PC_UMAP(:,1,mm),backward_PC_UMAP(:,2,mm),backward_PC_UMAP(:,3,mm)','.')
% end;
% 
% figure;
% scatter3(backward_SCC_UMAP(:,1,1),backward_SCC_UMAP(:,2,1),backward_SCC_UMAP(:,3,1))
% hold on
% for mm=2:size(backward_SCC_UMAP,3);
% scatter3(backward_SCC_UMAP(:,1,mm),backward_SCC_UMAP(:,2,mm),backward_SCC_UMAP(:,3,mm))
% end;
% 
% 
% figure;
% plot(PCratemaps_backward(:,50,1))
% hold on
% for mm=2:size(PCratemaps_backward,3);
% plot(PCratemaps_backward(:,50,mm))
% end;
% 
% 
% 
% 
% 
% backward_PC_UMAP=zeros(118,3,size(PCratemaps_backward,3));
% 
% for mm=1:size(PCratemaps_backward,3);
% 
%      [reduced_data_PC_f, umap, clusterIdentifiers] = run_umap(PCratemaps_backward(:,:,mm), 'n_components', 3,'n_neighbors',100,   'randomize', 'false' );
%     % [reduced_data_SCC_f, umap, clusterIdentifiers] = run_umap(SCCratemaps_backward(:,:,mm), 'n_components', 3, 'randomize', 'false' ); 
%      close all;
%      backward_PC_UMAP(:,:,mm)=reduced_data_PC_f;
%     % backward_SCC_UMAP(:,:,mm)=reduced_data_SCC_f;
%  end;
% 
% figure
%  patch(backward_PC_UMAP(:,1,1),backward_PC_UMAP(:,2,1),backward_PC_UMAP(:,3,1),c(:,1),'FaceColor','none','EdgeColor','interp')
% hold on
% for mm=2:size(backward_PC_UMAP,3);
%  patch(backward_PC_UMAP(:,1,mm),backward_PC_UMAP(:,2,mm),backward_PC_UMAP(:,3,mm),c(:,1),'FaceColor','none','EdgeColor','interp')
% end;
% 
% 
% 
%  backward_SCC_UMAP=zeros(118,3,size(SCCratemaps_backward,3));
% 
% for mm=1:size(SCCratemaps_backward,3);
% 
%      [reduced_data_PC_f, umap, clusterIdentifiers] = run_umap(SCCratemaps_backward(:,:,mm), 'n_components', 3,'n_neighbors',100,   'randomize', 'false' );
%     % [reduced_data_SCC_f, umap, clusterIdentifiers] = run_umap(SCCratemaps_backward(:,:,mm), 'n_components', 3, 'randomize', 'false' ); 
%      close all;
%      backward_SCC_UMAP(:,:,mm)=reduced_data_PC_f;
%     % backward_SCC_UMAP(:,:,mm)=reduced_data_SCC_f;
%  end;
% 
% 
% figure
%  patch(backward_SCC_UMAP(:,1,1),backward_SCC_UMAP(:,2,1),backward_SCC_UMAP(:,3,1),c(:,1),'FaceColor','none','EdgeColor','interp')
% hold on
% for mm=2:size(backward_SCC_UMAP,3);
%  patch(backward_SCC_UMAP(:,1,mm),backward_SCC_UMAP(:,2,mm),backward_SCC_UMAP(:,3,mm),c(:,1),'FaceColor','none','EdgeColor','interp')
% end;
% 
% 
% [reduced_data_PC_f1, umap, clusterIdentifiers] = run_umap(nanmean(PCratemaps_forward,3), 'n_components', 3,'n_neighbors',30,   'randomize', 'false' );
% [reduced_data_PC_b1, umap, clusterIdentifiers] = run_umap(nanmean(PCratemaps_backward,3), 'n_components', 3,'n_neighbors',30,   'randomize', 'false' );
% [reduced_data_SCC_f1, umap, clusterIdentifiers] = run_umap(nanmean(SCCratemaps_forward,3), 'n_components', 3,'n_neighbors',30,   'randomize', 'false' );
% [reduced_data_SCC_b1, umap, clusterIdentifiers] = run_umap(nanmean(SCCratemaps_backward,3), 'n_components', 3,'n_neighbors',30,   'randomize', 'false' );
% 
% [reduced_data_PC_f2, umap, clusterIdentifiers] = run_umap(nanmean(PCratemaps_forward,3), 'n_components', 3,'n_neighbors',30,   'randomize', 'false' );
% [reduced_data_PC_b2, umap, clusterIdentifiers] = run_umap(nanmean(PCratemaps_backward,3), 'n_components', 3,'n_neighbors',30,   'randomize', 'false' );
% [reduced_data_SCC_f2, umap, clusterIdentifiers] = run_umap(nanmean(SCCratemaps_forward,3), 'n_components', 3,'n_neighbors',30,   'randomize', 'false' );
% [reduced_data_SCC_b2, umap, clusterIdentifiers] = run_umap(nanmean(SCCratemaps_backward,3), 'n_components', 3,'n_neighbors',30,   'randomize', 'false' );
% 
% [reduced_data_PC_f3, umap, clusterIdentifiers] = run_umap(nanmean(PCratemaps_forward,3), 'n_components', 3,'n_neighbors',30,   'randomize', 'false' );
% [reduced_data_PC_b3, umap, clusterIdentifiers] = run_umap(nanmean(PCratemaps_backward,3), 'n_components', 3,'n_neighbors',30,   'randomize', 'false' );
% [reduced_data_SCC_f3, umap, clusterIdentifiers] = run_umap(nanmean(SCCratemaps_forward,3), 'n_components', 3,'n_neighbors',30,   'randomize', 'false' );
% [reduced_data_SCC_b3, umap, clusterIdentifiers] = run_umap(nanmean(SCCratemaps_backward,3), 'n_components', 3,'n_neighbors',30,   'randomize', 'false' );
% 
% [reduced_data_PC_f4, umap, clusterIdentifiers] = run_umap(nanmean(PCratemaps_forward,3), 'n_components', 3,'n_neighbors',30,   'randomize', 'false' );
% [reduced_data_PC_b4, umap, clusterIdentifiers] = run_umap(nanmean(PCratemaps_backward,3), 'n_components', 3,'n_neighbors',30,   'randomize', 'false' );
% [reduced_data_SCC_f4, umap, clusterIdentifiers] = run_umap(nanmean(SCCratemaps_forward,3), 'n_components', 3,'n_neighbors',30,   'randomize', 'false' );
% [reduced_data_SCC_b4, umap, clusterIdentifiers] = run_umap(nanmean(SCCratemaps_backward,3), 'n_components', 3,'n_neighbors',30,   'randomize', 'false' );
% 
% 
% 
% 
% 
% figure;
% patch([reduced_data_PC_f1(:,1,1); nan],[reduced_data_PC_f1(:,2,1); nan],[reduced_data_PC_f1(:,3,1); nan],[c(:,1); nan],'FaceColor','none','EdgeColor','interp');
% hold on
% patch([reduced_data_PC_f2(:,1,1); nan],[reduced_data_PC_f2(:,2,1); nan],[reduced_data_PC_f2(:,3,1); nan],[c(:,1); nan],'FaceColor','none','EdgeColor','interp');
% patch([reduced_data_PC_f3(:,1,1); nan],[reduced_data_PC_f3(:,2,1); nan],[reduced_data_PC_f3(:,3,1); nan],[c(:,1); nan],'FaceColor','none','EdgeColor','interp');
% patch([reduced_data_PC_f4(:,1,1); nan],[reduced_data_PC_f4(:,2,1); nan],[reduced_data_PC_f4(:,3,1); nan],[c(:,1); nan],'FaceColor','none','EdgeColor','interp');
% 
% patch([reduced_data_PC_b1(:,1,1); nan],[reduced_data_PC_b1(:,2,1); nan],[reduced_data_PC_b1(:,3,1); nan],[c(:,1); nan],'FaceColor','none','EdgeColor','interp');
% patch([reduced_data_PC_b2(:,1,1); nan],[reduced_data_PC_b2(:,2,1); nan],[reduced_data_PC_b2(:,3,1); nan],[c(:,1); nan],'FaceColor','none','EdgeColor','interp');
% patch([reduced_data_PC_b3(:,1,1); nan],[reduced_data_PC_b3(:,2,1); nan],[reduced_data_PC_b3(:,3,1); nan],[c(:,1); nan],'FaceColor','none','EdgeColor','interp');
% patch([reduced_data_PC_b4(:,1,1); nan],[reduced_data_PC_b4(:,2,1); nan],[reduced_data_PC_b4(:,3,1); nan],[c(:,1); nan],'FaceColor','none','EdgeColor','interp');
% 
% figure;
% patch([reduced_data_SCC_f1(:,1,1); nan],[reduced_data_SCC_f1(:,2,1); nan],[reduced_data_SCC_f1(:,3,1); nan],[c(:,1); nan],'FaceColor','none','EdgeColor','interp');
% hold on
% patch([reduced_data_SCC_f2(:,1,1); nan],[reduced_data_SCC_f2(:,2,1); nan],[reduced_data_SCC_f2(:,3,1); nan],[c(:,1); nan],'FaceColor','none','EdgeColor','interp');
% patch([reduced_data_SCC_f3(:,1,1); nan],[reduced_data_SCC_f3(:,2,1); nan],[reduced_data_SCC_f3(:,3,1); nan],[c(:,1); nan],'FaceColor','none','EdgeColor','interp');
% patch([reduced_data_SCC_f4(:,1,1); nan],[reduced_data_SCC_f4(:,2,1); nan],[reduced_data_SCC_f4(:,3,1); nan],[c(:,1); nan],'FaceColor','none','EdgeColor','interp');
% 
% patch([reduced_data_SCC_b1(:,1,1); nan],[reduced_data_SCC_b1(:,2,1); nan],[reduced_data_SCC_b1(:,3,1); nan],[c(:,1); nan],'FaceColor','none','EdgeColor','interp');
% patch([reduced_data_SCC_b2(:,1,1); nan],[reduced_data_SCC_b2(:,2,1); nan],[reduced_data_SCC_b2(:,3,1); nan],[c(:,1); nan],'FaceColor','none','EdgeColor','interp');
% patch([reduced_data_SCC_b3(:,1,1); nan],[reduced_data_SCC_b3(:,2,1); nan],[reduced_data_SCC_b3(:,3,1); nan],[c(:,1); nan],'FaceColor','none','EdgeColor','interp');
% patch([reduced_data_SCC_b4(:,1,1); nan],[reduced_data_SCC_b4(:,2,1); nan],[reduced_data_SCC_b4(:,3,1); nan],[c(:,1); nan],'FaceColor','none','EdgeColor','interp');
% 
% plot3(reduced_data_PC_b(:,1,1),reduced_data_PC_b(:,2,1),reduced_data_PC_b(:,3,1));
% 
% 
% plot3(reduced_data_SCC_f(:,1,1),reduced_data_SCC_f(:,2,1),reduced_data_SCC_f(:,3,1));
% plot3(reduced_data_SCC_b(:,1,1),reduced_data_SCC_b(:,2,1),reduced_data_SCC_b(:,3,1));
