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\day7'; 
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(isnan(placemapOF))=0;
    placemapOF=conv2(placemapOF, kernel, 'same');
    placemapOF=placemapOF/max(max(placemapOF));
    mask = (Occupancy == 0);
    placemapOF(mask) = NaN;
    pCmapOFfinal(:,:,counter)=placemapOF;
    counter=counter+1;
end;





% Get dimensions of your 3D matrix
[rows, cols, numPlots] = size(pCmapOFfinal);

% Define grid for subplots (e.g., automatically calculate rows/cols for the figure)
gridSize = ceil(sqrt(numPlots)); 

figure;
for i = 1:numPlots
    % Select the current slice
    dataSlice = pCmapOFfinal(:,:,i);
    
    % Create subplot
    subplot(gridSize, gridSize, i);
    
    % Plot data and set NaNs to transparent
    h = imagesc(dataSlice);
    set(h, 'AlphaData', ~isnan(dataSlice));
    
    % Set the background color of the axes to white
    set(gca, 'Color', 'w');
    
    % Remove axes and labels
    axis off;
    
    % Optional: keep the aspect ratio consistent
    axis image; 
    colormap(jet)
end


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\day7'; 
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(isnan(placemapOF))=0;
    placemapOF=conv2(placemapOF, kernel, 'same');
    placemapOF=placemapOF/max(max(placemapOF));
    mask = (Occupancy == 0);
    placemapOF(mask) = NaN;
    pCmapOFfinal(:,:,counter)=placemapOF;
    counter=counter+1;
end;




% Get dimensions of your 3D matrix
[rows, cols, numPlots] = size(pCmapOFfinal);

% Define grid for subplots (e.g., automatically calculate rows/cols for the figure)
gridSize = ceil(sqrt(numPlots)); 

figure;
for i = 1:numPlots
    % Select the current slice
    dataSlice = pCmapOFfinal(:,:,i);
    
    % Create subplot
    subplot(gridSize, gridSize, i);
    
    % Plot data and set NaNs to transparent
    h = imagesc(dataSlice);
    set(h, 'AlphaData', ~isnan(dataSlice));
    
    % Set the background color of the axes to white
    set(gca, 'Color', 'w');
    
    % Remove axes and labels
    axis off;
    
    % Optional: keep the aspect ratio consistent
    axis image; 
    colormap(jet)
end