0001 function result = ensemble_load_expinfo(indata,params)
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 if( ischar( indata ) && strcmp( indata, 'getDefaultParams' ) )
0048 result.ensemble.experiment_title = 'use local settings';
0049 result.mysql.host = 'use local settings';
0050 result.mysql.database = 'use local settings';
0051 result.mysql.conn_id = -1;
0052 return;
0053 end
0054
0055 if (iscell(indata) && ~isempty(indata) && isfield(indata{1},'task') && ...
0056 strcmp('return_outdir',indata{1}.task)) || ...
0057 (isstruct(indata) && isfield(indata,'task') && ...
0058 strcmp('return_outdir',indata.task))
0059 result = '';
0060 return
0061 end
0062
0063
0064 result = ensemble_init_data_struct;
0065 result.type = 'experiment_info';
0066
0067
0068 if ~isfield(params,'mysql')
0069 params.mysql = params.ensemble;
0070 end
0071 conn_id = mysql_make_conn(params.mysql);
0072
0073 expname = params.ensemble.experiment_title;
0074
0075
0076 fprintf('Getting experiment information for: %s\n', expname);
0077 exp_meta = mysql_extract_metadata('table','experiment', ...
0078 'experiment_title',expname, ...
0079 'conn_id', conn_id);
0080
0081 if isempty(exp_meta.response_table)
0082 warning(['Failed to locate response_table for experiment (%s)\n' ...
0083 'Check to make sure that you are connecting to the database ' ...
0084 'you think you are connecting to!\n'], expname);
0085 return
0086 end
0087
0088
0089 fprintf('Extracting the response table: %s\n', exp_meta.response_table);
0090 if ~isfield(params.ensemble, 'extract_vars') || isempty(params.ensemble.extract_vars)
0091 extract_vars = {'experiment_id','session_id','subject_id', ...
0092 'response_order','response_id', 'date_time', ...
0093 'form_id','form_order','question_id','subquestion', ...
0094 'stimulus_id','trial_id','response_enum','response_text','misc_info','decline'};
0095 else
0096 extract_vars = params.ensemble.extract_vars;
0097 end
0098
0099 exp_meta_dataStruct = ensemble_tree2datastruct(exp_meta);
0100 exp_meta_dataStruct.name = 'experiment_metadata';
0101 exp_meta_dataStruct.type = 'experiment_metadata';
0102
0103
0104 result.vars{end+1} = 'experiment_metadata';
0105 result.data{end+1} = exp_meta_dataStruct;
0106
0107
0108 exp_meta_cols = set_var_col_const(exp_meta_dataStruct.vars);
0109 experiment_id = exp_meta_dataStruct.data{exp_meta_cols.experiment_id};
0110
0111
0112 sessInfo = ensemble_init_data_struct;
0113 sessInfo.name = 'session_info';
0114 sessInfo.type = 'session_info';
0115 [sessInfo.data,sessInfo.vars] = mysql_extract_data('table','session', ...
0116 'experiment_id',experiment_id, ...
0117 'conn_id',conn_id);
0118
0119
0120 if(isfield(params,'filt'))
0121 sessFilt = params.filt;
0122 sessInfo = ensemble_filter(sessInfo,sessFilt);
0123 end
0124
0125
0126
0127 remove_incomplete = 0;
0128 if(isfield(params.ensemble,'remove_incomplete_sessions') && ...
0129 params.ensemble.remove_incomplete_sessions == 1)
0130 remove_incomplete = 1;
0131 if ~isfield(params.ensemble,'terminal_form') || isempty(params.ensemble.terminal_form)
0132 sessFilt.exclude.any.end_datetime = NaN;
0133 sessInfo = ensemble_filter(sessInfo,sessFilt);
0134 end
0135 end
0136
0137
0138 sess_colNames = set_var_col_const(sessInfo.vars);
0139 sessIDList = sessInfo.data{sess_colNames.session_id};
0140
0141
0142 fprintf('Loading response table information ...\n');
0143 respinfo = ensemble_init_data_struct;
0144
0145
0146 if isfield(params.ensemble, 'encrypted_fields')
0147 encrypted_flds = params.ensemble.encrypted_fields;
0148 enc_key = params.mysql.enc_key;
0149 else
0150 encrypted_flds = {};
0151 enc_key = '';
0152 end
0153
0154 [respinfo.data,respinfo.vars] = mysql_extract_data('table', exp_meta.response_table, ...
0155 'extract_flds', extract_vars, 'order_by','response_id', ...
0156 'session_id', sessIDList, ...
0157 'encrypted_flds', encrypted_flds, ...
0158 'enc_key', enc_key, ...
0159 'conn_id', conn_id);
0160 respinfo.name = 'response_data';
0161 respinfo.type = 'response_data';
0162 respcols = set_var_col_const(respinfo.vars);
0163
0164
0165 if any(isnan(respinfo.data{respcols.subquestion}))
0166 fprintf('Replacing NaNs in subquestion field with 1\n');
0167 respinfo.data{respcols.subquestion}(isnan(respinfo.data{respcols.subquestion})) = 1;
0168 end
0169
0170
0171 respFilt = [];
0172 if isfield(params.ensemble,'terminal_form')
0173 term_form = params.ensemble.terminal_form;
0174 else
0175 term_form = [];
0176 end
0177
0178 if ~isempty(term_form) && remove_incomplete
0179 form_mask = respinfo.data{respcols.form_id} == term_form;
0180 finished_sessids = unique(respinfo.data{respcols.session_id}(form_mask));
0181 respFilt.include.any.session_id = finished_sessids;
0182 end
0183
0184 if isfield(params,'filt')
0185 respFilt = add2filt(respFilt,params.filt);
0186 end
0187
0188 if(~isempty(respFilt))
0189 fprintf('Filtering response table ...\n');
0190 respinfo = ensemble_filter(respinfo,respFilt);
0191 end
0192
0193
0194 result.vars{end+1} = 'response_data';
0195 result.data{end+1} = respinfo;
0196
0197
0198 if remove_incomplete && ~isempty(term_form)
0199 fprintf('Removing incomplete sessions from session_info ...\n');
0200 clear tmpFilt
0201 tmpFilt.include.any.session_id = finished_sessids;
0202 sessInfo = ensemble_filter(sessInfo, tmpFilt);
0203 end
0204
0205
0206 result.vars{end+1} = 'session_info';
0207 result.data{end+1} = sessInfo;
0208 outCols = set_var_col_const(result.vars);
0209
0210
0211
0212 if isfield(params, 'ignoreSubjectTable') && params.ignoreSubjectTable
0213 fprintf('%s: ignoring subject table information\n', mfilename);
0214 else
0215 fprintf('Loading subject information ...\n');
0216 if ~isfield(params.ensemble,'enc_key')
0217 if isfield(params, 'mysql') && isfield(params.mysql, 'enc_key')
0218 params.ensemble.enc_key = params.mysql.enc_key;
0219 else
0220 params.ensemble.enc_key = '';
0221 end
0222 end
0223
0224 if ~isfield(params.ensemble, 'conn_id') || isempty(params.ensemble.conn_id)
0225 try
0226 params.ensemble.conn_id = params.mysql.conn_id;
0227 catch
0228 fprintf('Failed to locate connection ID');
0229 end
0230 end
0231
0232 if isfield(params,'mysql')
0233 login_params = params.mysql;
0234 elseif isfield(params,'ensemble')
0235 login_params = params.ensemble;
0236 else
0237 login_params = struct();
0238 end
0239
0240 subInfo = mysql_get_subinfo('subject_id', sessInfo.data{sess_colNames.subject_id}, ...
0241 'mysql', login_params);
0242 subInfo.name = 'subject_info';
0243 subInfo.type = 'subject_info';
0244
0245
0246 if isfield(params,'filt')
0247 subInfo = ensemble_filter(subInfo, params.filt);
0248 end
0249 scols = set_var_col_const(subInfo.vars);
0250
0251
0252 goodSubIDs = unique(subInfo.data{scols.subject_id});
0253
0254
0255 if isfield(params,'ensemble') && isfield(params.ensemble,'refilterBySubjectTableIDs') && ~params.ensemble.refilterBySubjectTableIDs
0256 fprintf('Not refiltering session and response data using subject table information ...\n');
0257 else
0258 fprintf('Refiltering session and response data using subject table information ...\n');
0259 tmpfilt = struct();
0260 tmpfilt.include.all.subject_id = goodSubIDs;
0261
0262 result.data{outCols.session_info} = ...
0263 ensemble_filter(result.data{outCols.session_info}, tmpfilt);
0264 sessInfo = result.data{outCols.session_info};
0265
0266 result.data{outCols.response_data} = ...
0267 ensemble_filter(result.data{outCols.response_data}, tmpfilt);
0268 respinfo = result.data{outCols.response_data};
0269 end
0270
0271
0272 result.vars{end+1} = 'subject_info';
0273 result.data{end+1} = subInfo;
0274 end
0275
0276
0277
0278
0279 if ~all(isnan(respinfo.data{respcols.stimulus_id}))
0280 fprintf('Loading stimulus and attribute information...\n');
0281 stimulusDataStruct = ensemble_get_stiminfo(respinfo, params);
0282 result.vars = [result.vars stimulusDataStruct.vars];
0283 result.data = [result.data stimulusDataStruct.data];
0284 end
0285
0286
0287 try
0288 subSummaryStats = ensemble_summary_subject_stats({subInfo sessInfo});
0289 result.vars{end+1} = 'subject_summary_stats';
0290 result.data{end+1} = subSummaryStats;
0291 catch
0292 warning('Unable to run ensemble_summary_subject_stats');
0293 end
0294
0295