comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_hits.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 [hits] = som_hits(sMap, sData, mode)
2
3 %SOM_HITS Calculate the response of the given data on the map.
4 %
5 % hits = som_hits(sMap, sData, [mode])
6 %
7 % h = som_hits(sMap,sData);
8 % h = som_hits(sMap,sData,'fuzzy');
9 %
10 % Input and output arguments ([]'s are optional):
11 % sMap (struct) map struct
12 % (matrix) codebook matrix, size munits x dim
13 % sData (struct) data struct
14 % (matrix) data matrix, size dlen x dim
15 % [mode] (string) 'crisp' (default), 'kernel', 'fuzzy'
16 %
17 % hits (vector) the number of hits in each map unit, length = munits
18 %
19 % The response of the data on the map can be calculated e.g. in
20 % three ways, selected with the mode argument:
21 % 'crisp' traditional hit histogram
22 % 'kernel' a sum of dlen neighborhood kernels, where kernel
23 % is positioned on the BMU of each data sample. The
24 % neighborhood function is sMap.neigh and the
25 % neighborhood width is sMap.trainhist(end).radius_fin
26 % or 1 if this is empty or NaN
27 % 'fuzzy' fuzzy response calculated by summing 1./(1+(q/a)^2)
28 % for each data sample, where q is a vector containing
29 % distance from the data sample to each map unit and
30 % a is average quantization error
31 %
32 % For more help, try 'type som_hits' or check out online documentation.
33 % See also SOM_AUTOLABEL, SOM_BMUS.
34
35 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %
37 % som_hits
38 %
39 % PURPOSE
40 %
41 % Calculate the response of the given data on the map.
42 %
43 % SYNTAX
44 %
45 % hits = som_hits(sMap, sData)
46 % hits = som_hits(M, D)
47 % hits = som_hits(..., mode)
48 %
49 % DESCRIPTION
50 %
51 % Returns a vector indicating the response of the map to the data.
52 % The response of the data on the map can be calculated e.g. in
53 % three ways, selected with the mode argument:
54 % 'crisp' traditional hit histogram: how many times each map unit
55 % was the BMU for the data set
56 % 'kernel' a sum of neighborhood kernels, where a kernel
57 % is positioned on the BMU of each data sample. The
58 % neighborhood function is sMap.neigh and the
59 % neighborhood width is sMap.trainhist(end).radius_fin
60 % or 1 if this is not available
61 % 'fuzzy' fuzzy response calculated by summing
62 %
63 % 1
64 % ------------
65 % 1 + (q/a)^2
66 %
67 % for each data sample, where q is a vector containing
68 % distance from the data sample to each map unit and
69 % a is average quantization error
70 %
71 % REQUIRED INPUT ARGUMENTS
72 %
73 % sMap The vectors from among which the BMUs are searched
74 % for. These must not have any unknown components (NaNs).
75 % (struct) map struct
76 % (matrix) codebook matrix, size munits x dim
77 %
78 % sData The data vector(s) for which the BMUs are searched.
79 % (struct) data struct
80 % (matrix) data matrix, size dlen x dim
81 %
82 % OPTIONAL INPUT ARGUMENTS
83 %
84 % mode (string) The respond mode: 'crisp' (default), 'kernel'
85 % or 'fuzzy'. 'kernel' can only be used if
86 % the first argument (sMap) is a map struct.
87 %
88 % OUTPUT ARGUMENTS
89 %
90 % hits (vector) The number of hits in each map unit.
91 %
92 % EXAMPLES
93 %
94 % hits = som_hits(sM,D);
95 % hits = som_hits(sM,D,'kernel');
96 % hits = som_hits(sM,D,'fuzzy');
97 %
98 % SEE ALSO
99 %
100 % som_bmus Find BMUs and quantization errors for a given data set.
101
102 % Copyright (c) 1997-2000 by the SOM toolbox programming team.
103 % http://www.cis.hut.fi/projects/somtoolbox/
104
105 % Version 1.0beta juuso 220997
106 % Version 2.0beta juuso 161199
107
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 %% check arguments
110
111 error(nargchk(2, 3, nargin)); % check no. of input args is correct
112
113 if isstruct(sMap),
114 switch sMap.type,
115 case 'som_map', munits = prod(sMap.topol.msize);
116 case 'som_data', munits = size(sMap.data,1);
117 otherwise,
118 error('Illegal struct for 1st argument.')
119 end
120 else
121 munits = size(sMap,1);
122 end
123 hits = zeros(munits,1);
124
125 if nargin<3, mode = 'crisp'; end
126
127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128 %% action
129
130 % calculate BMUs
131 [bmus,qerrs] = som_bmus(sMap,sData,1);
132
133 switch mode,
134 case 'crisp',
135
136 % for each unit, check how many hits it got
137 for i=1:munits, hits(i) = sum(bmus == i); end
138
139 case 'kernel',
140
141 % check that sMap really is a map
142 if ~isstruct(sMap) & ~strcmp(sMap.type,'som_map'),
143 error('Kernel mode can only be used for maps.');
144 end
145
146 % calculate neighborhood kernel
147 Ud = som_unit_dists(sMap.topol).^2;
148 sTrain = sMap.trainhist(end);
149 if ~isempty(sTrain),
150 rad = sTrain.radius_fin;
151 if isempty(rad) | isnan(rad), rad = 1; end
152 else
153 rad = 1;
154 end
155 rad = rad^2;
156 if rad==0, rad = eps; end % to avoid divide-by-0 errors
157 switch sTrain.neigh,
158 case 'bubble', H = (Ud<=rad);
159 case 'gaussian', H = exp(-Ud/(2*rad));
160 case 'cutgauss', H = exp(-Ud/(2*rad)) .* (Ud<=rad);
161 case 'ep', H = (1-Ud/rad) .* (Ud<=rad);
162 end
163
164 % weight hits with neighborhood kernel
165 hits = sum(H(bmus,:),1)';
166
167 case 'fuzzy',
168
169 % extract the two matrices (M, D) and the mask
170 mask = [];
171 if isstruct(sMap),
172 if strcmp(sMap.type,'som_data'), M = sMap.data;
173 else M = sMap.codebook; mask = sMap.mask;
174 end
175 else M = sMap;
176 end
177 if any(isnan(M(:))),
178 error('Data in first argument must not have any NaNs.');
179 end
180
181 if isstruct(sData),
182 switch sData.type,
183 case 'som_map',
184 D = sData.codebook;
185 if isempty(mask), mask = sData.mask; end
186 case 'som_data', D = sData.data;
187 otherwise, error('Illegal 2nd argument.');
188 end
189 else D = sData;
190 end
191 [dlen dim] = size(D);
192 if isempty(mask), mask = ones(dim,1); end
193
194 % scaling factor
195 a = mean(qerrs).^2;
196
197 % calculate distances & bmus
198 % (this is better explained in som_batchtrain and som_bmus)
199 Known = ~isnan(D); D(find(~Known)) = 0; % unknown components
200 blen = min(munits,dlen); % block size
201 W1 = mask*ones(1,blen); W2 = ones(munits,1)*mask'; D = D'; Known = Known';
202 i0 = 0;
203 while i0+1<=dlen,
204 inds = [(i0+1):min(dlen,i0+blen)]; i0 = i0+blen; % indeces
205 Dist = (M.^2)*(W1(:,1:length(inds)).*Known(:,inds)) ...
206 + W2*(D(:,inds).^2) ...
207 - 2*M*diag(mask)*D(:,inds); % squared distances
208 hits = hits + sum(1./(1+Dist/a),2);
209 end
210
211 otherwise,
212 error(['Unknown mode: ' mode]);
213
214 end
215
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217