0001 function [out] = fmri_generate_regress(pinfo, minfo, sess)
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 out = '';
0027
0028
0029 try USE_SPM = pinfo.USE_SPM; catch USE_SPM = 0; end
0030 try USE_FSL = pinfo.USE_FSL; catch USE_FSL = 0; end
0031 if (~USE_SPM && ~USE_FSL) || (USE_SPM && USE_FSL)
0032 error('must choose either FSL or SPM for output');
0033 end
0034
0035
0036 cnames = {};
0037 consets = {};
0038 durations = {};
0039
0040 if isfield(minfo,'condlist') && isstruct(minfo.condlist)
0041 condlist = fieldnames(minfo.condlist);
0042 ncond = length(condlist);
0043
0044
0045 for icond=1:ncond
0046
0047 cond = condlist{icond};
0048
0049 cstr = sprintf('fmri_cond_%s',cond);
0050 if ~exist(cstr,'file')
0051 warning('Unknown regressor: %s',cond);
0052 continue
0053 end
0054
0055 cfun = str2func(cstr);
0056
0057 [lons,ldurs] = cfun(pinfo,minfo,sess);
0058
0059 if isempty(lons) || isempty(ldurs) || (length(lons) ~= length(ldurs))
0060 warning('no regressors generated for regressor %s',cond);
0061 continue
0062 end
0063
0064 cnames = [cnames cond];
0065 consets = [consets lons];
0066 durations = [durations ldurs];
0067
0068 end
0069
0070
0071 if isfield(minfo,'pmodlist') && ~isempty(minfo.pmodlist)
0072 pmodlist = minfo.pmodlist;
0073 pmods = cell(length(cnames),1);
0074 ccm = parse_fh(minfo.cond_cue_map);
0075 else
0076 pmodlist = [];
0077 end
0078 nmod = size(pmodlist,1);
0079 pc = set_var_col_const(pinfo.vars);
0080
0081
0082 for imod = 1:nmod
0083 cond_name = pmodlist{imod,1};
0084
0085
0086
0087 cond_idx = strmatch(cond_name,cnames,'exact');
0088 if ~isempty(cond_idx)
0089 modlist = pmodlist{imod,2};
0090 nlmod = length(modlist);
0091 for ilmod = 1:nlmod
0092 modname = modlist{ilmod};
0093 lp = struct();
0094 lp.name = modname;
0095
0096 cue_type = ccm(modname);
0097
0098 rfilt.include.all = cue_type.filt;
0099 rdata = ensemble_filter(pinfo,rfilt);
0100
0101
0102 if isempty(rdata.data{1})
0103 fprintf('Could not match desired parametric modulator: %s\n', modname);
0104 else
0105
0106 respvect = rdata.data{pc.RESP_CODE};
0107
0108
0109 nval = length(respvect);
0110
0111 rmap = pinfo.resp_mapping;
0112 mapidx = strmatch(cue_type.name, {minfo.resp_mappings{rmap}{:,1}},'exact');
0113 param = [];
0114 for ival = 1:nval
0115 currmask = minfo.resp_mappings{rmap}{mapidx,2} == str2num(respvect{ival}{1});
0116 param(ival) = minfo.resp_mappings{rmap}{mapidx,3}(currmask);
0117 end
0118 lp.param = param(:);
0119 pmods{cond_idx} = [pmods{cond_idx} lp];
0120 end
0121 end
0122 end
0123 end
0124 end
0125
0126
0127 names = {};
0128 vals = [];
0129
0130 if isfield(minfo,'reglist') && ~isempty(minfo.reglist)
0131 reglist = minfo.reglist;
0132 nreg = length(reglist);
0133 reg_template = struct('name','','val',[]);
0134
0135
0136 for ireg = 1:nreg
0137
0138 regid = reglist{ireg};
0139
0140 linfo = pinfo;
0141 linfo.regid = regid;
0142
0143 if strmatch('stim_',regid)
0144 [lnames,lvals] = fmri_regress_stim(linfo,minfo,sess);
0145 else
0146 regstr = sprintf('fmri_regress_%s',regid);
0147 if ~exist(regstr,'file')
0148 warning('Unknown regressor: %s',regid);
0149 continue
0150 end
0151
0152 regfun = str2func(regstr);
0153
0154 [lnames,lvals] = regfun(linfo,minfo,sess);
0155 end
0156
0157 if isempty(lnames) || isempty(lvals)
0158 warning('no regressors generated for regressor %s',regid);
0159 continue
0160 end
0161
0162 names = [names lnames];
0163
0164 vrows = size(vals,1);
0165 lrows = size(lvals,1);
0166 if vrows < lrows && vrows > 0
0167 vals(vrows+1:lrows,:) = 0;
0168 elseif lrows < vrows
0169 lvals(lrows+1:vrows,:) = 0;
0170 end
0171
0172 vals = [vals lvals];
0173
0174 end
0175 end
0176
0177
0178
0179 if USE_FSL
0180
0181 evoutdir = pinfo.evoutdir;
0182 evstub = pinfo.evstub;
0183
0184 ev = create_ev;
0185 nev = 0;
0186
0187 for ir=1:length(names)
0188 nev = nev+1;
0189 outfname = fullfile(evoutdir,sprintf(evstub,nev));
0190 ev(nev).name = names{ir};
0191 ev(nev).fname = outfname;
0192 ev(nev).shape = 2;
0193 regdata = vals{ir};
0194 save(outfname,'regdata','-ascii');
0195 end
0196
0197
0198 ortho_idxs = [];
0199 for ic=1:length(cnames)
0200 nev = nev+1;
0201 ev(nev).name = cnames{ic};
0202 ev(nev).shape = 0;
0203 ev(nev).convolve = 3;
0204 ev(nev).on = consets{ic};
0205 ev(nev).off = consets{ic} + durations{ic};
0206 if isempty(pmods{ic}), continue, end
0207 lpmods = pmods{ic};
0208 lortho_idxs = [];
0209 for ip=1:length(lpmods)
0210
0211 nev = nev+1;
0212 outfname = fullfile(evoutdir,sprintf(evstub,nev));
0213 ev(nev).name = lpmods{ip}.name;
0214 ev(nev).fname = outfname;
0215 ev(nev).shape = 2;
0216 regdata = build_regress(consets{ic},durations{ic},...
0217 lpmods{ip}.param,pinfo.TR,pinfo.dt,pinfo.actual_nvol);
0218 save(outfname,'regdata','-ascii');
0219 lortho_idxs = [ortho_idxs nev];
0220 end
0221 ortho_idxs = [ortho_idxs lortho_idxs];
0222 end
0223
0224 [ev.ortho] = deal(zeros(1,nev));
0225
0226
0227 for io=1:length(ortho_idxs)
0228 lortho = ortho_idxs{io};
0229 for il=1:length(lortho)
0230 ev(lortho(il)).ortho(lortho) = 1;
0231 end
0232 end
0233
0234 out = ev;
0235
0236 elseif USE_SPM
0237
0238
0239 if ~isempty(cnames)
0240 condstruct = struct('name','','onset',[],'duration',[],'pmod',[]);
0241 for ic=1:length(cnames)
0242 condstruct(ic).name = cnames{ic};
0243 condstruct(ic).onset = consets{ic};
0244 condstruct(ic).duration = durations{ic};
0245 if exist('pmods','var'), condstruct(ic).pmod = pmods{ic}; end
0246 end
0247 [condstruct(1:end).tmod] = deal(0);
0248 out.cond = condstruct;
0249 end
0250
0251
0252 if ~isempty(names)
0253 regressors = struct('name','','val',[]);
0254 for ir=1:length(names)
0255 regressors(ir).name = names{ir};
0256 regressors(ir).val = vals(:,ir);
0257 end
0258 out.regress = regressors;
0259 end
0260
0261 end