function  C = analyze_PLE_context(S)

%ANALYZE_PLE_CONTEXT -- Context-specific effects on perceptual learning.
%
%  C = analyze_PLE_context(S)
%
%  S must be a structure array produced by PLEDATA_BY_SUBJECT.
%  C is a structure array with one element per subject and with fields:
%    1. P_right -- probability of Response="Right" as a function of
%           difficulty level (stimulus contrast Cpeak), context (noise
%           orientation), and target stimulus orientation; [3x2x2]
%    2. dprime -- sensitivity measures per difficulty and context; [3x2]
%    3. log_beta -- criterion log-values (see function DPRIME); [3x2]
%    4. tmean_RT -- trimmed mean response times per difficulty level,
%           context, and stimulus orientation; [3x2x2]
%
%  The data for each "context condition" are collected over 12 blocks:
%  Context 1 (noise_degr=-15) for Group 1 includes blocks 11-17 and 27-31.
%  Context 2 (noise_degr=+15) for Group 1 includes blocks 5:9 and 19:25.
%  Context 1 (noise_degr=-15) for Group 2 includes blocks 5:9 and 19:25.
%  Context 2 (noise_degr=+15) for Group 2 includes blocks 11-17 and 27-31.
%  The "transitional" blocks 1-4, 10, 18, 26, and 32 are not included in 
%  any of these analyses.
%
%  See also PLEDATA_BY_SBJ, PLEXP1.

%  Original coding by Alexander Petrov, UC Irvine.
%  $Revision: 1.0 $  $Date: 2002/02/12 16:17 $

P_right  = zeros([3,2,2]) ;   % Cpeak x Context x Stimulus
tmean_RT = zeros([3,2,2]) ;   % Cpeak x Context x Stimulus

for k = 1:length(S)
   s = S(k) ;
   %- Figure out which blocks belong to contexts A and B, respectively
   if  (s.group==1)
      context_A = [11:17,27:31]' ;
      context_B = [5:9,19:25]' ;
   elseif  (s.group==2)
      context_A = [5:9,19:25]' ;
      context_B = [11:17,27:31]' ;
   else
      error(['S(',int2str(k),').group should be either 1 or 2.']) ;
   end
   %- Sanity check: make sure all block indices belong where they should
   if (~isempty(setdiff(context_A,s.context{1})) | ...
       ~isempty(setdiff(context_B,s.context{2})) )
      error('Inconsistent assignments of contexts across blocks.') ;
   end
   %- Aggregate frequencies across blocks
   %  S.RESP_FREQ is a 4-D cross-tab:  block x Cpeak x Gabor_degr x resp
   resp_freq_A = squeeze(sum(s.resp_freq(context_A,:,:,:),1)) ;  % 3x2x2
   resp_freq_B = squeeze(sum(s.resp_freq(context_B,:,:,:),1)) ;  % 3x2x2
   N_cases_A = squeeze(sum(resp_freq_A,3)) ;  % 3 contrasts x 2 contexts
   N_cases_B = squeeze(sum(resp_freq_B,3)) ;  % 3 contrasts x 2 contexts
   %- Probability of responding "Right" (i.e., resp=2)
   P_right_A = resp_freq_A(:,:,2) ./ N_cases_A ;       % 3x2
   P_right_B = resp_freq_B(:,:,2) ./ N_cases_B ;       % 3x2
   %- Re-arrange:  Cpeak x Context x Gabor_degr
   P_right(:,1,:) = reshape(P_right_A,[3,1,2]) ;
   P_right(:,2,:) = reshape(P_right_B,[3,1,2]) ;
   %- Convert probabilities to d-primes and log-betas
   pHit = P_right(:,:,2) ;            % P(resp=2|stim=2)
   pFA  = P_right(:,:,1) ;            % P(resp=2|stim=1)
   [dpr,beta] = dprime(pHit,pFA) ;    % 3x2 each
   %- Aggregate mean RTs across blocks
   RT_A = squeeze(mean(s.tmean_RT(context_A,:,:),1)) ;      % 3x2
   RT_B = squeeze(mean(s.tmean_RT(context_B,:,:),1)) ;      % 3x2
   tmean_RT(:,1,:) = reshape(RT_A,[3,1,2]) ;
   tmean_RT(:,2,:) = reshape(RT_B,[3,1,2]) ;
   %- Pack everything into a big structure and return
   C(k).P_right = P_right ;
   C(k).dprime = dpr ;
   C(k).log_beta = log(beta) ;
   C(k).tmean_RT = tmean_RT ;
end

%--- Return C
%%%%% End of ANALYZE_PLE_CONTEXT.M
