plot_eimcorr() - Plots the correlation between EEG an some other experimental variable at multiple channels. The graph is produced from data in a format output by compile_erpimageMK. Function presumes data are stored in global variables unless input .eim filename is specified. Graph is interactive and allows one to see topography of correlations at arbitrary points in time. Usage: >> plot_eimcorr(sort_var,varargin) Required Inputs: sort_var = [string] Variable whose correlation with EEG will be computed (e.g., 'logitmnum'); Use [] to see list of available sorting variables. Optional Inputs I (Specifying Data & Function Verbosity) 'bins' = [integer vector] Bins to extract trials from. [] means to use all bins {default: use all bins} 'in_fname' = [string] File from compile_erpimageMK.m in which data are stored. Include the file's path unless the file is the current working directory. {default: use global variables} 'verblevel' = an integer specifiying the amount of information you want functions to provide about what they are doing during runtime. Options are: 0 - quiet, only show errors, warnings, and EEGLAB reports 1 - stuff anyone should probably know 2 - stuff you should know the first time you start working with a data set {default value} 3 - stuff that might help you debug (show all reports) 'title' = [string] Plot title {default: bin descriptor (if only one bin) or list of bins} 'limits' = [lotime hitime] Time axis minimum and maxmimum (in in milliseconds. Can use NaN (or nan, but not Nan) to use earliest and latest time points in data. {default: [NaN NaN]}. 'fig_id' = [integer(s)] ID number of Matlab figure window(s) in which to create the ERPimage. If ID is not specified, the erpimage will be generated in a new figure window. {default: []} Outputs: If data are not already in memory, the function sets some global variables (see below). Global Variables: VERBLEVEL = level of verbosity (i.e., tells functions how much how much to report about what they're doing during runtime) set by the optional function argument 'verblevel' in_fname = .eim file from which data were loaded (produced by compile_erpimageMK.m) chans = cell array specifiying the names of the channels loaded into memory chan_data = the EEG at all of the channels loaded into memory chanlocs = EEG.chanlocs struct specifying channel locations ep_info = information about each trial/epoch of data (in format of EEG.event struct) ep_times = start and stop time of each epoch (in ms) bindesc = struct array of bin descriptors srate = sampling rate eim_bins = bins that were expicitly loaded when .eim file was created rtmsec = reaction time info for each epoch (may be empty) rtbins = specifies which reaction time value in rtmsec goes with which bin (i.e., enables the same epoch to have different RTs depending on which bin it is assigned to). Example: >> setfile_tplate='/homes/dgroppe/SANDBOX/RTERPM/rterpm#.set'; >> chans={'MiPf','MiCe','MiOc','LLOc','RLOc'}; >> compile_erpimageMK(1:4,'setfile_tplate',setfile_tplate,'chans',chans, ... 'bins',[127 133 142 157],'out_fname','demo.eim'); >> plot_erpimageMK('MiCe','rtmsec','in_fname','demo.eim','avewidth',4,'fig_id',1); ?? Notes: Author: David Groppe Kutaslab, 8/2009
0001 function plot_eimcorr(sort_var,varargin) 0002 % plot_eimcorr() - Plots the correlation between EEG an some other experimental 0003 % variable at multiple channels. The graph is produced 0004 % from data in a format output by compile_erpimageMK. 0005 % Function presumes data are stored in global variables unless 0006 % input .eim filename is specified. Graph is interactive 0007 % and allows one to see topography of correlations at 0008 % arbitrary points in time. 0009 % 0010 % 0011 % Usage: 0012 % >> plot_eimcorr(sort_var,varargin) 0013 % 0014 % Required Inputs: 0015 % sort_var = [string] Variable whose correlation with EEG will be computed (e.g., 0016 % 'logitmnum'); Use [] to see list of available sorting 0017 % variables. 0018 % 0019 % 0020 % Optional Inputs I (Specifying Data & Function Verbosity) 0021 % 'bins' = [integer vector] Bins to extract trials from. [] means to 0022 % use all bins {default: use all bins} 0023 % 'in_fname' = [string] File from compile_erpimageMK.m in which data are 0024 % stored. Include the file's path unless the file is the 0025 % current working directory. {default: use global 0026 % variables} 0027 % 'verblevel' = an integer specifiying the amount of information you want 0028 % functions to provide about what they are doing during runtime. 0029 % Options are: 0030 % 0 - quiet, only show errors, warnings, and EEGLAB reports 0031 % 1 - stuff anyone should probably know 0032 % 2 - stuff you should know the first time you start working 0033 % with a data set {default value} 0034 % 3 - stuff that might help you debug (show all reports) 0035 % 'title' = [string] Plot title {default: bin descriptor (if only one 0036 % bin) or list of bins} 0037 % 'limits' = [lotime hitime] Time axis minimum and maxmimum (in 0038 % in milliseconds. Can use NaN (or nan, but not Nan) to 0039 % use earliest and latest time points in data. {default: 0040 % [NaN NaN]}. 0041 % 'fig_id' = [integer(s)] ID number of Matlab figure window(s) in which to create 0042 % the ERPimage. If ID is not specified, the erpimage will be generated 0043 % in a new figure window. {default: []} 0044 % 0045 % 0046 % Outputs: 0047 % If data are not already in memory, the function sets some global 0048 % variables (see below). 0049 % 0050 % 0051 % Global Variables: 0052 % VERBLEVEL = level of verbosity (i.e., tells functions how much 0053 % how much to report about what they're doing during 0054 % runtime) set by the optional function argument 'verblevel' 0055 % in_fname = .eim file from which data were loaded (produced by 0056 % compile_erpimageMK.m) 0057 % chans = cell array specifiying the names of the channels loaded 0058 % into memory 0059 % chan_data = the EEG at all of the channels loaded into memory 0060 % chanlocs = EEG.chanlocs struct specifying channel locations 0061 % ep_info = information about each trial/epoch of data (in format of 0062 % EEG.event struct) 0063 % ep_times = start and stop time of each epoch (in ms) 0064 % bindesc = struct array of bin descriptors 0065 % srate = sampling rate 0066 % eim_bins = bins that were expicitly loaded when .eim file was created 0067 % rtmsec = reaction time info for each epoch (may be empty) 0068 % rtbins = specifies which reaction time value in rtmsec goes with 0069 % which bin (i.e., enables the same epoch to have different 0070 % RTs depending on which bin it is assigned to). 0071 % 0072 % 0073 % Example: 0074 % >> setfile_tplate='/homes/dgroppe/SANDBOX/RTERPM/rterpm#.set'; 0075 % >> chans={'MiPf','MiCe','MiOc','LLOc','RLOc'}; 0076 % >> compile_erpimageMK(1:4,'setfile_tplate',setfile_tplate,'chans',chans, ... 0077 % 'bins',[127 133 142 157],'out_fname','demo.eim'); 0078 % >> plot_erpimageMK('MiCe','rtmsec','in_fname','demo.eim','avewidth',4,'fig_id',1); 0079 % ?? 0080 % 0081 % Notes: 0082 % 0083 % Author: 0084 % David Groppe 0085 % Kutaslab, 8/2009 0086 % 0087 0088 0089 global in_fname; 0090 global chan_data; 0091 global chans; 0092 global chanlocs; 0093 global ep_info; 0094 global ep_times; 0095 global bindesc; 0096 global srate; 0097 global rtmsec; 0098 global rtbins; 0099 global VERBLEVEL 0100 global eim_bins 0101 0102 0103 %Input Parser 0104 p=inputParser; 0105 %Note: the order of the required arguments needs to match precisely their 0106 %order in the function definition (which is also the order used by p.parse 0107 %below) 0108 p.addRequired('sort_var',@(x) ischar(x) || isempty(x)); 0109 p.addParamValue('bins',[],@isnumeric); %default is to use all bins 0110 p.addParamValue('in_fname',[],@ischar); %default is to use global variables 0111 p.addParamValue('fig_id',[],@(x) sum(x>=0) || isempty(x)); 0112 p.addParamValue('verblevel',[],@(x) x>=0); 0113 p.addParamValue('title',[],@(x) ischar(x) || isempty(x));%default is to use chan name & bin descriptor 0114 p.addParamValue('limits',[NaN NaN],@isnumeric); 0115 0116 0117 p.parse(sort_var,varargin{:}); 0118 0119 if isempty(VERBLEVEL) && isempty(p.Results.verblevel) 0120 VERBLEVEL=2; 0121 elseif ~isempty(p.Results.verblevel) 0122 VERBLEVEL=p.Results.verblevel; 0123 end 0124 0125 0126 if isempty(p.Results.in_fname) 0127 %if input file wasn't specified, assume that data are in global 0128 %variables 0129 if isempty(chan_data) || isempty(chans) || isempty(chanlocs) || isempty(ep_info) ... 0130 || isempty(ep_times) || isempty(bindesc) || isempty(srate), 0131 error(sprintf(['You either need to specify an input filename (argument "in_fname") ' ... 0132 'or you need to appropriately fill the following global variables: ' ... 0133 'chan_data, chans, chanlocs, ep_info, ep_times, bindesc, & srate.\n' ... 0134 'The function compile_erpimageMK.m can do this for you.\n'])); 0135 end 0136 in_fname=[]; %indicates that data were already global 0137 elseif strcmp(in_fname,p.Results.in_fname) 0138 %if input file matches file from which data in global variables were 0139 %taken from, use the data in global variables to save the time it takes 0140 %to reload the data from the file 0141 if isempty(chan_data) || isempty(chans) || isempty(chanlocs) || isempty(ep_info), 0142 %some critical variables are not already in global memory, try to 0143 %load everything from file afterall 0144 load(p.Results.in_fname,'-MAT'); 0145 else 0146 fprintf('in_filename %s matches source of data aleady in memory.\n',p.Results.in_fname); 0147 fprintf('Will use data already in memory to save time.\n'); 0148 fprintf('If you would rather load the data from the file enter the\n'); 0149 fprintf(' following at the Matlab command line: '); 0150 fprintf('>> clear global in_fname\n'); 0151 end 0152 else 0153 load(p.Results.in_fname,'-MAT'); 0154 in_fname=p.Results.in_fname; 0155 msg=sprintf('Loading file %s into global variables.\n',in_fname); 0156 msg=[msg sprintf('One of these variables, chan_data, may be quite large.\n')]; 0157 msg=[msg sprintf('To delete it, enter >> clear global chan_data\n')]; 0158 VerbReport(msg,2,VERBLEVEL); 0159 end 0160 0161 % Derive beginning and end time points 0162 if sum(isnan(p.Results.limits)) || isempty(p.Results.limits) || (length(p.Results.limits)~=2) 0163 start_pt=1; 0164 end_pt=length(ep_times); 0165 else 0166 start_pt=find_tpt(p.Results.limits(1),ep_times); 0167 end_pt=find_tpt(p.Results.limits(2),ep_times); 0168 end 0169 0170 %Check to make sure we have the desired sorting variable 0171 got_srtvar=0; 0172 %RT is a special sorting variable, since there may be more than one RT per 0173 %trial 0174 sortby_rt=0; 0175 rt_loaded=0; 0176 if exist('rtmsec','var') 0177 if ~isempty(rtmsec), 0178 rt_loaded=1; 0179 if strcmpi(sort_var,'rtmsec'), 0180 sortby_rt=1; 0181 got_srtvar=1; 0182 end 0183 end 0184 end 0185 0186 %Check for non-RT sorting variables 0187 if ~sortby_rt, 0188 fldnms=fieldnames(ep_info); 0189 possible=[]; 0190 desired=['event' sort_var]; 0191 for a=1:length(fldnms), 0192 if strcmpi('event',fldnms{a}(1:5)) && ~strcmpi('eventrtmsec',fldnms{a}) && (length(fldnms{a})>5), 0193 %i.e., it is not "event", note rtmsec sorting variable should 0194 %have been caught above already 0195 cmnd=['isnumeric(ep_info(a).' fldnms{a} ')']; 0196 if (eval(cmnd)), 0197 if strcmpi(desired,fldnms{a}), 0198 got_srtvar=1; 0199 break; %break a loop 0200 else 0201 possible=[possible a]; 0202 end 0203 end 0204 end 0205 end 0206 end 0207 if ~got_srtvar, 0208 msg=sprintf('Sorting variable %s is not an option.\nCurrently loaded sorting variables are: ',sort_var); 0209 for a=1:(length(possible)-1), 0210 msg=[msg fldnms{possible(a)}(6:end) ', ']; 0211 end 0212 msg=[msg fldnms{possible(end)}(6:end)]; %last/penultimate possibility doesn't need comma 0213 if rt_loaded, 0214 msg=[msg ', rtmsec']; 0215 end 0216 error(msg); 0217 end 0218 0219 %Extract data from specified bins and sorting variable from epoch info 0220 n_ep=length(ep_info); 0221 srt_val=zeros(1,n_ep)*NaN; 0222 %file_id=zeros(1,n_ep); %??DG 0223 0224 %note, srt_val indicates which epochs are to be ignored with NaN values 0225 %erpimage.m automatically ignores epochs with NaN srt_var values 0226 for a=1:n_ep, 0227 %%%%%% use a subset of bins %%%%%% 0228 if ~isempty(p.Results.bins), 0229 got_it=0; 0230 if sortby_rt, 0231 %make sure the RTs are the same for all bins 0232 %preallocate memory 0233 rt_ids=zeros(1,length(p.Results.bins)); 0234 rt_ids_ct=0; 0235 for d=p.Results.bins, 0236 rt_id=find(rtbins{a}==d); 0237 if ~isempty(rt_id), 0238 rt_ids_ct=rt_ids_ct+1; 0239 rt_ids(rt_ids_ct)=rt_id; 0240 end 0241 %rt_ids=[rt_ids find(rtbins{a}==d)]; %find all the rts for all the desired bins 0242 end 0243 if rt_ids_ct>0, 0244 uni_rt=unique(rtmsec{a}(rt_ids(1:rt_ids_ct))); 0245 if length(uni_rt)>1, 0246 error(sprintf(['Different bins have different RTs for epoch %d.\n' ... 0247 'You must specify bins that have the same RTs.'],a)); 0248 else 0249 srt_val(a)=uni_rt; %get the RT 0250 got_it=1; 0251 end 0252 end 0253 else %non-RT sorting variable 0254 for b=p.Results.bins, 0255 for c=1:length(ep_info(a).eventtype), 0256 if strcmpi(ep_info(a).eventtype{c},['bin' int2str(b)]), %does this trial fall in a desired bin? 0257 %c=length(ep_info(a).eventtype); %break c loop 0258 %b=p.Results.bins(end); %break b loop 0259 cmnd=['srt_val(a)=ep_info(a).event' p.Results.sort_var ';']; 0260 eval(cmnd); 0261 got_it=1; 0262 break; %break c loop 0263 end 0264 end 0265 if got_it, 0266 break; %break b loop 0267 end 0268 end 0269 end 0270 if ~got_it, %epoch not a member of desired bin 0271 srt_val(a)=NaN; 0272 end 0273 0274 %%%%%% no bins specified, use all bins %%%%%% 0275 else 0276 if sortby_rt, 0277 uni_rt=unique(rtmsec{a}); 0278 if length(uni_rt)>1, 0279 error(sprintf(['Different bins have different RTs for epoch %d.\n' ... 0280 'You must specify bins that have the same RTs.'],a)); 0281 else 0282 srt_val(a)=uni_rt; 0283 end 0284 else 0285 cmnd=['srt_val(a)=ep_info(a).event' p.Results.sort_var ';']; 0286 eval(cmnd); 0287 end 0288 end 0289 end 0290 0291 0292 %Compile plot title 0293 if isempty(p.Results.title), 0294 %Note, Matlab warning that fig_title might not be used is because Matlab can't figure out that it's used via eval.m 0295 if isempty(p.Results.bins), 0296 if length(eim_bins)==length(bindesc), 0297 fig_title='All Bins'; 0298 else 0299 fig_title='Bins: '; 0300 for a=1:(length(eim_bins)-1), %only a subset of bins stored in eim file 0301 fig_title=[fig_title int2str(eim_bins(a)) ', ']; 0302 end 0303 fig_title=[fig_title int2str(eim_bins(end))]; 0304 end 0305 elseif length(p.Results.bins)==1, 0306 %one bin, make title bin descriptor 0307 fig_title=['Bin ' int2str(p.Results.bins) ': ' bindesc{p.Results.bins}]; 0308 else 0309 fig_title='Bins: '; 0310 for a=1:(length(p.Results.bins)-1), 0311 fig_title=[fig_title int2str(p.Results.bins(a)) ', ']; 0312 end 0313 fig_title=[fig_title int2str(p.Results.bins(end))]; 0314 end 0315 else 0316 fig_title=p.Results.title; 0317 end 0318 0319 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0320 % Compute correlations between sort var and all loaded channels 0321 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0322 VerbReport(' ',2,VERBLEVEL); 0323 VerbReport('Computing and plotting linear correlation between single trials and sorting variable.',2,VERBLEVEL); 0324 0325 n_loc=length(chanlocs); 0326 n_chans=length(chans); 0327 %r=zeros(n_loc,length(ep_times)); 0328 r=zeros(n_loc,end_pt-start_pt+1); 0329 rho=r; 0330 ignore_trial=isnan(srt_val); 0331 srt_valCOR=srt_val(ignore_trial==0); 0332 for a=1:n_loc, 0333 for b=1:n_chans, 0334 if strcmpi(chanlocs(a).labels,chans{b}), 0335 %compute correlation 0336 ct_tpt=0; 0337 for t=1:length(ep_times), 0338 % for t=start_pt:end_pt, 0339 ct_tpt=ct_tpt+1; 0340 r(a,ct_tpt)=corr(srt_valCOR',chan_data{b}(t,ignore_trial==0)','type','Pearson'); 0341 rho(a,ct_tpt)=corr(srt_valCOR',chan_data{b}(t,ignore_trial==0)','type','Spearman'); 0342 end 0343 break; %leave b loop 0344 end 0345 end 0346 end 0347 0348 VerbReport('Left click on plot of correlations across time to see correlation topography at that point in time.',2,VERBLEVEL); 0349 VerbReport('Vertical solid black line indicates time point correspoding to currently shown topography.',2,VERBLEVEL); 0350 VerbReport(' ',2,VERBLEVEL); 0351 0352 0353 gui_eim_corr('initialize',p.Results.fig_id,r,rho,length(srt_valCOR),chanlocs,ep_times,[ep_times(start_pt) ep_times(end_pt)],sort_var,fig_title); 0354 %timtopo_corr(r,chanlocs,[ep_times(start_pt) ep_times(end_pt) 0 0],NaN,fig_title, ... 0355 % 0,0,['r (EEG vs. ' sort_var ')'],'emarkersize',16); 0356 set(gcf,'name',['EEG vs. ' sort_var ', ' fig_title]); 0357 0358 0359