

files = [{'DT3_rLS_rCA1_20150930_804um_576um'},...
    {'DT3_rLS_rCA1_20151002_948um_648um'},...
    {'DT3_rLS_rCA1_20151006_1524um_1224um'},...
    {'DT3_rLS_rCA1_20151006_1668um_1296um'},...
    {'DT3_rLS_rCA1_20151007_1740um_1296um'},...
    {'DT3_rLS_rCA1_20151008_1812um_1368um'},...
    {'DT3_rLS_rCA1_20151008_1956um_1440um'},...
    {'DT3_rLS_rCA1_20151009_2100um_1512um'},...
    ];

all_ripple_mod=[];
p=[];
z=[];
amplitude_rate_correlations = [];
amplitude_rate_correlations_flip = [];
rec = [];
depth=[];
% phase_locking_histo=zeros(794,100,180);
n=1;
nn=1;
nnn=1;
m=1;
nnnn=1;
all_pairs=[];
vals=[];
all_rate_homecage=[];
all_rate_track=[];


for f = 1:length(files);
    cd(files{f})
    xml = dir('*xml');
    SetCurrentSession(xml.name)
    nunits = GetUnits();
    nunits(nunits(:,1)<9,:)=[];
    lfp = GetLFP([27 60]);
    for i = 1:size(nunits,1);
        spikes{i} = GetSpikeTimes(nunits(i,:));
        all_rate{f}(i) = length(spikes{i})./length(lfp)*1000;
    end
%     if exist([files{f} '-states.mat'])
%         load([files{f} '-states.mat'])
%     end
      
%% head direction    
if exist(['hd.mat'])
    load('hd.mat','head_direction')   
    for i=1:size(head_direction,1)
       all_head_dir(nnn,:) = head_direction(i,:);
       nnn=1+nnn;
    end
end


%% ratemapping

if exist('ratemaps.mat')
    load('ratemaps.mat')
    for i=1:size(rateMap{1},1)
       all_rate_maps{m,1} = rateMap{1}(i,:,:);       
       all_rate_maps{m,2} = rateMap{2}(i,:,:);       
       all_rate_maps{m,3} = rateMap{3}(i,:,:);
       all_rate_maps{m,4} = rateMap{4}(i,:,:);
       all_phase_maps{m,1} = phaseMap{1};
       all_phase_maps{m,2} = phaseMap{2};
       all_phase_maps{m,3} = phaseMap{3};
       all_phase_maps{m,4} = phaseMap{4};
       m=1+m;
    end
    all_maps{f} = map;
    all_mappings{f} = mapping;
end

   %% phaselocking
if exist(['phaselocking_power_thresh.mat'])
    load('phaselocking_power_thresh.mat')
    p = [p; phl_p];
    z = [z; phl_z];
    for i=1:size(histo,1)
        for j=1:100
            all_phase_locking_histo(n,j,:) = hist(histo{i,j},180);
            all_circ_resultants(n,j) = circ_r(histo{i,j});
            all_circ_means(n,j) = circ_mean(histo{i,j});
        end
        n=1+n;
    end
end

%% ripple modulation

if exist(['ripple_modulation.mat'])
    load('ripple_modulation.mat')
    for r=1:size(ripple_mod,1)
%         if nunits(r,1)>8
        all_ripple_mod(nn,:) = ripple_mod(r,:);
        all_num_ripples(nn) = numRipples(r);
        nn=1+nn;
%         end
    end
end

%% state rate, home cage VS track


if exist(['state_rate.mat'])
    load('state_rate.mat')
    for i=1:length(rate_homecage)
        all_rate_homecage = [all_rate_homecage;rate_homecage(i)];
        all_rate_track = [all_rate_track;rate_track(i)];
    end
end


   %% phase/amp
% if exist(['lfp_phase_amplitude.mat'])
%     load('lfp_phase_amplitude.mat')
%     for i=1:100
%     p(i,:) = zscore(phase_amplitude(i,:));
%     end
%     subplot(4,4,n) 
%     imagesc(p)
%     axis([750 1750 0 100])
%     n=1+n;
% end

       %% ACG
if exist(['acg.mat'])
load('acg.mat');
for i=1:size(acg)
    for j=i       
        acg(i,1001)=nan;
        all_acg(nnnn,:) = acg(i,:);
        acg_behav(i,1001)=nan;
        all_acg_behav(nnnn,:) = acg_behav(i,:);
%         plot(acg{i,j})
%         pause
 nnnn=1+nnnn;
    end
end
end    

%% amp/power correlations

if exist('amp_rate_corrs.mat')
   load('amp_rate_corrs.mat')
   
   amplitude_rate_correlations = [amplitude_rate_correlations; amp_corr];
   amplitude_rate_correlations_flip = [amplitude_rate_correlations_flip; amp_corr_flip];
    
end


rec = [rec;repmat(f,size(acg_behav,1),1)];
    cd ..
end





%% plotting functionality here
for i=118:size(all_acg,1)
   subplot(4,4,1) 
   plot(all_acg(i,:))
   axis([500 1500 min(all_acg(i,:)) max(all_acg(i,:))])
   title('ACG')
   
   if all_num_ripples(i) > 800
   subplot(4,4,2)
   plot(fastrms(all_ripple_mod(i,:),5))
   axis([0 1251 0 max(all_ripple_mod(i,:))+1])
   line([625 625],[min(all_ripple_mod(i,:)) max(all_ripple_mod(i,:))],'color','r')
   title('ripple modulation')
   end
   
   
   subplot(4,4,5)
   bar(all_head_dir(i,:))
   axis([0 37 0 max(all_head_dir(i,:))])
   title('head dir')
   
   subplot(4,4,6)
   plot(freqs,amplitude_rate_correlations(i,:),'k')
   hold on
   plot(freqs,amplitude_rate_correlations_flip(i,:),'r')
   hold off
   xlabel('frequency (Hz)')
   title('amplitude/rate correlations')
   
   subplot(4,4,9)
   imagesc(squeeze(all_phase_locking_histo(i,:,:)))
   title(['cell #' num2str(i)])
   
   subplot(4,4,13)
   hold off
%    scatter(all_rate_homecage,all_rate_track,'.k')
   mag =(all_rate_track-all_rate_homecage)./(all_rate_track+all_rate_homecage);
   hist(mag,100)
   hold on
   bar(mag(i),10,.04,'r')
   title('hc             track')
   
   

   for ii =1:4
       subplot(4,4,3+(ii-1)*4)
       hold off
       if length(all_mappings{rec(i)}{ii}) > 5
       for s = 1:length(all_mappings{rec(i)}{ii})
            scatter(all_mappings{rec(i)}{ii}{s}(:,1),all_mappings{rec(i)}{ii}{s}(:,2),'.r')
            hold on
            scatter(all_mappings{rec(i)}{ii}{s}(:,3),all_mappings{rec(i)}{ii}{s}(:,4),'.b')
            scatter(all_mappings{rec(i)}{ii}{s}(1,3),all_mappings{rec(i)}{ii}{s}(1,4),150,'.g')
            scatter(all_mappings{rec(i)}{ii}{s}(1,3),all_mappings{rec(i)}{ii}{s}(1,4),150,'.g')
       end
       axis([0 500 0 500])
       title('tracking')
       end
   end
  for ii =1:4
      if length(all_mappings{rec(i)}{ii}) > 5
       subplot(4,4,4+(ii-1)*4)
       imagesc(squeeze(all_rate_maps{i,ii}))
       ylabel('trial #')
       xlabel('linearized position')
       title('single trials')
      end
  end
  pause
end
% 
