annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_fuzzycolor.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 [color,X]=som_fuzzycolor(sM,T,R,mode,initRGB,S)
Daniel@0 2
Daniel@0 3 % SOM_FUZZYCOLOR Heuristic contraction projection/soft cluster color coding for SOM
Daniel@0 4 %
Daniel@0 5 % function [color,X]=som_fuzzycolor(map,[T],[R],[mode],[initRGB],[S])
Daniel@0 6 %
Daniel@0 7 % sM (map struct)
Daniel@0 8 % [T] (scalar) parameter that defines the speed of contraction
Daniel@0 9 % T<1: slow contraction, T>1: fast contraction. Default: 1
Daniel@0 10 % [R] (scalar) number of rounds, default: 30
Daniel@0 11 % [mode] (string) 'lin' or 'exp', default: 'lin'
Daniel@0 12 % [initRGB] (string) Strings accepted by SOM_COLORCODE, default: 'rgb2'
Daniel@0 13 % [S] (matrix) MxM matrix a precalculated similarity matrix
Daniel@0 14 % color (matrix) of size MxRx3 resulting color codes at each step
Daniel@0 15 % X (matrix) of size MxRx2 coordiantes for projected unit weight vectors
Daniel@0 16 % at each step of iteration. (Color code C is calculated using this
Daniel@0 17 % projection.)
Daniel@0 18 %
Daniel@0 19 % The idea of the projection is to use a naive contraction model which
Daniel@0 20 % pulls the units together. Units that are close to each other in the
Daniel@0 21 % output space (clusters) contract faster into the same point in the
Daniel@0 22 % projection. The original position for each unit is its location in
Daniel@0 23 % the topological grid.
Daniel@0 24 %
Daniel@0 25 % This is an explorative tool to color code the map units so that
Daniel@0 26 % similar units (in the sense of euclidean norm) have similar coloring
Daniel@0 27 % (See also SOM_KMEANSCOLOR) The tool gives a series of color codings
Daniel@0 28 % which start from an initial color coding (see SOM_COLORCODE) and
Daniel@0 29 % show the how the fuzzy clustering process evolves.
Daniel@0 30 %
Daniel@0 31 % The speed of contraction is controlled by the input parameter T. If
Daniel@0 32 % it is high the projection contracts more slowly and reveals more
Daniel@0 33 % intermediate stages (hierarchy). A good value for T must be
Daniel@0 34 % searched manually. It is probable that the default values do not
Daniel@0 35 % yield good results.
Daniel@0 36 %
Daniel@0 37 % The conatrction process may be slow. In this case the mode can be
Daniel@0 38 % set to 'exp' instead of 'lin', however, then the computing becomes
Daniel@0 39 % heavier.
Daniel@0 40 %
Daniel@0 41 % EXAMPLE
Daniel@0 42 %
Daniel@0 43 % load iris; % or any other map struct sM
Daniel@0 44 % [color]=som_fuzzycolor(sM,'lin',10);
Daniel@0 45 % som_show(sM,'color',color);
Daniel@0 46 %
Daniel@0 47 % See also SOM_KMEANSCOLOR, SOM_COLORCODE, SOM_CLUSTERCOLOR
Daniel@0 48 %
Daniel@0 49 % REFERENCES
Daniel@0 50 %
Daniel@0 51 % Johan Himberg, "A SOM Based Cluster Visualization and Its
Daniel@0 52 % Application for False Coloring", in Proceedings of International
Daniel@0 53 % Joint Conference on Neural Networks (IJCNN2000)},
Daniel@0 54 % pp. 587--592,Vol. 3, 2000
Daniel@0 55 %
Daniel@0 56 % Esa Alhoniemi, Johan Himberg, and Juha Vesanto, Probabilistic
Daniel@0 57 % Measures for Responses of Self-Organizing Map Units, pp. 286--290,
Daniel@0 58 % in Proceedings of the International ICSC Congress on Computational
Daniel@0 59 % Intelligence Methods and Applications (CIMA '99)}, ICSC Academic
Daniel@0 60 % Press}, 1999
Daniel@0 61 %
Daniel@0 62 % Outline of the heuristic
Daniel@0 63 %
Daniel@0 64 % First a matrix D of squared pairwise euclidean distances
Daniel@0 65 % D(i,j)=d(i,j)^2 between map weight vectors is calculated. This
Daniel@0 66 % matrix is transformed into a similarity matrix S,
Daniel@0 67 % s(i,j)=exp(-(D(i,j)/(T.^2*v)), where T is a free input parameter and
Daniel@0 68 % v the variance of all elements of D v=var(D(:)). The matrix is
Daniel@0 69 % further normalized so that all rows sum to one. The original
Daniel@0 70 % topological coordinates X=som_unit_coords(sM) are successively
Daniel@0 71 % averaged using this matrix. X(:,:,i)=S^i*X(:,:,1); As the process is
Daniel@0 72 % actually a series of successive weighted averagings of the initial
Daniel@0 73 % coordinates, all projected points eventually contract into one
Daniel@0 74 % point. T is a user defined parameter that defines how fast the
Daniel@0 75 % projection contracts into this center point. If T is too small, the
Daniel@0 76 % process will end into the center point at once.
Daniel@0 77 %
Daniel@0 78 % In practise, we don't calculate powers of S, but compute
Daniel@0 79 %
Daniel@0 80 % X(:,:,i)=S.*X(:,:,i-1); % mode: 'lin'
Daniel@0 81 %
Daniel@0 82 % The contraction process may be slow if T is selected to be large,
Daniel@0 83 % then for each step the similarity matrix is squared
Daniel@0 84 %
Daniel@0 85 % X(:,:,i)=S*X(:,:,1); S=S*S % mode: 'exp'
Daniel@0 86 %
Daniel@0 87 % The coloring is done using the function SOM_COLORCODE according to
Daniel@0 88 % the projections in X, The coordinates are rescaled in order to
Daniel@0 89 % achieve maximum color resolution.
Daniel@0 90
Daniel@0 91 % Contributed to SOM Toolbox vs2, 2000 by Johan Himberg
Daniel@0 92 % Copyright (c) by Johan Himberg
Daniel@0 93 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 94
Daniel@0 95 % Previously rownorm function normalized the rows of S erroneously
Daniel@0 96 % into unit length, this major bug was corrected 14042003. Now the
Daniel@0 97 % rownorm normalizes the rows to have unit sum as it should johan 14042003
Daniel@0 98
Daniel@0 99 %% Check input arguments
Daniel@0 100
Daniel@0 101 if isstruct(sM),
Daniel@0 102 if ~isfield(sM,'topol')
Daniel@0 103 error('Topology field missing.');
Daniel@0 104 end
Daniel@0 105 M=size(sM.codebook,1);
Daniel@0 106 else
Daniel@0 107 error('Requires a map struct.');
Daniel@0 108 end
Daniel@0 109
Daniel@0 110 if nargin<2 | isempty(T),
Daniel@0 111 T=1;
Daniel@0 112 end
Daniel@0 113 if ~vis_valuetype(T,{'1x1'})
Daniel@0 114 error('Input for T must be a scalar.');
Daniel@0 115 end
Daniel@0 116
Daniel@0 117 if nargin<3 | isempty(R),
Daniel@0 118 R=30;
Daniel@0 119 end
Daniel@0 120 if ~vis_valuetype(R,{'1x1'})
Daniel@0 121 error('Input for R must be a scalar.');
Daniel@0 122 end
Daniel@0 123
Daniel@0 124 if nargin < 4 | isempty(mode),
Daniel@0 125 mode='lin';
Daniel@0 126 end
Daniel@0 127 if ~ischar(mode),
Daniel@0 128 error('String input expected for mode.');
Daniel@0 129 else
Daniel@0 130 mode=lower(mode);
Daniel@0 131 switch mode
Daniel@0 132 case {'lin','exp'}
Daniel@0 133 ;
Daniel@0 134 otherwise
Daniel@0 135 error('Input for mode must be ''lin'' or ''exp''.');
Daniel@0 136 end
Daniel@0 137 end
Daniel@0 138
Daniel@0 139 if nargin < 5 | isempty(initRGB)
Daniel@0 140 initRGB='rgb2';
Daniel@0 141 end
Daniel@0 142
Daniel@0 143 if ischar(initRGB),
Daniel@0 144 try
Daniel@0 145 dummy=som_colorcode(sM,initRGB);
Daniel@0 146 catch
Daniel@0 147 error(['Color code ''' initRGB ''' not known, see SOM_COLORCODE.']);
Daniel@0 148 end
Daniel@0 149 else
Daniel@0 150 error('Invalid color code string');
Daniel@0 151 end
Daniel@0 152
Daniel@0 153 if nargin<6 | isempty(S),
Daniel@0 154 S=fuzzysimilarity(sM,1./T);
Daniel@0 155 end
Daniel@0 156
Daniel@0 157 if ~vis_valuetype(S,{[M M]}),
Daniel@0 158 error('Similarity matrix must be a MunitsxMunits matrix.')
Daniel@0 159 end
Daniel@0 160
Daniel@0 161 x = maxnorm(som_unit_coords(sM.topol.msize,sM.topol.lattice,'sheet'));
Daniel@0 162
Daniel@0 163 x = x-repmat(mean(x),size(x,1),1);
Daniel@0 164
Daniel@0 165 X(:,:,1)=x;
Daniel@0 166 color(:,:,1)=som_colorcode(x,'rgb2',1);
Daniel@0 167
Daniel@0 168 %%% Actions
Daniel@0 169
Daniel@0 170 for i=1:R,
Daniel@0 171 switch mode
Daniel@0 172 case 'exp'
Daniel@0 173 S=rownorm(S*S);
Daniel@0 174 tmpX=S*X(:,:,1);
Daniel@0 175 case 'lin'
Daniel@0 176 tmpX=S*X(:,:,i);
Daniel@0 177 end
Daniel@0 178 X(:,:,i+1)=tmpX;
Daniel@0 179 color(:,:,i+1)=som_colorcode(X(:,:,i+1),initRGB);
Daniel@0 180 end
Daniel@0 181
Daniel@0 182 color(isnan(color))=0;
Daniel@0 183
Daniel@0 184 function r=fuzzysimilarity(sM,p)
Daniel@0 185 % Calculate a "fuzzy response" similarity matrix
Daniel@0 186 % sM: map
Daniel@0 187 % p: sharpness factor
Daniel@0 188 d=som_eucdist2(sM,sM);
Daniel@0 189 v=std(sqrt(d(:))).^2;
Daniel@0 190 r=rownorm(exp(-p^2*(d./v)));
Daniel@0 191 r(~isfinite(r))=0;
Daniel@0 192 return;
Daniel@0 193
Daniel@0 194
Daniel@0 195 function X = rownorm(X)
Daniel@0 196
Daniel@0 197 r = sum(X,2);
Daniel@0 198 X = X ./ r(:,ones(size(X,2),1));
Daniel@0 199 return;
Daniel@0 200
Daniel@0 201
Daniel@0 202 function X = maxnorm(X)
Daniel@0 203
Daniel@0 204 for i=1:size(X,2), r = (max(X(:,i))-min(X(:,i))); if r, X(:,i) = X(:,i) / r; end, end
Daniel@0 205 return;