Home > database > ensemble_parse_present.m

ensemble_parse_present

PURPOSE ^

parses presentation files, saves to appropriate place on disk

SYNOPSIS ^

function outdata = ensemble_parse_present(indata,defs)

DESCRIPTION ^

 parses presentation files, saves to appropriate place on disk
 
 REQUIRES
   defs.sinfo(:).sessinfo(:).pres
   defs.paths.inroot
   defs.paths.outroot
   defs.expinfo.id
   defs.fmri.protocol.id
   defs.init.WRITE2FILE
       if this, and defs.init.VOL_CHECK are set, then you must include
       defs.paths.figpath
   defs.init.USE_SPM
   defs.init.CLOBBER
   defs.LINK2FMRI

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function outdata = ensemble_parse_present(indata,defs)
0002 % parses presentation files, saves to appropriate place on disk
0003 %
0004 % REQUIRES
0005 %   defs.sinfo(:).sessinfo(:).pres
0006 %   defs.paths.inroot
0007 %   defs.paths.outroot
0008 %   defs.expinfo.id
0009 %   defs.fmri.protocol.id
0010 %   defs.init.WRITE2FILE
0011 %       if this, and defs.init.VOL_CHECK are set, then you must include
0012 %       defs.paths.figpath
0013 %   defs.init.USE_SPM
0014 %   defs.init.CLOBBER
0015 %   defs.LINK2FMRI
0016 
0017 % 2008/08/11 FB - adapted from proc_nostalgia_fmri_fmri
0018 % 2009/06/18 FB - generalized from fmri analyses to all analyses
0019 % 2014/10/22 PJ - added support for encapsulateEnsembleIDInCell to handle
0020 %                 situation in which multiple Ensemble IDs are present,
0021 %                 e.g. one ensemble_id per run
0022 
0023 outdata = ensemble_init_data_struct();
0024 
0025 global r
0026 
0027 r = init_results_struct;
0028 
0029 r.type = 'presentation';  % Identify the type of this reporting instance
0030 r.report_on_fly = 1;
0031 
0032 %%% INITIALIZE VARIABLES
0033 try WRITE2FILE = defs.present.WRITE2FILE;
0034 catch
0035   if isfield(defs,'present') && isfield(defs.present,'export_params')
0036     WRITE2FILE = 1;
0037   else
0038     WRITE2FILE = 0;
0039   end
0040 end
0041 
0042 if isstruct(indata), indata = {indata}; end
0043 
0044 % Parse out the input data
0045 for idata = 1:length(indata)
0046   if isfield(indata{idata},'type')
0047     switch indata{idata}.type
0048       case {'paths'}
0049         pathdata = indata{idata};
0050         pcol = set_var_col_const(pathdata.vars);
0051       case 'sinfo'
0052         sinfo = indata{idata};
0053         sinfo = sinfo.data;
0054         proc_subs = {sinfo(:).id};
0055         nsub_proc = length(proc_subs);
0056     end
0057   end
0058 end
0059 
0060 if isfield(defs,'sinfo') && ~exist('sinfo','var')
0061   sinfo = defs.sinfo;
0062   proc_subs = {sinfo(:).id};
0063   nsub_proc = length(proc_subs);
0064 end
0065 
0066 % check for required vars
0067 check_vars = {'sinfo','pathdata'};
0068 check_required_vars;
0069 
0070 if (iscell(indata) && ~isempty(indata) && isfield(indata{1},'task') && ...
0071         any(strncmp('return_outdir',indata{1}.task,length('return_outdir')))) || ...
0072         (isstruct(indata) && isfield(indata,'task') && ...
0073         any(strncmp('return_outdir',indata.task,length('return_outdir'))))
0074   if exist('pathdata','var') && ~isempty(pathdata.data{1})
0075     if length(nsub_proc) == 1
0076       pfilt = struct();
0077       pfilt.include.all.subject_id = proc_subs;
0078       lpathdata = ensemble_filter(pathdata,pfilt);
0079       if ~isempty(lpathdata.data{1})
0080         sfilt = pfilt;
0081         sfilt.include.all.path_type = {'sess_outdir'};
0082         spathdata = ensemble_filter(lpathdata,sfilt);
0083         if length(spathdata.data{1}) == 1
0084           % one session, save outdata = sess_outdir
0085           outdata = spathdata.data{pcol.path}{1};
0086         else
0087           sfilt = pfilt;
0088           sfilt.include.all.path_type = {'sub_outdir'};
0089           spathdata = ensemble_filter(lpathdata,sfilt);
0090           if length(spathdata.data{1}) == 1;
0091             outdata = spathdata.data{pcol.path}{1};
0092           end
0093         end
0094       end
0095     end
0096   end
0097   if ~exist('outdata','var') || ~exist(outdata,'dir'), outdata = ''; end
0098   return
0099 end
0100 
0101 outdata.vars = [outdata.vars 'sinfo'];
0102 sinfo_idx = length(outdata.vars);
0103 outdata.data{sinfo_idx} = ensemble_init_data_struct();
0104 outdata.data{sinfo_idx}.type = 'sinfo';
0105 outdata.data{sinfo_idx}.data = sinfo;
0106 
0107 outdata.vars = [outdata.vars 'pres_data'];
0108 pdata_idx = length(outdata.vars);
0109 outdata.data{pdata_idx} = ensemble_init_data_struct();
0110 outdata.data{pdata_idx}.type = 'presentation_data';
0111 outdata.data{pdata_idx}.vars = {'subject_id','session',...
0112     'ensemble_id','presdata'};
0113 pdcol = set_var_col_const(outdata.data{pdata_idx}.vars);
0114 outdata.data{pdata_idx}.data{pdcol.subject_id} = {};
0115 outdata.data{pdata_idx}.data{pdcol.session} = {};
0116 outdata.data{pdata_idx}.data{pdcol.ensemble_id} = [];
0117 outdata.data{pdata_idx}.data{pdcol.presdata} = {};
0118 
0119 if WRITE2FILE
0120   outdata.vars = [outdata.vars 'pres_paths'];
0121   ppaths_idx = length(outdata.vars);
0122   outdata.data{ppaths_idx} = ensemble_init_data_struct();
0123   outdata.data{ppaths_idx}.type='pres_paths';
0124   outdata.data{ppaths_idx}.vars = {'subject_id','session',...
0125       'ensemble_id','path'};
0126   ppcol = set_var_col_const(outdata.data{ppaths_idx}.vars);
0127   outdata.data{ppaths_idx}.data{ppcol.subject_id} = {};
0128   outdata.data{ppaths_idx}.data{ppcol.session} = {};
0129   outdata.data{ppaths_idx}.data{ppcol.ensemble_id} = [];
0130   outdata.data{ppaths_idx}.data{ppcol.path} = {};
0131 end
0132 
0133 ecdparams.outDataName = 'presentation_data';
0134 try LINK2FMRI = defs.LINK2FMRI; catch LINK2FMRI = 0; end
0135 
0136 %
0137 % START OF THE SUBJECT LOOP
0138 %
0139 
0140 for isub=1:nsub_proc
0141   subid = sinfo(isub).id;
0142   msg = sprintf('\t\tPROCESSING SUBJECT (%d/%d): %s\n', isub, nsub_proc,subid);
0143   r = update_report(r,msg);
0144 
0145   % Determine number of sessions for this subject
0146   nsess = length(sinfo(isub).sessinfo);
0147   
0148   %
0149   % START OF THE SESSION LOOP
0150   %
0151   
0152   for isess = 1:nsess
0153     sess = sinfo(isub).sessinfo(isess);
0154     
0155     if isfield(sess,'use_session') && ~sess.use_session
0156       msg = sprintf('\t\t\tSkipping %s\n',sess.id);
0157       r = update_report(r,msg);
0158       continue
0159     elseif ~isfield(sess,'pres') || ~isstruct(sess.pres)
0160       msg = sprintf(['\t\t\tNo Presentation sinfo for %s, '...
0161           'SKIPPING!\n'],sess.id);
0162       r = update_report(r,msg);
0163       continue
0164     end
0165     
0166     session_stub = sess.id;
0167     r = update_report(r,sprintf('\t\t\t%s\n', session_stub));
0168     
0169     % init session vars
0170     sdata = ensemble_init_data_struct();    
0171     pres = sinfo(isub).sessinfo(isess).pres;
0172     
0173     % get behav_indir/outdir
0174     sfilt = struct();
0175     sfilt.include.all.subject_id = {subid};
0176     sfilt.include.all.session = {sess.id};
0177     spaths = ensemble_filter(pathdata,sfilt);
0178     
0179     targStr = 'behav_indir';
0180     indx = strncmp(targStr,spaths.data{pcol.path_type},length(targStr));
0181     behav_indir = spaths.data{pcol.path}{indx};
0182 
0183     targStr = 'behav_outdir';
0184     outdx = strncmp(targStr,spaths.data{pcol.path_type}, length(targStr));
0185     behav_outdir = spaths.data{pcol.path}{outdx};
0186     
0187     targStr = 'anal_outdir';
0188     andx = strncmp(targStr,spaths.data{pcol.path_type}, length(targStr));
0189     if isempty(andx)
0190       anal_outdir = behav_outdir;
0191     else
0192       anal_outdir = spaths.data{pcol.path}{andx};
0193     end
0194     
0195     % Determine how many runs we're dealing with
0196     if LINK2FMRI
0197       runs = sess.use_epi_runs;
0198     else
0199       if isfield(sess,'use_runs')
0200         runs = sess.use_runs;
0201       else
0202         runs = 1:size(pres.logfiles,1);
0203       end
0204     end
0205     nruns = length(runs);  
0206     
0207     %
0208     % START OF RUN LOOP
0209     %
0210     for irun = 1:nruns
0211 
0212       presfname = fullfile(behav_indir,pres.logfiles{runs(irun),1});
0213       targruns  = pres.logfiles{runs(irun),2};
0214 
0215       if ~exist(presfname,'file') || exist(presfname,'dir')
0216         msg = sprintf('Presentation file (%s) not found, SKIPPING!\n',...
0217             presfname);
0218         r = update_report(r,msg);
0219         continue
0220       end
0221       
0222       pdata = presentation_parse(presfname,pres.params);
0223       if isempty(pdata)
0224         msg = sprintf('No Presentation Data Returned, SKIPPING\n');
0225         r = update_report(r,msg);
0226         continue
0227       end
0228       
0229       PL = set_var_col_const(pdata.vars);
0230       
0231       % filter for target runs within the pres file, identified in sinfo
0232       pfilt = struct();
0233       pfilt.include.all.RUN = targruns;
0234       pdata = ensemble_filter(pdata,pfilt);
0235       
0236       if isempty(pdata.data{1})
0237         error('Target run ID (%d) not found in Presentation file (%s)', targruns, presfname)
0238       end
0239       
0240       % set pdata.data{PL.RUN} to the run # in sinfo
0241       pdata.data{PL.RUN} = ones(length(pdata.data{1}),1)*runs(irun);
0242       
0243       % 22Oct2014 PJ - reversed order of sdata and pdata in this call since
0244       % it was choking the other way, and since the accumulating variable
0245       % should be on the left
0246       if irun == 1
0247         sdata = pdata;
0248       else
0249         sdata = ensemble_concat_datastruct({sdata,pdata},ecdparams);
0250       end
0251       
0252     end % for irun
0253     
0254     % concatenate sdata with outdata
0255     if isfield(defs.present, 'encapsulateEnsembleIDInCell')
0256       ensemble_id = {sess.ensemble_id};
0257     else
0258       ensemble_id = sess.ensemble_id;
0259     end
0260     
0261     outdata.data{pdata_idx} = ensemble_add_data_struct_row(...
0262         outdata.data{pdata_idx},'subject_id',subid,'session',sess.id,...
0263         'ensemble_id',ensemble_id,'presdata',sdata);
0264     
0265     % write out sdata
0266     if WRITE2FILE
0267       mat_fname = fullfile(behav_outdir,sprintf('%s_%s_present.mat',...
0268         subid,sess.id));
0269       save(mat_fname,'-struct','sdata');
0270       
0271       outdata.data{ppaths_idx} = ensemble_add_data_struct_row(...
0272           outdata.data{ppaths_idx},'subject_id',subid,'session',sess.id,...
0273           'ensemble_id',ensemble_id,'path',mat_fname);
0274       
0275       xdefs = defs.present.export_params;
0276       xdefs.export.fname = fullfile(anal_outdir,...
0277           sprintf('%s_%s_present.csv',subid,sess.id));
0278       xdefs.sas.fname = fullfile(anal_outdir,...
0279           sprintf('%s_%s_present.sas',subid,sess.id));
0280       
0281       ensemble_export_sastxt(sdata,xdefs);
0282     end
0283     
0284   end % for isess
0285 end % for isub=

Generated on Sun 25-Aug-2019 04:00:39 by m2html © 2003