function  seq = PLE_sequential(S)

%PLE_SEQUENTIAL -- Frequencies as a fun. of previous stimulus and response.
%
% seq = PLE_sequential(S)
%
% S must be a structure array created by PLEDATA_BY_SBJ. Only three 
% of its fields are used: 'sbj', 'group', and 'data'.
% The function returns a structure array SEQ with fields 'sbj','group',
% and 'counts', where each COUNTS is a 144x14 matrix with columns:
%  1. backgr -- [-15,+15] background noise orientation (in degrees)
%  2. prev_contrast -- [.10 .15 .23] peak contrast of previous stimulus
%  3. prev_Gabor -- [0=incongruent,1=congruent] w.r.t. background orient
%  4. prev_resp -- [0=incongr,1=congr] with respect to backgr orientation
%  5. prev_correct = (prev_resp==prev_Gabor) -- [0=incorrect,1=correct]
%  6. curr_contrast -- [.10 .15 .23] peak contrast of current stimulus
%  7. curr_Gabor -- [0=incongr,1=congr] orient of curr stim w.r.t. backgr
%  8. N_total -- total number of cases in the condition specified by 1.-7.
%  9. N_congr -- number of cases w curr response congruent w background
% 10. N_incongr -- number of cases w incongruent resp (==N_total-N_congr)
% 11. N_correct -- number of correct responses (where curr_resp==curr_Gabor)
% 12. P_congr = N_congr/N_total -- probability of responding "congruent"
% 13. P_incognr = N_incongr/N_total -- P("incongruent")=1-P("congruent")
% 14. P_correct = N_correct/N_total -- probability correct
%
%  See also PLE_SEQUENLR, PLEDATA_BY_SBJ, XTAB.

%  Original coding by Alexander Petrov, UC Irvine.
%  $Revision: 1.1 $  $Date: 2002/05/26 18:51 $

for k = 1:length(S)
  seq(k).sbj = S(k).sbj ;
  seq(k).group = S(k).group ;
  seq(k).counts = do_one(S(k).data) ;
end

%--- Return SEQ
%%%% End of the main function

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DATA is a matrix with 1 row per trial and 10 cols (see PLEDATA_BY_SBJ):
%  1=block [1:32], 2=noise_degr [-15,+15], 3=Cpeak [.10 .15 .23],
%  4=offset [-111,+111], 5=Gabor_degr [-10,+10], 6=congr [0,1]
%  7=resp [0,1,2], 8=correct [0,1], 9=RT [msec], 10=exitcode [0,1,2]
function  counts = do_one(data)

%-- Retrieve the relevant variables (i.e. columns)
d = data(:,[1,2,3,6,7,10]) ;    % [block,backgr,contrast,Gabor,resp,exitcode]

%-- Recode responses from [1="Left",2="Right"] to [0="incongr",1="congr"]
backgr = d(:,2) ;
LR = d(:,5) ;
congr = repmat(-9,size(LR)) ;
congr(find(backgr==-15 & LR==1)) = 1 ;   % congruent: Left-Left
congr(find(backgr==-15 & LR==2)) = 0 ;   % incongruent: Left-Right
congr(find(backgr==+15 & LR==1)) = 0 ;   % incongruent: Right-Left
congr(find(backgr==+15 & LR==2)) = 1 ;   % congruent: Right-Right
d(:,5) = congr ;

%-- Put "previous" trials on the left of "current" trials
d = [[[0 0 0 0 0 1];d(1:end-1,:)] , d] ;

%-- Reject all trials with missing values (EXITCODE > 0)
idx = find((d(:,6)==0) & (d(:,12)==0)) ;
d = d(idx,[1:5,7:11]) ;

%-- Reject all trials at block boundaries (PREV_BLOCK~=CURR_BLOCK)
idx = find(d(:,1)==d(:,6)) ;
d = d(idx,[2:5,8:10]) ;      % [bkgr,prCp,prGab,prResp,crCp,crGab,crResp]

%-- Cross-tabulate CURR_RESP of the other six (independent) variables
[count_congr,vals] = xtab2(d(:,1:6),d(:,7)) ;
if (length(vals{2})~=2)   % vals{2} must be [0 1], not [-9 0 1] or whatnot
  error('Invalid response value(s): [',num2str(vals{2}),']') ;
end
IVs = vals{1} ;           % indep.vars: [bkgr,prCp,prGab,prResp,crCp,crGab]
N = sum(count_congr,2) ;  % total number of cases per condition

%-- Correct responses
prev_correct = (IVs(:,4)==IVs(:,3)) ;  % prev_resp == prev_Gabor, [144x1]
curr_correct = (d(:,7)==d(:,6)) ;      % curr_resp == curr_Gabor, [9568x1]

%-- Cross-tabulate CURR_CORRECT of the other six (independent) variables
[count_corr,vals] = xtab2(d(:,1:6),curr_correct) ;
if (length(vals{2})~=2)   % vals{2} must be [0 1], not [-9 0 1] or whatnot
  error('Invalid response value(s): [',num2str(vals{2}),']') ;
end
if (any(sum(count_corr,2)~=N))
  error('Mistaken partitioning into correct vs. incorrect responses.') ;
end
count_corr = count_corr(:,2) ;   % 1st column counts INcorrect responses

%-- Calculate probabilities
Ns = [count_congr(:,[2 1]),count_corr] ;   % [Ncongr,Nincongr,Ncorr]
Ps = Ns ./ repmat(N,1,3) ;

%-- Put everything together and return
counts = [IVs(:,1:4) prev_correct IVs(:,5:6) N Ns Ps] ;

%--- Return COUNTS
%%%% End of subfunction DO_ONE

%%%%% End of PLE_SEQUENTIAL.M
