Home > database > ensemble_print_metadata.m

ensemble_print_metadata

PURPOSE ^

Displays metadata obtained from mysql_extract_metadata

SYNOPSIS ^

function ensemble_print_metadata(data_str,params)

DESCRIPTION ^

 Displays metadata obtained from mysql_extract_metadata


 ensemble_print_metadata(data_str,params)

 data_str is a data structure returned from mysql_extract_metadata
 either at the experiment, form, or question level

 params is a structure with display and tables subfields
 params may be left out, in which case default values will be used
 possible subfields of params.tables.report are write2file, fname, and columns

    (e.g. params.report.tables.write2file = 1;
          params.report.tables.fname      = '/tmp/mymeta.csv';
          params.report.tables.columns =
          {{'experiment_id','experiment_title'},...
           {'form_id','form_name'},...
           {'question_id','subquestion','question_text','enum_values'}};
          
 Each cell of the 'columns' field is a cell array of strings, which
 correspond to the fields of each table to print to screen
 or file. They should all be fieldnames of the corresponding
 table(s) with one exception: the string 'answer_format' can be
 used instead of 'enum_values'. Either value produces the same
 output: a column with an "Answer Format" heading that either has
 the enum values for a particular question, or the answer type
 (e.g. text,integer,double, etc.).

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function ensemble_print_metadata(data_str,params)
0002 % Displays metadata obtained from mysql_extract_metadata
0003 %
0004 %
0005 % ensemble_print_metadata(data_str,params)
0006 %
0007 % data_str is a data structure returned from mysql_extract_metadata
0008 % either at the experiment, form, or question level
0009 %
0010 % params is a structure with display and tables subfields
0011 % params may be left out, in which case default values will be used
0012 % possible subfields of params.tables.report are write2file, fname, and columns
0013 %
0014 %    (e.g. params.report.tables.write2file = 1;
0015 %          params.report.tables.fname      = '/tmp/mymeta.csv';
0016 %          params.report.tables.columns =
0017 %          {{'experiment_id','experiment_title'},...
0018 %           {'form_id','form_name'},...
0019 %           {'question_id','subquestion','question_text','enum_values'}};
0020 %
0021 % Each cell of the 'columns' field is a cell array of strings, which
0022 % correspond to the fields of each table to print to screen
0023 % or file. They should all be fieldnames of the corresponding
0024 % table(s) with one exception: the string 'answer_format' can be
0025 % used instead of 'enum_values'. Either value produces the same
0026 % output: a column with an "Answer Format" heading that either has
0027 % the enum values for a particular question, or the answer type
0028 % (e.g. text,integer,double, etc.).
0029 
0030 
0031 
0032 
0033 DEFAULT_EXPERIMENT_META = {'experiment_id','experiment_title','response_table'};
0034 DEFAULT_FORM_META = {'form_id','form_name','header'};
0035 DEFAULT_QUESTION_META = {'question_id','subquestion','question_text','answer_format'};
0036 
0037 %check if write2file parameter was set. If not, then print to screen;
0038 try write2file = params.report.tables.write2file; catch write2file = 0, end
0039 %if write2file was set, open the file and set the fid
0040 if write2file
0041   try fid = params.report.tables.fid;
0042   catch
0043     fid = fopen(params.report.tables.fname,'wt');
0044     params.report.tables.fid = fid;
0045     if fid == -1
0046       error(sprintf('Problem opening logfile: %s\n',params.report.tables.fname))
0047     end
0048     fprintf('Writing tables to file: %s\n', ...
0049         params.report.tables.fname);
0050     closeFid = 1;
0051   end
0052 else
0053   fid = 1;
0054   params.report.tables.fid = fid;
0055 end
0056 
0057 %check if column names were set. If not, set to default field names
0058 if(~isfield(params.report.tables,'columns') | isempty(params.report.tables.columns))
0059   if(isfield(data_str(1),'experiment_id'))
0060     params.report.tables.columns = {DEFAULT_EXPERIMENT_META,DEFAULT_FORM_META, DEFAULT_QUESTION_META};
0061   elseif(isfield(data_str(1),'form_id'))
0062     params.report.tables.columns = {DEFAULT_FORM_META, DEFAULT_QUESTION_META};
0063   elseif(isfield(data_str(1),'question_id'))
0064     params.report.tables.columns = DEFAULT_QUESTION_META;
0065   end
0066 end
0067 
0068 %check for table type by the primary key used. There is no table
0069 %name in the meta structure, so currently, this is the only way to
0070 %verify the table type. Printout format depends on table type.
0071 if(isfield(data_str(1),'experiment_id'))
0072   nForms = length(data_str.form);
0073   fprintf(fid,'EXPERIMENT TITLE "%s"\n',data_str.experiment_title);
0074   fprintf(fid,'%d FORMS\n\n',nForms);
0075   %call the subfunction to print the table
0076   ensemble_print_table(data_str,params);
0077   %set the childParams structure, which is the same except the
0078   %column names should be in the first element of the cell array
0079   childParams = params;
0080   childParams.report.tables.columns = params.report.tables.columns(2:end);
0081 
0082   for iForm = 1:nForms
0083     ensemble_print_metadata(data_str.form(iForm),childParams);
0084   end
0085   
0086 elseif(isfield(data_str(1),'form_id'))
0087 
0088   nQuestions = length(data_str.question);
0089   if(nQuestions == 1)
0090     qString = 'question';
0091   else
0092     qString = 'questions';
0093   end
0094 
0095   fprintf(fid,'\nForm Name "%s"\n',data_str.form_name);
0096   fprintf(fid,'%d %s\n\n',nQuestions,qString);
0097   ensemble_print_table(data_str,params);
0098   
0099   if(nQuestions > 0)
0100     fprintf(fid,'\n');
0101     childParams = params;
0102     childParams.report.tables.columns = params.report.tables.columns(2:end);
0103     ensemble_print_metadata(data_str.question,childParams);
0104   end
0105   
0106 elseif(isfield(data_str(1),'question_id'))
0107   ensemble_print_table(data_str,params);
0108 end
0109 
0110 if(write2file & exist('closeFid','var'))
0111   fclose(fid);
0112 end
0113 
0114 return
0115 
0116 
0117 
0118 
0119 function ensemble_print_table(data_st,params)
0120 %
0121 
0122 fid = params.report.tables.fid;
0123 
0124 %ASCII codes for LF, CR, and TAB
0125 %any occurrence of these need to be removed from string
0126 %fields. If these occur in any field in the database, they
0127 %are only a product of typing style during data entry, since
0128 %Ensemble only prints carriage returns and tabs in HTML format.
0129 LF_ASCII = 10;
0130 CR_ASCII = 13;
0131 TAB_ASCII = 9;
0132 
0133 %these variables were used in an attempt to provide nicer
0134 %alignment of columns, but this is better
0135 %achieved by importing the tab delimited text file into Excel
0136 %defaultSpacer=20;
0137 %useSpacer = defaultSpacer;
0138 
0139 
0140 %set the report fieldnames to the first cell (which is
0141 %itself a cell array of strings. If only printing one table
0142 %then allow bypassing the cell nesting.
0143 if(iscell(params.report.tables.columns{1}))
0144   displayColumns = params.report.tables.columns{1};
0145 else
0146   displayColumns = params.report.tables.columns;
0147 end
0148 
0149 %the only allowed argument that is not a fieldname is
0150 %'answer_format', which lists the answer type. If used, this argument
0151 %will first check enum_values, so we'll treat this as an
0152 %'enum_values' argument
0153 [answerFormatIsListed,answerFormatLoc] = ismember('answer_format',displayColumns);
0154 if(answerFormatIsListed)
0155   displayColumns{answerFormatLoc} = 'enum_values';
0156 end
0157 
0158 numColumns = length(displayColumns);
0159 numRecords = length(data_st);
0160 
0161 %The first column of this 2-D cell array stores actual fieldnames
0162 %(also used as tags to the 'columns' report field). The second
0163 %column stores the string that is used to reformat the column name
0164 %for nicer printout.
0165 headingReformat = { 'experiment_id','Experiment ID';
0166             'start_date','Start Date';
0167             'experiment_title','Experiment Title';
0168             'experiment_description','Experiment Description';
0169             'response_table','Response Table';
0170             'irb_id','IRB ID';
0171             'end_date','End Date';
0172             'language','Language';
0173             'locked','Locked';
0174             'form_id','Form ID';
0175             'form_name','Form Name';
0176             'form_category','Form Category';
0177             'header','Header';
0178             'footer','Footer';
0179             'version','Version';
0180             'locked','Locked';
0181             'condition','Condition';
0182             'condition_matlab','Condition Matlab';
0183             'visit_once','Visit Once';
0184             'compqid','Composite Question ID';
0185             'question_id','Question ID';
0186             'question_text','Question Text';
0187             'question_category','Question Category';
0188             'heading_format','Heading Format';
0189             'locked','Locked';
0190             'data_format_id','Data Format ID';
0191             'type','Type';
0192             'enum_values','Answer Format';
0193             'subquestion','Subquestion';
0194             'heading','Heading';
0195             'range','Range';
0196             'default','Default';
0197             'html_field_type','HTML Field Type';
0198             'required','Required'};
0199             
0200 
0201 [tf,colRftIdx] = ismember(displayColumns,{headingReformat{:,1}}); 
0202 
0203 for iColumn = 1:numColumns
0204   columnName = displayColumns{iColumn};
0205   columnHeading = headingReformat{colRftIdx(iColumn),2};
0206   fprintf(fid,'%s',columnHeading);
0207   
0208   if(iColumn ~= numColumns)
0209     fprintf(fid,'\t');
0210   end
0211 end
0212 
0213 fprintf(fid,'\n');
0214 
0215 for iRecord = 1:numRecords
0216   for iColumn = 1:numColumns
0217     
0218     %if the display column is not a valid field, then throw an
0219     %error message
0220     if(~isfield(data_st(iRecord),displayColumns{iColumn}))
0221       error(sprintf('Field %s is invalid for specified table structure', displayColumns{iColumn}));
0222     end
0223     
0224     %get the field value. This will need to be reformatted during
0225     %printout, depending on its type
0226     fieldVal = getfield(data_st(iRecord),displayColumns{iColumn});
0227     %get rid of any carriage return, line feed, and tab characters
0228     %in the field for clean tab delimited output
0229     if (ischar(fieldVal) | iscell(fieldVal))
0230       fieldVal = strrep(fieldVal,'\r','');
0231       fieldVal = strrep(fieldVal,'\t','');
0232       fieldVal = strrep(fieldVal,'\n','');
0233     end
0234     
0235     %check the field value type and format accordingly
0236     if(isnumeric(fieldVal))
0237       %spacer = useSpacer-length(headingReformat{colRftIdx(iColumn),2})+1;
0238       spacer = [];
0239       fmtString = ['%' num2str(spacer) 'd'];
0240       fprintf(fid,fmtString,fieldVal);
0241       
0242       
0243     elseif(strcmp(displayColumns{iColumn},'enum_values'))
0244       
0245       
0246       if(~isempty(fieldVal))
0247     for iCell = 1:length(fieldVal)
0248       if(iCell == 1)
0249         %spacer = useSpacer-length(headingReformat{colRftIdx(iColumn),2})+1;
0250         spacer = [];
0251         fmtString = ['%' num2str(spacer) 'd=%s,'];
0252       elseif(iCell == length(fieldVal))
0253         fmtString = '%d=%s';
0254       else
0255         fmtString = '%d=%s, ';
0256       end
0257       
0258       fprintf(fid,fmtString,iCell,fieldVal{iCell});
0259     end
0260       
0261       else
0262     
0263     dataType = getfield(data_st(iRecord),'type');
0264     switch dataType
0265      case {'int16','int32','int64'}
0266       fieldReport = 'Any Integer';
0267      case 'double'
0268       fieldReport = 'Any Double';
0269      case {'varchar','text'}
0270       fieldReport = 'Any String';
0271      case 'date'
0272       fieldReport = 'Any valid date';
0273      case 'year'
0274       fieldReport = 'Any valid year';
0275      case 'null'
0276       fieldReport = 'None';
0277     end
0278     
0279     %spacer = useSpacer-length(headingReformat{colRftIdx(iColumn),2})+1;
0280     spacer = [];
0281     fmtString = ['%' num2str(spacer) 's\t'];
0282  
0283     fprintf(fid,fmtString,fieldReport);
0284     
0285       end
0286     
0287     elseif(ischar(fieldVal))
0288       
0289       %remove CR, LF, and TABS from the string (could not find a
0290       %way to use string matching functions for this)
0291       fieldVal(find(fieldVal == CR_ASCII | fieldVal == LF_ASCII | fieldVal == TAB_ASCII)) = [];
0292       
0293       %spacer = useSpacer-length(headingReformat{colRftIdx(iColumn),2})+length(fieldVal);
0294       spacer = [];
0295       fmtString = ['%' num2str(spacer) 's'];
0296       fprintf(fid,fmtString,fieldVal);
0297 
0298     
0299     end
0300     
0301     if(iColumn ~= numColumns)
0302       fprintf(fid,'\t');
0303     end
0304     
0305   end
0306   
0307   fprintf(fid,'\n');
0308 end
0309 
0310 
0311 
0312 
0313 return

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003