Home > functions > job > vb_job_bad_trial.m

vb_job_bad_trial

PURPOSE ^

Find bad channel/trials by checking max value in each channel/trial

SYNOPSIS ^

function [fileinfo,flg] = vb_job_bad_trial(datafile,job_mode,thred_val,prob_val,thred_ch,actfile,stat_file,std_mode)

DESCRIPTION ^

 Find bad channel/trials by checking max value in each channel/trial
 --- Usage
 vb_job_bad_trial(datafile,job_mode, ...
    thred_val,prob_val,thred_ch,actfile,stat_file,std_mode)
 --- Input
 datafile: cell string for multiple MEG/EEG epoch data files
 job_mode: 
      1: channel/trial rejection in batch mode
     10: save trial statics without channel/trial rejection
 [actfile   '.info.mat']
  : file name to save bad channel/trial info for batch mode
    or save trial statics without channel/trial rejection
 [stat_file '.info.mat']
  : file name having trial statics made at the previous job
 
 ---- Threshold value for bad channel selection
 - thred_val = [9 8]; 
   threshold value for max(y) & diff(y) in one trial
   if (ymax/ystd) > thred_val(1) | difmax/difstd > thred_val(2), 
      it is marked as bad
 - prob_val  = 0.023;
   threshold probability to select bad channel
   if more than (prob_val*Ntrial) trials are bad, the channel is rejected
 - thred_ch  = 1/4;
   if more than (Nch*thred_ch) channels are bad, the trial is rejected,
   before checking bad channel.
 - std_mode  = 1;
   = 0: std is averaged over all trials
   = 1: std is averaged within one session (one BDF file)
 
 2009-2-1 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_bad_trial(datafile,job_mode, ...
0002     thred_val,prob_val,thred_ch,actfile,stat_file,std_mode)
0003 % Find bad channel/trials by checking max value in each channel/trial
0004 % --- Usage
0005 % vb_job_bad_trial(datafile,job_mode, ...
0006 %    thred_val,prob_val,thred_ch,actfile,stat_file,std_mode)
0007 % --- Input
0008 % datafile: cell string for multiple MEG/EEG epoch data files
0009 % job_mode:
0010 %      1: channel/trial rejection in batch mode
0011 %     10: save trial statics without channel/trial rejection
0012 % [actfile   '.info.mat']
0013 %  : file name to save bad channel/trial info for batch mode
0014 %    or save trial statics without channel/trial rejection
0015 % [stat_file '.info.mat']
0016 %  : file name having trial statics made at the previous job
0017 %
0018 % ---- Threshold value for bad channel selection
0019 % - thred_val = [9 8];
0020 %   threshold value for max(y) & diff(y) in one trial
0021 %   if (ymax/ystd) > thred_val(1) | difmax/difstd > thred_val(2),
0022 %      it is marked as bad
0023 % - prob_val  = 0.023;
0024 %   threshold probability to select bad channel
0025 %   if more than (prob_val*Ntrial) trials are bad, the channel is rejected
0026 % - thred_ch  = 1/4;
0027 %   if more than (Nch*thred_ch) channels are bad, the trial is rejected,
0028 %   before checking bad channel.
0029 % - std_mode  = 1;
0030 %   = 0: std is averaged over all trials
0031 %   = 1: std is averaged within one session (one BDF file)
0032 %
0033 % 2009-2-1 Masa-aki Sato
0034 %
0035 % Copyright (C) 2011, ATR All Rights Reserved.
0036 % License : New BSD License(see VBMEG_LICENSE.txt)
0037 
0038 if ~exist('std_mode','var') , std_mode = 1; end;
0039 
0040 if ~isempty(datafile) && ~iscell(datafile),
0041     tmp = datafile;
0042     datafile = {tmp};
0043 end;
0044 
0045 if exist('stat_file','var') && exist([stat_file '.info.mat'],'file')
0046     % Load statics info (ratio. ymax)
0047     load([stat_file '.info.mat'],'fileinfo')
0048 else
0049     % Get file info for multiple session MEG/EEG files
0050     % All trials are loaded
0051     fileinfo = vb_get_multi_fileinfo(datafile);
0052 end
0053 
0054 
0055 % ystd: amplitude mean calculated by histrgam for all trials in one channel
0056 % ymax : Nch x Ntrial
0057 % ystd : Nch x 1
0058 
0059 if isfield(fileinfo,'ymax1')
0060     ymax1 = fileinfo.ymax1;
0061     ymax2 = fileinfo.ymax2;
0062     ystd1 = fileinfo.ystd1;
0063     ystd2 = fileinfo.ystd2;
0064     if ~isempty(datafile) 
0065         fileinfo = vb_get_multi_fileinfo(datafile);
0066     end
0067 else
0068     ymax1 = [];
0069     ymax2 = [];
0070     ystd1 = [];
0071     ystd2 = [];
0072     fprintf('Load data ')
0073     for n=1:length(datafile)
0074         % Load MEG/EEG data
0075         fprintf('-')
0076         data = vb_load_meg_data(datafile{n});
0077         [max1,max2,std1,std2] = vb_channel_statics(data);
0078         ymax1 = [ymax1 , max1];
0079         ymax2 = [ymax2 , max2];
0080         ystd1 = [ystd1 , std1];
0081         ystd2 = [ystd2 , std2];
0082     end
0083     fprintf('\n')
0084 end
0085 
0086 if std_mode == 0 
0087     ystd1 = mean(ystd1,2);
0088     ystd2 = mean(ystd2,2);
0089 end
0090 
0091 ratio1 = ymax1;
0092 ratio2 = ymax2;
0093 
0094 ix1  = find(ystd1 > 0);
0095 ix2  = find(ystd2 > 0);
0096 
0097 if size(ystd1,2) == 1
0098     ratio1(ix1,:) = vb_repmultiply(ymax1(ix1,:), 1./ystd1(ix1));
0099     ratio2(ix2,:) = vb_repmultiply(ymax2(ix2,:), 1./ystd2(ix2));
0100 else
0101     ratio1(ix1) = ymax1(ix1)./ystd1(ix1);
0102     ratio2(ix2) = ymax2(ix2)./ystd2(ix2);
0103 end
0104 
0105 [Nch,Ntry] = size(ymax1);
0106 thred_num  = Nch * thred_ch;
0107 
0108 switch job_mode
0109 case    1
0110     % Remove error trials first (batch mode)
0111 
0112     % 1. Find noisy trials with multiple bad channels
0113     ch_act  = [1:Nch];
0114     [try_err ,flg]= vb_find_bad_trial(ratio1,ratio2,ch_act,thred_val,thred_num);
0115     try_act = vb_setdiff2([1:Ntry],try_err);
0116 
0117     % 2. Find Bad Channel after removing noisy trials
0118     [ch_bad,flg] = vb_find_bad_channel(ratio1(:,try_act),ratio2(:,try_act), ...
0119                                     thred_val,prob_val);
0120     ch_act = vb_setdiff2([1:Nch],ch_bad);
0121     
0122     % 3. Find bad trials after removing bad channel
0123     try_bad  = vb_find_bad_trial(ratio1,ratio2,ch_act,thred_val);
0124     try_act = vb_setdiff2([1:Ntry],try_bad);
0125 case    2
0126     %  2D-Plot bad trial & channel for set of thresholds
0127     vb_plot_max_ratio_image(ratio1,thred_val(1))
0128     vb_plot_max_ratio_image(ratio2,thred_val(2))
0129     
0130     ch_act  = 1:Nch;
0131     try_act = 1:Ntry;
0132 case    10
0133     % save trial statics
0134     ch_act  = 1:Nch;
0135     try_act = 1:Ntry;
0136     try_err = [];
0137     flg = [];
0138 end
0139 
0140 Ntry = length(try_act);
0141 Nact = length(ch_act);
0142 
0143 if job_mode==1 || job_mode==2
0144     fprintf('Active %d channel & %d epoch\n',Nact, Ntry)
0145     fprintf('Err trial= %d\n',length(try_err));
0146 end
0147 
0148 % channel & trial statistics
0149 fileinfo.ymax1 = ymax1;
0150 fileinfo.ymax2 = ymax2;
0151 fileinfo.ystd1 = ystd1;
0152 fileinfo.ystd2 = ystd2;
0153 
0154 fileinfo.channel_id = ch_act;
0155 fileinfo.act_trial  = try_act;
0156 fileinfo.err_trial  = try_err;
0157 
0158 if ~exist('actfile','var')||isempty(actfile), return;end
0159 
0160 vb_save_active_info(fileinfo, [actfile '.info.mat']);
0161 
0162 return
0163 %% ---- END ---- %%

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005