Home > database > stim_ids_from_resp_tbl.m

stim_ids_from_resp_tbl

PURPOSE ^

returns all unique stimulus ids from a given response table(s)

SYNOPSIS ^

function [data,vars] = stim_ids_from_resp_tbl(params)

DESCRIPTION ^

 returns all unique stimulus ids from a given response table(s)
 
   [data,vars] = stim_ids_from_resp_tbl(params)
 
 REQUIRED
   params.stim_ids_from_resp_tbl.tables - struct array identifying
       response tables from which to collect stimulus ids, as well as
       optional filtering criteria to be applied to the data from each
       table
       .name - response table name
       .filt - filtering criteria to be applied to the given table
   params.mysql (params.ensemble) - database connection information
       .host, .database, .conn_id
 
 FIXME: uses the slightly unconventional output format of
 mysql_get_stim_by_attribute, with two returns: data and vars, as opposed
 to one return of an ensemble data struct containing data and vars

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [data,vars] = stim_ids_from_resp_tbl(params)
0002 
0003 % returns all unique stimulus ids from a given response table(s)
0004 %
0005 %   [data,vars] = stim_ids_from_resp_tbl(params)
0006 %
0007 % REQUIRED
0008 %   params.stim_ids_from_resp_tbl.tables - struct array identifying
0009 %       response tables from which to collect stimulus ids, as well as
0010 %       optional filtering criteria to be applied to the data from each
0011 %       table
0012 %       .name - response table name
0013 %       .filt - filtering criteria to be applied to the given table
0014 %   params.mysql (params.ensemble) - database connection information
0015 %       .host, .database, .conn_id
0016 %
0017 % FIXME: uses the slightly unconventional output format of
0018 % mysql_get_stim_by_attribute, with two returns: data and vars, as opposed
0019 % to one return of an ensemble data struct containing data and vars
0020 %
0021 
0022 % FB 2009.05.11
0023 % 06/15/10 PJ - sanitized connection handling
0024 
0025 % initialize vars
0026 data{1} = nan(0);
0027 vars = {'stimulus__stimulus_id'};
0028 
0029 % initialize mysql struct, check mysql connection
0030 if isfield(params,'mysql')
0031   m = params.mysql;
0032 elseif isfield(params,'ensemble')
0033   m = params.ensemble;
0034 else
0035   warning('no database connection info provided, assuming ensemble_main');
0036   m = struct();
0037 end
0038 
0039 if ~isfield(m,'conn_id') || mysql(m.conn_id,'status')
0040   if ~all(isfield(m,{'host','database','user','passwd'}))
0041     error('%s: No valid connection or insufficient information to establish one', mfilename);
0042   else
0043     m.conn_id = 6;
0044     mysql_make_conn(m);
0045     tmp_conn_id = 1;
0046   end
0047 else
0048   tmp_conn_id = 0;
0049 end
0050 
0051 % get response table information
0052 if ~isfield(params,'stim_ids_from_resp_tbl') || ...
0053         ~isfield(params.stim_ids_from_resp_tbl,'tables') || ...
0054         ~isstruct(params.stim_ids_from_resp_tbl.tables) || ...
0055         ~isfield(params.stim_ids_from_resp_tbl.tables,'name')
0056   error('required parameters not found\n');
0057 else
0058   t = params.stim_ids_from_resp_tbl.tables;
0059 end
0060 
0061 % iterate over tables, extract unique stimulus_ids
0062 for i = 1:length(t)
0063 
0064   % get data from the given response table
0065   tbl = t(i).name;
0066   tdata = ensemble_init_data_struct();
0067   tbl_desc = mysql_describe_table(tbl,m.conn_id);
0068   tdata.vars = transpose(tbl_desc.flds);
0069   tcol = set_var_col_const(tdata.vars);
0070   
0071   mysql_str = sprintf('SELECT %s FROM %s WHERE stimulus_id IS NOT NULL',...
0072       cell2str(tdata.vars,', '),tbl);
0073 
0074   [tdata.data{1:length(tdata.vars)}] = mysql(m.conn_id, mysql_str);
0075   
0076   % filter this data?
0077   if isfield(t(i),'filt') && isstruct(t(i).filt)
0078     tdata = ensemble_filter(tdata,t(i).filt);
0079   end
0080   
0081   % get the unique stimulus IDs
0082   b = unique(tdata.data{tcol.stimulus_id});
0083   data{1} = union(b,data{1});
0084 end
0085 
0086 if tmp_conn_id
0087   mysql(m.conn_id,'close');
0088   m.conn_id = [];
0089 end

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