% Matlab script to load all subject
% animal/non-animal picture from session 1
%
% Arnaud Delorme - Feb 2007
% ========================================

alls = { 'sph' 'wpa' 'sce' 'pla' 'mma' 'mta' 'mba' 'lmi' 'hth' 'gro' 'fsa' 'ega' 'clm' 'cba' };

eeglab;

% scan subjects
% -------------
for is = 1:length(alls)

    s = alls{is};
    files = { [ s '1ff01' ] [ s '1ff04'] [ s '1ff07'] [ s '1ff10' ] [ s '1ff13' ] };

    % scan animal files
    % -----------------
    fi = 1;
    if strcmpi(s, 'wpa'), files(3) = []; end;
    for fi = 1:4

        EEG = pop_loadcnt( [ s '/' files{fi} '.cnt' ], 'dataformat', 'int16');
        indbnd = [];
        for index = 1:length(EEG.event)
            if isstr(EEG.event(index).type)
                if ~strcmpi(EEG.event(index).type, 'boundary')
                    EEG.event(index).type = 'stimulus';
                else
                    indbnd = [ indbnd index ];
                end;
            else    
                EEG.event(index).type = 'stimulus';
            end;
        end;
        EEG.event(indbnd) = [];
        EEG = pop_epoch( EEG, { 'stimulus' }, [-1  2], 'newname', 'CNT file epochs', 'epochinfo', 'yes');
        EEG = eeg_checkset(EEG);

        EEG = pop_loaddat(EEG, [ s '/' files{fi} '.dat' ], 1000);

        for index = 1:length(EEG.event)
            if strcmpi(EEG.event(index).type, 'stimulus')
                tmp = num2str(EEG.event(index).eegtype);
                if tmp(3) == '0' | tmp(3) == '1' 
                     EEG.event(index).type = 'Animal';
                elseif tmp(3) == '5'| tmp(3) == '6' 
                    EEG.event(index).type = 'Distractor';
                else error('Unknown type');
                end;
            end;
        end;

        pop_saveset(EEG, 'filename', [ s '/data' files{fi} '.set' ]);

    end;

    % merge datasets and select target and distractors
    % ------------------------------------------------
    eeglab;
    for fi = 1:4
        EEG = pop_loadset( 'filename', ['data' files{fi} '.set'], 'filepath', s );
        [ALLEEG, EEG, CURRENTSET] = eeg_store( ALLEEG, EEG, 0 );
    end;
    EEG = pop_mergeset( ALLEEG, [1  2  3  4], 0);
    EEG=pop_chanedit(EEG,  'load',{ 'fam2.loc', 'filetype', 'autodetect'}, 'delete',32);
    EEG = eeg_checkset( EEG );
    EEG1 = pop_selectevent( EEG,  'type',{ 'Animal'}, 'deleteevents', 'off', 'deleteepochs', 'on');
    EEG1 = pop_selectevent( EEG1, 'type',{ 'rt'}, 'deleteevents', 'off', 'deleteepochs', 'on');
    EEG1 = pop_selectevent( EEG1, 'type',{ 'Animal'}, 'latency', '1 <= 2000', 'select', 'inverse', 'deleteevents', 'on', 'deleteepochs', 'off', 'invertepochs', 'off');
    pop_saveset(EEG1, 'filename', [ s '/' s 'animal.set' ]);
    EEG2 = pop_selectevent( EEG,  'type',{ 'Distractor'}, 'deleteevents', 'off', 'deleteepochs', 'on');
    EEG2 = pop_selectevent( EEG2, 'type',{ 'rt'}, 'deleteevents', 'off', 'deleteepochs', 'on', 'invertepochs', 'on');
    EEG2 = pop_selectevent( EEG2, 'type',{ 'Distractor'}, 'latency', '1 <= 2000', 'select', 'inverse', 'deleteevents', 'on', 'deleteepochs', 'off', 'invertepochs', 'off');
    pop_saveset(EEG2, 'filename', [ s '/' s 'distractor.set' ]);
    
end;

return;

% load all datasets
% -----------------
for is = 1:length(alls)
    s = alls{is};
    EEG = pop_loadset( 'filename', [ s 'animal.set'], 'filepath', s );
    [ALLEEG, EEG, CURRENTSET] = eeg_store( ALLEEG, EEG, 0 );
    EEG = pop_loadset( 'filename', [ s 'distractor.set'], 'filepath', s );
    [ALLEEG, EEG, CURRENTSET] = eeg_store( ALLEEG, EEG, 0 );
end;

% NOTES for the STUDY tutorial
%
% In the STUDY tutorial available at ftp://sccn.ucsd.edu/pub/animal_study.zip
% all datasets were processed using the script above, then average reference
% One dataset (hth) had one bad electrode that was interpolated
% All datasets were pruned of artifact manually (see each individual dataset history)
% 3 datasets were extensively prunned (LMI, HTH, GRO) before ICA would work
% All ICA computation was done in single precision
