clear all;close all;

all_SC=[];
all_PC=[];
all_HPC=[];

num_bins=21;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=1;

cd 'D:\ACC Data\ACC\AllDays\635\day9'; 
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('SC.csv'));
SCid=find(SCid(:,2));

bad_data=find(data(:,2)==1);

%Open field
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);

x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];

vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);

notactive=find(sum(data)<5);

data=data(:,SCid);

data(still_periods,:)=[];
x_posn(still_periods)=[];
y_posn(still_periods)=[];
[Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
Occupancy=Occupancy/20;

pCmapOFfinal=zeros(20,20,size(data,2));
kernel = ones(3,3) / 9;

counter=1;
for cell_num=1:size(data,2);
    tempcell=data(:,cell_num);
    tempcell=find(tempcell);
    [placemapOF,XEDGES,YEDGES] = histcounts2(x_posn(tempcell),y_posn(tempcell),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    placemapOF=placemapOF./Occupancy;
    placemapOF=conv2(placemapOF, kernel, 'same');
    placemapOF=placemapOF/max(max(placemapOF));
    placemapOF(isnan(placemapOF))=0;
    pCmapOFfinal(:,:,counter)=placemapOF;
    counter=counter+1;
end;

for nn=1:size(pCmapOFfinal,3);
all_SC=[all_SC;nnz(pCmapOFfinal(:,:,nn))/400];
end;

clearvars -except all_*

num_bins=21;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=1;

cd 'D:\ACC Data\ACC\AllDays\681\day9'; 
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('SC.csv'));
SCid=find(SCid(:,2));

bad_data=find(data(:,2)==1);

%Open field
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);

x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];

vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);

notactive=find(sum(data)<5);

data=data(:,SCid);

data(still_periods,:)=[];
x_posn(still_periods)=[];
y_posn(still_periods)=[];
[Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
Occupancy=Occupancy/20;

pCmapOFfinal=zeros(20,20,size(data,2));
kernel = ones(3,3) / 9;

counter=1;
for cell_num=1:size(data,2);
    tempcell=data(:,cell_num);
    tempcell=find(tempcell);
    [placemapOF,XEDGES,YEDGES] = histcounts2(x_posn(tempcell),y_posn(tempcell),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    placemapOF=placemapOF./Occupancy;
    placemapOF=conv2(placemapOF, kernel, 'same');
    placemapOF=placemapOF/max(max(placemapOF));
    placemapOF(isnan(placemapOF))=0;
    pCmapOFfinal(:,:,counter)=placemapOF;
    counter=counter+1;
end;

for nn=1:size(pCmapOFfinal,3);
all_SC=[all_SC;nnz(pCmapOFfinal(:,:,nn))/400];
end;

clearvars -except all_*

num_bins=21;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=1;

cd 'D:\ACC Data\ACC\AllDays\691\day9'; 
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('SC.csv'));
SCid=find(SCid(:,2));

bad_data=find(data(:,2)==1);

%Open field
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);

x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];

vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);

notactive=find(sum(data)<5);

data=data(:,SCid);

data(still_periods,:)=[];
x_posn(still_periods)=[];
y_posn(still_periods)=[];
[Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
Occupancy=Occupancy/20;

pCmapOFfinal=zeros(20,20,size(data,2));
kernel = ones(3,3) / 9;

counter=1;
for cell_num=1:size(data,2);
    tempcell=data(:,cell_num);
    tempcell=find(tempcell);
    [placemapOF,XEDGES,YEDGES] = histcounts2(x_posn(tempcell),y_posn(tempcell),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    placemapOF=placemapOF./Occupancy;
    placemapOF=conv2(placemapOF, kernel, 'same');
    placemapOF=placemapOF/max(max(placemapOF));
    placemapOF(isnan(placemapOF))=0;
    pCmapOFfinal(:,:,counter)=placemapOF;
    counter=counter+1;
end;

for nn=1:size(pCmapOFfinal,3);
all_SC=[all_SC;nnz(pCmapOFfinal(:,:,nn))/400];
end;

clearvars -except all_*

num_bins=21;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=1;

cd 'D:\ACC Data\ACC\AllDays\747\day9'; 
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('SC.csv'));
SCid=find(SCid(:,2));

bad_data=find(data(:,2)==1);

%Open field
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);

x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];

vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);

notactive=find(sum(data)<5);

data=data(:,SCid);

data(still_periods,:)=[];
x_posn(still_periods)=[];
y_posn(still_periods)=[];
[Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
Occupancy=Occupancy/20;

pCmapOFfinal=zeros(20,20,size(data,2));
kernel = ones(3,3) / 9;

counter=1;
for cell_num=1:size(data,2);
    tempcell=data(:,cell_num);
    tempcell=find(tempcell);
    [placemapOF,XEDGES,YEDGES] = histcounts2(x_posn(tempcell),y_posn(tempcell),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    placemapOF=placemapOF./Occupancy;
    placemapOF=conv2(placemapOF, kernel, 'same');
    placemapOF=placemapOF/max(max(placemapOF));
    placemapOF(isnan(placemapOF))=0;
    pCmapOFfinal(:,:,counter)=placemapOF;
    counter=counter+1;
end;

for nn=1:size(pCmapOFfinal,3);
all_SC=[all_SC;nnz(pCmapOFfinal(:,:,nn))/400];
end;

clearvars -except all_*


num_bins=21;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=1;

cd 'D:\ACC Data\ACC\AllDays\635\day9'; 
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));

bad_data=find(data(:,2)==1);

%Open field
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);

x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];

vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);

notactive=find(sum(data)<5);

data=data(:,SCid);

data(still_periods,:)=[];
x_posn(still_periods)=[];
y_posn(still_periods)=[];
[Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
Occupancy=Occupancy/20;

pCmapOFfinal=zeros(20,20,size(data,2));
kernel = ones(3,3) / 9;

counter=1;
for cell_num=1:size(data,2);
    tempcell=data(:,cell_num);
    tempcell=find(tempcell);
    [placemapOF,XEDGES,YEDGES] = histcounts2(x_posn(tempcell),y_posn(tempcell),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    placemapOF=placemapOF./Occupancy;
    placemapOF=conv2(placemapOF, kernel, 'same');
    placemapOF=placemapOF/max(max(placemapOF));
    placemapOF(isnan(placemapOF))=0;
    pCmapOFfinal(:,:,counter)=placemapOF;
    counter=counter+1;
end;

for nn=1:size(pCmapOFfinal,3);
all_PC=[all_PC;nnz(pCmapOFfinal(:,:,nn))/400];
end;

clearvars -except all_*

num_bins=21;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=1;

cd 'D:\ACC Data\ACC\AllDays\681\day9'; 
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));

bad_data=find(data(:,2)==1);

%Open field
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);

x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];

vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);

notactive=find(sum(data)<5);

data=data(:,SCid);

data(still_periods,:)=[];
x_posn(still_periods)=[];
y_posn(still_periods)=[];
[Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
Occupancy=Occupancy/20;

pCmapOFfinal=zeros(20,20,size(data,2));
kernel = ones(3,3) / 9;

counter=1;
for cell_num=1:size(data,2);
    tempcell=data(:,cell_num);
    tempcell=find(tempcell);
    [placemapOF,XEDGES,YEDGES] = histcounts2(x_posn(tempcell),y_posn(tempcell),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    placemapOF=placemapOF./Occupancy;
    placemapOF=conv2(placemapOF, kernel, 'same');
    placemapOF=placemapOF/max(max(placemapOF));
    placemapOF(isnan(placemapOF))=0;
    pCmapOFfinal(:,:,counter)=placemapOF;
    counter=counter+1;
end;

for nn=1:size(pCmapOFfinal,3);
all_PC=[all_PC;nnz(pCmapOFfinal(:,:,nn))/400];
end;

clearvars -except all_*

num_bins=21;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=1;

cd 'D:\ACC Data\ACC\AllDays\691\day9'; 
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));

bad_data=find(data(:,2)==1);

%Open field
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);

x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];

vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);

notactive=find(sum(data)<5);

data=data(:,SCid);

data(still_periods,:)=[];
x_posn(still_periods)=[];
y_posn(still_periods)=[];
[Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
Occupancy=Occupancy/20;

pCmapOFfinal=zeros(20,20,size(data,2));
kernel = ones(3,3) / 9;

counter=1;
for cell_num=1:size(data,2);
    tempcell=data(:,cell_num);
    tempcell=find(tempcell);
    [placemapOF,XEDGES,YEDGES] = histcounts2(x_posn(tempcell),y_posn(tempcell),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    placemapOF=placemapOF./Occupancy;
    placemapOF=conv2(placemapOF, kernel, 'same');
    placemapOF=placemapOF/max(max(placemapOF));
    placemapOF(isnan(placemapOF))=0;
    pCmapOFfinal(:,:,counter)=placemapOF;
    counter=counter+1;
end;

for nn=1:size(pCmapOFfinal,3);
all_PC=[all_PC;nnz(pCmapOFfinal(:,:,nn))/400];
end;

clearvars -except all_*

num_bins=21;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=1;

cd 'D:\ACC Data\ACC\AllDays\747\day9'; 
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));

bad_data=find(data(:,2)==1);

%Open field
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);

x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];

vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);

notactive=find(sum(data)<5);

data=data(:,SCid);

data(still_periods,:)=[];
x_posn(still_periods)=[];
y_posn(still_periods)=[];
[Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
Occupancy=Occupancy/20;

pCmapOFfinal=zeros(20,20,size(data,2));
kernel = ones(3,3) / 9;

counter=1;
for cell_num=1:size(data,2);
    tempcell=data(:,cell_num);
    tempcell=find(tempcell);
    [placemapOF,XEDGES,YEDGES] = histcounts2(x_posn(tempcell),y_posn(tempcell),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    placemapOF=placemapOF./Occupancy;
    placemapOF=conv2(placemapOF, kernel, 'same');
    placemapOF=placemapOF/max(max(placemapOF));
    placemapOF(isnan(placemapOF))=0;
    pCmapOFfinal(:,:,counter)=placemapOF;
    counter=counter+1;
end;

for nn=1:size(pCmapOFfinal,3);
all_PC=[all_PC;nnz(pCmapOFfinal(:,:,nn))/400];
end;

clearvars -except all_*



num_bins=21;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=1;

cd 'D:\ACC Data\HPC\57\day9'; 
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));

bad_data=find(data(:,2)==1);

%Open field
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);

x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];

vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);

notactive=find(sum(data)<5);

data=data(:,SCid);

data(still_periods,:)=[];
x_posn(still_periods)=[];
y_posn(still_periods)=[];
[Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
Occupancy=Occupancy/20;

pCmapOFfinal=zeros(20,20,size(data,2));
kernel = ones(3,3) / 9;

counter=1;
for cell_num=1:size(data,2);
    tempcell=data(:,cell_num);
    tempcell=find(tempcell);
    [placemapOF,XEDGES,YEDGES] = histcounts2(x_posn(tempcell),y_posn(tempcell),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    placemapOF=placemapOF./Occupancy;
    placemapOF=conv2(placemapOF, kernel, 'same');
    placemapOF=placemapOF/max(max(placemapOF));
    placemapOF(isnan(placemapOF))=0;
    pCmapOFfinal(:,:,counter)=placemapOF;
    counter=counter+1;
end;

for nn=1:size(pCmapOFfinal,3);
all_HPC=[all_HPC;nnz(pCmapOFfinal(:,:,nn))/400];
end;

clearvars -except all_*



num_bins=21;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=1;

cd 'D:\ACC Data\HPC\221\day9'; 
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));

bad_data=find(data(:,2)==1);

%Open field
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);

x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];

vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);

notactive=find(sum(data)<5);

data=data(:,SCid);

data(still_periods,:)=[];
x_posn(still_periods)=[];
y_posn(still_periods)=[];
[Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
Occupancy=Occupancy/20;

pCmapOFfinal=zeros(20,20,size(data,2));
kernel = ones(3,3) / 9;

counter=1;
for cell_num=1:size(data,2);
    tempcell=data(:,cell_num);
    tempcell=find(tempcell);
    [placemapOF,XEDGES,YEDGES] = histcounts2(x_posn(tempcell),y_posn(tempcell),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    placemapOF=placemapOF./Occupancy;
    placemapOF=conv2(placemapOF, kernel, 'same');
    placemapOF=placemapOF/max(max(placemapOF));
    placemapOF(isnan(placemapOF))=0;
    pCmapOFfinal(:,:,counter)=placemapOF;
    counter=counter+1;
end;

for nn=1:size(pCmapOFfinal,3);
all_HPC=[all_HPC;nnz(pCmapOFfinal(:,:,nn))/400];
end;

clearvars -except all_*



num_bins=21;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=1;

cd 'D:\ACC Data\HPC\851\day9'; 
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));

bad_data=find(data(:,2)==1);

%Open field
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);

x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];

vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);

notactive=find(sum(data)<5);

data=data(:,SCid);

data(still_periods,:)=[];
x_posn(still_periods)=[];
y_posn(still_periods)=[];
[Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
Occupancy=Occupancy/20;

pCmapOFfinal=zeros(20,20,size(data,2));
kernel = ones(3,3) / 9;

counter=1;
for cell_num=1:size(data,2);
    tempcell=data(:,cell_num);
    tempcell=find(tempcell);
    [placemapOF,XEDGES,YEDGES] = histcounts2(x_posn(tempcell),y_posn(tempcell),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    placemapOF=placemapOF./Occupancy;
    placemapOF=conv2(placemapOF, kernel, 'same');
    placemapOF=placemapOF/max(max(placemapOF));
    placemapOF(isnan(placemapOF))=0;
    pCmapOFfinal(:,:,counter)=placemapOF;
    counter=counter+1;
end;

for nn=1:size(pCmapOFfinal,3);
all_HPC=[all_HPC;nnz(pCmapOFfinal(:,:,nn))/400];
end;

clearvars -except all_*



num_bins=21;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=1;

cd 'D:\ACC Data\HPC\887\day9'; 
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));

bad_data=find(data(:,2)==1);

%Open field
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);

x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];

vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);

notactive=find(sum(data)<5);

data=data(:,SCid);

data(still_periods,:)=[];
x_posn(still_periods)=[];
y_posn(still_periods)=[];
[Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
Occupancy=Occupancy/20;

pCmapOFfinal=zeros(20,20,size(data,2));
kernel = ones(3,3) / 9;

counter=1;
for cell_num=1:size(data,2);
    tempcell=data(:,cell_num);
    tempcell=find(tempcell);
    [placemapOF,XEDGES,YEDGES] = histcounts2(x_posn(tempcell),y_posn(tempcell),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    placemapOF=placemapOF./Occupancy;
    placemapOF=conv2(placemapOF, kernel, 'same');
    placemapOF=placemapOF/max(max(placemapOF));
    placemapOF(isnan(placemapOF))=0;
    pCmapOFfinal(:,:,counter)=placemapOF;
    counter=counter+1;
end;

for nn=1:size(pCmapOFfinal,3);
all_HPC=[all_HPC;nnz(pCmapOFfinal(:,:,nn))/400];
end;

clearvars -except all_*


num_bins=21;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=1;

cd 'D:\ACC Data\HPC\987\day9'; 
data=table2array(readtable('XYtotalspike2.csv'));
SCid=table2array(readtable('pPC.csv'));
SCid=find(SCid(:,2));

bad_data=find(data(:,2)==1);

%Open field
x_posn=data(:,2);
x_posn=smooth(x_posn,15);
y_posn=data(:,3);
y_posn=smooth(y_posn,15);

x_posn(bad_data)=[];
y_posn(bad_data)=[];
data(bad_data,:)=[];

vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
vel_cmsec=smooth(vel_cmsec,30);
still_periods=find(vel_cmsec<vel_threshold);
data=data(1:end,6:end);

notactive=find(sum(data)<5);

data=data(:,SCid);

data(still_periods,:)=[];
x_posn(still_periods)=[];
y_posn(still_periods)=[];
[Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
Occupancy=Occupancy/20;

pCmapOFfinal=zeros(20,20,size(data,2));
kernel = ones(3,3) / 9;

counter=1;
for cell_num=1:size(data,2);
    tempcell=data(:,cell_num);
    tempcell=find(tempcell);
    [placemapOF,XEDGES,YEDGES] = histcounts2(x_posn(tempcell),y_posn(tempcell),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    placemapOF=placemapOF./Occupancy;
    placemapOF=conv2(placemapOF, kernel, 'same');
    placemapOF=placemapOF/max(max(placemapOF));
    placemapOF(isnan(placemapOF))=0;
    pCmapOFfinal(:,:,counter)=placemapOF;
    counter=counter+1;
end;

for nn=1:size(pCmapOFfinal,3);
all_HPC=[all_HPC;nnz(pCmapOFfinal(:,:,nn))/400];
end;

clearvars -except all_*

mean(all_HPC)
mean(all_PC)
mean(all_SC)