comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_label.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 [sTo] = som_label(sTo, mode, inds, labels)
2
3 %SOM_LABEL Give/clear labels to/from map or data struct.
4 %
5 % sTo = som_label(sTo, mode, inds [, labels])
6 %
7 % sD = som_label(sD,'add',20,'a_label');
8 % sM = som_label(sM,'replace',[2 4],'a_label');
9 % sM = som_label(sM,'add',som_bmus(sM,x),'BMU');
10 % sD = som_label(sD,'prune',[1:10]');
11 % sM = som_label(sM,'clear','all');
12 %
13 % Input and output arguments ([]'s are optional):
14 % sTo (struct) data or map struct to which the labels are put
15 % mode (string) 'add' or 'replace' or 'prune' or 'clear'
16 % inds (vector) indeces of the vectors to which the labels
17 % are put. Note: this must be a column vector!
18 % (matrix) subscript indeces to the '.labels' field. The vector
19 % is given by the first index (e.g. inds(i,1)).
20 % (string) for 'prune' and 'clear' modes, the string 'all'
21 % means that all vectors should be pruned/cleared
22 % [labels] The labels themselves. The number of rows much match
23 % the number of given indeces, except if there is either
24 % only one index or only one label. If mode is
25 % 'prune' or 'clear', labels argument is ignored.
26 % (string) Label.
27 % (string array) Each row is a label.
28 % (cell array of strings) All labels in a cell are handled
29 % as a group and are applied to the same vector given
30 % on the corresponding row of inds.
31 %
32 % Note: If there is only one label/index, it is used for each specified
33 % index/label.
34 %
35 % For more help, try 'type som_label' or check out online documentation.
36 % See also SOM_AUTOLABEL, SOM_SHOW_ADD, SOM_SHOW.
37
38 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %
40 % som_label
41 %
42 % PURPOSE
43 %
44 % Add (or remove) labels to (from) map and data structs.
45 %
46 % SYNTAX
47 %
48 % sTo = som_label(sTo, 'clear', inds)
49 % sTo = som_label(sTo, 'prune', inds)
50 % sTo = som_label(sTo, 'add', inds, labels)
51 % sTo = som_label(sTo, 'replace', inds, labels)
52 %
53 % DESCRIPTION
54 %
55 % This function can be used to give and remove labels in map and data
56 % structs. Of course the same operation could be done by hand, but this
57 % function offers an alternative and hopefully slightly user-friendlier
58 % way to do it.
59 %
60 % REQUIRED INPUT ARGUMENTS
61 %
62 % sTo (struct) data or map struct to which the labels are put
63 % mode (string) The mode of operation.
64 % 'add' : adds the given labels
65 % 'clear' : removes labels
66 % 'replace' : replaces current labels with given
67 % labels; basically same as 'clear'
68 % followed by 'add'
69 % 'prune' : removes empty labels ('') from between
70 % non-empty labels, e.g. if the labels of
71 % a vector were {'A','','','B','','C'}
72 % they'd become {'A','B','C'}. Some empty
73 % labels may be left at the end of the list.
74 %
75 % inds Identifies the vectors to which the operation
76 % (given by mode) is applied to.
77 % (vector) Linear indexes of the vectors, size n x 1.
78 % Notice! This should be a column vector!
79 % (matrix) The labels are in a cell matrix. By giving matrix
80 % argument for inds, you can address this matrix
81 % directly. The first index gives the vector and the
82 % second index the vertical position of the label in
83 % the labels array. Size n x 2, where n is the
84 % number of labels.
85 % (string) for 'prune' and 'clear' modes, the string 'all'
86 % means that all vectors should be pruned/cleared
87 %
88 % OPTIONAL INPUT ARGUMENTS
89 %
90 % [labels] The labels themselves. The number of rows much match
91 % the number of given indeces, except if there is either
92 % only one index or only one label.
93 % (string) Label, e.g. 'label'
94 % (string array) Each row is a label,
95 % e.g. ['label1'; 'label2'; 'label3']
96 % (cell array of strings) All labels in a cell are handled
97 % as a group and are applied to the same vector given
98 % on the corresponding row of inds.
99 % e.g. three labels: {'label1'; 'label2'; 'label3'}
100 % e.g. a group of labels: {'label1', 'label2', 'label3'}
101 % e.g. three groups: {{'la1'},{'la21','la22'},{'la3'}
102 %
103 % OUTPUT ARGUMENTS
104 %
105 % sTo (struct) the given data/map struct with modified labels
106 %
107 % EXAMPLES
108 %
109 % This is the basic way to add a label to map structure:
110 % sMap = som_label(sMap,'add',3,'label');
111 %
112 % The following examples have identical results:
113 % sMap = som_label(sMap,'add',[4; 13], ['label1'; 'label2']);
114 % sMap = som_label(sMap,'add',[4; 13], {{'label1'};{'label2'}});
115 %
116 % Labeling the BMU of a vector x (and removing any old labels)
117 % sMap = som_label(sMap,'replace',som_bmus(sMap,x),'BMU');
118 %
119 % Pruning labels
120 % sMap = som_label(sMap,'prune','all');
121 %
122 % Clearing labels from a struct
123 % sMap = som_label(sMap,'clear','all');
124 % sMap = som_label(sMap,'clear',[1:4, 9:30]');
125 %
126 % SEE ALSO
127 %
128 % som_autolabel Automatically label a map/data set.
129 % som_show Show map planes.
130 % som_show_add Add for example labels to the SOM_SHOW visualization.
131
132 % Copyright (c) 1997-2000 by the SOM toolbox programming team.
133 % http://www.cis.hut.fi/projects/somtoolbox/
134
135 % Version 2.0beta juuso 101199
136
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 %% check arguments
139
140 error(nargchk(3, 4, nargin)); % check no. of input args is correct
141
142 % sTo
143 switch sTo.type,
144 case 'som_map', [dlen dim] = size(sTo.codebook);
145 case 'som_data', [dlen dim] = size(sTo.data);
146 end
147 maxl = size(sTo.labels,2); % maximum number of labels for a single vector
148
149 % inds
150 if ischar(inds) & strcmp(inds,'all'),
151 inds = [1:dlen]';
152 end
153 if length(inds)>2 & size(inds,2)>2, inds = inds'; end
154 ni = size(inds,1);
155 n = ni;
156
157 % labels
158 if nargin==4,
159 % convert labels to a cell array of cells
160 if ischar(labels), labels = cellstr(labels); end
161 if iscellstr(labels),
162 tmplab = labels;
163 nl = size(labels,1);
164 labels = cell(nl,1);
165 for i=1:nl,
166 if ~iscell(tmplab{i})
167 if ~isempty(tmplab{i}), labels{i} = tmplab(i,:);
168 else labels{i} = {}; end
169 else
170 labels(i) = tmplab(i);
171 end
172 end
173 clear tmplab;
174 end
175 nl = size(labels,1);
176 end
177
178 % the case of a single label/index
179 if any(strcmp(mode,{'add','replace'})),
180 n = max(nl,ni);
181 if n>1,
182 if ni==1,
183 inds = zeros(n,1)+inds(1);
184 elseif nl==1,
185 label = labels{1};
186 labels = cell(n,1);
187 for i=1:n, labels{i} = label; end
188 elseif ni ~= nl,
189 error('The number of labels and indexes does not match.');
190 end
191 end
192 end
193
194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195 %% action
196
197 switch mode,
198 case 'clear',
199 if size(inds,2)>2,
200 inds = inds(find(inds(:,2)<=maxl),:); % ignore if subindex is out-of-range
201 inds = sub2ind([dlen maxl],inds(:,1),inds(:,2));
202 sTo.labels{inds} = [];
203 else
204 sTo.labels(inds,:) = cell(n,maxl);
205 end
206 case 'prune',
207 if size(inds,2)==1,
208 % subindex gives the index from which the pruning is started
209 inds = [inds, ones(n,1)]; % from 1 by default
210 end
211 select = ones(1,maxl);
212 for i=1:n,
213 v = inds(i,1); s = inds(i,2); select(:) = 1;
214 for j=s:maxl, select(j) = ~isempty(sTo.labels{v,j}); end
215 if ~all(select),
216 labs = cell(1,maxl);
217 labs(1:sum(select)) = sTo.labels(v,find(select));
218 sTo.labels(v,:) = labs;
219 end
220 end
221 case 'add',
222 if size(inds,2)==1,
223 % subindex gives the index from which the adding is started
224 inds = [inds, ones(n,1)]; % from 1 by default
225 end
226 for i=1:n,
227 v = inds(i,1); s = inds(i,2); l = length(labels{i});
228 for j=1:l,
229 while s<=size(sTo.labels,2) & ~isempty(sTo.labels{v,s}), s=s+1; end
230 sTo.labels{v,s} = labels{i}{j};
231 s=s+1;
232 end
233 end
234 case 'replace',
235 if size(inds,2)==1,
236 % subindex gives the index from which the replacing is started
237 inds = [inds, ones(n,1)]; % from 1 by default
238 end
239 for i=1:n,
240 v = inds(i,1); s = inds(i,2); l = length(labels(i));
241 for j=1:l, sTo.labels{v,s-1+j} = labels{i}{j}; end
242 end
243 otherwise
244 error(['Unrecognized mode: ' mode]);
245 end
246
247 sTo.labels = remove_empty_columns(sTo.labels);
248
249 [dlen maxl] = size(sTo.labels);
250 for i=1:dlen,
251 for j=1:maxl,
252 if isempty(sTo.labels{i,j}) & ~ischar(sTo.labels{i,j}),
253 sTo.labels{i,j} = '';
254 end
255 end
256 end
257
258 return;
259
260
261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262 %% subfunctions
263
264 function labels = remove_empty_columns(labels)
265
266 [dlen maxl] = size(labels);
267
268 % find which columns are empty
269 cols = zeros(1,maxl);
270 for i=1:dlen,
271 for j=1:maxl,
272 cols(j) = cols(j) + ~isempty(labels{i,j});
273 end
274 end
275 while maxl>0 & cols(maxl)==0, maxl = maxl-1; end % check starting from end
276
277 if maxl==0, labels = cell(dlen,1);
278 elseif maxl<size(labels,2), labels = labels(:,1:maxl);
279 else % ok
280 end
281 % end of remove_empty_columns
282
283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%