

basepaths = { ...
    '/media/sam/SM5/camkii10/camkii10_171222'; ...
    '/media/sam/SM5/camkii10/camkii10_171227'; ...
    '/media/sam/SM5/camkii10/camkii10_171228'; ...
    '/media/sam/SM5/camkii10/camkii10_171231'; ...
    '/media/sam/SM5/camkii10/camkii10_180102';...
    '/media/sam/SM5/camkii10/camkii10_180103';...
    '/media/sam/SM5/camkii10/camkii10_180104';...
    '/media/sam/SM5/camkii10/camkii10_180105';...
    '/media/sam/SM5/camkii10/camkii10_180109';...
    '/media/sam/SM5/camkii10/camkii10_180110';...
    '/media/sam/SM5/camkii10/camkii10_180111';...
    '/media/sam/SM5/camkii10/camkii10_180117';...
    '/media/sam/SM5/camkii10/camkii10_180118';...
    '/media/sam/SM5/camkii10/camkii10_180119';...
    '/media/sam/SM5/camkii10/camkii10_180123';...
    '/media/sam/SM5/camkii10/camkii10_180125';...
    '/media/sam/SM5/camkii10/camkii10_180126';...
    '/media/sam/SM5/camkii10/camkii10_180203';...
    '/media/sam/SM5/camkii10/camkii10_180206';...
    '/media/sam/SM5/camkii10/camkii10_180208';...
    '/media/sam/SM5/camkii7/camkii7_171014'; ...
    '/media/sam/SM5/camkii7/camkii7_171016'; ...
    '/media/sam/SM5/camkii7/camkii7_171017'; ...
    '/media/sam/SM5/camkii7/camkii7_171018'; ...
    '/media/sam/SM5/camkii7/camkii7_171019'; ...
    '/media/sam/SM5/camkii7/camkii7_171020'; ...
    '/media/sam/SM5/camkii7/camkii7_171021'; ...
    '/media/sam/SM5/camkii7/camkii7_171022'; ...
    '/media/sam/SM5/camkii7/camkii7_171023';...
    '/media/sam/SM5/camkii7/camkii7_171024'; ...
    '/media/sam/SM5/camkii7/camkii7_171029'; ...
    '/media/sam/SM5/camkii7/camkii7_171030'; ...
    % '/media/sam/SM5/camkii7/camkii7_171031'; ...
    '/media/sam/SM5/camkii7/camkii7_171101'; ...
    '/media/sam/SM5/camkii7/camkii7_171102'; ...
    '/media/sam/SM4/SeqGen/camkii4/20160910'; ...
    '/media/sam/SM4/SeqGen/camkii4/20160911'; ...
    '/media/sam/SM4/SeqGen/camkii4/20160912'; ...
    '/media/sam/SM4/SeqGen/camkii4/20160913'; ...
    '/media/sam/SM5/camkii11/camkii11_180614'; ...
    '/media/sam/SM5/camkii11/camkii11_180615'; ...
    '/media/sam/SM5/camkii11/camkii11_180616'; ...
    '/media/sam/SM5/camkii11/camkii11_180617'; ...
    '/media/sam/SM5/camkii11/camkii11_180618'; ...
    
    '/media/sam/SM5/camkii11/camkii11_180620'; ...
    '/media/sam/SM5/camkii11/camkii11_180621'; ...
    '/media/sam/SM5/camkii11/camkii11_180623'; ...
    
    '/media/sam/SM5/camkii11/camkii11_180625'; ...
    '/media/sam/SM5/camkii11/camkii11_180626'; ...
    '/media/sam/SM5/camkii11/camkii11_180627'; ...
    '/media/sam/SM5/camkii11/camkii11_180628'; ...
    '/media/sam/SM5/camkii11/camkii11_180629'; ...
    '/media/sam/SM5/camkii11/camkii11_180703'; ...
    '/media/sam/SM5/camkii11/camkii11_180705'; ...
    '/media/sam/SM5/camkii11/camkii11_180706'; ...
    '/media/sam/SM5/camkii11/camkii11_180707'; ...
    '/media/sam/SM5/camkii11/camkii11_180708'; ...
    %  '/media/sam/SM5/camkii12/camkii12_180727'; ...
    '/media/sam/SM5/camkii12/camkii12_180728'; ...
    '/media/sam/SM5/camkii12/camkii12_180730'; ...
    '/media/sam/SM5/camkii12/camkii12_180731';...
    '/media/sam/SM5/camkii12/camkii12_180801'; ...
    '/media/sam/SM5/camkii12/camkii12_180802'; ...
    '/media/sam/SM5/camkii12/camkii12_180803'; ...
    '/media/sam/SM5/camkii12/camkii12_180804'; ...
    
    
    
    '/media/sam/SM5/camkii12/camkii12_180807'; ...
    '/media/sam/SM5/camkii12/camkii12_180809'; ...
    
    
    '/media/sam/SM6/camkii13/camkii13_181215'; ...
    '/media/sam/SM6/camkii13/camkii13_181216'; ...
    '/media/sam/SM6/camkii13/camkii13_181217'; ...
    '/media/sam/SM6/camkii13/camkii13_181218'; ...
    '/media/sam/SM6/camkii13/camkii13_181219'; ...
    '/media/sam/SM6/camkii13/camkii13_181220'; ...
    '/media/sam/SM6/camkii13/camkii13_181221'; ...
    '/media/sam/SM6/camkii13/camkii13_181228'; ...
    '/media/sam/SM6/camkii13/camkii13_181229'; ...
    '/media/sam/SM6/camkii13/camkii13_181230'; ...
    '/media/sam/SM6/camkii13/camkii13_181231'; ...
    '/media/sam/SM6/camkii13/camkii13_190109'; ...
    '/media/sam/SM6/camkii13/camkii13_190110'; ...
    '/media/sam/SM6/camkii13/camkii13_190112'; ...
    '/media/sam/SM6/camkii13/camkii13_190113'; ...
    
    
    
    };


CA1 = {...
    '/media/sam/SM5/camkii10/camkii10_171222'; ...
    '/media/sam/SM5/camkii10/camkii10_171227'; ...
    '/media/sam/SM5/camkii10/camkii10_171228'; ...
    '/media/sam/SM5/camkii10/camkii10_171231'; ...
    '/media/sam/SM5/camkii10/camkii10_180102'; ...
    '/media/sam/SM5/camkii10/camkii10_180103';...
    '/media/sam/SM5/camkii10/camkii10_180104';...
    '/media/sam/SM5/camkii10/camkii10_180105';...
    '/media/sam/SM5/camkii10/camkii10_180109';...
    '/media/sam/SM5/camkii10/camkii10_180110';...
    '/media/sam/SM5/camkii10/camkii10_180111';...
    '/media/sam/SM5/camkii7/camkii7_171014'; ...
    '/media/sam/SM5/camkii7/camkii7_171016'; ...
    '/media/sam/SM5/camkii7/camkii7_171017'; ...
    '/media/sam/SM5/camkii7/camkii7_171018'; ...
    '/media/sam/SM5/camkii7/camkii7_171019'; ...
    '/media/sam/SM5/camkii7/camkii7_171020'; ...
    '/media/sam/SM5/camkii7/camkii7_171021'; ...
    '/media/sam/SM5/camkii7/camkii7_171022'; ...
    '/media/sam/SM5/camkii7/camkii7_171023'; ...
    '/media/sam/SM5/camkii11/camkii11_180614'; ...
    '/media/sam/SM5/camkii11/camkii11_180615'; ...
    '/media/sam/SM5/camkii11/camkii11_180616'; ...
    '/media/sam/SM5/camkii11/camkii11_180617'; ...
    '/media/sam/SM5/camkii11/camkii11_180618'; ...
    '/media/sam/SM5/camkii11/camkii11_180619'; ...
    '/media/sam/SM5/camkii11/camkii11_180620'; ...
    '/media/sam/SM5/camkii11/camkii11_180621'; ...
    '/media/sam/SM5/camkii11/camkii11_180703'; ...
    '/media/sam/SM5/camkii11/camkii11_180705'; ...
    '/media/sam/SM5/camkii11/camkii11_180707'; ...
    '/media/sam/SM5/camkii11/camkii11_180708'; ...
    '/media/sam/SM5/camkii12/camkii12_180728'; ...
    
    '/media/sam/SM5/camkii12/camkii12_180730'; ...
    '/media/sam/SM5/camkii12/camkii12_180731';...
    '/media/sam/SM5/camkii12/camkii12_180801'
    '/media/sam/SM5/camkii12/camkii12_180802'; ...
    '/media/sam/SM5/camkii12/camkii12_180803'; ...
    '/media/sam/SM5/camkii12/camkii12_180804'; ...
    '/media/sam/SM5/camkii12/camkii12_180727';...
    '/media/sam/SM5/camkii12/camkii12_180731';...
    '/media/sam/SM5/camkii12/camkii12_180802';...
    '/media/sam/SM5/camkii12/camkii12_180807'; ...
    '/media/sam/SM5/camkii12/camkii12_180809'; ...
    
    '/media/sam/SM5/camkii11/camkii11_180621';...
    '/media/sam/SM5/camkii11/camkii11_180617'; ...
    '/media/sam/SM5/camkii11/camkii11_180706'; ...
    
    
    '/media/sam/SM6/camkii13/camkii13_181215'; ...
    '/media/sam/SM6/camkii13/camkii13_181216'; ...
    '/media/sam/SM6/camkii13/camkii13_181217'; ...
    '/media/sam/SM6/camkii13/camkii13_181218'; ...
    '/media/sam/SM6/camkii13/camkii13_181219'; ...
    '/media/sam/SM6/camkii13/camkii13_181220'; ...
    '/media/sam/SM6/camkii13/camkii13_181221'; ...
    '/media/sam/SM6/camkii13/camkii13_181228'; ...
    '/media/sam/SM6/camkii13/camkii13_181229'; ...
    '/media/sam/SM6/camkii13/camkii13_181230'; ...
    '/media/sam/SM6/camkii13/camkii13_181231'; ...
    '/media/sam/SM6/camkii13/camkii13_190109'; ...
    '/media/sam/SM6/camkii13/camkii13_190110'; ...
    '/media/sam/SM6/camkii13/camkii13_190112'; ...
    '/media/sam/SM6/camkii13/camkii13_190113'; ...
    
    
    };

DG = {...
    '/media/sam/SM5/camkii7/camkii7_171024'; ...
    '/media/sam/SM5/camkii7/camkii7_171029'; ...
    '/media/sam/SM5/camkii7/camkii7_171030'; ...
    
    '/media/sam/SM5/camkii7/camkii7_171101'; ...
    % '/media/sam/SM5/camkii7/camkii7_171102'; ...
    '/media/sam/SM5/camkii10/camkii10_180117';...
    '/media/sam/SM5/camkii10/camkii10_180118';...
    '/media/sam/SM5/camkii10/camkii10_180119';...
    '/media/sam/SM5/camkii10/camkii10_180123'};
CA3 = {...
    '/media/sam/SM4/SeqGen/camkii4/20160910' ; ...
    % '/media/sam/SM5/camkii10/camkii10_180125';...
    '/media/sam/SM5/camkii10/camkii10_180126';...
    '/media/sam/SM5/camkii10/camkii10_180206';...
    '/media/sam/SM5/camkii11/camkii11_180623'; ...
    '/media/sam/SM5/camkii11/camkii11_180625'; ...
    '/media/sam/SM5/camkii11/camkii11_180626'; ...
    '/media/sam/SM5/camkii11/camkii11_180627'; ...
    '/media/sam/SM5/camkii11/camkii11_180628';...
    '/media/sam/SM5/camkii11/camkii11_180629'; ...
    
    
    };

Control = {...
    '/media/sam/SM5/camkii12/camkii12_180727';...
    % '/media/sam/SM5/camkii12/camkii12_180731';...
    %  '/media/sam/SM5/camkii12/camkii12_180802';... %fair
    % '/media/sam/SM5/camkii12/camkii12_180807'; ... % bad behavior
    '/media/sam/SM5/camkii12/camkii12_180809'; ...
    '/media/sam/SM5/camkii11/camkii11_180628';...
    '/media/sam/SM5/camkii11/camkii11_180621';...
    '/media/sam/SM5/camkii11/camkii11_180615'; ...
    '/media/sam/SM5/camkii11/camkii11_180617'; ...
    '/media/sam/SM5/camkii11/camkii11_180706'; ...
    '/media/sam/SM5/camkii7/camkii7_171029'; ...
    
    
    
    '/media/sam/SM6/camkii13/camkii13_181216'; ...
    '/media/sam/SM6/camkii13/camkii13_181221'; ...
    '/media/sam/SM6/camkii13/camkii13_181229'; ...
    %  '/media/sam/SM6/camkii13/camkii13_181231'; ...
    '/media/sam/SM6/camkii13/camkii13_190109'; ...
    '/media/sam/SM6/camkii13/camkii13_190113'; ...
    
    
    };
%%
for i = 1:length(basepaths)
    i
    try
        warning off
        sl = regexp(basepaths{i},'/');
        basename = basepaths{i}(sl(end)+1:end);
        getAllPeerPredict(basename,basepaths{i});
        
    end
    
end

%%

%%
clear ses1
ix = 1;
for i = 1:length(basepaths)
    
    if exist ([basepaths{i} '/LL.mat'])
        ses1(ix) = load([basepaths{i} '/LL.mat']);
        ix = ix+1
    end
    
end

%%

CA1k = ismember(basepaths(1:length(ses1)),CA1)';
stim = arrayfun(@(a) any(strmatch(a.ses.condition,'Stim')),ses1);

kp  = CA1k & stim;

kp1  = CA1k & ~stim;

pre_stim = cell2mat(arrayfun(@(a) linearize(a.ses.pre.weights),ses1(kp),'UniformOutput',false)');

preRUN_stim = cell2mat(arrayfun(@(a) linearize(a.ses.preRUN.weights),ses1(kp),'UniformOutput',false)');

ISIRUN_stim = cell2mat(arrayfun(@(a) linearize(a.ses.ISIRUN.weights),ses1(kp),'UniformOutput',false)');

ISI_stim = cell2mat(arrayfun(@(a) linearize(a.ses.ISI.weights),ses1(kp),'UniformOutput',false)');

pre_con = cell2mat(arrayfun(@(a) linearize(a.ses.pre.weights),ses1(kp1),'UniformOutput',false)');

preRUN_con = cell2mat(arrayfun(@(a) linearize(a.ses.preRUN.weights),ses1(kp1),'UniformOutput',false)');

ISIRUN_con = cell2mat(arrayfun(@(a) linearize(a.ses.ISIRUN.weights),ses1(kp1),'UniformOutput',false)');

ISI_con = cell2mat(arrayfun(@(a) linearize(a.ses.ISI.weights),ses1(kp1),'UniformOutput',false)');

%%

load('/home/sam/Dropbox/data_analysis/Buzsaki/PatternCompletion/data/placeFieldBeforeAfter_190610.mat')

%%
for i = 1:length(ses)
    
    ix = find(arrayfun(@(a) any(strmatch(a.ses.basepath,ses(i).basepath)),ses1));
    
    if any(ix)
        rm_pre = cell2mat({ses(i).pre.cell.rm_out}');
        rm_ISI = cell2mat({ses(i).ISI.cell.rm_out}');
        cellType = cell2mat({ses(i).CellParams.cellType})==1;
        [a1,b1]  =max(rm_pre,[],2);
        [a2,b2]  =max(rm_ISI,[],2);
        
        PF = a2>1 & b2>1;
        b1(~cellType'  ) = nan;
        b2(~cellType' ) = nan;
        [xx1,yy1] = meshgrid(b1);
        [xx2,yy2] = meshgrid(b2);
        
        ses1(ix).PF_dist = xx2-yy2;
        ses1(ix).PF1 = xx2;
        ses1(ix).PF2 = yy2;
        
    end
end

%%

kp  = CA1k & stim;

kp1  = CA1k & ~stim;


PF_dist_stim = cell2mat(arrayfun(@(a) linearize(a.PF_dist),ses1(kp),'UniformOutput',false)');

PF_PF1_stim = cell2mat(arrayfun(@(a) linearize(a.PF1),ses1(kp),'UniformOutput',false)');

PF_PF2_stim = cell2mat(arrayfun(@(a) linearize(a.PF2),ses1(kp),'UniformOutput',false)');

PF_dist_con = cell2mat(arrayfun(@(a) linearize(a.PF_dist),ses1(kp1),'UniformOutput',false)');

%%
nbins = 20;
bins = round([-fliplr(logspace(0,2,nbins)) 0 logspace(0,2,nbins)]);
bins = unique(bins);

%bins = -20:5:20;
figure
ax = tight_subplot(2,2);
axes(ax(1))
plot(bins,avghist((PF_dist_stim),pre_stim,bins,@nanmean),'o','color','k')
axes(ax(2))
plot(bins,avghist((PF_dist_stim),preRUN_stim,bins,@nanmean),'o','color','k')
axes(ax(3))
plot(bins,avghist((PF_dist_stim),ISI_stim,bins,@nanmean),'o','color','k')
axes(ax(4))
plot(bins,avghist((PF_dist_stim),ISIRUN_stim,bins,@nanmean),'o','color','k')

