comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_modify_dataset.m @ 0:e9a9cd732c1e tip

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