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