annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_modify_dataset.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function sD = som_modify_dataset(sD,action,varargin)
Daniel@0 2
Daniel@0 3 %SOM_MODIFY_DATASET Add or remove components or samples to/from a data struct.
Daniel@0 4 %
Daniel@0 5 % sD = som_modify_dataset(sD, 'addcomp', D, [indsto], [cnames})
Daniel@0 6 % sD = som_modify_dataset(sD, 'addsamp', D, [indsto], ['norm'])
Daniel@0 7 % sD = som_modify_dataset(sD, 'removecomp', [inds])
Daniel@0 8 % sD = som_modify_dataset(sD, 'removesamp', inds)
Daniel@0 9 % sD = som_modify_dataset(sD, 'extractcomp', [inds])
Daniel@0 10 % sD = som_modify_dataset(sD, 'extractsamp', inds)
Daniel@0 11 % sD = som_modify_dataset(sD, 'movecomp', inds, indsto)
Daniel@0 12 % sD = som_modify_dataset(sD, 'movesamp', inds, indsto)
Daniel@0 13 %
Daniel@0 14 % Input and output arguments ([]'s are optional)
Daniel@0 15 % sD (struct) data struct
Daniel@0 16 % action (string) 'addcomp', 'addsamp', 'removecomp', 'removesamp',
Daniel@0 17 % 'extractcomp', 'extractsamp', 'movecomp', or 'movesamp'
Daniel@0 18 %
Daniel@0 19 % other input arguments depend on the action
Daniel@0 20 %
Daniel@0 21 % 'addcomp':
Daniel@0 22 % D (matrix) data matrix, size [dlen x d]
Daniel@0 23 % (struct) data struct, size of .data field [dlen x d]
Daniel@0 24 % [indsto] (vector) new indeces of the components, length=d
Daniel@0 25 % [cnames] (cellstr) of size d x 1, the component names
Daniel@0 26 %
Daniel@0 27 % 'addsamp':
Daniel@0 28 % D (matrix) data matrix, size [n x dim]
Daniel@0 29 % [indsto] (vector) new indeces of the samples, length=n
Daniel@0 30 % ['norm'] (string) specified if the normalization procedure
Daniel@0 31 % should be applied to the new samples
Daniel@0 32 %
Daniel@0 33 % 'removecomp', 'extractcomp':
Daniel@0 34 % [inds] (vector) indeces of the components to be removed/extracted.
Daniel@0 35 % If not given, a prompt will appear from which the
Daniel@0 36 % user can select the appropriate components.
Daniel@0 37 %
Daniel@0 38 % 'removesamp', 'extractsamp':
Daniel@0 39 % inds (vector) indeces of the samples to be removed/extracted
Daniel@0 40 %
Daniel@0 41 % 'movecomp', 'movesamp':
Daniel@0 42 % inds (vector) indeces of the components/samples to be moved
Daniel@0 43 % indsto (vector) new indeces of the components/samples
Daniel@0 44 %
Daniel@0 45 % See also SOM_DATA_STRUCT.
Daniel@0 46
Daniel@0 47 % Copyright (c) 2000 by Juha Vesanto
Daniel@0 48 % Contributed to SOM Toolbox on June 16th, 2000 by Juha Vesanto
Daniel@0 49 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 50
Daniel@0 51 % Version 2.0beta juuso 200400 160600 280800
Daniel@0 52
Daniel@0 53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 54 %% input arguments
Daniel@0 55
Daniel@0 56 [dlen dim] = size(sD.data);
Daniel@0 57
Daniel@0 58 switch action,
Daniel@0 59 case 'addcomp',
Daniel@0 60 D = varargin{1};
Daniel@0 61 if isstruct(D), [n d] = size(D.data); else [n d] = size(D); end
Daniel@0 62 if n ~= dlen, error('The number of samples in the data struct and new data should match.'); end
Daniel@0 63 indsto = [];
Daniel@0 64 cnames = [];
Daniel@0 65 for i=2:length(varargin),
Daniel@0 66 if isnumeric(varargin{i}),
Daniel@0 67 indsto = varargin{i};
Daniel@0 68 if length(indsto) ~= d,
Daniel@0 69 error('The number of indeces should match the number of new components');
Daniel@0 70 end
Daniel@0 71 else
Daniel@0 72 if ischar(varargin{i}), cnames = cellstr(varargin{i});
Daniel@0 73 elseif iscellstr(varargin{i}), cnames = varargin{i};
Daniel@0 74 else
Daniel@0 75 error(['[som_modify_dataset] Unrecognized argument #' num2str(i+1)]);
Daniel@0 76 end
Daniel@0 77 if length(cnames) ~= d,
Daniel@0 78 error('The number of component names should match the number of new components');
Daniel@0 79 end
Daniel@0 80 end
Daniel@0 81 end
Daniel@0 82 case 'addsamp',
Daniel@0 83 D = varargin{1};
Daniel@0 84 if isstruct(D),
Daniel@0 85 lab = D.labels;
Daniel@0 86 if isfield(D,'data'), D = D.data; else D = D.codebook; end
Daniel@0 87 else lab = [];
Daniel@0 88 end
Daniel@0 89 [n d] = size(D);
Daniel@0 90 if d ~= dim,
Daniel@0 91 error(['The dimensions of the old and new data sets should match.']);
Daniel@0 92 end
Daniel@0 93 norm = 0;
Daniel@0 94 indsto = [];
Daniel@0 95 for i=2:length(varargin),
Daniel@0 96 if ischar(varargin{i}) & strcmp(varargin{i},'norm'), norm = 1;
Daniel@0 97 elseif isnumeric(varargin{i}),
Daniel@0 98 indsto = varargin{i};
Daniel@0 99 if length(indsto) ~= n,
Daniel@0 100 error(['The number of new indeces should match the number of new' ...
Daniel@0 101 ' samples']);
Daniel@0 102 end
Daniel@0 103 else
Daniel@0 104 warning(['[som_modify_dataset] Ignoring unrecognized argument #', ...
Daniel@0 105 num2str(i+2)]);
Daniel@0 106 end
Daniel@0 107 end
Daniel@0 108 case 'removecomp',
Daniel@0 109 if length(varargin)>0,
Daniel@0 110 inds = varargin{1};
Daniel@0 111 else
Daniel@0 112 [inds, ok] = listdlg('ListString',sD.comp_names, 'PromptString', ...
Daniel@0 113 'Components', 'Name', 'Remove components', 'uh', 25);
Daniel@0 114 if ~ok, return; end
Daniel@0 115 end
Daniel@0 116 if min(inds)<1 | max(inds)>dim,
Daniel@0 117 error('The component indeces must be within [1,dim]');
Daniel@0 118 end
Daniel@0 119 case 'removesamp',
Daniel@0 120 inds = varargin{1};
Daniel@0 121 if min(inds)<1 | max(inds)>dlen,
Daniel@0 122 error('The sample indeces must be within [1,dlen]');
Daniel@0 123 end
Daniel@0 124 case 'extractcomp',
Daniel@0 125 if length(varargin)>0,
Daniel@0 126 inds = varargin{1};
Daniel@0 127 else
Daniel@0 128 [inds, ok] = listdlg('ListString',sD.comp_names, 'PromptString',...
Daniel@0 129 'Components', 'Name', 'Extract components', 'uh', 25);
Daniel@0 130 if ~ok, return; end
Daniel@0 131 end
Daniel@0 132 if min(inds)<1 | max(inds)>dim,
Daniel@0 133 error('The component indeces must be within [1,dim]');
Daniel@0 134 end
Daniel@0 135 case 'extractsamp',
Daniel@0 136 inds = varargin{1};
Daniel@0 137 if min(inds)<1 | max(inds)>dlen,
Daniel@0 138 error('The sample indeces must be within [1,dlen]');
Daniel@0 139 end
Daniel@0 140 case 'movecomp',
Daniel@0 141 inds = varargin{1};
Daniel@0 142 indsto = varargin{2};
Daniel@0 143 if min(inds)<1 | max(inds)>dim | min(indsto)<1 | max(indsto)>dim,
Daniel@0 144 error('The component indeces must be within [1,dim]');
Daniel@0 145 end
Daniel@0 146 case 'movesamp',
Daniel@0 147 inds = varargin{1};
Daniel@0 148 indsto = varargin{2};
Daniel@0 149 if min(inds)<1 | max(inds)>dlen | min(indsto)<1 | max(indsto)>dlen,
Daniel@0 150 error('The sample indeces must be within [1,dlen]');
Daniel@0 151 end
Daniel@0 152 otherwise,
Daniel@0 153 error('Unrecognized action mode');
Daniel@0 154 end
Daniel@0 155
Daniel@0 156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 157 %% action
Daniel@0 158
Daniel@0 159 switch action,
Daniel@0 160 case 'addcomp',
Daniel@0 161 if isstruct(D),
Daniel@0 162 sD.data = [sD.data, D.data];
Daniel@0 163 sD.comp_names(dim+[1:d]) = D.comp_names;
Daniel@0 164 sD.comp_norm(dim+[1:d]) = D.comp_norm;
Daniel@0 165 sD = som_label(sD,'add',1:dlen,D.labels);
Daniel@0 166 else
Daniel@0 167 sD.data = [sD.data, D];
Daniel@0 168 if isempty(cnames),
Daniel@0 169 for i=1:d, sD.comp_names(dim+i) = {sprintf('Variable%d',i+dim)}; end
Daniel@0 170 else
Daniel@0 171 sD.comp_names(dim+[1:d]) = cnames;
Daniel@0 172 end
Daniel@0 173 for i=1:d, sD.comp_norm(dim+i) = {[]}; end
Daniel@0 174 end
Daniel@0 175 if ~isempty(indsto),
Daniel@0 176 sD = som_modify_dataset(sD,'movecomp',dim+[1:d],indsto);
Daniel@0 177 end
Daniel@0 178
Daniel@0 179 case 'addsamp',
Daniel@0 180 if norm, D = som_normalize(D,sD); end
Daniel@0 181 sD.data = [sD.data; D];
Daniel@0 182 nl = size(sD.labels,2);
Daniel@0 183 sD.labels(dlen+[1:n],1:nl) = {''};
Daniel@0 184 if ~isempty(lab),
Daniel@0 185 nl2 = size(lab,2);
Daniel@0 186 if nl2>nl, sD.labels(1:dlen,nl+[1:(nl2-nl)]) = {''}; end
Daniel@0 187 sD.labels(dlen+[1:n],1:nl2) = lab;
Daniel@0 188 end
Daniel@0 189 if ~isempty(indsto),
Daniel@0 190 sD = som_modify_dataset(sD,'movesamp',dlen+[1:n],indsto);
Daniel@0 191 end
Daniel@0 192
Daniel@0 193 case 'removecomp',
Daniel@0 194 includeinds = 1:dim;
Daniel@0 195 includeinds(inds) = 0;
Daniel@0 196 sD = som_modify_dataset(sD,'extractcomp',find(includeinds));
Daniel@0 197
Daniel@0 198 case 'removesamp',
Daniel@0 199 includeinds = 1:dlen;
Daniel@0 200 includeinds(inds) = 0;
Daniel@0 201 sD = som_modify_dataset(sD,'extractsamp',find(includeinds));
Daniel@0 202
Daniel@0 203 case 'extractcomp',
Daniel@0 204 sD.data = sD.data(:,inds);
Daniel@0 205 sD.comp_names = sD.comp_names(inds);
Daniel@0 206 sD.comp_norm = sD.comp_norm(inds);
Daniel@0 207
Daniel@0 208 case 'extractsamp',
Daniel@0 209 sD.data = sD.data(inds,:);
Daniel@0 210 sD.labels = sD.labels(inds,:);
Daniel@0 211
Daniel@0 212 case 'movecomp',
Daniel@0 213 [indsto,order] = sort(indsto);
Daniel@0 214 inds = inds(order);
Daniel@0 215 oldinds = 1:dim;
Daniel@0 216 oldinds(inds) = 0;
Daniel@0 217 newinds = oldinds(oldinds>0);
Daniel@0 218 for i=1:length(indsto),
Daniel@0 219 ifrom = inds(i); ito = indsto(i);
Daniel@0 220 if ito==1, newinds = [ifrom, newinds];
Daniel@0 221 else newinds = [newinds(1:ito-1), ifrom, newinds(ito:end)];
Daniel@0 222 end
Daniel@0 223 end
Daniel@0 224 sD.data = sD.data(:,newinds);
Daniel@0 225 sD.comp_names = sD.comp_names(:,newinds);
Daniel@0 226 sD.comp_norm = sD.comp_norm(:,newinds);
Daniel@0 227
Daniel@0 228 case 'movesamp',
Daniel@0 229 [indsto,order] = sort(indsto);
Daniel@0 230 inds = inds(order);
Daniel@0 231 oldinds = 1:dim;
Daniel@0 232 oldinds(inds) = 0;
Daniel@0 233 newinds = oldinds(oldinds>0);
Daniel@0 234 for i=1:length(indsto),
Daniel@0 235 ifrom = inds(i); ito = indsto(i);
Daniel@0 236 if ito==1, newinds = [ifrom, newinds];
Daniel@0 237 else newinds = [newinds(1:ito-1), ifrom, newinds(ito:end)];
Daniel@0 238 end
Daniel@0 239 end
Daniel@0 240 sD.data = sD.data(newinds,:);
Daniel@0 241 sD.labels = sD.labels(newinds,:);
Daniel@0 242
Daniel@0 243 end
Daniel@0 244
Daniel@0 245 %som_set(sD);
Daniel@0 246
Daniel@0 247