Home > functions > internal > dsCheckCovary.m

dsCheckCovary

PURPOSE ^

CHECKCOVARY - TODO I assume this checks if any varied parameters are covaried?

SYNOPSIS ^

function [effective_vary_indices, linked_indices] = dsCheckCovary(vary_lengths, vary_params, varargin)

DESCRIPTION ^

CHECKCOVARY - TODO I assume this checks if any varied parameters are covaried?

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [effective_vary_indices, linked_indices] = dsCheckCovary(vary_lengths, vary_params, varargin)
0002   %CHECKCOVARY - TODO I assume this checks if any varied parameters are covaried?
0003  
0004   %% localfn output
0005   if ~nargin
0006       effective_vary_indices = localfunctions; % output var name specific to this fn
0007       return
0008   end
0009   
0010   %% auto_gen_test_data_flag argin
0011   options = dsCheckOptions(varargin,{'auto_gen_test_data_flag',0,{0,1}},false);
0012   if options.auto_gen_test_data_flag
0013       varargs = varargin;
0014       varargs{find(strcmp(varargs, 'auto_gen_test_data_flag'))+1} = 0;
0015       varargs(end+1:end+2) = {'unit_test_flag',1};
0016       argin = [{vary_lengths}, {data_length}, varargs]; % specific to this function
0017   end
0018   
0019   %% Function start.
0020   data_length = size(vary_params, 1);
0021 
0022   % Remove any vary statements used to set parameter values.
0023   non_singleton_vary_indices = vary_lengths > 1;
0024   non_singleton_vary_lengths = vary_lengths(non_singleton_vary_indices);
0025   non_singleton_vary_params = vary_params(:, non_singleton_vary_indices);
0026   n_varied = length(non_singleton_vary_lengths);
0027 
0028   %% Check all combinations of varied parameters to see if the product of the number
0029   % of their unique values describes the amount of data simulated.
0030   full_rank_choices = {};
0031   index = 1;
0032 
0033   for n_chosen = 1:n_varied
0034     % Check all pairs, then all triplets...
0035     choices = nchoosek(1:n_varied, n_chosen);
0036     n_choices = nchoosek(n_varied, n_chosen);
0037 
0038     % For each subset of varied parameters, see if the product of the numbers of
0039     % unique parameters equals data_length.
0040     for choice = 1:n_choices
0041       estimated_data_length = prod(non_singleton_vary_lengths(choices(choice, :)));
0042       if estimated_data_length == data_length
0043         full_rank_choices{index} = choices(choice, :);
0044         index = index + 1;
0045       end
0046     end
0047 
0048   end
0049 
0050   linked_indices = {};
0051   
0052   %% Parse the results of finding "full rank" choices.
0053   if isempty(full_rank_choices)
0054     % No combination of varied parameters equals total data length,
0055     % so treat data as linear.
0056     linked_indices{1} = 1:n_varied;
0057 
0058   else
0059     % If one or more combinations of varied parameters describes the total data length,
0060     % we need to link covaried parameters.
0061     no_full_rank_choices = length(full_rank_choices);
0062 
0063     % Create a matrix in which each row represents a full rank choice of varied parameters.
0064     full_rank_participation = zeros(no_full_rank_choices, n_varied);
0065     for frc = 1:no_full_rank_choices
0066       full_rank_participation(frc, full_rank_choices{frc}) = 1;
0067     end
0068     
0069     % If a certain parameter is not involved in any "full rank" product,
0070     % consider it linked.
0071     index = 1;
0072     non_participating_parameters = find(all(full_rank_participation == 0, 1));
0073     if ~isempty(non_participating_parameters)
0074         linked_indices{index} = [0 non_participating_parameters];
0075         index = index + 1;
0076     end
0077 
0078     if no_full_rank_choices > 1
0079         %%
0080         % If more than one combination of varied parameters describes the total
0081         % data length, we have to figure out which of these varied
0082         % parameters is co-varied.
0083         param_indices = find(sum(full_rank_participation, 1) > 0);
0084         
0085         iteration_number = 1;
0086         while length(param_indices) > 1
0087             [linked_set, param_indices] = find_linked_params(param_indices, non_singleton_vary_params, varargin{:});
0088             
0089             if ~isempty(linked_set)
0090                 linked_indices{index} = linked_set;
0091                 index = index + 1;
0092             end
0093             
0094             iteration_number = iteration_number + 1;
0095             if iteration_number > 10
0096                 return
0097             end
0098         end
0099         
0100     end
0101 
0102   end
0103 
0104   % Remove linked sets of parameters from effective_vary_lengths.
0105   marked_for_removal = zeros(size(non_singleton_vary_lengths));
0106   for l = 1:length(linked_indices)
0107     marked_for_removal(linked_indices{l}(2:end)) = 1;
0108   end
0109   % effective_vary_lengths = non_singleton_vary_lengths;
0110   % effective_vary_lengths(logical(marked_for_removal)) = [];
0111   effective_non_singleton_vary_indices = ~marked_for_removal;
0112   
0113   effective_vary_indices = false(size(vary_lengths));
0114   
0115   effective_vary_indices(non_singleton_vary_indices) = logical(effective_non_singleton_vary_indices);
0116   
0117   %% auto_gen_test_data_flag argout
0118   if options.auto_gen_test_data_flag
0119       argout = {effective_vary_lengths, linked_indices}; % specific to this function
0120       
0121       dsUnitSaveAutoGenTestData(argin, argout);
0122   end
0123 
0124 end
0125 
0126 function [linked_indices, non_linked_indices] = find_linked_params(param_indices, vary_params, varargin)
0127 
0128 %% auto_gen_test_data_flag argin
0129 options = dsCheckOptions(varargin,{'auto_gen_test_data_flag',0,{0,1}},false);
0130 if options.auto_gen_test_data_flag
0131   varargs = varargin;
0132   varargs{find(strcmp(varargs, 'auto_gen_test_data_flag'))+1} = 0;
0133   varargs(end+1:end+2) = {'unit_test_flag',1};
0134   argin = [{param_indices},{vary_params}, varargs];
0135 end
0136 
0137 param1_index = param_indices(1);
0138 
0139 if iscellstr(vary_params)
0140     params_at_value1 = vary_params(strcmp(vary_params(:, param1_index),vary_params(1, param1_index)), :);
0141     
0142 %params_at_value1 = vary_params(cellfun(@(x) isequal(x, vary_params(1, param1_index)),vary_params(:, param1_index)), :);
0143 elseif isnumeric(vary_params)
0144     params_at_value1 = vary_params(vary_params(:, param1_index) == vary_params(1, param1_index), :);
0145 elseif iscellnum(vary_params)
0146     vary_params2 = cell2mat(vary_params);
0147     params_at_value1 = vary_params(vary_params2(:, param1_index) == vary_params2(1, param1_index), :);
0148 else
0149     try
0150         params_at_value1 = vary_params(vary_params(:, param1_index) == vary_params(1, param1_index), :);
0151     catch
0152         error('case not implemented. vary_params must all cell array of chars of all numeric');
0153     end
0154 end
0155 
0156 no_values_at_value1 = nan(size(param_indices));
0157 
0158 no_values_at_value1(1) = 1;
0159 
0160 for p = 2:length(param_indices)
0161   
0162   param_index = param_indices(p);
0163   
0164   no_values_at_value1(p) = length(unique(params_at_value1(:, param_index)));
0165   
0166 end
0167 
0168 linked_indices = param_indices(no_values_at_value1 == 1);
0169 
0170 non_linked_indices = param_indices(no_values_at_value1 > 1);
0171 
0172 
0173 %% auto_gen_test_data_flag argout
0174 if options.auto_gen_test_data_flag
0175   argout = {linked_indices, non_linked_indices}; % specific to this function
0176   
0177   dsUnitSaveAutoGenTestDataLocalFn(argin, argout); % localfn
0178 end
0179 
0180 end

Generated on Tue 12-Dec-2017 11:32:10 by m2html © 2005