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