function varargout=dopEventActRej(eventData,varargin)
% dopOSCCI Mark II: dopEventActRej ~ 11-Jan-2011 (last edit)
%
% examine activation range in order for event epoch screening (activation
% rejection screening).
%
% expects eventData = dop.stack (see dopEventStack function)
%
% adding additional screen - left right separation
% - sometimes ones channel drops out but not enough to be rejected - look
% for left right separation
%

% -------------------------------------------------------------------------
%     'dopOSCCI' summarises functional transcranial Doppler ultrasonography
%     (fTCD) data.  The primary function of the software is to assess the
%     hemispheric lateralization of cognitive function.
%
%     Copyright (C) 2011 the Chancellor, Masters and Scholars of the
%     University of Oxford.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/> 
%     or write to the Free Software Foundation, Inc., 51 Franklin Street,
%     Fifth Floor, Boston, MA 02110-1301, USA.
%
%     Authors: Nic Badcock, Georgina Holt, Anneka Holden, & Dorothy Bishop.
%       contact: nicholas.badcock@gmail.com
%
%     Type 'dopOSCCIlicense' for license details or see dopOSCCIlicense.txt
%
fprintf('\n%s\n\t%s',['Running ',mfilename],...
    'Examining Event Activation Range:');
try
    inputs.varargin=varargin;
    inputs.defaults=struct('actRej',[70 130],...); %,... % activation rejection limits (blood velocity per second)
        'actSep',100); % separation between left and right channels - 100 won't do anything
    actr=setGetInputsStruct(inputs);
    
    actr.translation='activation rejection';
    actr.structAbbreviations='actr';
    actr.data=eventData; clear eventData
    actr.out.notes=sprintf('%s\n\t%s\n\t%s\n\t%s\n\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n',...
        'Activation Rejection Notes:',...
        ' - X (row) = Event',...
        [' - Y (column) 1 = # samples < min (',num2str(actr.actRej(1)),') ',...
        ', 2 = # samples > max (',num2str(actr.actRej(2)),')'],...
        ' - Z (?) 1 = Left doppler channel, 2 = Right doppler channel.',...
        '# samples > 0 indicates to-be-rejected epoch.',...
        ' Structure:','sample.count = count of samples outside range',...
        'sample.percent = percent of samples outside range',...
        'reject = logical array of epochs with values outside range');
    
    actr.evNames=actr.data.evNames; % prior 29-10-10 fieldnames(actr.data);
    for i=1:length(actr.evNames)
        actr.evn=i; % channel number
        actr.en=actr.evNames{actr.evn}; % event name
        actr.el=length(actr.data.(actr.en)); % event length
        % subject mean baseline for each event epoch from each event epoch
        % this uses an array operation which is quite tidy.
        actr.out.(actr.en).samples.count(:,1,:)=sum(bsxfun(@lt,actr.data.(actr.en),actr.actRej(1)),2);
        actr.out.(actr.en).samples.count(:,2,:)=sum(bsxfun(@gt,actr.data.(actr.en),actr.actRej(2)),2);
        actr.out.(actr.en).samples.percent=bsxfun(@rdivide,actr.out.(actr.en).samples.count,actr.el);
        % event/epoch screen = any trials with left or right values outside
        % of activation rejection range
        actr.out.(actr.en).reject=sum(sum(actr.out.(actr.en).samples.count>0,3),2)>0;
        
        %% activation separation
        actr.out.(actr.en).diff=abs(bsxfun(@minus,actr.data.(actr.en)(:,:,1),actr.data.(actr.en)(:,:,2)));
        actr.out.(actr.en).diffSamples=sum(bsxfun(@gt,actr.out.(actr.en).diff,actr.actSep),2);
        actr.out.(actr.en).reject=actr.out.(actr.en).reject+actr.out.(actr.en).diffSamples~=0;
    end
    %% set ouputs
    varargout{1}=actr.out;
    fprintf('\t%s\n','Done');
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err);
end

clear actr inputs

