annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_hits.m @ 0:cc4b1211e677 tip

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