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