comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_autolabel.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_autolabel(sTo, sFrom, mode, inds)
2
3 %SOM_AUTOLABEL Automatical labeling, or clearing of labels.
4 %
5 % sTo = som_autolabel(sTo, sFrom, [mode], [inds])
6 %
7 % sM = som_autolabel(sM,sD);
8 % sD = som_autolabel(sD,sM);
9 % sM = som_autolabel(sM,sD,'vote',[5]);
10 %
11 % Input and output arguments ([]'s are optional):
12 % sTo (struct) data or map struct to which the labels are put,
13 % the modified struct is returned
14 % sFrom (struct) data or map struct from which the labels are taken
15 % [mode] (string) labeling algorithm: 'add' (the default), 'freq'
16 % or 'vote'
17 % [inds] (vector) the column-indexes of the labels that are to be
18 % used in the operation (e.g. [2] would mean to use
19 % only the second column of labels array in sFrom)
20 %
21 % The modes:
22 % 'add': all labels from sFrom are added to sTo (even multiple
23 % copies of same)
24 % 'add1': only one instance of each label is kept
25 % 'freq': only one instance of each label is kept and '(#)', where
26 % # is the frequency of the label, is added to the end of
27 % the label. Labels are ordered according to frequency.
28 % 'vote': only the label with most instances is kept
29 %
30 % NOTE: The operations are only performed for the new labels.
31 % The old labels in sTo are left as they are.
32 % NOTE: all empty labels ('') are ignored.
33 %
34 % For more help, try 'type som_autolabel' or check out online documentation.
35 % See also SOM_LABEL, SOM_BMUS, SOM_SHOW_ADD, SOM_SHOW.
36
37 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 %
39 % som_autolabel
40 %
41 % PURPOSE
42 %
43 % Automatically label to map/data structs based on given data/map.
44 %
45 % SYNTAX
46 %
47 % sTo = som_autolabel(sTo, sFrom)
48 % sTo = som_autolabel(sTo, sFrom, 'add')
49 % sTo = som_autolabel(sTo, sFrom, 'freq')
50 % sTo = som_autolabel(sTo, sFrom, 'vote')
51 % sTo = som_autolabel(..., inds)
52 %
53 % DESCRIPTION
54 %
55 % This function automatically labels given map/data struct based on an
56 % already labelled data/map struct. Basically, the BMU of each vector in the
57 % sFrom struct is found from among the vectors in sTo, and the vectors in
58 % sFrom are added to the corresponding vector in the sTo struct. The actual
59 % labels to add are selected based on the mode ('add', 'freq' or 'vote').
60 %
61 % 'add' : all labels from sFrom are added to sTo - even if there would
62 % be multiple instances of the same label
63 % 'add1' : only one instance of each label is kept
64 % 'freq' : only one instance of each label is kept and '(#)', where
65 % # is the frequency of the label, is added to the end of
66 % the label. Labels are ordered according to frequency.
67 % 'vote' : only the label with most instances is added
68 %
69 % Note that these operations do not effect the old labels of sTo: they
70 % are left as they were.
71 %
72 % NOTE: empty labels ('') are ignored.
73 %
74 % REQUIRED INPUT ARGUMENTS
75 %
76 % sTo (struct) data or map struct to which the labels are put
77 % sFrom (struct) data or map struct from which the labels are taken
78 %
79 % OPTIONAL INPUT ARGUMENTS
80 %
81 % mode (string) The mode of operation: 'add' (default),
82 % 'add1', 'freq' or 'vote'
83 % inds (vector) The columns of the '.labels' field in sFrom to be
84 % used in operation
85 %
86 % OUTPUT ARGUMENTS
87 %
88 % sTo (struct) the given data/map struct with modified labels
89 %
90 % EXAMPLES
91 %
92 % To label a trained map based on (labelled) training data, just do
93 %
94 % sM = som_autolabel(sM,sD);
95 %
96 % This operation is sometimes called "calibration" in the literature.
97 % You can also do this the other way around: use a labelled map to
98 % label a data set:
99 %
100 % sD = som_autolabel(sD,sM);
101 %
102 % If you only want a single instance of each label, use the 'freq' mode:
103 %
104 % sM = som_autolabel(sM,sD,'freq');
105 %
106 % If you already have labels in the struct, and want to perform 'freq' on
107 % them, do the following:
108 %
109 % sMtemp = som_label(sM,'clear','all'); % make a map struct with no labels
110 % sM = som_autolabel(sMtemp,sM,'freq'); % add labels to it
111 %
112 % The third mode 'vote' votes between the labels and only adds the one
113 % which is most frequent. If two labels are equally frequent, one or the
114 % other is chosen based on which appears first in the list.
115 %
116 % sM = som_autolabel(sM,sD,'vote');
117 %
118 % The lat argument is useful if you have specific labels in each column
119 % of the '.labels' field. For example, the first column might be an
120 % identifier, the next a typecode and the last a year. In this case, you
121 % might want to label the map based only on the typecode:
122 %
123 % sM = som_autolabel(sM,sD,'vote',2);
124 %
125 % SEE ALSO
126 %
127 % som_label Give/remove labels from a map/data set.
128 % som_bmus Find BMUs from the map for the given set of data vectors.
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 1.0beta juuso 101297
136 % Version 2.0beta juuso 101199
137
138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 %% check arguments
140
141 error(nargchk(2, 4, nargin)); % check no. of input args is correct
142
143 % sTo
144 todata = strcmp(sTo.type,'som_data');
145
146 % sFrom
147 [dummy m] = size(sFrom.labels);
148
149 % mode
150 if nargin<3 | isempty(mode), mode = 'add'; end
151
152 % inds
153 if nargin<4, inds = 1:m; end
154 inds = inds(find(inds>0 & inds<=m));
155
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 %% get a list of the labels to be added to each vector
158
159 % calculate BMUs
160 if todata, bmus = som_bmus(sFrom,sTo,1);
161 else bmus = som_bmus(sTo,sFrom,1); end
162
163 % for each vector in sTo, make a list of all new labels
164 Labels = cell(size(sTo.labels,1),1);
165 for d=1:length(bmus),
166 m = bmus(d);
167 if todata, t = d; f = m; else t = m; f = d; end
168 if ~isnan(m),
169 % add the labels
170 for j=1:length(inds),
171 if ~isempty(sFrom.labels{f,inds(j)}),
172 Labels{t}{length(Labels{t})+1} = sFrom.labels{f,inds(j)};
173 end
174 end
175 end
176 end
177
178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 %% insert the labels to sTo
180
181
182 if strcmp(mode,'add1') | strcmp(mode,'freq') | strcmp(mode,'vote'),
183
184 % modify the Labels array apprpriately
185
186 for i=1:length(Labels),
187
188 % calculate frequency of each label in each node
189 new_labels = {};
190 new_freq = [];
191 for j=1:length(Labels{i}),
192 if isempty(Labels{i}{j}), % ignore
193 elseif ~any(strcmp(Labels{i}{j},new_labels)), % a new one!
194 k = length(new_labels) + 1;
195 new_labels{k} = Labels{i}{j};
196 new_freq(k) = sum(strcmp(new_labels{k},Labels{i}));
197 else, % an old one, ignore
198 end
199 end
200
201 % based on frequency, select label(s) to be added
202 if length(new_labels) > 0,
203 if strcmp(mode,'add1'),
204 Labels{i} = new_labels;
205 else
206
207 % sort labels according to frequency
208 [dummy order] = sort(1./(1+new_freq));
209 new_labels = new_labels(order);
210 new_freq = new_freq(order);
211
212 switch mode,
213 case 'freq',
214 % replace each label with 'label(#)' where # is the frequency
215 for j=1:length(new_labels),
216 labf = sprintf('%s(%d)',new_labels{j},new_freq(j));
217 new_labels{j} = labf;
218 end
219 Labels{i} = new_labels;
220 case 'vote',
221 % place only the one with most votes
222 Labels{i} = {new_labels{1}};
223 end
224 end
225 end
226
227 end
228
229 end
230
231 sTo = som_label(sTo,'add',[1:length(Labels)]',Labels);
232
233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234
235
236
237