addpath('C:\Users\Steve\Desktop\Neuro_Utilities\Matlab Scripts\Hayashi Lab Code');

clear all; clc;

% Import Data
Ms1A = readtable('D:/ACC Data/ACC/635/XYtotalspike1.csv');
Ms1B = readtable('D:/ACC Data/ACC/635/XYtotalspike2.csv');
Ms2A = readtable('D:/ACC Data/ACC/681/XYtotalspike1.csv');
Ms2B = readtable('D:/ACC Data/ACC/681/XYtotalspike2.csv');
Ms3A = readtable('D:/ACC Data/ACC/691/XYtotalspike1.csv');
Ms3B = readtable('D:/ACC Data/ACC/691/XYtotalspike2.csv');
Ms4A = readtable('D:/ACC Data/ACC/747/XYtotalspike1.csv');
Ms4B = readtable('D:/ACC Data/ACC/747/XYtotalspike2.csv');


CellIDsMs1=readtable('D:\ACC Data\ACC\CellIDs\635\SC.csv');
CellIDsMs2=readtable('D:\ACC Data\ACC\CellIDs\681\SC.csv');
CellIDsMs3=readtable('D:\ACC Data\ACC\CellIDs\691\SC.csv');
CellIDsMs4=readtable('D:\ACC Data\ACC\CellIDs\747\SC.csv');

SCcellsMs1=find(sum(table2array(CellIDsMs1),2)>0);
SCcellsMs2=find(sum(table2array(CellIDsMs2),2)>0);
SCcellsMs3=find(sum(table2array(CellIDsMs3),2)>0);
SCcellsMs4=find(sum(table2array(CellIDsMs4),2)>0);

SessionAlength = min([size(Ms1A,1),size(Ms2A,1),size(Ms3A,1),size(Ms4A,1)]);
SessionBlength = min([size(Ms1B,1),size(Ms2B,1),size(Ms3B,1),size(Ms4B,1)]);
Ms1A=Ms1A(1:SessionAlength,6:end);Ms2A=Ms2A(1:SessionAlength,6:end);Ms3A=Ms3A(1:SessionAlength,6:end);Ms4A=Ms4A(1:SessionAlength,6:end);
Ms1B=Ms1B(1:SessionBlength,6:end);Ms2B=Ms2B(1:SessionBlength,6:end);Ms3B=Ms3B(1:SessionBlength,6:end);Ms4B=Ms4B(1:SessionBlength,6:end);

Ms1numcells=size(Ms1A,2);
Ms2numcells=size(Ms2A,2);
Ms3numcells=size(Ms3A,2);
Ms4numcells=size(Ms4A,2);

temp1=[Ms1A;Ms1B];
temp2=[Ms2A;Ms2B];
temp3=[Ms3A;Ms3B];
temp4=[Ms4A;Ms4B];

SCcellsMs2=SCcellsMs2+Ms1numcells;
SCcellsMs3=SCcellsMs3+Ms1numcells+Ms2numcells;
SCcellsMs4=SCcellsMs4+Ms1numcells+Ms2numcells+Ms3numcells;
AllSCCells=[SCcellsMs1;SCcellsMs2;SCcellsMs3;SCcellsMs4];

Alldata=[table2array(temp1),table2array(temp2),table2array(temp3),table2array(temp4)];
Alldata=sparse(Alldata);
%clear Ms1A Ms1B Ms1C Ms2A Ms2B Ms2C Ms3A Ms3B Ms3C Ms4A Ms4B Ms4C temp*

temp1 = cell(1, SessionAlength); temp1(:) = {'track'};
temp2 = cell(1, SessionBlength); temp2(:) = {'openfield'};

labels=[temp1';temp2']; clear temp*

SCcellsAlldata=Alldata(:,AllSCCells);
% Linear Classifier
fraction_train = 0.3;
[trainInd,valInd,testInd] = dividerand(size(SCcellsAlldata,1),fraction_train,0,(1-fraction_train));
[Model,FitInfo] = fitclinear(SCcellsAlldata(trainInd,:),labels(trainInd,:));

newly_labelled_data=predict(Model,SCcellsAlldata(testInd,:));
tf = strcmp(newly_labelled_data,labels(testInd)); 
decodingaccuracy=sum(tf)/length(tf)*100;


x = setdiff(1:size(Alldata,2), AllSCCells);
Surrogate_Decoding_Vals=NaN(1000,1);

for num_shuffs=1:1000;

    rx = randsample(x,length(AllSCCells));
    tempdata=Alldata(:,rx);
    [trainInd,valInd,testInd] = dividerand(size(Alldata,1),fraction_train,0,(1-fraction_train));
    [Model,FitInfo] = fitclinear(tempdata(trainInd,:),labels(trainInd,:));
    PCCnewly_labelled_data=predict(Model,tempdata(testInd,:));
    PCCtf = strcmp(PCCnewly_labelled_data,labels(testInd)); 
    Surrogate_Decoding_Vals(num_shuffs)=sum(PCCtf)/length(PCCtf)*100;
    
end;

save 'ContextDecodingAyakasSCcellsoutput.mat'



clear all; clc;

addpath('C:\Users\Steve\Desktop\Neuro_Utilities\Matlab Scripts\Hayashi Lab Code');

% Import Data
Ms1A = readtable('D:/ACC Data/HPC/57/day9/XYtotalspike1.csv');
Ms1B = readtable('D:/ACC Data/HPC/57/day9/XYtotalspike2.csv');


Ms2A = readtable('D:/ACC Data/HPC/221/day9/XYtotalspike1.csv');
Ms2B = readtable('D:/ACC Data/HPC/221/day9/XYtotalspike2.csv');


Ms3A = readtable('D:/ACC Data/HPC/851/day9/XYtotalspike1.csv');
Ms3B = readtable('D:/ACC Data/HPC/851/day9/XYtotalspike2.csv');


Ms4A = readtable('D:/ACC Data/HPC/887/day9/XYtotalspike1.csv');
Ms4B = readtable('D:/ACC Data/HPC/887/day9/XYtotalspike2.csv');


Ms5A = readtable('D:/ACC Data/HPC/987/day9/XYtotalspike1.csv');
Ms5B = readtable('D:/ACC Data/HPC/987/day9/XYtotalspike2.csv');


% Organize Data
SessionAlength = min([size(Ms1A,1),size(Ms2A,1),size(Ms3A,1),size(Ms4A,1),size(Ms5A,1)]);
SessionBlength = min([size(Ms1B,1),size(Ms2B,1),size(Ms3B,1),size(Ms4B,1),size(Ms5B,1)]);

Ms1A=Ms1A(1:SessionAlength,6:end);Ms2A=Ms2A(1:SessionAlength,6:end);Ms3A=Ms3A(1:SessionAlength,6:end);Ms4A=Ms4A(1:SessionAlength,6:end);Ms5A=Ms5A(1:SessionAlength,6:end);
Ms1B=Ms1B(1:SessionBlength,6:end);Ms2B=Ms2B(1:SessionBlength,6:end);Ms3B=Ms3B(1:SessionBlength,6:end);Ms4B=Ms4B(1:SessionBlength,6:end);Ms5B=Ms5B(1:SessionBlength,6:end);

Ms1numcells=size(Ms1A,2);
Ms2numcells=size(Ms2A,2);
Ms3numcells=size(Ms3A,2);
Ms4numcells=size(Ms4A,2);
Ms5numcells=size(Ms5A,2);

temp1=[Ms1A;Ms1B];
temp2=[Ms2A;Ms2B];
temp3=[Ms3A;Ms3B];
temp4=[Ms4A;Ms4B];
temp5=[Ms5A;Ms5B];

Alldata=[table2array(temp1),table2array(temp2),table2array(temp3),table2array(temp4),table2array(temp5)];
Alldata=sparse(Alldata);
clear Ms1A Ms1B Ms1C Ms2A Ms2B Ms2C Ms3A Ms3B Ms3C Ms4A Ms4B Ms4C Ms5A Ms5B Ms5C temp*

temp1 = cell(1, SessionAlength); temp1(:) = {'track'};
temp2 = cell(1, SessionBlength); temp2(:) = {'openfield'};
labels=[temp1';temp2']; clear temp*
 

% Linear Classifier
fraction_train = 0.3;
[trainInd,valInd,testInd] = dividerand(size(Alldata,1),fraction_train,0,(1-fraction_train));
[Model,FitInfo] = fitclinear(Alldata(trainInd,:),labels(trainInd,:));

newly_labelled_data=predict(Model,Alldata(testInd,:));
tf = strcmp(newly_labelled_data,labels(testInd)); 
%decodingaccuracy=sum(tf)/length(tf)*100;


x = 1:size(Alldata,2);
Surrogate_Decoding_Vals_HPC=NaN(1000,1);

for num_shuffs=1:1000;

    rx = randsample(x,116);
    tempdata=Alldata(:,rx);
    [trainInd,valInd,testInd] = dividerand(size(Alldata,1),fraction_train,0,(1-fraction_train));
    [Model,FitInfo] = fitclinear(tempdata(trainInd,:),labels(trainInd,:));
    PCCnewly_labelled_data=predict(Model,tempdata(testInd,:));
    PCCtf = strcmp(PCCnewly_labelled_data,labels(testInd)); 
    Surrogate_Decoding_Vals_HPC(num_shuffs)=sum(PCCtf)/length(PCCtf)*100;
    
end;


figure;histogram(Surrogate_Decoding_Vals,'facecolor',[141/255, 198/255, 63/255]);
hold on
histogram(Surrogate_Decoding_Vals_HPC,'facecolor',[0, 174/255, 239/255]);
xline(decodingaccuracy,'color',[251/255, 176/255, 64/255]);
xlim([50 90])

