Home > database > ensemble_data_by_question.m

ensemble_data_by_question

PURPOSE ^

Filters/reshapes a struct such that compqids are now fields within the return struct.

SYNOPSIS ^

function an_st = ensemble_data_by_question(data_st,params)

DESCRIPTION ^

 Filters/reshapes a struct such that compqids are now fields within the return struct.
 an_st = ensemble_data_by_question(data_st,params);

 Filters and reshapes a data structure such that desired composite question
 IDs are now the different variables in the resulting data structure.

 params.compqids - list of composite qids that we want to extract. If none are
                   given, then use all.
 params.extract_vars - optional cell string array which gives list of
                       variables in data_st that should be transferred to the
                       new structure. Default is to transfer all except for
                       compqid

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function an_st = ensemble_data_by_question(data_st,params)
0002 % Filters/reshapes a struct such that compqids are now fields within the return struct.
0003 % an_st = ensemble_data_by_question(data_st,params);
0004 %
0005 % Filters and reshapes a data structure such that desired composite question
0006 % IDs are now the different variables in the resulting data structure.
0007 %
0008 % params.compqids - list of composite qids that we want to extract. If none are
0009 %                   given, then use all.
0010 % params.extract_vars - optional cell string array which gives list of
0011 %                       variables in data_st that should be transferred to the
0012 %                       new structure. Default is to transfer all except for
0013 %                       compqid
0014 
0015 % 02/04/07 Petr Janata
0016 % 10/07/08 PJ - generalized to handle other databases
0017 % 22/07/10 PJ - fixed conn_id handling
0018 % 22/08/14 PJ - fixed search through ensemble,mysql structs for database
0019 %               info
0020 
0021 an_st = ensemble_init_data_struct;
0022 an_st.type = 'data_by_compqid'; 
0023 
0024 % Set the column constants
0025 incol = set_var_col_const(data_st.vars);
0026 
0027 % Make sure that we have either compqid or question and subquestion ID information
0028 if ~isfield(incol,'compqid') && ~all(isfield(incol,{'question_id','subquestion'}))
0029   fprintf(['Did not find necessary question and subquestion or composite question ID' ...
0030     ' information in the input data']);
0031   return
0032 end
0033 
0034 % Extract info regarding the database and connection ID we should be talking to
0035 param_fld_names = {'ensemble','mysql'};
0036 idxs = find(isfield(params,param_fld_names));
0037 nidx = length(idxs);
0038 if isempty(idxs)
0039   error('%s: Do not have sufficient database connection information', mfilename)
0040 else
0041   for iidx = 1:nidx
0042     if isfield(params.(param_fld_names{idxs(iidx)}), 'database')
0043       conn_id = params.(param_fld_names{idxs(iidx)}).conn_id;
0044       continue
0045     end
0046   end
0047 end
0048 
0049 % Apply any specified filtering to the input data
0050 if isfield(params,'filt')
0051   fprintf('Applying filtering criteria\n')
0052   data_st = ensemble_filter(data_st, params.filt);
0053 end
0054 
0055 % Generate the compqid for each row in the data if necessary.  We want to do
0056 % this before filtering, since some of the filters might be specified in terms
0057 % of the composite question ID (compqid).  Because we need these anyway, we
0058 % might as well make sure we have them now.
0059 if ~isfield(incol,'compqid')
0060   data_st.vars{end+1} = 'compqid';
0061   data_st.data{end+1} = make_compqid(data_st.data{incol.question_id}, ...
0062       data_st.data{incol.subquestion});
0063   incol = set_var_col_const(data_st.vars);
0064 end
0065 
0066 % Check to see if we need to prune the dataset further to contain only the
0067 % composite question IDs that we want.
0068 unique_src_compqid = unique(data_st.data{incol.compqid}); % existing compqids
0069 if isfield(params,'compqids') && ~isempty(params.compqids)
0070   if ~(all(ismember(unique_src_compqid,params.compqids)))
0071     filt = [];  % clear the filter structure
0072     filt.include.all.compqid = params.compqids;
0073     data_st = ensemble_filter(data_st,filt);
0074   end
0075   compqids = params.compqids;
0076 else
0077   compqids = unique(data_st.data{incol.compqid});
0078 end
0079 
0080 % Get final list of compqids
0081 nqid = length(compqids);
0082 
0083 % Extract the question info and attach it to the output metadata
0084 qinfo = mysql_extract_metadata('table','question', ...
0085     'question_id',unique(fix(compqids)),'conn_id',conn_id);
0086 qinfo = qinfo(ismember([qinfo.compqid],compqids));
0087 an_st.meta.question = qinfo;
0088 
0089 % Make list of variables to extract
0090 if isfield(params,'extract_vars') && ~isempty(params.extract_vars)
0091   extract_vars = params.extract_vars;
0092 else
0093   extract_vars = data_st.vars;
0094 end
0095 
0096 % get column indices into the source data
0097 [~,src_col] = ismember(extract_vars, data_st.vars);
0098 
0099 for iqid = 1:nqid
0100   newVar = sprintf('s%d_%02d',qinfo(iqid).question_id,qinfo(iqid).subquestion);
0101   
0102   % Rename the default compqid string if there is a compqid to variable
0103   % name mapping specified
0104   if isfield(params,'var_name_map') && isfield(params.var_name_map,newVar)
0105     newVar = params.var_name_map.(newVar);
0106   end
0107   an_st.vars{iqid} = newVar;
0108 
0109   % Initialize the data structure that will be assigned to this variable
0110   curr_st = ensemble_init_data_struct;
0111   curr_st.type = 'raw_data';
0112   curr_st.vars = extract_vars;
0113   curr_st_cols = set_var_col_const(curr_st.vars);
0114     
0115   % Filter the data for this particular compqid
0116   clear filt
0117   filt.include.all.compqid = qinfo(iqid).compqid;
0118   tmpdata = ensemble_filter(data_st,filt);
0119 
0120   % Retain only the source columns we want
0121   curr_st.data = tmpdata.data(src_col);  
0122 
0123   an_st.data{iqid} = curr_st;
0124 end % for iqid
0125 
0126 end

Generated on Sun 19-May-2019 04:00:51 by m2html © 2003