0001 function [fileinfo,flg,fig_handler] = vb_job_bad_trial(datafile,job_mode, ...
0002 thred_val,prob_val,thred_ch,savefile,stat_file,std_mode)
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
0045
0046
0047
0048
0049
0050
0051 if ~exist('std_mode','var') , std_mode = 1; end;
0052
0053 if ~isempty(datafile) && ~iscell(datafile),
0054 tmp = datafile;
0055 datafile = {tmp};
0056 end;
0057
0058 if exist('stat_file','var') && exist([stat_file '.info.mat'],'file')
0059
0060 load([stat_file '.info.mat'],'fileinfo')
0061 else
0062
0063
0064 if length(datafile)==1&&strcmp(datafile{1}(end-8:end), '.info.mat')
0065
0066 info = load(datafile{1});
0067 datafile = info.fileinfo.filename;
0068 end
0069 fileinfo = vb_get_multi_fileinfo(datafile);
0070 end
0071
0072
0073 if isfield(fileinfo, 'channel_id')
0074 data_format = 'old';
0075 elseif isfield(fileinfo, 'ActiveChannel')
0076 data_format = 'new';
0077 end
0078 if strcmp(data_format, 'new')
0079 fileinfo = vb_fileinfo_active_field_convert_to('old', fileinfo, datafile{1});
0080 end
0081
0082
0083
0084
0085
0086 if isfield(fileinfo,'ymax1')
0087 ymax1 = fileinfo.ymax1;
0088 ymax2 = fileinfo.ymax2;
0089 ystd1 = fileinfo.ystd1;
0090 ystd2 = fileinfo.ystd2;
0091 if ~isempty(datafile)
0092 fileinfo = vb_get_multi_fileinfo(datafile);
0093 end
0094 else
0095 ymax1 = [];
0096 ymax2 = [];
0097 ystd1 = [];
0098 ystd2 = [];
0099 fprintf('Load data ')
0100 for n=1:length(datafile)
0101
0102 fprintf('-')
0103 data = vb_load_meg_data(datafile{n});
0104 [max1,max2,std1,std2] = vb_channel_statics(data);
0105 ymax1 = [ymax1 , max1];
0106 ymax2 = [ymax2 , max2];
0107 ystd1 = [ystd1 , std1];
0108 ystd2 = [ystd2 , std2];
0109 end
0110 fprintf('\n')
0111 end
0112
0113
0114 if job_mode==3||job_mode==4||job_mode==5||job_mode==6
0115 Nchannel = fileinfo.Nchannel;
0116 Nsample = fileinfo.Nsample ;
0117 Ntrial = fileinfo.Ntrial ;
0118 Ntotal = fileinfo.Ntotal ;
0119
0120 tstep = 1;
0121 t = 1:tstep:Nsample;
0122 data_plt = zeros(Nchannel,length(t),Ntotal);
0123 jtry = 1;
0124 fprintf('Load data ')
0125 for n=1:length(datafile)
0126
0127 fprintf('-')
0128 data_tmp = vb_load_meg_data(datafile{n});
0129 data_plt(:,:,jtry:jtry+Ntrial(n)-1) = data_tmp(:,t,:);
0130 jtry = jtry+Ntrial(n);
0131 end
0132 [Nch,T,Ntry] = size(data_plt);
0133 fprintf('[Nch,T,Ntry] = [%d, %d, %d] \n',Nch,T,Ntry)
0134 end
0135
0136 if std_mode == 0
0137 ystd1 = mean(ystd1,2);
0138 ystd2 = mean(ystd2,2);
0139 end
0140
0141 ratio1 = ymax1;
0142 ratio2 = ymax2;
0143
0144 ix1 = find(ystd1 > 0);
0145 ix2 = find(ystd2 > 0);
0146
0147 if size(ystd1,2) == 1
0148 ratio1(ix1,:) = vb_repmultiply(ymax1(ix1,:), 1./ystd1(ix1));
0149 ratio2(ix2,:) = vb_repmultiply(ymax2(ix2,:), 1./ystd2(ix2));
0150 else
0151 ratio1(ix1) = ymax1(ix1)./ystd1(ix1);
0152 ratio2(ix2) = ymax2(ix2)./ystd2(ix2);
0153 end
0154
0155 [Nch,Ntry] = size(ymax1);
0156 thred_num = Nch * thred_ch;
0157
0158 switch job_mode
0159 case 1
0160
0161
0162
0163 ch_act = [1:Nch];
0164 [try_err ,flg]= vb_find_bad_trial(ratio1,ratio2,ch_act,thred_val,thred_num);
0165 try_act = vb_setdiff2([1:Ntry],try_err);
0166
0167
0168 [ch_bad,flg] = vb_find_bad_channel(ratio1(:,try_act),ratio2(:,try_act), ...
0169 thred_val,prob_val);
0170 ch_act = vb_setdiff2([1:Nch],ch_bad);
0171
0172
0173 try_bad = vb_find_bad_trial(ratio1,ratio2,ch_act,thred_val);
0174 try_act = vb_setdiff2([1:Ntry],try_bad);
0175 case 2
0176
0177 vb_plot_max_ratio_image(ratio1,thred_val(1))
0178 vb_plot_max_ratio_image(ratio2,thred_val(2))
0179
0180 ch_act = 1:Nch;
0181 try_act = 1:Ntry;
0182
0183
0184 case 3
0185
0186
0187
0188 ch_act = [1:Nch];
0189 [try_err ,flg]= vb_find_bad_trial(ratio1,ratio2,ch_act,thred_val,thred_num);
0190 try_act = vb_setdiff2([1:Ntry],try_err);
0191
0192
0193 ch_bad = vb_find_bad_channel(ratio1(:,try_act),ratio2(:,try_act),...
0194 thred_val,prob_val);
0195
0196
0197 ch_bad = vb_plot_bad_channel(data_plt,ch_bad,flg);
0198
0199
0200 ch_act = vb_setdiff2([1:Nch],ch_bad);
0201 try_bad = vb_find_bad_trial(ratio1,ratio2,ch_act,thred_val);
0202 try_bad = vb_plot_bad_trial(data_plt(ch_act,:,:),try_bad,flg(ch_act,:));
0203 try_act = vb_setdiff2([1:Ntry],try_bad);
0204 case 4
0205
0206
0207 ch_act = [1:Nch];
0208 [try_err ,flg]= vb_find_bad_trial(ratio1,ratio2,ch_act,thred_val,thred_num);
0209
0210
0211 try_bad = vb_plot_bad_trial(data_plt(ch_act,:,:),try_err,flg(ch_act,:));
0212 try_act = vb_setdiff2([1:Ntry],try_bad);
0213
0214
0215 ch_bad = vb_find_bad_channel(ratio1(:,try_act),ratio2(:,try_act),...
0216 thred_val,prob_val);
0217
0218 ch_act = vb_setdiff2([1:Nch],ch_bad);
0219 case 5
0220
0221
0222 ch_act = [1:Nch];
0223 [try_err ,flg]= vb_find_bad_trial(ratio1,ratio2,ch_act,thred_val,thred_num);
0224 try_act = vb_setdiff2([1:Ntry],try_err);
0225
0226
0227 ch_bad = vb_find_bad_channel(ratio1(:,try_act),ratio2(:,try_act),...
0228 thred_val,prob_val);
0229 ch_act = vb_setdiff2([1:Nch],ch_bad);
0230
0231
0232 try_bad = vb_find_bad_trial(ratio1,ratio2,ch_act,thred_val);
0233 try_bad = vb_plot_bad_trial(data_plt(ch_act,:,:),try_bad,flg(ch_act,:));
0234 try_act = vb_setdiff2([1:Ntry],try_bad);
0235 case 6
0236
0237
0238 [ch_bad,flg] = vb_find_bad_channel(ratio1,ratio2,thred_val,prob_val);
0239
0240
0241 ch_act = vb_setdiff2([1:Nch],ch_bad);
0242 try_bad = vb_find_bad_trial(ratio1,ratio2,ch_act,thred_val);
0243
0244
0245 try_bad = vb_plot_bad_trial(data_plt(ch_act,:,:),try_bad,flg(ch_act,:));
0246 try_act = vb_setdiff2([1:Ntry],try_bad);
0247 try_err = [];
0248
0249
0250 case 7
0251
0252 fig_handler(1)=figure;
0253 subplot 121
0254 [ylist1,h1,y1] = inner_plot_stat_hist(ratio1 ,[]);
0255 hold on
0256 title('Max/std of amplitude')
0257 subplot 122
0258 [ylist2,h2,y2] = inner_plot_stat_hist(ratio2 ,[]);
0259 hold on
0260 title('Max/std of derivative')
0261
0262
0263
0264 ch_act = 1:Nch;
0265 try_act = 1:Ntry;
0266 try_err = [];
0267 flg = [];
0268 case 8
0269 if size(thred_val,1)==2
0270 thred_val_lst1 = thred_val(1,:);
0271 thred_val_lst2 = thred_val(2,:);
0272 else
0273 thred_val_lst1 = thred_val(1);
0274 thred_val_lst2 = thred_val(2);
0275 end
0276
0277 fig_handler(1)=figure;
0278 subplot 121
0279 [ylist1,h1,y1] = inner_plot_stat_hist(ratio1 ,[]);
0280 hold on
0281 title('Max/std of amplitude')
0282 subplot 122
0283 [ylist2,h2,y2] = inner_plot_stat_hist(ratio2 ,[]);
0284 hold on
0285 title('Max/std of derivative')
0286
0287
0288
0289 ch_act = 1:Nch;
0290 try_act = 1:Ntry;
0291 try_err = [];
0292 flg = [];
0293
0294 vb_plot_max_ratio_image(ratio1,thred_val_lst1)
0295 fig_handler(2)=gcf;
0296 vb_plot_max_ratio_image(ratio2,thred_val_lst2)
0297 fig_handler(3)=gcf;
0298 vb_plot_channel_trial(ratio1,ratio2, ...
0299 thred_val_lst1,thred_val_lst2,prob_val,thred_num)
0300 fig_handler(4)=gcf;
0301
0302 case 10
0303 ch_act = 1:Nch;
0304 try_act = 1:Ntry;
0305 try_err = [];
0306 flg = [];
0307 end
0308
0309 Ntry = length(try_act);
0310 Nact = length(ch_act);
0311
0312 if job_mode==1 || job_mode==2 || job_mode==3 || job_mode==4 || job_mode==5 || job_mode==6
0313 fprintf('Active %d channel & %d epoch\n',Nact, Ntry)
0314 fprintf('Err trial= %d\n',length(try_err));
0315 end
0316
0317
0318 fileinfo.ymax1 = ymax1;
0319 fileinfo.ymax2 = ymax2;
0320 fileinfo.ystd1 = ystd1;
0321 fileinfo.ystd2 = ystd2;
0322
0323 fileinfo.channel_id = ch_act;
0324 fileinfo.act_trial = try_act;
0325 fileinfo = vb_fileinfo_active_field_convert_to(data_format, fileinfo, datafile{1});
0326
0327 if ~exist('savefile','var')||isempty(savefile), return;end
0328
0329 vb_save_active_info(fileinfo, [savefile '.info.mat']);
0330
0331 return
0332 end
0333
0334
0335
0336 function [ylist,h,yy] = inner_plot_stat_hist(y,plist)
0337
0338
0339
0340
0341 [Nch, Ntry] = size(y);
0342 Nbin = 500;
0343 ymax = max(y(:));
0344
0345 [h, yy] =hist(y(:),Nbin);
0346
0347 [ylist, p] = vb_find_hist_threshold(h,yy,plist);
0348
0349
0350 dy = yy(2) - yy(1);
0351 pd = p / dy;
0352
0353 hold on
0354 type = {'r--', 'b--', 'm--', 'c--'};
0355
0356 N = length(ylist);
0357 lgn = cell(N-1,1);
0358
0359 for n=1:N
0360 plot([ylist(n) ylist(n)],[0 max(pd)], type{n})
0361 lgn{n} = ['p=' num2str(plist(n))];
0362 end
0363
0364 xlim([0 ymax])
0365
0366
0367 plot(yy,pd);
0368 end