0001
0002 function [data, variedname_merged, varied_vals ] = dsAutoMergeVarieds(data,strategy,verbose_on,varargin)
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
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
0033
0034
0035
0036 N = length(data);
0037 vary_labels = data(1).varied;
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
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
0056 cols_list{1} = cols2merge;
0057 cols_list = subdivide_ind_list(vary_params,cols_list,success_thresh,maxiter);
0058
0059
0060 vary_labels = data(1).varied;
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
0069 if options.auto_gen_test_data_flag
0070 argout = {linked_indices, non_linked_indices};
0071
0072 dsUnitSaveAutoGenTestDataLocalFn(argin, argout);
0073 end
0074
0075
0076
0077 end
0078
0079
0080
0081
0082 function [cols2merge_out] = find_shortest_mergelist(vary_params,success_thresh,maxiter)
0083
0084
0085
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
0094 count = 0;
0095 cols2merge_out = [];
0096 for i = 2:Nparams-1
0097 b = nchoosek(Nparams,i);
0098 C = nchoosek(1:Nparams,i);
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
0115
0116
0117 ind_last = cols_list{end};
0118 Nlast = length(ind_last);
0119 if Nlast < 4; return; end
0120
0121 count = 0;
0122 for i = 2:floor(Nlast/2)
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);
0129 colslast1 = ind_last(inds2merge1);
0130 colslast2 = ind_last(inds2merge2);
0131 vary_merged = mergevary(vary_params,cols_list{1:end-1},colslast1,colslast2);
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);
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
0147
0148
0149
0150 function vary_params = all2str(vary_params)
0151
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});
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
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
0203
0204
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