Home > matlabmk > crw2set_cont.m

crw2set_cont

PURPOSE ^

crw2set() - Create and save to disk an EEGLAB set file from a

SYNOPSIS ^

function crw2set_cont(crwfile,logfile,blfORbdf_file,setfile,varargin)

DESCRIPTION ^

 crw2set() - Create and save to disk an EEGLAB set file from a
             Kutaslab crw, log, and associated files. EEGLAB must
             be started before running this function.

 Usage:

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function crw2set_cont(crwfile,logfile,blfORbdf_file,setfile,varargin)
0002 % crw2set() - Create and save to disk an EEGLAB set file from a
0003 %             Kutaslab crw, log, and associated files. EEGLAB must
0004 %             be started before running this function.
0005 %
0006 % Usage:
0007 
0008 
0009 
0010 %%%%%%%%%%%%%%%% FUNCTION OUTLINE %%%%%%%%%%%%%%%%%
0011 % I.    IF ALT CAL PULSE CRW FILE SPECIFIED, GET THOSE CAL PULSES FIRST
0012 % II.   OPEN CRW FILE USING ERPIO() & GET EXP INFO AND RECORDING PARAMETERS
0013 % III.  IMPORT BLF FILE INFORMATION
0014 % IV.   IF ARF FILE SPECIFIED, FLAG ARTIFACTS IN LOG FILE
0015 % V.    IMPORT LOG INFO FROM ASCII VERSION OF LOGFILE
0016 % VI.   COMBINE LOG & BIN INFORMATION
0017 % VII.  LOAD INFORMATION ABOUT CLASSES OF EVENTS FROM TEXT FILE(S) [OPTIONAL]
0018 % VIII. LOAD INFORMATION ABOUT PARTICIPANT FROM TEXT FILE(S) [OPTIONAL]
0019 % IX.   LOAD INFORMATION ABOUT LOG EVENTS FROM TEXT FILE(S) [OPTIONAL]
0020 % X.    IMPORT CDBL PRODUCED REACTION TIME INFORMATION [OPTIONAL]
0021 % XI.   IMPORT EEG AND CALIBRATION PULSES
0022 % XII.  NORMALIZE CAL PULSES AND DATA
0023 % XIII. CREATE EEGLAB SET FILE AND SAVE TO DISK
0024 % XIV.  RE-REFERENCE TO BIMASTOID REF [OPTIONAL]
0025 % XV.   REMOVE BASELINE MEAN FROM EEG [OPTIONAL]
0026 % XVI.  SAVE SET AND CLEANUP
0027 
0028 % NOTES
0029 %  erpio('getcdata', 'rawfile', 'event'|'msec', start, stop, padding)
0030 %         This opens the specified rawfile and reads in continuous
0031 %         data from start to stop.  If the third parameter is the string
0032 %         "event", start and stop are taken as ordinal event numbers
0033 %         in the raw file (as shown by "item number" by the logexam
0034 %         program - note they start at 0); if it is "msec", start and
0035 %         stop are taken as integer numbers of milliseconds from the
0036 %         beginning of the raw file.  A start of 0 is considered the
0037 %         beginning of the raw file; a stop of 0 is considered the end
0038 %         of the raw file.  'padding' specifies to include an additional
0039 %         number of msec of data on either side of the boundaries (0 if
0040 %         none is desired).  The return value is an MxN matrix containing
0041 %         the continuous section of data, where M is the number of
0042 %         channels (including an additional channel containing event
0043 %         codes as the first channel) and N is the number of sample
0044 %         points in the continuous section of data.  Note the continuous
0045 %         section of data is aligned on the nearest raw record boundaries
0046 %         that include the start and stop boundaries, so the amount of
0047 %         data returned may be slightly more than specified.
0048 
0049 
0050 %%%%%%%%%%%%%%%% REVISION LOG %%%%%%%%%%%%%%%%%
0051 %
0052 %
0053 
0054 % POSSIBLE FUTURE DEVELOPMENT
0055 %
0056 
0057 % TO DO ??
0058 % Make it possible to use a different crw file to calibrate
0059 % Make it possible to combine multiple crw files into a single file
0060 % Double check optional inputs (make sure they're used) and update comments
0061 
0062 global VERBLEVEL
0063 global SUBJECT
0064 
0065 %EEGLAB global variables:
0066 global EEG
0067 global ALLEEG
0068 
0069 %Input Parser
0070 p=inputParser;
0071 %Note: the order of the required arguments needs to match precisely their
0072 %order in the function definition (which is also the order used by p.parse
0073 %below)
0074 
0075 % limits,limit_types, (make these optional)
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); %made optional
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); % new ??
0088 p.addParamValue('limits_type','msec',@(x) strcmpi(x,'msec') || strcmpi(x,'event')); %new ??
0089 p.addParamValue('padding',0,@(x) isnumeric(x) && length(x)==1 && x>=0); %NEW in units of ms
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); %plot cal pulses
0095 p.addParamValue('cal_plot_avg',0,@(x) x==0 | x==1); %plot cal pulse avg (time intensive)
0096 p.addParamValue('cal_npts',10,@(x) x>0 & mod(x,1)==0); %number of time
0097 p.addParamValue('block_plot_opt',1,@(x) x==0 | x==1); %plot sweeps with amplifier blocking
0098 %points to include in cal window
0099 p.addParamValue('low_cursor',-50,@(x) x<0); %center of pre-pulse
0100 %baseline window (in msec)
0101 p.addParamValue('hi_cursor',50,@(x) x>0); %center of post-pulse
0102 %baseline window (in msec)
0103 p.addParamValue('cal_amp',10,@isnumeric); %cal pulse amplitude
0104 %(in microvolts)
0105 p.addParamValue('cal_polarity',1,@(x) x==1 | x==-1); %cal pulse polarity
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 %remove mean EEG in given time window (in msec) from each epoch
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 %Show settings of all arguments
0124 if VERBLEVEL>0
0125     fprintf('crw2set argument values:\n');
0126     disp(p.Results);
0127 end
0128 
0129 if iscell(p.Results.garv_flags) %?? relevant?
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 % I. IMPORT EEG DATA FROM CRW FILE
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 % Misc additional info that needs to be read from crw file
0151 srate=erpio2('get_srate',crwfile);
0152 sub_codename=erpio2('get_subdes',crwfile);
0153 
0154 
0155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0156 % II. TAKE CARE OF CHANNEL LOCATION INFORMATION
0157 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0158 
0159 %convert p.Results.exclude_chans/include_chans to a cell array if it is not already
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 % III. IMPORT BLF FILE INFORMATION
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 % Each row of blf_info corresponds to an event, the 1's in the row indicate
0189 % which bin the event belongs to
0190 
0191 
0192 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0193 % IV. IMPORT LOG INFO FROM ASCII VERSION OF LOGFILE
0194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0195 VerbReport(' ',1,VERBLEVEL);
0196 % If ascii version of logfile not specified or if new log file created with artifact flags,
0197 % create temporary ascii file
0198 
0199 VerbReport(sprintf('Creating temporary ascii version of logfile %s',logfile),1,VERBLEVEL);
0200 %remove temp.asci if it exists
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 %import log information for events caught by blf file
0214 ep_info=import_entire_log(asciilogfile,blf_evnum);
0215 
0216 %If a temp ascii version of logfile was created, delete it
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 % Build epoch information descriptions for later ...
0228 log_LongDesc = {'Log Item Number', 'Event Code', 'Condition Code', 'Log Flag (40=nonblink art; 60=blink art)','TIME'};
0229 
0230 
0231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0232 % IV. CALIBRATE DATA
0233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0234 
0235 if isempty(p.Results.cal_crwfile)
0236     % Use cal pulses in the crw file whose data we're reading
0237     % Note, function assumes that any event captured by the bdf file with a
0238     % condition code of 0 is a cal pulse
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 % ?? add used_cal_ids to EEG variable
0246 
0247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0248 % IV??. CREATE BASIC EEG VARIABLE
0249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0250 % unused pop_importdata options
0251 % 'condition'  - [string] task condition. For example, 'Targets'
0252 %                    {default: none -> all datasets from one condition}
0253 %    'group'      - [string] subject group. For example 'Patients' or 'Control'.
0254 %                    {default: none -> all subjects in one group}
0255 %    'session'    - [integer] session number (from the same subject). All datasets
0256 %                    from the same subject and session will be assumed to use the
0257 %                    same ICA decomposition {default: none -> each dataset from
0258 %                    a different session}
0259 %     'ref'        - [string or integer] reference channel indices. 'averef' indicates
0260 %                    average reference. Note that this does not perform referencing
0261 %                    but only sets the initial reference when the data is imported.
0262 %     'icaweight'  - [matrix] ICA weight matrix.
0263 %     'icasphere'  - [matrix] ICA sphere matrix. By default, the sphere matrix
0264 %                    is initialized to the identity matrix if it is left empty.
0265 %     'comments'   - [string] Comments on the dataset, accessible through the EEGLAB
0266 %                    main menu using (Edit > About This Dataset). Use this to attach
0267 %                    background information about the experiment or data to the dataset.
0268 % Perhaps these would be of use someday?
0269 
0270 %cont_data=[cont_data; eventcodes];
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; %save memory
0275 % Note, first time point is also 0 in Kutaslab logfiles
0276 % Also note that pop_importdata will throw some warnings as it
0277 % figures out how to set some parameters that can't (apparently) be set via
0278 % input arguments
0279 EEG = eeg_checkset( EEG );
0280 
0281 
0282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0283 % IV??. IMPORT EVENT INFORMATION
0284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0285 % Write temporary text file of event information.  There's got to be a
0286 % way of doing this strictly within MATLAB, but this was the best I could
0287 % do without going into EEGLAB source code.
0288 if isempty(p.Results.event_list_file),
0289     %make text file of event information
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     % First add events from the "Event #" column of the logfile along with
0298     % the corresponding time (in msec) from the "Time" column
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); %ep_info is in units of seconds but timestamps in units of ms
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); %note time stamps is in msec and eventcodes should be an array of integers
0309     end
0310     fclose(fid);
0311     
0312     %Add event information to EEG variable
0313     EEG = pop_importevent(EEG, 'event', ...
0314         'temp_event_info.txt', 'fields', {'latency','type','conditioncode'}, ...
0315         'skipline',1,'timeunit',0.001 );
0316     
0317     %Remove temporary text file
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     %Do the same thing as above but with specified text file
0327     %Add event information to EEG variable
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 %% Now add "bin#" to events based on the blf file
0335 n_blf=length(blf_evnum);
0336 for a=1:n_blf,
0337     % Get latency of this event in units of time points
0338     EEGevent_id=blf_evnum(a)+1;
0339 
0340     %Add bin information to EEG.event
0341     EEG.event(EEGevent_id).bins=find(blf_info(a,:))-1;
0342 end
0343 
0344 
0345 %% Find all events of type -16384 and turn them into 'boundary' events. This
0346 % event is pause on dig machines.  'boundary' is a special type of event in
0347 % EEGLAB.
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 %% Report # of trials in each bin
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; % Have to add 1 because Kutaslab bin counts start at 0
0366             bin_ct(bin_nums)=bin_ct(bin_nums)+1;
0367         end
0368     end
0369     fprintf('BIN\tLOADED TRIALS\tBIN DESCRIPTOR\n');
0370     % [blf_info, bin_LongDesc, blffile, blf_evnum, rtfile, cal_bin, cond_LongDesc, bin_CondCode]= ...
0371     % import_blf(blfORbdf_file,logfile,srate,p.Results.rtfile);
0372     % num_bins=size(blf_info,2);
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 % ??VII. LOAD INFORMATION ABOUT CLASSES OF EVENTS FROM TEXT FILE(S) [OPTIONAL]
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, %code_infofile is cell array of text file names
0401             [new_info, info_names]=add_code_info(p.Results.code_infofile{dd},ep_info(:,2)); %second column of
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 %presumably code_infofile is just a string name of a text file
0411         [new_info, info_names]=add_code_info(p.Results.code_infofile,ep_info(:,2)); %second column of
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 % ??IX. LOAD INFORMATION ABOUT LOG EVENTS FROM TEXT FILE(S) [OPTIONAL]
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, %event_infofile is cell array of text file names
0433             [new_info, info_names]=add_ev_info(p.Results.event_infofile{dd},blf_evnum); %blf_evnum is log item numbers
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 %presumably event_infofile is just a string name of a text file
0442         [new_info, info_names]=add_ev_info(p.Results.event_infofile, ...
0443             blf_evnum); %blf_evnum is log item numbers
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 % ??X. IMPORT CDBL PRODUCED REACTION TIME INFORMATION [OPTIONAL]
0457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0458 %Grab RTs from specified rtfile or from temporarily generated rtfile
0459 rtvector=[]; %marker for whether or not RTs will be imported to EEG.event
0460 rtmsec=[];  %marker for whether or not RTs will be imported to EEG.rtmsec & EEG.rtbins
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         %add RTs to other epoch info
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 % ??X. SAVE EEG VARIABLE TO DISK
0477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0478 EEG = pop_saveset( EEG, 'savemode','resave');
0479 EEG = eeg_checkset( EEG );
0480 
0481 
0482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0483 %                  END OF MAIN FUNCTION
0484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0485 
0486 
0487 function [scale_fact, used_cal_ids]=cont_data_calibrate_alt_crw(p,eeg_chanlocs,VERBLEVEL) 
0488 %function [scale_fact, used_cal_ids]=cont_data_calibrate_alt_crw(p,eeg_chanlocs,VERBLEVEL)
0489 
0490 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0491 % A. IMPORT EEG DATA FROM CRW FILE
0492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0493 %read in entire crw file
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 % Misc additional info that needs to be read from crw file
0502 srate=erpio2('get_srate',p.Results.cal_crwfile);
0503 
0504 
0505 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0506 % B. TAKE CARE OF CHANNEL LOCATION INFORMATION
0507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0508 
0509 %convert p.Results.exclude_chans/include_chans to a cell array if it is not already
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 % Make sure channels of cal pulse crw file are compatible with those
0531 % from crw file whose EEG we want to import
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 % C. IMPORT BLF FILE INFORMATION
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 % Each row of blf_info corresponds to an event, the 1's in the row indicate
0548 % which bin the event belongs to
0549 
0550 
0551 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0552 % D. IMPORT LOG INFO FROM ASCII VERSION OF LOGFILE
0553 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0554 VerbReport(' ',1,VERBLEVEL);
0555 % If ascii version of logfile not specified or if new log file created with artifact flags,
0556 % create temporary ascii file
0557 
0558 VerbReport(sprintf('Creating temporary ascii version of logfile %s',p.Results.cal_logfile),1,VERBLEVEL);
0559 %remove temp.asci if it exists
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 %import log information for events caught by blf file
0573 ep_info=import_entire_log(asciilogfile,blf_evnum);
0574 
0575 %If a temp ascii version of logfile was created, delete it
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 % E. GET CALIBRATION SCALING FACTOR
0588 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0589 [scale_fact, used_cal_ids]=cont_data_calibrate(cont_data,timestamps,ep_info,blf_evnum,srate,p.Results);
0590 
0591 
0592

Generated on Tue 10-May-2016 16:37:45 by m2html © 2005