Home > vbmeg > functions > job > trial_rejection > vb_job_reject_channels_trials_gui.m

vb_job_reject_channels_trials_gui

PURPOSE ^

Find bad channel/trials based on maximum amplitude and 1st differece

SYNOPSIS ^

function [fileinfo,flg] = vb_job_reject_channels_trials_gui(datafile, amp_thres,thred_val, per_ch,per_tr, rej_criterion)

DESCRIPTION ^

 Find bad channel/trials based on maximum amplitude and 1st differece
 signals. This program is based on vb_job_bad_trial_interact.m but run structure is completely ignored. 

 [Usage]
  > vb_job_reject_channels_trials_gui(datafile, 2e-12)  for gradio-MEG
  > vb_job_reject_channels_trials_gui(datafile, 1e-4)   for EEG

 [Input]
   datafile: cell string for multiple MEG/EEG epoch data files
   amp_thres : amplitude threshold in abolute range. 
               For axial-gradio MEG data, magnetic field more than 1~2pT/cm is not considered from brain. 
               Thus amp_thres = 2e-12 (T/cm) may be a reasonable choice.
               For EEG data, electric potential more than 100 microV is
               not considered from brain. Thus amp_thres = 1e-4 may be a
               reasonable choice.

 [Optional Input]
   thres_val : threshold value for amplitude and 1st differential signals
               see below for more details  {Def:[9 8]}
   per_ch   : percent threshold for channel rejection. {Def : 0.05}
   per_tr   : percent threshold for trial rejection. {Def: 0.05}
   rej_criterion : rejection criterion {Def:0}. 
                  = 0 uses criterion 1 and 2 below.
                  = 1 uses criterion 1, 2 and 3 below.
                
 * Rejection criteria
   Timeseries at a specified channel and trial is marked as bad if either
   of following three conditions is satisfied. 
   1. maximum ampulitude exeeds amp_thres.  Note that this is absolute value threshold. 
     This value must be adjusted accroding to measurement dynamic range (EEG or MEG).
   2. maximum amplitude relative to standard deviation (within sessions or
   all session average) exceed thres_val(1)
   3. maximum 1st differential siganal relative to standard deviation
   (within sessions or all session average) exceed thres_val(2)
  
 2014/09/14 Okito Yamashita
 2009/02/01 Masa-aki Sato

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function  [fileinfo,flg] = vb_job_reject_channels_trials_gui(datafile, amp_thres,...
0002     thred_val, per_ch,per_tr, rej_criterion)
0003 % Find bad channel/trials based on maximum amplitude and 1st differece
0004 % signals. This program is based on vb_job_bad_trial_interact.m but run structure is completely ignored.
0005 %
0006 % [Usage]
0007 %  > vb_job_reject_channels_trials_gui(datafile, 2e-12)  for gradio-MEG
0008 %  > vb_job_reject_channels_trials_gui(datafile, 1e-4)   for EEG
0009 %
0010 % [Input]
0011 %   datafile: cell string for multiple MEG/EEG epoch data files
0012 %   amp_thres : amplitude threshold in abolute range.
0013 %               For axial-gradio MEG data, magnetic field more than 1~2pT/cm is not considered from brain.
0014 %               Thus amp_thres = 2e-12 (T/cm) may be a reasonable choice.
0015 %               For EEG data, electric potential more than 100 microV is
0016 %               not considered from brain. Thus amp_thres = 1e-4 may be a
0017 %               reasonable choice.
0018 %
0019 % [Optional Input]
0020 %   thres_val : threshold value for amplitude and 1st differential signals
0021 %               see below for more details  {Def:[9 8]}
0022 %   per_ch   : percent threshold for channel rejection. {Def : 0.05}
0023 %   per_tr   : percent threshold for trial rejection. {Def: 0.05}
0024 %   rej_criterion : rejection criterion {Def:0}.
0025 %                  = 0 uses criterion 1 and 2 below.
0026 %                  = 1 uses criterion 1, 2 and 3 below.
0027 %
0028 % * Rejection criteria
0029 %   Timeseries at a specified channel and trial is marked as bad if either
0030 %   of following three conditions is satisfied.
0031 %   1. maximum ampulitude exeeds amp_thres.  Note that this is absolute value threshold.
0032 %     This value must be adjusted accroding to measurement dynamic range (EEG or MEG).
0033 %   2. maximum amplitude relative to standard deviation (within sessions or
0034 %   all session average) exceed thres_val(1)
0035 %   3. maximum 1st differential siganal relative to standard deviation
0036 %   (within sessions or all session average) exceed thres_val(2)
0037 %
0038 % 2014/09/14 Okito Yamashita
0039 % 2009/02/01 Masa-aki Sato
0040 %
0041 % Copyright (C) 2011, ATR All Rights Reserved.
0042 % License : New BSD License(see VBMEG_LICENSE.txt)
0043 
0044 if isempty(datafile) | nargin < 2
0045  eval(['help vb_job_reject_channels_trials_gui']);
0046  error('see help comments above !');
0047 end
0048 
0049 if ~isempty(datafile) && iscell(datafile),
0050     error('datafile is a string specifying one data file ! \n');
0051 end;
0052 
0053 if nargin < 6 | isempty(rej_criterion) 
0054     rej_criterion = 0;
0055 end
0056 if nargin < 5 | isempty(per_tr)
0057     per_tr = 0.05;
0058 end
0059 if nargin < 4 | isempty(per_ch)
0060     per_ch = 0.05;
0061 end
0062 
0063 if nargin < 3 | isempty(thred_val)
0064     thred_val = [9 8];
0065 end
0066 
0067 
0068 fprintf('----------------------------------------------------------\n');
0069 fprintf(' Trial and channel rejections (ver.1) \n');
0070 fprintf('----------------------------------------------------------\n');
0071 fprintf('  Absolute amplitude threshold : %e \n', amp_thres);
0072 fprintf('  Relative amplitude threshold : %f \n', thred_val(1));
0073 if rej_criterion == 1, fprintf('  Relative 1st differential signal threshold : %f \n', thred_val(2)); end
0074 fprintf('  Percent threshold for channels : %f \n', per_ch);
0075 fprintf('  Percent threshold for trials   : %f \n', per_tr);
0076 
0077 
0078 % ystd: amplitude mean calculated by histrgam for all trials in one channel
0079 % ymax : Nch x Ntrial
0080 % ystd : Nch x 1
0081     
0082 load(datafile, 'fileinfo'); 
0083 if ~exist('fileinfo', 'var')
0084     error('datafile is not ''fileinfo'' format.');
0085 end
0086 
0087 % for compatibility
0088 if isfield(fileinfo, 'channel_id')
0089     data_format = 'old';
0090 elseif isfield(fileinfo, 'ActiveChannel')
0091     data_format = 'new';
0092 end
0093 if strcmp(data_format, 'new')
0094     fileinfo = vb_fileinfo_active_field_convert_to('old', fileinfo, datafile);
0095 end
0096 
0097 ixchact = fileinfo.channel_id;
0098 ixtract = fileinfo.act_trial;
0099     
0100 if isfield(fileinfo,'ymax1')
0101     fprintf('  Load data  \n')
0102     ymax1 = fileinfo.ymax1;
0103     ymax2 = fileinfo.ymax2;
0104     ystd1 = fileinfo.ystd1;
0105     ystd2 = fileinfo.ystd2;
0106     
0107     data = vb_load_meg_data(datafile);
0108         
0109 else
0110     fprintf('    Load data and compute statistics \n')
0111     data = vb_load_meg_data(datafile);
0112     [ymax1,ymax2,ystd1,ystd2] = vb_channel_statics(data);
0113 
0114     fprintf('\n')
0115 end
0116 
0117 %----------------------------
0118 % standardization
0119 %----------------------------
0120 
0121 ratio1 = ymax1;
0122 ratio2 = ymax2;
0123 
0124 ix1  = find(ystd1 > 0);
0125 ix2  = find(ystd2 > 0);
0126 
0127 if size(ystd1,2) == 1
0128     ratio1(ix1,:) = vb_repmultiply(ymax1(ix1,:), 1./ystd1(ix1));
0129     ratio2(ix2,:) = vb_repmultiply(ymax2(ix2,:), 1./ystd2(ix2));
0130 else
0131     ratio1(ix1) = ymax1(ix1)./ystd1(ix1);
0132     ratio2(ix2) = ymax2(ix2)./ystd2(ix2);
0133 end
0134 
0135 %
0136 [Nch,T,Ntry] = size(data);
0137 fprintf('#Channel = %d, #Time Points = %d, #Trials = %d \n',Nch,T,Ntry)
0138 
0139 [Nch,Ntry] = size(ymax1);
0140 
0141 %---------------------------------------------------------
0142 % detect rejected channels/trials and visual inspection
0143 %---------------------------------------------------------
0144 
0145 
0146 fprintf('Channel rejection, Then Trial rejection \n')
0147 
0148 % Find Bad Channel with large max_ratio
0149 flg1 = ymax1 > amp_thres;
0150 flg2 = ratio1 > thred_val(1);
0151 flg3 = ratio2 > thred_val(2);
0152 if rej_criterion == 0
0153     flg = flg1+flg2;
0154 else
0155     flg = flg1+flg2+flg3;
0156 end
0157 flg = flg > 0;
0158 figure, imagesc(flg), colormap('hot'); xlabel('trial indicies'); ylabel('channel indices');
0159 fprintf('press any keys \n');
0160 pause
0161 
0162 
0163 %----------------------------
0164 % visual confirmation
0165 %----------------------------
0166 ch_bad = find( sum(flg, 2) > per_ch*size(flg,2));
0167 
0168 % Check Bad Channels
0169 if ~isempty(ch_bad)
0170 ch_bad = vb_plot_bad_channels2(data,ch_bad,flg,'ylimits', [-amp_thres amp_thres],'label',ixchact);
0171 end
0172 ch_act  = vb_setdiff2([1:Nch],ch_bad);
0173 fprintf('   rejected channels %d \n', ixchact(ch_bad));
0174 fprintf('\n')
0175 
0176 % Find bad trials with large max_ratio after removing bad channel
0177 flg_tmp = flg(ch_act,:);
0178 try_bad = find( sum(flg_tmp, 1) > floor(per_tr*Nch));
0179 
0180 % Check Bad trials
0181 if ~isempty(try_bad)
0182 try_bad = vb_plot_bad_trials2(data(ch_act,:,:),try_bad,flg(ch_act,:), 'ylimits', [-amp_thres amp_thres],'label',ixtract);
0183 end
0184 try_act = vb_setdiff2([1:Ntry],try_bad);
0185 fprintf('   rejected trials %d \n', ixtract(try_bad));
0186 fprintf('\n')
0187 
0188 %-------------------------
0189 %  Save Results
0190 %-------------------------
0191 
0192 Ntry = length(try_act);
0193 Nact = length(ch_act);
0194 
0195 
0196 % channel & trial statistics
0197 fileinfo.ymax1 = ymax1;
0198 fileinfo.ymax2 = ymax2;
0199 fileinfo.ystd1 = ystd1;
0200 fileinfo.ystd2 = ystd2;
0201 
0202 fileinfo.channel_id = fileinfo.channel_id(ch_act);
0203 fileinfo.act_trial  = fileinfo.act_trial(try_act);
0204 fileinfo = vb_fileinfo_active_field_convert_to(data_format, fileinfo, datafile);
0205 
0206 %% ---- END ---- %%

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005