comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_kmeanscolor2.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,centroids]=som_kmeanscolor2(mode,sM,C,initRGB,contrast,R)
2
3 % SOM_KMEANSCOLOR2 Color codes a SOM according to averaged or best K-means clustering
4 %
5 % color = som_kmeanscolor2('average',sM, C, [initRGB], [contrast],[R])
6 %
7 % color=som_kmeanscolor2('average',sM,[2 4 8 16],som_colorcode(sM,'rgb1'),'enhanced');
8 % [color,centroid]=som_kmeanscolor2('best',sM,15,[],'flat',R);
9 %
10 % Input and output arguments ([]'s are optional):
11 %
12 % mode (string) 'average' or 'best', defalut: 'average'
13 % sM (struct) a map struct
14 % C (vector) number of clusters
15 % [initRGB] (string, matrix) a color code string accepted by SOM_COLORCODE
16 % or an Mx3 matrix of RGB triples, where M is the number
17 % of map units. Default: SOM_COLORCODEs default
18 % [contrast] (string) 'flat', 'enhanced' color contrast mode, default:
19 % 'enhanced'.
20 % [R] (scalar) number of K-means trials, default: 30.
21 % color (matrix) Mx3xC of RGB triples
22 % centroid (array of matrices) centroid{i} includes codebook for the best
23 % k-means for C(i) clusters, i.e. the cluster centroids corresponding to
24 % the color code color(:,:,i).
25 %
26 % The function gives a set of color codes for the SOM according to K-means
27 % clustering. It has two operation modes:
28 %
29 % 'average': The idea of coloring is that the color of the units belonging to the same
30 % cluster is the mean of the original RGB values (see SOM_COLORCODE) of the map units
31 % belonging to the cluster (see SOM_CLUSTERCOLOR). The K-means clustering is made,
32 % by default, 30 times and the resulting color codes are averaged for
33 % each specified number of clusters C(i), i=1,...,k. In a way, the resulting averaged color
34 % codes reflect the stability of the K-means clustering made on the map units.
35 %
36 % 'best': runs the k-means R times for C(i), i=1,...,n clusters as in previous mode,
37 % but instead of averaging all the R color codes, it picks the one that corresponds to the
38 % best k-means clustering for each C(i). The 'best' is the one with the lowest
39 % quantization error. The result may differ from run to run.
40 %
41 % EXAMPLE
42 %
43 % load iris; % or any other map struct sM
44 % color=som_kmeanscolor2('average',sM,[2:6]);
45 % som_show(sM,'umat','all','color',color);
46 %
47 % See also SOM_KMEANS, SOM_SHOW, SOM_COLORCODE, SOM_CLUSTERCOLOR, SOM_KMEANSCOLOR
48
49 % Contributed to SOM Toolbox 2.0, 2001 February by Johan Himberg
50 % Copyright (c) by Johan Himberg
51 % http://www.cis.hut.fi/projects/somtoolbox/
52
53 %%% Check number of inputs
54
55 error(nargchk(3, 6, nargin)); % check no. of input args
56
57 %%% Check input args & set defaults
58
59 if ~vis_valuetype(mode,{'string'}),
60 error('Mode must be a string.');
61 end
62 switch lower(mode),
63 case{'average','best'}
64 ;
65 otherwise
66 error('Mode must be string ''average'' or ''best''.');
67 end
68
69 if isstruct(sM) & isfield(sM,'type') & strcmp(sM.type,'som_map'),
70 [tmp,lattice,msize]=vis_planeGetArgs(sM);
71 munits=prod(msize);
72 if length(msize)>2
73 error('Does not work with 3D maps.')
74 end
75 else
76 error('Map struct required for the second input argument!');
77 end
78
79 if ~vis_valuetype(C,{'1xn','nx1'}),
80 error('Vector value expected for cluster number.');
81 end
82
83 % Round C and check
84 C=round(C(:)');
85
86 if any(C<2),
87 error('Cluster number must be 2 or more.');
88 end
89
90 % check initial color coding
91 if nargin<4 | isempty(initRGB)
92 initRGB=som_colorcode(sM);
93 end
94
95 % check contrast checking
96 if nargin<5 | isempty(contrast),
97 contrast='enhanced';
98 end
99
100 if ~ischar(contrast),
101 error('String input expected for input arg. ''contrast''.');
102 else
103 switch lower(contrast)
104 case {'flat','enhanced'}
105 ;
106 otherwise
107 error(['''flat'' or ''enhanced'' expected for '...
108 'input argument ''contrast''.']);
109 end
110 end
111
112 if ischar(initRGB),
113 try
114 initRGB=som_colorcode(sM,initRGB);
115 catch
116 error(['Color code ' initRGB ...
117 'was not recognized by SOM_COLORCODE.']);
118 end
119 elseif vis_valuetype(initRGB,{'nx3rgb',[munits 3]},'all'),
120 ;
121 else
122 error(['The initial color code must be a string '...
123 'or an Mx3 matrix of RGB triples.']);
124 end
125
126 if nargin<6|isempty(R),
127 R=30;
128 end
129
130 if ~vis_valuetype(R,{'1x1'}),
131 error('''R'' must be scalar.');
132 end
133
134 %%% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135
136 disp('Wait...');
137 index=0; hit_=zeros(munits,munits);
138
139 switch mode,
140 %% Averaged k-means coloring
141 case 'average'
142 for k=C,
143 disp(['Running K-means for ' num2str(k) ' clusters...']);
144 color_=zeros(munits,3);
145 colord_=color_;
146 % Average R k-means colorings for C clusters
147 for j=1:R,
148 [dummy,c]=som_kmeans('batch',sM,k,100,0); % max 100 iterations, verbose off
149 color_=color_+som_clustercolor(sM,c,initRGB);
150 end
151 index=index+1;
152 color(:,:,index)=color_./R;
153 end
154
155 %% coloring for 'best' k-means coloring
156 case 'best'
157 for k=C,
158 disp(['Running K-means for ' num2str(k) ' clusters...']);
159 c=[];err=Inf; div=[];
160 %% look for the best k-means among R trials
161 for i=1:R,
162 [c_,div_,err_(i)]=som_kmeans('batch',sM,k,100,0); % max 100 iterations, verbose off
163 if err_(i)<err,
164 err=err_(i); c=c_; div=div_;
165 end
166 end
167 % record the 'best' k-means for C clusters
168 index=index+1;
169 color(:,:,index)=som_clustercolor(sM,div,initRGB);
170 centroid{index}=c;
171 end
172 end
173
174 %%% Build output
175
176 switch contrast
177 case 'flat'
178 ;
179 case 'enhanced'
180 warning off;
181 ncolor=maxnorm(color);
182 ncolor(~isfinite(ncolor))=color(~isfinite(ncolor));
183 color=ncolor;
184 warning on;
185 end
186
187
188 %%% Subfunctions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 function X=maxnorm(x)
190 % normalize columns of x between [0,1]
191
192 x=x-repmat(min(x),[size(x,1) 1 1]);
193 X=x./repmat(max(x),[size(x,1) 1 1]);