function  Zcongr = PLM_Pblock1(stim,Zinp,trials_per_block)

%PLM_PBLOCK1 -- z-Probability correct by block and stimulus type.
%
%  Zcongr = PLM_Pblock1(stim,Zinp)
%  Zcongr = PLM_Pblock1(stim,Zinp,trials_per_block)
%
%  STIM is is a matrix describing the trial sequence. It usually is
%  generated by BLOCK_FROM_CACHE and has five columns (see PLMODEL1).
%  Only the first three columns are relevant (the rest can be omitted):
%   1. Gabor orientation: 1=-10 or 2=+10 deg, see PLEXP1_PARAMS
%   2. Gabor peak contrast: 1=0.10, 2=0.15 or 3=0.23, see PLEXP1_PARAMS
%   3. noise orientation: 1=-15 or 2=+15 deg, see PLEXP1_PARAMS
%  TRIALS_PER_BLOCK defaults to 300, as in PLEXP1.
%
%  ZINP is a column vector of the bottom-up input to the output node
%  (including the bias) divided by the standard deviation of the output
%  noise (that is, Mparams.out_noise). Any thresholds must also have been
%  taken into account. Thus ZINP(t) is the z-transform of the probability 
%  of responding "Right" on trial T. The bottom-up input is returned as 
%  the trird column of the OUTPUT matrix produced by PLMODEL1. 
%
%  ZCONGR is a 6-by-N_blocks matrix of z-transformed probabilities of
%  correct response for each stimulus type across blocks.
%  The rows (or "conditions") in ZCONGR have the same indices (or IDs)
%  as in PLM_SUMSQFIT (see also PLOT_PLM_FIT and REARRANGE_BY_CONTEXT):
%   1: congruent Gabor, contrast 0.23
%   2: incongruent Gabor, contrast 0.23
%   3: congruent Gabor, contrast 0.15
%   4: incongruent Gabor, contrast 0.15
%   5: congruent Gabor, contrast 0.10
%   6: incongruent Gabor, contrast 0.10
%
%  PLM_PBLOCK1 does not depend on any global variables.
%  Unlike PLM_PROBAB1, it does not use the weight history and does not
%  probe each stimulus type on each trial. Instead, it simply takes
%  whatever stimulus type was presented during the actual PLMODEL1 run.
%  There are 50 trials for each stimulus type in each block, which
%  provides enough power, especially given that ZINP is based on the
%  contunuous internal variable rather than the binary response.
%  Only the "current" background is available because the "other"
%  is not probed at all (see PLM_PROBAB1 and REARRANGE_BY_CONTEXT).
%
%  Typical usage:
%    Mparams = PLM_params ; [output,W_hist] = PLModel1(stim,Mparams) ;
%    Zin = output(:,3) ./ Mparams.out_noise ;
%    Zcongr = PLM_Pblock1(stim,Zin) ; plot_PLM_fit(Zcongr) ;
%
%  See also PLM_ZCONGR1, PLMODEL1, PLM_SUMSQFIT, PLM_PROBAB1.

%  Original coding by Alexander Petrov, UC Irvine.
%  $Revision: 1.0 $  $Date: 2002/08/12 15:30 $

%-- Form the six "conditions"
% incongr = (Gabor~=background) = (stim(:,1)~=stim(:,3))
cond = (stim(:,1)~=stim(:,3)) + 2.*stim(:,2) - 1 ;

%-- Blocks
if (nargin < 3)  trials_per_block = 300 ; end
N_trials = length(cond) ;
N_blocks = N_trials / trials_per_block ;   % assumed integer

%-- Transform ZINP to probabilities, so as to average expectations
Pinp = normcdf(Zinp) ;

%-- Main loop
Pcongr = zeros(6,N_blocks) ;
trial_idx = [1:trials_per_block]' ;
for b = 1:N_blocks
   P = Pinp(trial_idx) ;
   C = cond(trial_idx) ;
   Pcongr(1,b) =   mean(P(find(C==1))) ;  % congruent, assuming bkgr="R"
   Pcongr(2,b) = 1-mean(P(find(C==2))) ;  % incongruent, assum. bkgr="R"
   Pcongr(3,b) =   mean(P(find(C==3))) ;
   Pcongr(4,b) = 1-mean(P(find(C==4))) ;
   Pcongr(5,b) =   mean(P(find(C==5))) ;
   Pcongr(6,b) = 1-mean(P(find(C==6))) ;
   trial_idx = trial_idx + trials_per_block ;
end

%-- Transform back to z-scores
Zcongr = max(-10,min(+10,norminv(Pcongr))) ;    % avoid overflows

%-- Reverse the sign in those blocks where the congruent response
% is "Left". Take the first trial in each block as a representative,
% assuming constant background throughout the block.
trial_idx = [1:trials_per_block:N_trials] ;
backgr = stim(trial_idx,3) ;      % 1="Left", 2="Right"
idx = find(backgr==1) ;
Zcongr(:,idx) = -Zcongr(:,idx) ;  % norminv(prob(congruent|condition,time))

%--- Return ZCONGR
%%%%% End of file PLM_PBLOCK1.M
