

h_rate=[];
h_phase=[];
l_rate=[];
l_phase=[];
h_u=[];
c=1;
cc=1;
% 
for i=1:length(dat)
units = dat(i).units;
spikes = dat(i).spikes;
trials = dat(i).trials;
pos = dat(i).pos;
map = dat(i).map;
mapping = dat(i).mapping;
lfp = dat(i).lfp;
% [rateMap occuMap phaseMap] = spaceRateMap(spikes,pos,map,mapping,trials,lfp(:,[1 3]));
% [rateMap occuMap phaseMap] = spaceRateMap(spikes,pos,map,mapping,trials,lfp);
rateMap = dat(i).rateMap;
occuMap = dat(i).occuMap;
phaseMap = dat(i).phaseMap;

 [beta_rates{i} dev_rates{i} stats_rates{i} actual_rates{i} beta_phases{i}...
     dev_phases{i} stats_phases{i} actual_phases{i}  predictor{i}]...
     = decodeVariablesSeperately(trials,phaseMap,rateMap,units,map,mapping);
% [beta_rates beta_phases dev_rates dev_phases...
%     beta_rates_control beta_phases_control dev_rates_control dev_phases_control...
%     stats_rates stats_phases stats_rates_control stats_phases_control...
   
i 
% dp{i}=dev_phases;
% dr{i}=dev_rates;
% dpc{i}=dev_phases_control;
% drc{i}=dev_rates_control;
% stats(i).stats_rates=stats_rates;
% stats(i).stats_rates_control=stats_rates_control;
% stats(i).stats_phases=stats_phases;
% stats(i).stats_phases_control=stats_phases_control;
% end
% 
% for i=1:length(dat)
% % iterate through conditions here
%     units = dat(i).units;
%     for c=1:length(dev{i})
%         
%         figure(8)
% %         dev_phases_control{c}=mean(dpc{i}{c},2)';
% %         dev_rates_control{c}=mean(drc{i}{c},2)';
%         subplot(2,2,1)
%         hold on
% 
%         f = find(units(:,1)>6);
%         for ff= 1:length(f)
%             if ~isnan(dev{i}{c}(f(ff)))
% %         plot(((dp{i}{c}(f(ff)))+2*(stats(i).stats_phases{c}(f(ff)).dfe)),((dr{i}{c}(f(ff)))+2*(stats(i).stats_rates{c}(f(ff)).dfe)),'.k')
% %  plot(stats(i).stats_phases{c}(f(ff)).p(2),stats(i).stats_rates{c}(f(ff)).p(2),'.k')
%             end
%         end
% %         plot(((dp{i}{c}(f))+2*(dev_phases_control{c}(f))),((dr{i}{c}(f))+2*(dev_rates_control{c}(f))),'.k')
% %         plot(dp{i}{c}(f),dr{i}{c}(f),'.k')
%                set(gca,'yscale','log')
%         set(gca,'xscale','log')
% axis([10e-20 1 10e-50 1])
% 
%         subplot(2,2,2)
%         f = find(units(:,1)<7);
%         for ff= 1:length(f)
%             if ~isnan(dev{i}{c}(f(ff)))
% %         plot(((dp{i}{c}(f(ff)))+2*(stats(i).stats_phases{c}(f(ff)).dfe)),((dr{i}{c}(f(ff)))+2*(stats(i).stats_rates{c}(f(ff)).dfe)),'.r')
% %   plot(stats(i).stats_phases{c}(f(ff)).p(2),stats(i).stats_rates{c}(f(ff)).p(2),'.r')
%             end
%         end
% %         plot(((dp{i}{c}(f))+2*(dev_phases_control{c}(f))),((dr{i}{c}(f))+2*(dev_rates_control{c}(f))),'.r')
% %         plot(dp{i}{c}(f),(dr{i}{c}(f)),'.r')
%       
%         hold on
%         set(gca,'yscale','log')
%         set(gca,'xscale','log')
% axis([10e-20 1 10e-50 1])
%         subplot(2,2,3)
%         f = find(units(:,1)<7);
%         for ff= 1:length(f)
%             if ~isnan(dp{i}{c}(f(ff)))
% %         plot(((dp{i}{c}(f(ff)))+2*(stats(i).stats_phases{c}(f(ff)).dfe)),((dr{i}{c}(f(ff)))+2*(stats(i).stats_rates{c}(f(ff)).dfe)),'.r')
%     plot(stats(i).stats_phases{c}(f(ff)).p(2),stats(i).stats_rates{c}(f(ff)).p(2),'.r')
%             end
%         end
% %         plot(((dp{i}{c}(f))+2*(dev_phases_control{c}(f))),((dr{i}{c}(f))+2*(dev_rates_control{c}(f))),'.r')
% %         plot(dp{i}{c}(f),dr{i}{c}(f),'.r')
%     
%         hold on
%         f = find(units(:,1)>6);
%         for ff= 1:length(f)
%             if ~isnan(dp{i}{c}(f(ff)))
% %         plot(((dp{i}{c}(f(ff)))+2*(stats(i).stats_phases{c}(f(ff)).dfe)),((dr{i}{c}(f(ff)))+2*(stats(i).stats_rates{c}(f(ff)).dfe)),'.k')
%         plot(stats(i).stats_phases{c}(f(ff)).p(2),stats(i).stats_rates{c}(f(ff)).p(2),'.k')
%             end
%         end
% %         plot(((dp{i}{c}(f))+2*(dev_phases_control{c}(f))),((dr{i}{c}(f))+2*(dev_rates_control{c}(f))),'.k')
% %         plot(dp{i}{c}(f),dr{i}{c}(f),'.k')
%         set(gca,'yscale','log')
%         set(gca,'xscale','log')
% axis([10e-20 1 10e-50 1])
%   f = find(units(:,1)>6);
%     for ff= 1:length(f)
%             if ~isnan(dp{i}{c}(f(ff)))
%     h_rate=[h_rate,stats(i).stats_rates{c}(f(ff)).p(2)];
%     h_phase=[h_phase,stats(i).stats_phases{c}(f(ff)).p(2)];
% %     h_rate=[h_rate,((dr{i}{c}(f(ff)))+2*(stats(i).stats_rates{c}(f(ff)).dfe))];
% %     h_phase=[h_phase,((dp{i}{c}(f(ff)))+2*(stats(i).stats_phases{c}(f(ff)).dfe))];
%             end
%     end
%     f = find(units(:,1)<7);
%     for ff= 1:length(f)
%             if ~isnan(dp{i}{c}(f(ff)))
%     l_rate=[l_rate,stats(i).stats_rates{c}(f(ff)).p(2)];
%     l_phase=[l_phase,stats(i).stats_phases{c}(f(ff)).p(2)];
% %     l_rate=[l_rate,((dr{i}{c}(f(ff)))+2*(stats(i).stats_rates{c}(f(ff)).dfe))];
% %     l_phase=[l_phase,((dp{i}{c}(f(ff)))+2*(stats(i).stats_phases{c}(f(ff)).dfe))];
%             end
%     end
% %     
%     end
% pause(.1)
% end
% 
% for i=1:length(dat)
%    for j=1:length(actual_phases{i})
%        for k=1:length(actual_phases{i}{j})
%        if ~isempty(actual_phases{i}{j}{k,1})
%            yfit_phases=glmval(beta_phases{i}{j}(k,[1 2])',[actual_phases{i}{j}{k,1}],'identity');
%            yfit_rates=glmval(beta_rates{i}{j}(k,[1 2])',[actual_rates{i}{j}{k,2}],'identity');
%            
%             p = normpdf(predictor{i}{j}{k},yfit_phases);
%             p(p==0)=nan;
%             logl_phases{i}{j}{k} = nansum(log(p));
%             p = normpdf(predictor{i}{j}{k},yfit_rates);
%             p(p==0)=nan;
%             logl_rates{i}{j}{k} = nansum(log(p));
%             
%             % plotting
%             if dat(i).units(k,1)>6
%                 if stats_phases{i}{j}(k).p(2) <.05 || stats_phases{i}{j}(k).p(2) <.05
%                 subplot(2,2,1)
%                 scatter(logl_phases{i}{j}{k},logl_rates{i}{j}{k},'.k')
%                 hold on
%                 subplot(2,2,3)
%                 scatter(stats_rates{i}{j}(k).p(2),stats_phases{i}{j}(k).p(2),'.k')
%                 set(gca,'yscale','log')
%                 set(gca,'xscale','log')
%                 hold on
%                 end
%             elseif dat(i).units(k,1)<7 
%                if stats_phases{i}{j}(k).p(2) <.05 || stats_phases{i}{j}(k).p(2) <.05
%                subplot(2,2,2) 
%                scatter(logl_phases{i}{j}{k},logl_rates{i}{j}{k},'.r')
%                hold on
%                subplot(2,2,4)
%                scatter(stats_rates{i}{j}(k).p(2),stats_phases{i}{j}(k).p(2),'.r')
%                set(gca,'yscale','log')
%                set(gca,'xscale','log')
%                hold on
%                end
%             end
%        end
%        end
%    end   
%    for i=1:length(dat)
      for j=1:length(actual_phases{i})
       for k=1:length(actual_phases{i}{j})
       if ~isempty(actual_phases{i}{j}{k,1})
           yfit_phases=glmval(beta_phases{i}{j}(k,[1 2])',[actual_phases{i}{j}{k,1}],'identity');
           yfit_rates=glmval(beta_rates{i}{j}(k,[1 2])',[actual_rates{i}{j}{k,2}],'identity');
           
            p = normpdf(predictor{i}{j}{k},yfit_phases);
            p(p==0)=nan;
            logl_phases{i}{j}{k} = nansum(log(p));
            p = normpdf(predictor{i}{j}{k},yfit_rates);
            p(p==0)=nan;
            logl_rates{i}{j}{k} = nansum(log(p));
            
            % plotting
            if dat(i).units(k,1)<5
%                 if stats_phases{i}{j}(k).p(2) <.05 || stats_phases{i}{j}(k).p(2) <.05
                subplot(2,2,1)
                scatter(logl_phases{i}{j}{k},logl_rates{i}{j}{k},'.k')
                hold on
                subplot(2,2,3)
                scatter(stats_rates{i}{j}(k).p(2),stats_phases{i}{j}(k).p(2),'.k')
                set(gca,'yscale','log')
                set(gca,'xscale','log')
                hold on
                d(c,:)=[dev_rates{i}{j}(k);dev_phases{i}{j}(k)];
                c=1+c;
%                 end
            end
       end
       end
      end   
   
   pause(.1)
   
   
end





