0001
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 function crw2set_contNEO(crwfile,logfile,blfORbdf_file,setfile,varargin)
0062
0063
0064 global VERBLEVEL
0065 global SUBJECT
0066
0067
0068 global EEG
0069 global ALLEEG
0070
0071
0072
0073 p=inputParser;
0074
0075
0076
0077
0078
0079 p.addRequired('crwfile',@ischar);
0080 p.addRequired('logfile',@ischar);
0081 p.addRequired('blfORbdf_file',@ischar);
0082 p.addRequired('setfile',@ischar);
0083 p.addParamValue('locfile',[],@ischar);
0084 p.addParamValue('presam',100,@(x) x>0);
0085 p.addParamValue('cprecis',1,@(x) x>0 & mod(x,1)==0);
0086 p.addParamValue('decimat',1,@(x) x>0 & mod(x,1)==0);
0087 p.addParamValue('verblevel',2,@(x) x>=0);
0088 p.addParamValue('rtfile',[],@ischar);
0089 p.addParamValue('event_list_file',[],@ischar);
0090 p.addParamValue('limits',[0 0],@(x) isnumeric(x) && length(x)==2);
0091 p.addParamValue('limits_type','msec',@(x) strcmpi(x,'msec') || strcmpi(x,'event'));
0092 p.addParamValue('padding',0,@(x) isnumeric(x) && length(x)==1 && x>=0);
0093 p.addParamValue('event_infofile',[],@(x) ischar(x) | iscell(x));
0094 p.addParamValue('code_infofile',[],@(x) ischar(x) | iscell(x));
0095 p.addParamValue('sbjct_infofile',[],@(x) ischar(x) | iscell(x));
0096 p.addParamValue('sbjct_id',[],@(x) ischar(x) | isnumeric(x));
0097 p.addParamValue('cal_plot_opt',1,@(x) x==0 | x==1);
0098 p.addParamValue('cal_plot_avg',0,@(x) x==0 | x==1);
0099 p.addParamValue('cal_npts',10,@(x) x>0 & mod(x,1)==0);
0100 p.addParamValue('block_plot_opt',1,@(x) x==0 | x==1);
0101
0102 p.addParamValue('low_cursor',-50,@(x) x<0);
0103
0104 p.addParamValue('hi_cursor',50,@(x) x>0);
0105
0106 p.addParamValue('cal_amp',10,@isnumeric);
0107
0108 p.addParamValue('cal_polarity',1,@(x) x==1 | x==-1);
0109 p.addParamValue('trm_pcnt',5,@(x) x>=0 & x<100);
0110 p.addParamValue('reref_bimast','no',@(x) strcmpi('yes',x) | strcmpi('no',x) );
0111 p.addParamValue('rm_blocking','no',@(x) strcmpi('yes',x) | strcmpi('no',x) );
0112 p.addParamValue('reref_ignore',[],@(x) ischar(x) | iscell(x));
0113 p.addParamValue('exclude_chans',[],@(x) ischar(x) | iscell(x));
0114 p.addParamValue('include_chans',[],@(x) ischar(x) | iscell(x));
0115
0116 p.addParamValue('rm_bsln_mn',[],@(x) isnumeric(x) & (length(x)==2));
0117 p.addParamValue('cal_crwfile',[],@ischar);
0118 p.addParamValue('cal_logfile',[],@ischar);
0119 p.addParamValue('cal_bdf_file',[],@ischar);
0120
0121 p.parse(crwfile,logfile,blfORbdf_file,setfile,varargin{:});
0122
0123 VERBLEVEL=p.Results.verblevel;
0124
0125
0126 if VERBLEVEL>0
0127 fprintf('crw2set argument values:\n');
0128 disp(p.Results);
0129 end
0130
0131
0132
0133
0134
0135 VerbReport(' ',1,VERBLEVEL);
0136
0137
0138
0139
0140 srate=erpio2('get_srate',crwfile);
0141 sub_codename=erpio2('get_subdes',crwfile);
0142
0143 VerbReport(sprintf('Creating temporary ascii version of logfile %s',logfile),1,VERBLEVEL);
0144
0145 asci_cmnd='rm -f temp.asci';
0146 unix(asci_cmnd);
0147 asci_cmnd=sprintf('log2asci %s temp.asci -d %d',logfile,srate);
0148 [s, w]=unix(asci_cmnd);
0149 if s~=0,
0150 disp('Could not create ascii version of logfile using log2asci.');
0151 error('According to log2asci: %s',w);
0152 end
0153 asciilogfile='temp.asci';
0154 tempascii=1;
0155 VerbReport(sprintf('Getting log information from %s', asciilogfile), 1, VERBLEVEL);
0156
0157
0158 log_info=import_entire_log(asciilogfile);
0159
0160
0161 if tempascii,
0162 VerbReport(sprintf('Removing temporary ascii version of logfile.'),1,VERBLEVEL);
0163 asci_cmnd='rm -f temp.asci';
0164 [s, w]=unix(asci_cmnd);
0165 if s~=0,
0166 disp('Could not remove ascii version of logfile: temp.asci.');
0167 error('According to rm: %s',w);
0168 end
0169 end
0170
0171
0172 log_LongDesc = {'Log Item Number', 'Event Code', 'Condition Code', 'Log Flag (40=nonblink art; 60=blink art)','TIME'};
0173
0174
0175 [cc_bounds, cc_bounds_codes]=parse_log_by_cc(log_info);
0176
0177
0178
0179
0180
0181 [blf_info, bin_LongDesc, blffile, blf_evnum, rtfile, cal_bin, cond_LongDesc, bin_CondCode]= ...
0182 import_blf(blfORbdf_file,logfile,srate,p.Results.rtfile);
0183 num_bins=size(blf_info,2);
0184
0185
0186
0187
0188
0189
0190
0191
0192 if isequal(p.Results.limits,[0 0])
0193
0194 cont_data=[];
0195 eventcodes=[];
0196 timestamps=[];
0197 used_blf_info=[];
0198 used_blf_evnum=[];
0199 n_cc_bounds=size(cc_bounds,1);
0200 for a=1:n_cc_bounds,
0201 if cc_bounds_codes(a)
0202
0203 [cont_dataTEMP, eventcodesTEMP, timestampsTEMP]=erpio2('get_cdata', ...
0204 crwfile,'event',cc_bounds(a,1)-1,cc_bounds(a,2)-1, ...
0205 p.Results.padding);
0206 if isempty(cont_dataTEMP),
0207 error(erpio2('get_errstr'));
0208 end
0209
0210 blf_ids=find( (blf_evnum>=(cc_bounds(a,1)-1)).*(blf_evnum<=(cc_bounds(a,2)-1)) );
0211
0212
0213 used_blf_info=[used_blf_info; blf_info(blf_ids,:)];
0214 used_blf_evnum=[used_blf_evnum; blf_evnum(blf_ids)];
0215 cont_data=[cont_data cont_dataTEMP];
0216 eventcodes=[eventcodes eventcodesTEMP];
0217 timestamps=[timestamps timestampsTEMP];
0218 end
0219 end
0220 else
0221 [cont_data, eventcodes, timestamps]=erpio2('get_cdata', ...
0222 crwfile,p.Results.limits_type,p.Results.limits(1),p.Results.limits(2), ...
0223 p.Results.padding);
0224 if isempty(cont_data),
0225 error(erpio2('get_errstr'));
0226 end
0227 used_blf_info=blf_info;
0228 used_blf_evnum=blf_evnum;
0229 end
0230 clear cont_dataTEMP eventcodesTEMP timestampsTEMP
0231 [n_chan, n_pnt]=size(cont_data);
0232
0233
0234
0235
0236
0237
0238
0239 if isempty(p.Results.exclude_chans),
0240 exclude_chans=[];
0241 elseif iscell(p.Results.exclude_chans),
0242 exclude_chans=p.Results.exclude_chans;
0243 else
0244 exclude_chans{1}=p.Results.exclude_chans;
0245 end
0246 if isempty(p.Results.include_chans),
0247 include_chans=[];
0248 elseif iscell(p.Results.include_chans),
0249 include_chans=p.Results.include_chans;
0250 else
0251 include_chans{1}=p.Results.include_chans;
0252 end
0253 if isempty(p.Results.locfile)
0254
0255 use_chanlocs=chanlocs_from_crw_erpio2(crwfile,exclude_chans,include_chans);
0256 else
0257 use_chanlocs=p.Results.locfile;
0258
0259 n_chans=erpio2('get_nchans',crwfile);
0260 errstring=erpio2('get_errstr');
0261 if ~isempty(errstring),
0262 error(errstring);
0263 end
0264
0265 crw_chanlabels=cell(1,n_chans);
0266 excluded_chan_ct=0;
0267 use_chans=zeros(1,n_chans);
0268 for c=1:n_chans,
0269 chan_label=erpio2('get_chandes',crwfile,c-1);
0270 errstring=erpio2('get_errstr');
0271 if ~isempty(errstring),
0272 error(errstring);
0273 end
0274 crw_chanlabels{c}=chan_label;
0275 use_chans(c)=1;
0276 if ~isempty(exclude_chans) && ismember(chan_label,exclude_chans),
0277 excluded_chan_ct=excluded_chan_ct+1;
0278 excluded_chans{excluded_chan_ct}=chan_label;
0279 use_chans(c)=0;
0280 end
0281 if ~isempty(include_chans) && ~ismember(chan_label,include_chans),
0282 use_chans(c)=0;
0283 end
0284 end
0285 use_chan_labels=crw_chanlabels(find(use_chans));
0286
0287 if ~isempty(exclude_chans)
0288
0289 missed_chans=setdiff(exclude_chans,excluded_chans);
0290 if ~isempty(missed_chans),
0291 msg=['I could not find the following channels in your crw file and thus cannot exclude them:'];
0292 for a=1:length(missed_chans),
0293 msg=[msg ' ' missed_Chans{a}];
0294 end
0295 error(msg);
0296 end
0297 elseif ~isempty(include_chans)
0298
0299 missed_chans=setdiff(include_chans,use_chan_labels);
0300 if ~isempty(missed_chans),
0301 msg=['I could not find the following channels in your crw file:'];
0302 for a=1:length(missed_chans),
0303 msg=[msg ' ' missed_Chans{a}];
0304 end
0305 error(msg);
0306 end
0307 end
0308
0309
0310 fid = fopen(use_chanlocs);
0311 locfile_stuff = textscan(fid,'%s%s%s%s');
0312 fclose(fid);
0313 locfile_channames=locfile_stuff{4};
0314
0315 if length(locfile_channames)~=length(use_chan_labels),
0316 error('Number of channels in file %s differ from the number requested from %s.', ...
0317 use_chanlocs,crwfile);
0318 end
0319
0320 for a=1:length(locfile_channames),
0321
0322
0323
0324 locfile_channames{a}=locfile_channames{a}(find(locfile_channames{a}~='.'));
0325
0326
0327
0328 if ~strcmpi(locfile_channames{a},use_chan_labels{a})
0329 error('Channel #%d in file %s is %s, but requested Channel #%d from %s is %s.', ...
0330 a,use_chanlocs,locfile_channames{a},a,crwfile,use_chan_labels{a});
0331 end
0332 end
0333 end
0334
0335
0336 cont_data=cont_data(find(use_chans),:);
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366 [setfilepath, setfilename]=pathNname(setfile);
0367 EEG = pop_importdata('dataformat','array','data',cont_data, ...
0368 'setname',setfilename(1:(length(setfilename)-4)),'srate',srate,'subject',sub_codename, ...
0369 'chanlocs',use_chanlocs,'pnts',n_pnt,'nbchan',n_chan,'xmin',0);
0370 clear cont_data;
0371
0372
0373
0374
0375 EEG = eeg_checkset( EEG );
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388 if isempty(p.Results.event_list_file),
0389
0390 VerbReport(sprintf('Creating temporary textfile of event information (e.g., the latency and event code of each event): temp_event_info.txt'),1,VERBLEVEL);
0391 [fid, fopen_msg]=fopen('temp_event_info.txt','w');
0392 if fid==-1,
0393 disp('Could not create temporary event information file.');
0394 error('According to fopen: %s',fopen_msg);
0395 end
0396
0397
0398
0399 fprintf(fid,'latency type conditioncode\n');
0400 event_ids=find(eventcodes~=0);
0401 for a=event_ids,
0402 log_info_id=find(log_info(:,5)==timestamps(a)/1000);
0403 if isempty(log_info_id),
0404 error('Can''t find condition code for Event #%d, Time %g ms.\n', ...
0405 eventcodes(a),timestamps(a));
0406 end
0407 ccode=log_info(log_info_id,3);
0408 fprintf(fid,'%d %d %d\n',timestamps(a),eventcodes(a),ccode);
0409 end
0410 fclose(fid);
0411
0412
0413 EEG = pop_importevent(EEG, 'event', ...
0414 'temp_event_info.txt', 'fields', {'latency','type','conditioncode'}, ...
0415 'skipline',1,'timeunit',0.001 );
0416
0417 if 0,
0418
0419 VerbReport(sprintf('Removing temporary textfile of event information: temp_event_info.txt'),1,VERBLEVEL);
0420 asci_cmnd='rm -f temp_event_info.txt';
0421 [s, unix_msg]=unix(asci_cmnd);
0422 if s~=0,
0423 disp('Could not remove temporary textfile of event information: temp_event_info.txt');
0424 error('According to rm: %s',unix_msg);
0425 end
0426 end
0427 else
0428
0429
0430 EEG = pop_importevent(EEG, 'event', ...
0431 p.Results.event_list_file, 'fields', {'latency','type'}, ...
0432 'skipline',1,'timeunit',0.001 );
0433 end
0434 n_EEG_ev=length(EEG.event);
0435
0436
0437
0438 EEG_ev_latencies=zeros(1,n_EEG_ev);
0439 for a=1:n_EEG_ev,
0440 EEG_ev_latencies(a)=EEG.event(a).latency;
0441 end
0442
0443 n_blf=length(used_blf_evnum);
0444 n_log_info=size(log_info,1);
0445 for a=1:n_blf,
0446
0447 log_id=used_blf_evnum(a)+1;
0448
0449 if log_id>n_log_info,
0450 error('blf file thinks there should be at least % events in the log file. However, there are only %d.\n', ...
0451 log_id,n_log_info);
0452 end
0453
0454
0455 blf_evnum_lat=round(log_info(log_id,5)*EEG.srate)+1;
0456 EEG_event_id=find(round(EEG_ev_latencies)==blf_evnum_lat);
0457 if isempty(EEG_event_id),
0458
0459 EEG_event_id=find(round(EEG_ev_latencies)==(blf_evnum_lat-1));
0460 if isempty(EEG_event_id),
0461
0462 EEG_event_id=find(round(EEG_ev_latencies)==(blf_evnum_lat+1));
0463 if isempty(EEG_event_id),
0464 error('Could not find an entry in EEG.event with the same latency as Item #%d in the blf file.', ...
0465 a);
0466 else
0467 watchit(sprintf('Could not find an exact latency match for Item #%d in the blf file, but I''m using a match that''s one time point later.', ...
0468 a));
0469 end
0470 else
0471 watchit(sprintf('Could not find an exact latency match for Item #%d in the blf file, but I''m using a match that''s one time point earlier.', ...
0472 a));
0473 end
0474 end
0475
0476
0477 EEG.event(EEG_event_id).bins=find(used_blf_info(a,:))-1;
0478 end
0479
0480
0481 bin_ct=zeros(1,num_bins);
0482 for a=1:n_EEG_ev,
0483 bins=EEG.event(a).bins;
0484 if ~isempty(bins)
0485 bin_ct(bins+1)=bin_ct(bins+1)+1;
0486 end
0487 end
0488
0489
0490 fprintf('The following number of trials were found for each bin.\n');
0491 for a=1:num_bins,
0492 if (a-1)==cal_bin,
0493 fprintf('Bin #%d (Cal Pulses): %d\n',a-1,sum(used_blf_info(:,a)));
0494 else
0495 if (a-1)>cal_bin,
0496 fprintf('Bin #%d [%s]: %d\n',a-1,bin_LongDesc{a-1},sum(used_blf_info(:,a)));
0497 else
0498 fprintf('Bin #%d [%s]: %d\n',a-1,bin_LongDesc{a},sum(used_blf_info(:,a)));
0499 end
0500 end
0501 end
0502
0503
0504
0505
0506 boundary_ct=0;
0507 for a=1:n_EEG_ev,
0508 boundary_flag=0;
0509 if ischar(EEG.event(a).type)
0510 if strcmpi(EEG.event(a).type,'-16384')
0511 boundary_flag=1;
0512 end
0513 elseif EEG.event(a).type==-16384
0514 boundary_flag=1;
0515 end
0516
0517 if boundary_flag
0518 EEG.event(a).type='boundary';
0519 boundary_ct=boundary_ct+1;
0520 end
0521 end
0522 VerbReport(sprintf('Number of boundaries (e.g., pauses of dig machine) detected in imported continuous data: %d\n',boundary_ct), ...
0523 2,VERBLEVEL);
0524
0525
0526 EEG.bindesc=bin_LongDesc;
0527 EEG.binccodes=bin_CondCode;
0528 EEG.condesc=cond_LongDesc;
0529 EEG.crw2set_params=p.Results(1);
0530
0531
0532
0533
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549 if isempty(p.Results.cal_crwfile)
0550
0551
0552
0553 [scale_fact, used_cal_ids]=cont_data_calibrate(crwfile,logfile, ...
0554 blfORbdf_file,EEG.chanlocs,p.Results);
0555 else
0556
0557 [scale_fact, used_cal_ids]=cont_data_calibrate(p.Results.cal_crwfile, ...
0558 p.Results.cal_logfile,p.Results.cal_bdf_file,EEG.chanlocs,p.Results);
0559 end
0560 EEG.data=EEG.data.*repmat(scale_fact,1,n_pnt);
0561
0562 if 0,
0563
0564 EEG.cal_info.erps=cal_erps;
0565 EEG.cal_info.usedcal_itemnum=usedcal_itemnum;
0566 EEG.cal_info.npulse_psbl=npulse_psbl;
0567 EEG.cal_info.npulse_used=npulse_used;
0568 end
0569
0570
0571
0572
0573
0574 if strcmpi(p.Results.reref_bimast,'yes')
0575
0576 mastoid_id=[];
0577 VerbReport(' ',1,VERBLEVEL);
0578 VerbReport('Looking for channel labeled A1 or A2.',2,VERBLEVEL);
0579 for dg=1:length(EEG.chanlocs),
0580 if strcmpi(EEG.chanlocs(dg).labels,'A2') || strcmpi(EEG.chanlocs(dg).labels,'A1')
0581 mastoid_id=dg;
0582 VerbReport(sprintf('Found mastoid electrode: Channel %d, %s', ...
0583 dg,EEG.chanlocs(dg).labels), 1, VERBLEVEL);
0584 break;
0585 end
0586 end
0587 if isempty(mastoid_id)
0588 watchit(sprintf(['Could not find mastoid electrode labeled "A1"' ...
0589 ' or "A2".\nRe-reference aborted. Data were' ...
0590 ' not modified.']));
0591 else
0592
0593
0594 if isempty(p.Results.reref_ignore)
0595 ignore=[];
0596 VerbReport(['I assume all electrodes were' ...
0597 ' recorded referenced to the other mastoid.'],1,VERBLEVEL);
0598 else
0599 if ischar(p.Results.reref_ignore)
0600 ignore{1}=p.Results.reref_ignore;
0601 else
0602 ignore=p.Results.reref_ignore;
0603 end
0604 VerbReport(sprintf(['I assume all electrodes were' ...
0605 ' recorded referenced to the other mastoid save for the' ...
0606 ' %d that I was told to ignore (via the "reref_ignore" option).'],length(ignore)),1,VERBLEVEL);
0607 end
0608 reref_bimastoidEEG(mastoid_id,ignore);
0609 end
0610 VerbReport(' ',1,VERBLEVEL);
0611 end
0612
0613
0614
0615
0616 EEG=eeg_checkset(EEG);
0617 [ALLEEG EEG ALLEEGid] = eeg_store(ALLEEG, EEG, 1);
0618
0619
0620
0621
0622 EEG = pop_saveset( EEG, 'filename', setfilename, 'filepath', setfilepath);
0623
0624
0625
0626
0627 [ALLEEG EEG] = eeg_store(ALLEEG, EEG, ALLEEGid);
0628
0629
0630
0631 clear global SUBJECT
0632
0633 if strcmpi(logfile,'temp.log'),
0634 VerbReport('Removing temporary logfile: temp.log', 2, VERBLEVEL);
0635 unix('rm -f temp.log');
0636 end
0637 if strcmpi(blffile,'temp.blf'),
0638 VerbReport('Removing temporary files: temp.rt, temp.blf', 2, VERBLEVEL);
0639 unix('rm -f temp.blf');
0640 unix('rm -f temp.rt');
0641 end
0642
0643
0644
0645 disp('HERE!!');