function  plot_PLM_dynamics(sumstats)

%PLOT_PLM_DYNAMICS -- Plot the profiles produced by PLM_SUMMARY.
%
% plot_PLM_dynamics(sumstats)
%
% Typical usage:
%  stats = PLM_stats(descr,o(:,1,:),o(:,2,:),o(:,3,:)) ;
%  sumstats = PLM_summary(stats) ; plot_PLM_dynamics(sumstats)
%
% See also PLM_SUMMARY, PLM_STATS, PLOT_PLM_FITS, PLMODEL1.

% Original coding by Alexander Petrov, UC Irvine.
% $Revision: 1.0 $  $Date: 2003/03/05 11:22 $

%-- Plot z-probability curves, averaged across the difficulty levels.
blocks = {[1],[2:9],[10:17],[18:25],[26:31],[32]} ;
N_blocks = length(blocks) ;
xtick = [0:4:32] ;
zPc = mean(sumstats.zP_congr,2) ;    % [32x1]
zPi = mean(sumstats.zP_incon,2) ;    % [32x1]
dpr = mean(sumstats.dprime,2)./2 ;      % [32x1]

subplot(3,1,1) ; cla ; hold on ;
for k = 1:N_blocks
   idx = blocks{k} ;
   plot(idx,zPc(idx),'b.-',idx,zPi(idx),'r.-',idx,dpr(idx),'k.-') ;
end
axis([0 33 0 2]) ; set(gca,'xtick',xtick) ; box on ; grid on ;
ylabel('\Phi^{-1}(Pcorr)') ; hold off ; 

sc = sumstats.accZP(1:3) ; si = sumstats.accZP(4:6) ; d = (sc+si)./2 ;
title([sprintf('d''/2 = %4.2f = [%4.2f  %4.2f  %4.2f]',mean(d),d(:)),...
       sprintf('   zPcon = [%4.2f  %4.2f  %4.2f]',sc(:)),...
       sprintf('   zPinc = [%4.2f  %4.2f  %4.2f]',si(:))]) ;

%-- Plot the three difficulty levels in the Congruent condition
zPc = sumstats.zP_congr ;    % [32x3]
style = {'b^-','ro-','gs-'} ;

subplot(3,1,2) ; cla ; hold on ;
for c = 1:3
   for k = 1:N_blocks
      idx = blocks{k} ;
      plot(idx,zPc(idx,c),style{c}) ;
   end
end
axis([0 33 0 2]) ; set(gca,'xtick',xtick) ; box on ; grid on ;
ylabel('\Phi^{-1}(Pcorr)') ; hold off ;

title_str = sprintf('zP = [%4.2f  %4.2f  %4.2f]',sumstats.accZP(1:3)) ;
if (isfield(sumstats,'accA'))
   title_str = [title_str, ...
         sprintf('     A = [%4.2f  %4.2f  %4.2f]',sumstats.accA(1:3))] ;
end
if (isfield(sumstats,'accI'))
   title_str = [title_str, ...
         sprintf('     I = [%5.2f  %5.2f  %5.2f]',sumstats.accI(1:3))] ;
end
title(title_str) ;


%-- Fine-grain dynamics around the context transitions
t = [-285:30:+285] ;             % see PLM_transitions
pre = [5:10] ; post = [11:20] ;
xtick = [t(pre),0,t(post)] ;
xticklabel = '-165||-105||-45||0||45||105||165||225||285' ;
cc = sumstats.contextc ;         % [1x(10+10)]
ci = sumstats.contexti ;         % [1x(10+10)]
cm = (cc+ci)./2 ;

subplot(3,2,5);
plot(t(pre),cc(pre),'b.-',t(pre),ci(pre),'r.-',t(pre),cm(pre),'k.-',...
     t(post),cc(post),'b.-',t(post),ci(post),'r.-',t(post),cm(post),'k.-');
axis([-180 300 0 1.4]) ; ylabel('\Phi^{-1}(Pcorr)') ; box on ;
hh=line([0 0],[0 1.4]) ; set(hh,'Color','k','LineStyle',':') ; 
set(gca,'xtick',xtick,'xticklabel',xticklabel,...
        'ytick',[0:0.2:2],'yGrid','on') ;

title([sprintf('pre=[%3.1f  %4.2f  %3.1f]',...
                mean(cc(8:10)),mean(cm(8:10)),mean(ci(8:10))),...
     sprintf('  sw=[%3.1f  %4.2f  %3.1f]',cc(11),cm(11),ci(11))]) ;


%-- "Baseline" transitions
oc = sumstats.otherc ;         % [1x(10+10)]
oi = sumstats.otheri ;         % [1x(10+10)]
om = (oc+oi)./2 ;

subplot(3,2,6);
plot(t(pre),oc(pre),'b.-',t(pre),oi(pre),'r.-',t(pre),om(pre),'k.-',...
     t(post),oc(post),'b.-',t(post),oi(post),'r.-',t(post),om(post),'k.-');
axis([-180 300 0 1.4]) ; ylabel('\Phi^{-1}(Pcorr)') ; box on ;
hh=line([0 0],[0 1.4]) ; set(hh,'Color','k','LineStyle',':') ; 
set(gca,'xtick',xtick,'xticklabel',xticklabel,...
        'ytick',[0:0.2:2],'yGrid','on') ;

title([sprintf('pre=[%3.1f  %3.1f  %3.1f]',...
                mean(oc(pre)),mean(om(pre)),mean(oi(pre))),...
     sprintf('  post=[%4.2f  %4.2f  %4.2f]',...
                mean(oc(post)),mean(om(post)),mean(oi(post)))]) ;

%%%%% End of PLOT_PLM_DYNAMICS.M
