Home > database > fmri > regressors > fmri_generate_regress.m

fmri_generate_regress

PURPOSE ^

generates regressors for SPM5 or FSL fMRI analysis design matrices

SYNOPSIS ^

function [out] = fmri_generate_regress(pinfo, minfo, sess)

DESCRIPTION ^

 generates regressors for SPM5 or FSL fMRI analysis design matrices
 
   [out] = fmri_generate_regress(pinfo,minfo,sess)
 
 Given a set of parameters, this function generates regressors by calling
 sub-functions named "fmri_regress_%regid%.m", where 'regid' is the name
 of the regressor as provided in the model information structure. Upon
 compiling a set of regressors, this function will package those
 regressors as necessary, for either FSL or SPM5, and will return the
 requisite structure (an fsf.ev structure for FSL, or a .cond or .regress
 part of the .sess structure for SPM).
 
 REQUIRES
   pinfo - run information structure, including presentation data
   minfo - model information structure
   sess  - session information structure
 
 RETURNS
   out - structure in either fsf.ev, spmsess.regress, or spmsess.cond
       format
 
 2009.11.05 FB - adapted from fmri_fsl_generate_evs,
 fmri_spm_generate_regress, and fmri_spm_generate_conds

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [out] = fmri_generate_regress(pinfo, minfo, sess)
0002 % generates regressors for SPM5 or FSL fMRI analysis design matrices
0003 %
0004 %   [out] = fmri_generate_regress(pinfo,minfo,sess)
0005 %
0006 % Given a set of parameters, this function generates regressors by calling
0007 % sub-functions named "fmri_regress_%regid%.m", where 'regid' is the name
0008 % of the regressor as provided in the model information structure. Upon
0009 % compiling a set of regressors, this function will package those
0010 % regressors as necessary, for either FSL or SPM5, and will return the
0011 % requisite structure (an fsf.ev structure for FSL, or a .cond or .regress
0012 % part of the .sess structure for SPM).
0013 %
0014 % REQUIRES
0015 %   pinfo - run information structure, including presentation data
0016 %   minfo - model information structure
0017 %   sess  - session information structure
0018 %
0019 % RETURNS
0020 %   out - structure in either fsf.ev, spmsess.regress, or spmsess.cond
0021 %       format
0022 %
0023 % 2009.11.05 FB - adapted from fmri_fsl_generate_evs,
0024 % fmri_spm_generate_regress, and fmri_spm_generate_conds
0025 
0026 out = '';
0027 
0028 % generate final structure for SPM or FSL?
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 % compile condition
0036 cnames = {};
0037 consets = {};
0038 durations = {};
0039 
0040 if isfield(minfo,'condlist') && isstruct(minfo.condlist)
0041   condlist = fieldnames(minfo.condlist);
0042   ncond = length(condlist); % number of conditions
0043 
0044   % iterate over conditions, form condition list
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 % for icond=1:ncond
0069   
0070   % compile parametric condition modulations
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); % number of parametric modulation sets
0079   pc = set_var_col_const(pinfo.vars);
0080 
0081   % iterate over parametric modulations
0082   for imod = 1:nmod
0083     cond_name = pmodlist{imod,1};
0084   
0085     % Determine which conds array element we are attaching the parametric
0086     % modulation to.
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         % Get the parameter value for each event
0102         if isempty(rdata.data{1})
0103             fprintf('Could not match desired parametric modulator: %s\n', modname);
0104         else
0105           % Extract the button codes
0106           respvect = rdata.data{pc.RESP_CODE};
0107 
0108           % Map the button codes to the desired values
0109           nval = length(respvect);
0110           % Get correct index in response mappings
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 % if ~isempty(ridx);
0121       end % for imod
0122     end % if ~isempty(cond_idx)
0123   end % for icondimod
0124 end % if isfield(minfo,'condlist
0125 
0126 % compile regressors
0127 names = {};
0128 vals = [];
0129 
0130 if isfield(minfo,'reglist') && ~isempty(minfo.reglist)
0131   reglist = minfo.reglist;
0132   nreg = length(reglist);  % Number of regressors
0133   reg_template = struct('name','','val',[]);
0134 
0135   % iterate over regressors, form regressor list
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 % if strmatch('stim_
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 % if vrows < lrows
0171   
0172     vals = [vals lvals];
0173   
0174   end % for ireg=1:nreg
0175 end % if isfield(minfo,'reglist
0176 
0177 % % % BUILD TOOL-SPECIFIC REGRESSOR STRUCTURE
0178 
0179 if USE_FSL
0180     
0181   evoutdir = pinfo.evoutdir;
0182   evstub = pinfo.evstub;
0183 
0184   ev = create_ev;
0185   nev = 0;
0186   % add regressors
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   % add conditions
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       % add parametric modulations
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   % set orthogonalization for pmod regressor sets
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   % Initiate the condition output variable
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   % Initialize the regressors output variable to an empty instance of the structure
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 % if USE_FSL/USE_SPM

Generated on Thu 09-Dec-2010 04:01:40 by m2html © 2003