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