0001 function [fileinfo,flg] = vb_job_reject_channels_trials_gui(datafile, amp_thres,...
0002 thred_val, per_ch,per_tr, rej_criterion)
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
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
0079
0080
0081
0082 load(datafile, 'fileinfo');
0083 if ~exist('fileinfo', 'var')
0084 error('datafile is not ''fileinfo'' format.');
0085 end
0086
0087
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
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
0143
0144
0145
0146 fprintf('Channel rejection, Then Trial rejection \n')
0147
0148
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
0165
0166 ch_bad = find( sum(flg, 2) > per_ch*size(flg,2));
0167
0168
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
0177 flg_tmp = flg(ch_act,:);
0178 try_bad = find( sum(flg_tmp, 1) > floor(per_tr*Nch));
0179
0180
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
0190
0191
0192 Ntry = length(try_act);
0193 Nact = length(ch_act);
0194
0195
0196
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