0001 function ensemble_copy_stimuli2dir(varargin)
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 expname = '';
0030 resptbl = '';
0031 stimids = [];
0032 conn_id = [];
0033 outroot = './stimuli';
0034 stimroot = '';
0035 filt_st = [];
0036
0037
0038 for iarg = 1:2:nargin
0039 currArg = varargin{iarg};
0040 switch currArg
0041 case {'experiment','experiment_name','experiment_title'}
0042 expname = varargin{iarg+1};
0043
0044 case {'conn_id'}
0045 conn_id = varargin{iarg+1};
0046
0047 case {'stimulus_id','stimids'}
0048 stimids = varargin{iarg+1};
0049
0050 case {'response_table'}
0051 resptbl = varargin{iarg+1};
0052
0053 case {'filt','filter'}
0054 filt_st = varargin{iarg+1};
0055
0056 case {'stimroot'}
0057 stimroot = varargin{iarg+1};
0058
0059 case {'outpath','outdir','outroot'}
0060 outroot = varargin{iarg+1};
0061
0062 otherwise
0063 fprintf('Input argument (%s) not recognized\n', currArg);
0064 end
0065 end
0066
0067 if isempty(stimids) && isempty(resptbl) && isempty(expname)
0068 error('Must specify one of the following: stimulus_id, response_table, experiment_name')
0069 end
0070
0071 if isempty(conn_id) || mysql_check_conn(conn_id)
0072 error('Must specify conn_id for MySQL database connection. The conn_id must be active')
0073 end
0074
0075 if isempty(stimroot)
0076 error('Must specify root location of stimuli (stimroot)')
0077 end
0078
0079
0080 if isempty(stimids)
0081
0082 if isempty(resptbl)
0083 fprintf('Fetching response_table name from experiment: %s\n', expname);
0084 mysql_str = sprintf('SELECT response_table FROM experiment WHERE experiment_title="%s";', expname);
0085 resptbl = mysql(conn_id, mysql_str);
0086 resptbl = resptbl{1};
0087 end
0088
0089
0090 fprintf('Fetching distinct stimulus IDs from response_table: %s\n', resptbl);
0091 mysql_str = sprintf('SELECT DISTINCT stimulus_id FROM %s;', resptbl);
0092 stimids = mysql(conn_id, mysql_str);
0093
0094 end
0095
0096
0097 stimids = unique(stimids(~isnan(stimids)));
0098
0099
0100 fprintf('Extracting stimulus metadata for %d stimuli\n', length(stimids));
0101 stimMeta = mysql_extract_metadata('table','stimulus', ...
0102 'stimulus_id',stimids, ...
0103 'conn_id', conn_id);
0104
0105
0106 stim_st = ensemble_tree2datastruct(stimMeta,struct('ignore_reserved_names',true));
0107
0108
0109 if ~isempty(filt_st)
0110 stim_st = ensemble_filter(stim_st, filt_st);
0111 end
0112
0113 scols = set_var_col_const(stim_st.vars);
0114
0115
0116 check_dir(outroot);
0117
0118
0119 srcs = strcat(fullfile(stimroot,filesep), stim_st.data{scols.location});
0120 nstim = size(srcs,1);
0121 fprintf('Copying %d files\n', nstim);
0122 for istim = 1:nstim
0123 srcLoc = srcs{istim};
0124
0125 [~,destStub,ext] = fileparts(srcLoc);
0126
0127 destLoc = fullfile(outroot,[destStub ext]);
0128
0129
0130 unix_str = sprintf('cp %s %s', srcLoc, destLoc);
0131 status = unix(unix_str);
0132 if status
0133 error('Problem executing: %s', unix_str)
0134 else
0135 fprintf('.');
0136 end
0137
0138 end
0139 fprintf('\nDone copying files!\n');
0140
0141 return