0001 function cmpt_corrMK(setfiles,out_fname,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
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084 global VERBLEVEL
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097 p=inputParser;
0098
0099
0100
0101 p.addRequired('setfiles',@(x) (ischar(x) || isnumeric(x)) || iscell(x));
0102 p.addRequired('out_fname',@ischar);
0103 p.addParamValue('setfile_tplate',[],@ischar);
0104 p.addParamValue('verblevel',[],@(x) x>=0);
0105 p.addParamValue('cor_vars',[],@(x) (ischar(x) || iscell(x));
0106 p.addParamValue('ignore_vars',[],@(x) (ischar(x) || iscell(x));
0107
0108
0109
0110 p.parse(setfiles,out_fname,varargin{:});
0111
0112
0113 if isempty(VERBLEVEL) && isempty(p.Results.verblevel)
0114 VERBLEVEL=2;
0115 elseif ~isempty(p.Results.verblevel)
0116 VERBLEVEL=p.Results.verblevel;
0117 end
0118
0119
0120 if ischar(setfiles),
0121 sfiles{1}=setfiles;
0122 elseif iscell(setfiles),
0123 sfiles=setfiles;
0124 elseif isempty(setfiles),
0125 error('Argument setfiles is currently empty. You need to specify something.');
0126 else
0127 if isempty(p.Results.setfile_tplate)
0128 error(['If setfiles argument is numeric, you must specify setfile_tplate.' ...
0129 ' Type ">>help mk_erpimage" for an example.']);
0130 else
0131 sfiles=cell(1,length(setfiles));
0132 sfile_ct=0;
0133 pnd_id=find(p.Results.setfile_tplate=='#');
0134 if length(pnd_id)>1 || isempty(pnd_id)
0135 error(['You must use the "#" character in your setfile name template ' ...
0136 'to indicate where the participant numbers should go.']);
0137 end
0138 beg_tplate=p.Results.setfile_tplate(1:pnd_id-1);
0139 end_tplate=p.Results.setfile_tplate(pnd_id+1:end);
0140 for a=setfiles,
0141 sfile_ct=sfile_ct+1;
0142 sfiles{sfile_ct}=[beg_tplate int2str(a) end_tplate];
0143 end
0144 end
0145 end
0146
0147
0148 ignore_vars{1}='event';
0149 ignore_vars{2}='eventlatency';
0150 ignore_vars{3}='eventccode';
0151 ignore_vars{4}='eventevcode';
0152 ignore_vars{5}='eventlogflag';
0153 if ~isempty(p.Results.ignore_vars),
0154 if ischar(p.Results.ignore_vars),
0155 ignore_vars{length(ignore_vars)+1}=p.Results.ignore_vars;
0156 elseif iscell(p.Results.ignore_vars),
0157 for a=1:length(p.Results.ignore_vars),
0158 ignore_vars{length(ignore_vars)+1}=p.Results.ignore_vars{a};
0159 end
0160 end
0161 end
0162
0163
0164 if ~isempty(p.Results.cor_vars),
0165 if ischar(p.Results.cor_vars),
0166 cor_vars{1}=p.Results.cor_vars;
0167 elseif iscell(p.Results.cor_vars),
0168 cor_vars=p.Results.cor_vars;
0169 end
0170 end
0171
0172
0173 clear ep_info;
0174 rtmsec=[];
0175 rtbins=[];
0176 grand_ep_ct=0;
0177 use_ep_this_set=cell(1,length(sfiles));
0178
0179
0180
0181
0182
0183 VerbReport('Loading set files to check for consistency and to extract sorting variabls.',2,VERBLEVEL);
0184 for set_ct=1:length(sfiles),
0185
0186 EEG=pop_loadset(sfiles{set_ct});
0187
0188 if set_ct==1,
0189
0190
0191
0192 srate=EEG.srate;
0193
0194
0195 ep_times=EEG.times;
0196
0197
0198 bindesc=EEG.bindesc;
0199 n_bin=length(bindesc);
0200
0201
0202 chanlocs=EEG.chanlocs;
0203 n_chan=length(chanlocs);
0204
0205
0206 fldnames=fieldnames(EEG);
0207 rtmsec_attached=0;
0208 rtbins_attached=0;
0209 for d=1:length(fldnames),
0210 if strcmpi(fldnames{d},'rtmsec')
0211 rtmsec_attached=1;
0212 elseif strcmpi(fldnames{d},'rtbins')
0213 rtbins_attached=1;
0214 end
0215 end
0216 rts_attached=rtbins_attached*rtmsec_attached;
0217
0218
0219
0220 epinfo_fldnames=fieldnames(EEG.epoch);
0221 use_ep_this_set{set_ct}=[];
0222
0223 for a=1:length(EEG.epoch),
0224 EEG.epoch(a).sourcefile=sfiles{set_ct};
0225
0226 for j=1:length(EEG.epoch(a).eventtype),
0227 if strcmpi(EEG.epoch(a).eventtype{j}(1:3),'bin')
0228 use_ep_this_set{set_ct}=[use_ep_this_set{set_ct} a];
0229 grand_ep_ct=grand_ep_ct+1;
0230 ep_info(grand_ep_ct)=EEG.epoch(a);
0231 if rts_attached,
0232 rtbins{grand_ep_ct}=EEG.rtbins{a};
0233 rtmsec{grand_ep_ct}=EEG.rtmsec{a};
0234 end
0235 break;
0236 end
0237 end
0238 end
0239
0240 if isempty(use_ep_this_set{set_ct}),
0241 watchit(sprintf('File %s has no trials in any bins.\n',sfiles{set_ct}));
0242 end
0243 else
0244
0245
0246
0247
0248
0249 if (srate~=EEG.srate),
0250 error('EEG.srate in set %s does not match that of other sets.\n', ...
0251 sfiles{set_ct});
0252 end
0253
0254
0255 if (length(ep_times)~=length(EEG.times)),
0256 error('EEG.times in set %s does not match that of other sets.\n', ...
0257 sfiles{set_ct});
0258 elseif max(abs(ep_times-EEG.times))
0259 error('EEG.times in set %s does not match that of other sets.\n', ...
0260 sfiles{set_ct});
0261 end
0262
0263
0264
0265 if n_bin==length(EEG.bindesc),
0266 for b=1:n_bin,
0267 if ~strcmpi(bindesc{b},EEG.bindesc{b}),
0268 error('EEG.bindesc in set %s does not match that of other sets.\n', ...
0269 sfiles{set_ct});
0270 end
0271 end
0272 else
0273 error('EEG.bindesc in set %s does not match that of other sets.\n', ...
0274 sfiles{set_ct});
0275 end
0276
0277
0278
0279 if n_chan~=length(EEG.chanlocs),
0280 error('EEG.chanlocs in set %s does not match that of other sets.\n', ...
0281 sfiles{set_ct});
0282 else
0283 for oldchan=1:length(chanlocs),
0284 matchchan=0;
0285 for newchan=1:length(chanlocs),
0286 if strcmpi(chanlocs(oldchan).labels,EEG.chanlocs(newchan).labels),
0287 matchchan=1;
0288 end
0289 end
0290 if ~matchchan
0291 error('EEG.chanlocs in set %s does not match that of other sets.\n', ...
0292 sfiles{set_ct});
0293 end
0294 end
0295 end
0296
0297
0298 fldnames=fieldnames(EEG);
0299 rtmsec_attached=0;
0300 rtbins_attached=0;
0301 for d=1:length(fldnames),
0302 if strcmpi(fldnames{d},'rtmsec')
0303 rtmsec_attached=1;
0304 elseif strcmpi(fldnames{d},'rtbins')
0305 rtbins_attached=1;
0306 end
0307 end
0308 new_rts_attached=rtbins_attached*rtmsec_attached;
0309 if new_rts_attached && ~rts_attached,
0310 error(['File %s has reaction time information, ' ...
0311 'but the other file(s) you loaded do not.'],sfiles{set_ct});
0312 elseif ~new_rts_attached && rts_attached,
0313 error(['File %s does not have reaction time information, ' ...
0314 'but the other file(s) you loaded do.'],sfiles{set_ct});
0315 end
0316
0317
0318 new_fldnames=fieldnames(EEG.epoch);
0319
0320 if length(new_fldnames)~=length(epinfo_fldnames),
0321 error(['EEG.epoch field of file %s does not match ' ...
0322 'that of previously loaded file(s).'],sfiles{set_ct});
0323 else
0324 for a=1:length(new_fldnames),
0325 fld_match=0;
0326 for b=1:length(epinfo_fldnames),
0327 if strcmpi(epinfo_fldnames{b},new_fldnames{a}),
0328 fld_match=1;
0329 break;
0330 end
0331 end
0332 if ~fld_match,
0333 error(['EEG.epoch field of file %s does not match ' ...
0334 'that of previously loaded file(s).'],sfiles{set_ct});
0335 end
0336 end
0337 end
0338
0339
0340 use_ep_this_set{set_ct}=[];
0341
0342 for a=1:length(EEG.epoch),
0343 EEG.epoch(a).sourcefile=sfiles{set_ct};
0344
0345 for j=1:length(EEG.epoch(a).eventtype),
0346 if strcmpi(EEG.epoch(a).eventtype{j}(1:3),'bin')
0347 use_ep_this_set{set_ct}=[use_ep_this_set{set_ct} a];
0348 grand_ep_ct=grand_ep_ct+1;
0349 ep_info(grand_ep_ct)=EEG.epoch(a);
0350 if rts_attached,
0351 rtbins{grand_ep_ct}=EEG.rtbins{a};
0352 rtmsec{grand_ep_ct}=EEG.rtmsec{a};
0353 end
0354 break;
0355 end
0356 end
0357 end
0358
0359 if isempty(use_ep_this_set{set_ct}),
0360 watchit(sprintf('File %s has no trials in any bins.\n',sfiles{set_ct}));
0361 end
0362 end
0363 end
0364
0365
0366
0367
0368
0369
0370
0371 VerbReport(' ',2,VERBLEVEL);
0372 VerbReport('Identifying all possible variables for computing correlations.',2,VERBLEVEL);
0373 VerbReport('NOT considering fields EEG.event, EEG.eventccode, EEG.eventevcode, EEG.eventlogflag, nor EEG.eventlatency as they are probably not meaningful.',2,VERBLEVEL);
0374 VerbReport(' ',2,VERBLEVEL);
0375 var_ct=0;
0376 use_fields=zeros(1,length(epinfo_fldnames));
0377
0378 for a=1:length(epinfo_fldnames),
0379 use_var=1;
0380 for b=1:length(ignore_vars),
0381 if strcmpi(epinfo_fldnames{a},ignore_vars{b}),
0382 use_var=0;
0383 break;
0384 end
0385 end
0386 if use_var
0387 runme=['isnumeric(ep_info(1).' epinfo_fldnames{a} ')'];
0388 if eval(runme)
0389 use_fields(a)=1;
0390 VerbReport(sprintf('Using: %s',epinfo_fldnames{a}),2,VERBLEVEL);
0391 var_ct=var_ct+1;
0392 sorting_vars{var_ct}=epinfo_fldnames{a};
0393 end
0394 end
0395 end
0396 use_fields=find(use_fields);
0397
0398
0399 if rts_attached,
0400 VerbReport('Using: rtmsec',2,VERBLEVEL);
0401 var_ct=var_ct+1;
0402 sorting_vars{var_ct}='rtmsec';
0403 end
0404 VerbReport(' ',2,VERBLEVEL);
0405
0406
0407 sort_var=cell(1,length(use_fields));
0408
0409
0410 for a=1:length(sort_var),
0411 sort_var{1,a}=zeros(1,grand_ep_ct);
0412 end
0413 bin_tracker=zeros(n_bin,grand_ep_ct);
0414
0415
0416 for a=1:grand_ep_ct,
0417
0418
0419 for b=1:(length(sort_var)),
0420 runme=['sort_var{1,b}(a)=ep_info(a).' epinfo_fldnames{use_fields(b)} ';'];
0421 eval(runme);
0422 end
0423
0424
0425 for c=1:length(ep_info(a).eventtype),
0426 if strcmpi(ep_info(a).eventtype{c}(1:3),'bin'),
0427 bin_num=str2num(ep_info(a).eventtype{c}(4:end));
0428 bin_tracker(bin_num,a)=1;
0429 end
0430 end
0431 end
0432
0433
0434
0435
0436
0437
0438
0439 r=zeros(n_chan,length(ep_times),n_bin,length(sort_var));
0440 tau=zeros(n_chan,length(ep_times),n_bin,length(sort_var));
0441 for chan=1:n_chan,
0442 VerbReport(sprintf('\nLoading EEG from channel %s (%d of %d)',chanlocs(chan).labels, ...
0443 chan,n_chan),2,VERBLEVEL);
0444
0445
0446 chan_dat=zeros(grand_ep_ct,length(ep_times));
0447 chan_dat_ct=0;
0448 for set_ct=1:length(sfiles),
0449
0450 EEG=pop_loadset(sfiles{set_ct});
0451
0452 for d=1:length(EEG.epoch),
0453
0454 for j=1:length(EEG.epoch(d).eventtype),
0455 if strcmpi(EEG.epoch(d).eventtype{j}(1:3),'bin')
0456 chan_dat_ct=chan_dat_ct+1;
0457 chan_dat(chan_dat_ct,:)=squeeze(EEG.data(chan,:,d));
0458 break;
0459 end
0460 end
0461 end
0462 end
0463
0464
0465 for b=1:n_bin,
0466 fprintf('Bin %d\n',b);
0467 use_ep=find(bin_tracker(b,:));
0468 for s=1:length(sort_var),
0469 if isempty(use_ep)
0470 r(chan,:,b,s)=NaN;
0471 tau(chan,:,b,s)=NaN;
0472 else
0473 for t=1:length(ep_times),
0474 r(chan,t,b,s)=corr(chan_dat(use_ep,t),sort_var{s}(use_ep)');
0475 tau(chan,t,b,s)=corr(chan_dat(use_ep,t),sort_var{s}(use_ep)', ...
0476 'type','Kendall');
0477 end
0478 end
0479 end
0480
0481
0482 if rts_attached,
0483 if isempty(use_ep)
0484 r(chan,:,b,s+1)=NaN;
0485 tau(chan,:,b,s+1)=NaN;
0486 else
0487
0488 rts=zeros(1,length(use_ep));
0489 rt_ct=0;
0490 for ep=use_ep,
0491 rt_id=find(rtbins{ep}==b);
0492 rt_ct=rt_ct+1;
0493 if isempty(rt_id),
0494 rts(rt_ct)=NaN;
0495 else
0496
0497 rts(rt_ct)=rtmsec{ep}(rt_id);
0498 end
0499 end
0500 nonNaN=find(~isnan(rts));
0501 if isempty(nonNaN),
0502
0503 r(chan,:,b,s+1)=NaN;
0504 tau(chan,:,b,s+1)=NaN;
0505 else
0506 for t=1:length(ep_times),
0507 r(chan,t,b,s+1)=corr(chan_dat(use_ep(nonNaN),t),rts(nonNaN)');
0508 tau(chan,t,b,s+1)=corr(chan_dat(use_ep(nonNaN),t),rts(nonNaN)', ...
0509 'type','Kendall');
0510 end
0511 end
0512 end
0513 end
0514 end
0515 end
0516
0517
0518 n=sum(bin_tracker,2);
0519
0520 save(out_fname,'r','tau','n','sorting_vars','sfiles','ep_info');