%% 02_visual_inspection

% manually label bad segments

%% initialize

restoredefaultpath;
clear all; close all; pack; clc;

%% pathdef

currentFile = mfilename('fullpath');
[pathstr,~,~] = fileparts( currentFile );
cd(pathstr)

% inputs
pn.eeg_BIDS = fullfile('..', 'data', 'inputs', 'rawdata', 'eeg_BIDS');
pn.channel_locations = fullfile('..', 'data', 'inputs', 'rawdata', 'channel_locations');
pn.events = fullfile('..', 'data', 'inputs', 'rawdata', 'events');
% outputs
pn.eeg_ft = fullfile('..', 'data', 'outputs', 'eeg'); if ~exist(pn.eeg_ft); mkdir(pn.eeg_ft); end
pn.history = fullfile('..', 'data', 'outputs', 'history'); if ~exist(pn.history); mkdir(pn.history); end

% add fieldtrip toolbox
addpath(fullfile('..', 'tools', 'fieldtrip')); ft_defaults;

%% define IDs for visual screening

% N = 33
IDs = tdfread(fullfile(pn.eeg_BIDS, 'participants.tsv'));
IDs = cellstr(IDs.participant_id);

%% loop IDs

for id = 1:length(IDs)
    %% load data, start screening
    clc;
    ID = str2num(IDs{id}); disp(['Processing ',IDs{id}]);
    % load config
    load(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'config');
    % load data
    load(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_raw.mat']),'data_eeg');
    % data browser
    if ~isfield(config,'visual_inspection')
        cfg = [];
        cfg.continuous      = 'yes';                           
        cfg.preproc.demean  = 'yes';
        cfg.blocksize       = 400;
        cfg.ylim            = [-50 50];
        cfg.viewmode        = 'vertical';
        cfg.plotlabels      = 'yes';
        cfg.linecolor       = [.2 .2 .2];
    else
        cfg = config.visual_inspection;
    end

    cfg.channel = {'all'};

    % inspect data
    cfg = ft_databrowser(cfg,data_eeg);

    % check if ICA conducted on this dataset; if so new artifacts not saved
    if ~isfield(config,'ica1')

        % marked segments
        reject = cfg.artfctdef.visual.artifact;

        trl = [];

        % generate "trial" (i.e. segment) structure for ICA
        if exist('reject','var')
            if min(size(reject)) ~= 0 % JQK fix: 170915
                for j = 1:size(reject,1)+1
                    if j == 1
                        trl(j,:) = [1                reject(j,1)-1  0];
                    elseif j <= size(reject,1)
                        trl(j,:) = [reject(j-1,2)+1  reject(j,1)-1  0];
                    else
                        trl(j,:) = [reject(j-1,2)+1  size(data_eeg.trial{1},2) 0];
                    end
                end; clear j
            else
                trl = [1 size(data_eeg.trial{1},2) 0];
            end
        end

        % eliminate trials without data points
        ex = find((trl(:,1)-trl(:,2))==0);
        trl(ex,:) = []; clear ex

        % eliminate trials shorter than 3 sec (! srate = 250 Hz)
        ex = find((trl(:,2)-trl(:,1))<750);
        trl(ex,:) = []; clear ex

        % add to config structure
        config.visual_inspection = cfg;
        config.trl_ica1          = trl;

        % save config
        save(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'config');

    else

        warning('ICA already conducted. Changes not saved.')

    end
end % id loop
