Home > functions > internal > dsAutoMergeVarieds.m

dsAutoMergeVarieds

PURPOSE ^

[data_new, variedname_merged, varied_vals ] = mergeVarieds(data,varied_fields)

SYNOPSIS ^

function [data, variedname_merged, varied_vals ] = dsAutoMergeVarieds(data,strategy,verbose_on,varargin)

DESCRIPTION ^

 [data_new, variedname_merged, varied_vals ] = mergeVarieds(data,varied_fields)

 Need to write this description.
 
 % % % % % % % % % % % % % % % % % % % % 
 
 A1=[1,3,1];A2=[1,5,2]; A3=[2,3,4];
 B1=[6,2,9];B2=[4,5,1]; B3=[1,2,5];
 M = [A1 B1 ; A2 B1 ; A3 B1 ; 
 A1 B2; A2 B2; A3 B2;
 A1 B3; A2 B3; A3 B3];

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 
0002 function [data, variedname_merged, varied_vals ] = dsAutoMergeVarieds(data,strategy,verbose_on,varargin)
0003 % [data_new, variedname_merged, varied_vals ] = mergeVarieds(data,varied_fields)
0004 %
0005 % Need to write this description.
0006 %
0007 % % % % % % % % % % % % % % % % % % % % %
0008 %
0009 % A1=[1,3,1];A2=[1,5,2]; A3=[2,3,4];
0010 % B1=[6,2,9];B2=[4,5,1]; B3=[1,2,5];
0011 % M = [A1 B1 ; A2 B1 ; A3 B1 ;
0012 % A1 B2; A2 B2; A3 B2;
0013 % A1 B3; A2 B3; A3 B3];
0014 
0015 % % % % % % % % % % % % % % % % % % % % %
0016 
0017 % auto_gen_test_data_flag argin
0018 options = dsCheckOptions(varargin,{'auto_gen_test_data_flag',0,{0,1}},false);
0019 if options.auto_gen_test_data_flag
0020     varargs = varargin;
0021     varargs{find(strcmp(varargs, 'auto_gen_test_data_flag'))+1} = 0;
0022     varargs(end+1:end+2) = {'unit_test_flag',1};
0023 end
0024 
0025     maxiter = 10000;
0026     success_thresh = 0.5;
0027     variedname_merged = {};
0028     varied_vals = {};
0029     if nargin < 3; verbose_on = false; end
0030     
0031 % % % % % % % % % % % %
0032 % Add in section for pre-screening for linearly depndence as well
0033 % % % % % % % % % % % %
0034 
0035 % Build vary_parms - cell array of varied params (sims x parameters)
0036 N = length(data);
0037 vary_labels = data(1).varied; % data(1).simulator_options.vary;
0038 Nvarieds = length(vary_labels);
0039 for i = 1:N
0040     for j = 1:Nvarieds
0041         vary_params{i,j} = data(i).(vary_labels{j});
0042     end
0043 end
0044 
0045 % Convert everything to strings for easier analysis
0046 vary_params = all2str(vary_params);
0047 
0048 percent_full = getpercentfull(vary_params);
0049 if percent_full > success_thresh; return; end
0050 
0051 [cols2merge] = find_shortest_mergelist(vary_params,success_thresh,maxiter);  %
0052 
0053 if isempty(cols2merge); cols2merge = 1:size(vary_params,2); end
0054 
0055 % Now, see if it is possible to subdivide the merged list
0056 cols_list{1} = cols2merge;
0057 cols_list = subdivide_ind_list(vary_params,cols_list,success_thresh,maxiter);
0058 
0059 % Merge eached linked_ind into a single varied statement
0060 vary_labels = data(1).varied; % data(1).simulator_options.vary;
0061 Nlinked = length(cols_list);
0062 variedname_merged = cell(1,Nlinked);
0063 varied_vals = cell(1,Nlinked);
0064 for j = 1:Nlinked
0065     [data, variedname_merged{j}, varied_vals{j} ] = dsMergeVarieds(data,vary_labels(cols_list{j}));
0066 end
0067 
0068 % auto_gen_test_data_flag argout
0069 if options.auto_gen_test_data_flag
0070     argout = {linked_indices, non_linked_indices}; % specific to this function
0071     
0072     dsUnitSaveAutoGenTestDataLocalFn(argin, argout); % localfn
0073 end
0074 
0075 
0076 
0077 end
0078 
0079 % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
0080 % % % % % % % % % % % % Main subfunctions % % % % % % % % % %
0081 % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
0082 function [cols2merge_out] = find_shortest_mergelist(vary_params,success_thresh,maxiter)
0083 
0084 %     maxiter = 10000;
0085 %     success_thresh = 0.999;
0086 
0087     if ~ismatrix(vary_params); error('vary_params cell array must be MxN');end
0088 
0089     sz = size(vary_params);
0090 
0091     Nparams = sz(2);
0092 
0093     % Loop through various options
0094     count = 0;
0095     cols2merge_out = [];
0096     for i = 2:Nparams-1
0097         b = nchoosek(Nparams,i);        % Number of possible choices
0098         C = nchoosek(1:Nparams,i);      % Combinations
0099         for j = 1:b
0100             count = count + 1;
0101             cols2merge = C(j,:);
0102             vary_merged = mergevary(vary_params,cols2merge);
0103             percent_full = getpercentfull(vary_merged);
0104             if percent_full > success_thresh; cols2merge_out = cols2merge; return; end
0105             if count > maxiter ; cols2merge_out = []; return; end
0106 
0107         end
0108     end
0109 
0110 end
0111 
0112 function cols_list = subdivide_ind_list(vary_params,cols_list,success_thresh,maxiter)
0113 
0114 %     maxiter = 10000;
0115 %     success_thresh = 0.999;
0116     
0117     ind_last = cols_list{end};
0118     Nlast = length(ind_last);
0119     if Nlast < 4; return; end       % We can't up anything less than 4 columns, since the smallest grouping we can have is 2 (e.g. 3 columns would divide into 2 and 1; but can't merge 1 column)
0120     
0121     count = 0;
0122     for i = 2:floor(Nlast/2)  % If Nlast it 9, only go up to 4 since choosing 4 will implicitly choose 5 leftover
0123         b = nchoosek(Nlast,i);
0124         C = nchoosek(1:Nlast,i);
0125         for j = 1:b
0126             count = count + 1;
0127             inds2merge1 = C(j,:);
0128             inds2merge2 = setxor(1:Nlast,inds2merge1);                                                              % Get complement of chosen inds (e.g. unchosen columns) (see https://www.mathworks.com/matlabcentral/newsreader/view_thread/45631)
0129             colslast1 = ind_last(inds2merge1);
0130             colslast2 = ind_last(inds2merge2);
0131             vary_merged = mergevary(vary_params,cols_list{1:end-1},colslast1,colslast2);     % Merge columns as needed (note: need to convert indices to be in terms of vary_params)
0132             percent_full = getpercentfull(vary_merged);
0133             if percent_full > success_thresh
0134                 cols_list{end} = inds2merge1;
0135                 cols_list{end+1} = inds2merge2;
0136                 cols_list = subdivide_ind_list(vary_params,cols_list,success_thres,maxiter);    % Recursive call (see if we can subdivide it further).
0137                 return;
0138             end
0139             if count > maxiter ; cols2merge_out = []; warning('Aborting: Exceeded maximum iterations; consider increasing maxiter '); return; end
0140         end
0141     end
0142 
0143 end
0144 
0145 % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
0146 % % % % % % % % % % Supporting subfunctions % % % % % % % % %
0147 % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
0148 
0149 
0150 function vary_params = all2str(vary_params)
0151     % Convert all vary_params from numerics to strings
0152     for i = 1:numel(vary_params)
0153         if isnumeric(vary_params{i}); vary_params{i} = num2str(vary_params{i});
0154         elseif ischar(vary_params{i}); % Good
0155         else
0156             error('vary_param entries must be of type numeric or char');
0157         end
0158     end
0159 end
0160 
0161 
0162 
0163 function out = mergevary(in,varargin)
0164 
0165     merge_inds_cell = varargin;
0166     
0167     sz = size(in);
0168     Ncols = sz(2);
0169     
0170     out = in;
0171     for i = 1:length(merge_inds_cell)
0172         out = mergevary_sub(out,merge_inds_cell{i});
0173     end
0174     
0175     % Discard cols2merge(2:end), which have been merged into cols2merge(1)
0176     ci = true(1,Ncols);
0177     for i = 1:length(merge_inds_cell)
0178         cols_curr = merge_inds_cell{i};
0179         ci(cols_curr(2:end)) = false;
0180     end
0181     out = out(:,ci);    
0182 
0183 end
0184 
0185 
0186 function out = mergevary_sub(in,cols2merge)
0187     if length(cols2merge) < 2; error('Must supply at least 2 cols to merge');
0188     end
0189     
0190     sz = size(in);
0191     Nrows = sz(1);
0192     
0193     out = in;
0194     for i = 1:Nrows
0195         out{i,cols2merge(1)} = cat_with_underscores(in(i,cols2merge));
0196     end
0197 
0198 end
0199 
0200 
0201 function str_out = cat_with_underscores(cellstr_in)
0202     % Takes in a cell array of chars and concatenates them together with
0203     % underscores separating the original divisions between cells. E.g.
0204     % {'cat','dog'} becomes 'cat_dog'
0205 
0206     temp = vertcat(cellstr_in(:)', repmat({'_'},1,length(cellstr_in)));
0207     temp = temp(:)';
0208     str_out = horzcat(temp{1:end-1});
0209 end
0210 
0211 function percent_full = getpercentfull(vary_merged)
0212 
0213     sz = size(vary_merged);
0214 
0215     Nsims = sz(1);
0216     Nparams = sz(2);
0217     vmu = cell(1,Nparams);
0218     for j = 1:Nparams
0219         vmu{j} = uniqueCellGeneralized(vary_merged(:,j));
0220     end
0221     Nuniq = cellfun(@length,vmu);
0222     percent_full = Nsims / prod(Nuniq);
0223 end

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