0001 function crw2set_cont(crwfile,logfile,blfORbdf_file,setfile,varargin)
0002
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
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062 global VERBLEVEL
0063 global SUBJECT
0064
0065
0066 global EEG
0067 global ALLEEG
0068
0069
0070 p=inputParser;
0071
0072
0073
0074
0075
0076 p.addRequired('crwfile',@ischar);
0077 p.addRequired('logfile',@ischar);
0078 p.addRequired('blfORbdf_file',@ischar);
0079 p.addRequired('setfile',@ischar);
0080 p.addParamValue('locfile',[],@ischar);
0081 p.addParamValue('presam',100,@(x) x>0);
0082 p.addParamValue('cprecis',1,@(x) x>0 & mod(x,1)==0);
0083 p.addParamValue('decimat',1,@(x) x>0 & mod(x,1)==0);
0084 p.addParamValue('verblevel',2,@(x) x>=0);
0085 p.addParamValue('rtfile',[],@ischar);
0086 p.addParamValue('event_list_file',[],@ischar);
0087 p.addParamValue('limits',[0 0],@(x) isnumeric(x) && length(x)==2);
0088 p.addParamValue('limits_type','msec',@(x) strcmpi(x,'msec') || strcmpi(x,'event'));
0089 p.addParamValue('padding',0,@(x) isnumeric(x) && length(x)==1 && x>=0);
0090 p.addParamValue('event_infofile',[],@(x) ischar(x) | iscell(x));
0091 p.addParamValue('code_infofile',[],@(x) ischar(x) | iscell(x));
0092 p.addParamValue('sbjct_infofile',[],@(x) ischar(x) | iscell(x));
0093 p.addParamValue('sbjct_id',[],@(x) ischar(x) | isnumeric(x));
0094 p.addParamValue('cal_plot_opt',1,@(x) x==0 | x==1);
0095 p.addParamValue('cal_plot_avg',0,@(x) x==0 | x==1);
0096 p.addParamValue('cal_npts',10,@(x) x>0 & mod(x,1)==0);
0097 p.addParamValue('block_plot_opt',1,@(x) x==0 | x==1);
0098
0099 p.addParamValue('low_cursor',-50,@(x) x<0);
0100
0101 p.addParamValue('hi_cursor',50,@(x) x>0);
0102
0103 p.addParamValue('cal_amp',10,@isnumeric);
0104
0105 p.addParamValue('cal_polarity',1,@(x) x==1 | x==-1);
0106 p.addParamValue('trm_pcnt',5,@(x) x>=0 & x<100);
0107 p.addParamValue('garv_flags','ignore arf',@(x) iscell(x) | ischar(x));
0108 p.addParamValue('reref_bimast','no',@(x) strcmpi('yes',x) | strcmpi('no',x) );
0109 p.addParamValue('rm_blocking','no',@(x) strcmpi('yes',x) | strcmpi('no',x) );
0110 p.addParamValue('reref_ignore',[],@(x) ischar(x) | iscell(x));
0111 p.addParamValue('exclude_chans',[],@(x) ischar(x) | iscell(x));
0112 p.addParamValue('include_chans',[],@(x) ischar(x) | iscell(x));
0113
0114 p.addParamValue('rm_bsln_mn',[],@(x) isnumeric(x) & (length(x)==2));
0115 p.addParamValue('cal_crwfile',[],@ischar);
0116 p.addParamValue('cal_logfile',[],@ischar);
0117 p.addParamValue('cal_blfORbdf_file',[],@ischar);
0118
0119 p.parse(crwfile,logfile,blfORbdf_file,setfile,varargin{:});
0120
0121 VERBLEVEL=p.Results.verblevel;
0122
0123
0124 if VERBLEVEL>0
0125 fprintf('crw2set argument values:\n');
0126 disp(p.Results);
0127 end
0128
0129 if iscell(p.Results.garv_flags)
0130 garv_flags=p.Results.garv_flags{1};
0131 for a=2:length(p.Results.garv_flags),
0132 garv_flags=[garv_flags ' ' p.Results.garv_flags{a}];
0133 end
0134 else
0135 garv_flags=p.Results.garv_flags;
0136 end
0137
0138
0139
0140
0141
0142 [cont_data, eventcodes, timestamps]=erpio2('get_cdata',crwfile,p.Results.limits_type, ...
0143 p.Results.limits(1),p.Results.limits(2),p.Results.padding);
0144
0145 if isempty(cont_data),
0146 error(erpio2('get_errstr'));
0147 end
0148 [n_chan, n_pnt]=size(cont_data);
0149
0150
0151 srate=erpio2('get_srate',crwfile);
0152 sub_codename=erpio2('get_subdes',crwfile);
0153
0154
0155
0156
0157
0158
0159
0160 if isempty(p.Results.exclude_chans),
0161 exclude_chans=[];
0162 elseif iscell(p.Results.exclude_chans),
0163 exclude_chans=p.Results.exclude_chans;
0164 else
0165 exclude_chans{1}=p.Results.exclude_chans;
0166 end
0167 if isempty(p.Results.include_chans),
0168 include_chans=[];
0169 elseif iscell(p.Results.include_chans),
0170 include_chans=p.Results.include_chans;
0171 else
0172 include_chans{1}=p.Results.include_chans;
0173 end
0174 if isempty(p.Results.locfile)
0175 use_chanlocs=chanlocs_from_crw_erpio2(crwfile,exclude_chans,include_chans);
0176 else
0177 use_chanlocs=p.Results.locfile;
0178 end
0179
0180
0181
0182
0183
0184 [blf_info, bin_LongDesc, blffile, blf_evnum, rtfile, cal_bin, cond_LongDesc, bin_CondCode]= ...
0185 import_blf(blfORbdf_file,logfile,srate,p.Results.rtfile);
0186 num_bins=size(blf_info,2);
0187
0188
0189
0190
0191
0192
0193
0194
0195 VerbReport(' ',1,VERBLEVEL);
0196
0197
0198
0199 VerbReport(sprintf('Creating temporary ascii version of logfile %s',logfile),1,VERBLEVEL);
0200
0201 asci_cmnd='rm -f temp.asci';
0202 unix(asci_cmnd);
0203 asci_cmnd=sprintf('log2asci %s temp.asci -d %d',logfile,srate);
0204 [s, w]=unix(asci_cmnd);
0205 if s~=0,
0206 disp('Could not create ascii version of logfile using log2asci.');
0207 error('According to log2asci: %s',w);
0208 end
0209 asciilogfile='temp.asci';
0210 tempascii=1;
0211 VerbReport(sprintf('Getting log information from %s', asciilogfile), 1, VERBLEVEL);
0212
0213
0214 ep_info=import_entire_log(asciilogfile,blf_evnum);
0215
0216
0217 if tempascii,
0218 VerbReport(sprintf('Removing temporary ascii version of logfile.'),1,VERBLEVEL);
0219 asci_cmnd='rm -f temp.asci';
0220 [s, w]=unix(asci_cmnd);
0221 if s~=0,
0222 disp('Could not remove ascii version of logfile: temp.asci.');
0223 error('According to rm: %s',w);
0224 end
0225 end
0226
0227
0228 log_LongDesc = {'Log Item Number', 'Event Code', 'Condition Code', 'Log Flag (40=nonblink art; 60=blink art)','TIME'};
0229
0230
0231
0232
0233
0234
0235 if isempty(p.Results.cal_crwfile)
0236
0237
0238
0239 [scale_fact, used_cal_ids]=cont_data_calibrate(cont_data,timestamps,ep_info,blf_evnum,srate,p.Results);
0240 cont_data=cont_data.*repmat(scale_fact,1,n_pnt);
0241 else
0242 [scale_fact, used_cal_ids]=cont_data_calibrate_alt_crw(p,use_chanlocs,VERBLEVEL);
0243 cont_data=cont_data.*repmat(scale_fact,1,n_pnt);
0244 end
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271 EEG = pop_importdata('dataformat','array','data',cont_data, ...
0272 'setname',setfile,'srate',srate,'subject',sub_codename, ...
0273 'chanlocs',use_chanlocs,'pnts',n_pnt,'nbchan',n_chan,'xmin',0);
0274 clear cont_data;
0275
0276
0277
0278
0279 EEG = eeg_checkset( EEG );
0280
0281
0282
0283
0284
0285
0286
0287
0288 if isempty(p.Results.event_list_file),
0289
0290 VerbReport(sprintf('Creating temporary textfile of event information (e.g., the latency and event code of each event): temp_event_info.txt'),1,VERBLEVEL);
0291 [fid, fopen_msg]=fopen('temp_event_info.txt','w');
0292 if fid==-1,
0293 disp('Could not create temporary event information file.');
0294 error('According to fopen: %s',fopen_msg);
0295 end
0296
0297
0298
0299 fprintf(fid,'latency type conditioncode\n');
0300 event_ids=find(eventcodes~=0);
0301 for a=event_ids,
0302 ep_info_id=find(ep_info(:,5)==timestamps(a)/1000);
0303 if isempty(ep_info_id),
0304 error('Can''t find condition code for Event #%d, Time %g ms.\n', ...
0305 eventcodes(a),timestamps(a));
0306 end
0307 ccode=ep_info(ep_info_id,3);
0308 fprintf(fid,'%g %d %d\n',timestamps(a),eventcodes(a),ccode);
0309 end
0310 fclose(fid);
0311
0312
0313 EEG = pop_importevent(EEG, 'event', ...
0314 'temp_event_info.txt', 'fields', {'latency','type','conditioncode'}, ...
0315 'skipline',1,'timeunit',0.001 );
0316
0317
0318 VerbReport(sprintf('Removing temporary textfile of event information: temp_event_info.txt'),1,VERBLEVEL);
0319 asci_cmnd='rm -f temp_event_info.txt';
0320 [s, unix_msg]=unix(asci_cmnd);
0321 if s~=0,
0322 disp('Could not remove temporary textfile of event information: temp_event_info.txt');
0323 error('According to rm: %s',unix_msg);
0324 end
0325 else
0326
0327
0328 EEG = pop_importevent(EEG, 'event', ...
0329 p.Results.event_list_file, 'fields', {'latency','type'}, ...
0330 'skipline',1,'timeunit',0.001 );
0331 end
0332 n_EEG_ev=length(EEG.event);
0333
0334
0335 n_blf=length(blf_evnum);
0336 for a=1:n_blf,
0337
0338 EEGevent_id=blf_evnum(a)+1;
0339
0340
0341 EEG.event(EEGevent_id).bins=find(blf_info(a,:))-1;
0342 end
0343
0344
0345
0346
0347
0348 boundary_ct=0;
0349 for a=1:n_EEG_ev,
0350 if strcmpi(EEG.event(a).type,'-16384')
0351 EEG.event(a).type='boundary';
0352 boundary_ct=boundary_ct+1;
0353 end
0354 end
0355 VerbReport(sprintf('Number of boundaries (e.g., pauses of dig machine) detected in continuous data file: %d\n',boundary_ct), ...
0356 2,VERBLEVEL);
0357
0358
0359 bin_ct=zeros(1,num_bins);
0360 bin_descriptors=cell(1,num_bins);
0361 if VERBLEVEL>1,
0362 for a=1:n_EEG_ev,
0363 bin_nums=EEG.event(a).bins;
0364 if ~isempty(bin_nums)
0365 bin_nums=bin_nums+1;
0366 bin_ct(bin_nums)=bin_ct(bin_nums)+1;
0367 end
0368 end
0369 fprintf('BIN\tLOADED TRIALS\tBIN DESCRIPTOR\n');
0370
0371
0372
0373 non_cal_bin_ct=0;
0374 for binloop=1:num_bins,
0375 if (binloop-1)==cal_bin,
0376 bin_descriptors{binloop}='Cal Pulse';
0377 else
0378 non_cal_bin_ct=non_cal_bin_ct+1;
0379 bin_descriptors{binloop}=bin_LongDesc{non_cal_bin_ct};
0380 end
0381
0382 if binloop<10
0383 fprintf('Bin %d: \t%d\t%s\n',binloop-1,bin_ct(binloop),bin_descriptors{binloop});
0384 elseif binloop<100
0385 fprintf('Bin %d: \t%d\t%s\n',binloop-1,bin_ct(binloop),bin_descriptors{binloop});
0386 else
0387 fprintf('Bin %d:\t%d\t%s\n',binloop-1,bin_ct(binloop),bin_descriptors{binloop});
0388 end
0389 end
0390 end
0391
0392
0393
0394
0395
0396 if ~isempty(p.Results.code_infofile)
0397 VerbReport(' ',1, VERBLEVEL);
0398 if iscell(p.Results.code_infofile)
0399 n_file=length(p.Results.code_infofile);
0400 for dd=1:n_file,
0401 [new_info, info_names]=add_code_info(p.Results.code_infofile{dd},ep_info(:,2));
0402 n_fields=length(info_names);
0403 for a=1:n_EEG_ev,
0404 for b=1:n_fields
0405 cmnd=['EEG.event(a).' info_names{b} '=' num2str(new_info(a)) ';'];
0406 eval(cmnd);
0407 end
0408 end
0409 end
0410 else
0411 [new_info, info_names]=add_code_info(p.Results.code_infofile,ep_info(:,2));
0412 n_fields=length(info_names);
0413 for a=1:n_EEG_ev,
0414 for b=1:n_fields
0415 cmnd=['EEG.event(a).' info_names{b} '=' num2str(new_info(a)) ';'];
0416 eval(cmnd);
0417 end
0418 end
0419 end
0420 end
0421 clear n_file new_info info_names
0422
0423
0424
0425
0426
0427
0428 if ~isempty(p.Results.event_infofile)
0429 VerbReport(' ',1, VERBLEVEL);
0430 if iscell(p.Results.event_infofile)
0431 n_file=length(p.Results.event_infofile);
0432 for dd=1:n_file,
0433 [new_info, info_names]=add_ev_info(p.Results.event_infofile{dd},blf_evnum);
0434 ep_info=[ep_info new_info];
0435 for dg=1:length(info_names),
0436 EventTableColNames{length(EventTableColNames)+1}= ...
0437 info_names{dg};
0438 log_LongDesc{length(log_LongDesc)+1}=info_names{dg};
0439 end
0440 end
0441 else
0442 [new_info, info_names]=add_ev_info(p.Results.event_infofile, ...
0443 blf_evnum);
0444 ep_info=[ep_info new_info];
0445 for dg=1:length(info_names),
0446 EventTableColNames{length(EventTableColNames)+1}= ...
0447 info_names{dg};
0448 log_LongDesc{length(log_LongDesc)+1}=info_names{dg};
0449 end
0450 end
0451 end
0452 clear n_file new_info info_names
0453
0454
0455
0456
0457
0458
0459 rtvector=[];
0460 rtmsec=[];
0461 if ~isempty(rtfile),
0462 non_calpulse_eps=find(ep_info(:,3)>0);
0463 [rtmsec, rtbins, rtvector]=import_rts(rtfile,blf_evnum(non_calpulse_eps));
0464 if ~isempty(rtvector)
0465
0466 ep_info=[ep_info zeros(size(ep_info,1),1)];
0467 ep_info(non_calpulse_eps,end)=rtvector;
0468
0469 EventTableColNames{length(EventTableColNames)+1} = 'rtmsec';
0470 rt_LongDesc = {'Reaction Time in Milliseconds'};
0471 end
0472 end
0473
0474
0475
0476
0477
0478 EEG = pop_saveset( EEG, 'savemode','resave');
0479 EEG = eeg_checkset( EEG );
0480
0481
0482
0483
0484
0485
0486
0487 function [scale_fact, used_cal_ids]=cont_data_calibrate_alt_crw(p,eeg_chanlocs,VERBLEVEL)
0488
0489
0490
0491
0492
0493
0494 [cont_data, eventcodes, timestamps]=erpio2('get_cdata',p.Results.cal_crwfile,'msec',0,0,0);
0495
0496 if isempty(cont_data),
0497 error(erpio2('get_errstr'));
0498 end
0499 [n_chan, n_pnt]=size(cont_data);
0500
0501
0502 srate=erpio2('get_srate',p.Results.cal_crwfile);
0503
0504
0505
0506
0507
0508
0509
0510 if isempty(p.Results.exclude_chans),
0511 exclude_chans=[];
0512 elseif iscell(p.Results.exclude_chans),
0513 exclude_chans=p.Results.exclude_chans;
0514 else
0515 exclude_chans{1}=p.Results.exclude_chans;
0516 end
0517 if isempty(p.Results.include_chans),
0518 include_chans=[];
0519 elseif iscell(p.Results.include_chans),
0520 include_chans=p.Results.include_chans;
0521 else
0522 include_chans{1}=p.Results.include_chans;
0523 end
0524 if isempty(p.Results.locfile)
0525 use_chanlocs=chanlocs_from_crw_erpio2(p.Results.cal_crwfile,exclude_chans,include_chans);
0526 else
0527 use_chanlocs=p.Results.locfile;
0528 end
0529
0530
0531
0532 n_use_chan=length(use_chanlocs);
0533 if n_use_chan~=length(eeg_chanlocs),
0534 error('Cal pulse crwfile, %s, has a different number of included electrodes than EEG crwfile.',p.Results.cal_crwfile);
0535 end
0536 for a=1:n_use_chan,
0537 if ~strcmpi(use_chanlocs(a).labels,eeg_chanlocs(a).labels)
0538 error('Cal pulse crwfile, %s, has different electrode names than EEG crwfile.',p.Results.cal_crwfile);
0539 end
0540 end
0541
0542
0543
0544
0545 [blf_info, bin_LongDesc, blffile, blf_evnum, rtfile, cal_bin, cond_LongDesc, bin_CondCode]= ...
0546 import_blf(p.Results.cal_blfORbdf_file,p.Results.cal_logfile,srate,[]);
0547
0548
0549
0550
0551
0552
0553
0554 VerbReport(' ',1,VERBLEVEL);
0555
0556
0557
0558 VerbReport(sprintf('Creating temporary ascii version of logfile %s',p.Results.cal_logfile),1,VERBLEVEL);
0559
0560 asci_cmnd='rm -f temp.asci';
0561 unix(asci_cmnd);
0562 asci_cmnd=sprintf('log2asci %s temp.asci -d %d',p.Results.cal_logfile,srate);
0563 [s, w]=unix(asci_cmnd);
0564 if s~=0,
0565 disp('Could not create ascii version of logfile using log2asci.');
0566 error('According to log2asci: %s',w);
0567 end
0568 asciilogfile='temp.asci';
0569 tempascii=1;
0570 VerbReport(sprintf('Getting log information from %s', asciilogfile), 1, VERBLEVEL);
0571
0572
0573 ep_info=import_entire_log(asciilogfile,blf_evnum);
0574
0575
0576 if tempascii,
0577 VerbReport(sprintf('Removing temporary ascii version of logfile.'),1,VERBLEVEL);
0578 asci_cmnd='rm -f temp.asci';
0579 [s, w]=unix(asci_cmnd);
0580 if s~=0,
0581 disp('Could not remove ascii version of logfile: temp.asci.');
0582 error('According to rm: %s',w);
0583 end
0584 end
0585
0586
0587
0588
0589 [scale_fact, used_cal_ids]=cont_data_calibrate(cont_data,timestamps,ep_info,blf_evnum,srate,p.Results);
0590
0591
0592