Home > database > ensemble_copy_stimuli2dir.m

ensemble_copy_stimuli2dir

PURPOSE ^

Copies stimuli specified either in data_st or in the experiment specified

SYNOPSIS ^

function ensemble_copy_stimuli2dir(varargin)

DESCRIPTION ^

 Copies stimuli specified either in data_st or in the experiment specified
 in params to a desired output directory.

 ensemble_copy_stimuli2dir(varargin)

 Takes a series of tag/value pairs. The following tags are recognized:

 REQUIRES either
 {'experiment_title', 'experiment_name', 'experiment'} - the value in the
      experiment_title field of the experiment table
 OR
 {'stimulus_id'} - an array of stimulus IDs to export
 OR
 {'response_table'} - name of response table to pull stimulus IDs from
 {'conn_id'} - REQUIRED - an active connection ID to the database

 OPTIONAL
 {'filt','filter'} - a structure compatible with ensemble_filter() 
 {'outpath'} - path where stimuli should be copied to. Default is
      './stimuli'

 This script is useful for sharing stimuli used in an experiment with
 other people.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function ensemble_copy_stimuli2dir(varargin)
0002 % Copies stimuli specified either in data_st or in the experiment specified
0003 % in params to a desired output directory.
0004 %
0005 % ensemble_copy_stimuli2dir(varargin)
0006 %
0007 % Takes a series of tag/value pairs. The following tags are recognized:
0008 %
0009 % REQUIRES either
0010 % {'experiment_title', 'experiment_name', 'experiment'} - the value in the
0011 %      experiment_title field of the experiment table
0012 % OR
0013 % {'stimulus_id'} - an array of stimulus IDs to export
0014 % OR
0015 % {'response_table'} - name of response table to pull stimulus IDs from
0016 % {'conn_id'} - REQUIRED - an active connection ID to the database
0017 %
0018 % OPTIONAL
0019 % {'filt','filter'} - a structure compatible with ensemble_filter()
0020 % {'outpath'} - path where stimuli should be copied to. Default is
0021 %      './stimuli'
0022 %
0023 % This script is useful for sharing stimuli used in an experiment with
0024 % other people.
0025 
0026 % 09Jan2014 Petr Janata
0027 
0028 % Initialize required arguments
0029 expname = '';
0030 resptbl = '';
0031 stimids = [];
0032 conn_id = [];
0033 outroot = './stimuli';
0034 stimroot = '';
0035 filt_st = [];
0036 
0037 % Process input arguments
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 % switch currArg
0065 end % for iarg
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 % We need a list of stimulus IDs
0080 if isempty(stimids)
0081   % We need a response table name
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 % if isempty(resptbl)
0088   
0089   % Get the stimuli from the response table
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 % if isempty(stimids)
0095 
0096 % Eliminate any NaNs from the stimids and make sure they are unique
0097 stimids = unique(stimids(~isnan(stimids)));
0098 
0099 % Get the stimulus info
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 % Convert the stimMeta tree to an Ensemble data structure
0106 stim_st = ensemble_tree2datastruct(stimMeta,struct('ignore_reserved_names',true));
0107 
0108 % Perform any desired filtering
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 % Make sure the output directory exists
0116 check_dir(outroot);
0117 
0118 % Copy the stimuli
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}; % get the current stimulus path
0124   
0125   [~,destStub,ext] = fileparts(srcLoc); % get the current stimulus name
0126   
0127   destLoc = fullfile(outroot,[destStub ext]); % figure out where it's going
0128   
0129   % Copy it
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 % for istim
0139 fprintf('\nDone copying files!\n');
0140 
0141 return

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