comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_kmeanscolor.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,best,kmeans]=som_kmeanscolor(sM,C,initRGB,contrast)
2
3 % SOM_KMEANSCOLOR Map unit color code according to K-means clustering
4 %
5 % [color, best, kmeans] = som_kmeanscolor(sM, C, [initRGB],[contrast])
6 %
7 % color = som_kmeanscolor(sM,15,som_colorcode(sM,'rgb1'),'enhance');
8 % [color,best] = som_kmeanscolor(sM,15,[],'normal');
9 %
10 % Input and output arguments ([]'s are optional):
11 % sM (struct) map struct
12 % C (scalar) maximum number of clusters
13 % initRGB (string, matrix) color code string accepted by SOM_COLORCODE
14 % or an Mx3 matrix of RGB triples, where M is the number
15 % of map units. Default: SOM_COLORCODEs default
16 % contrast (string) 'flat', 'enhanced' color contrast mode, default:
17 % 'enhanced'
18 %
19 % color (matrix) MxCx3 of RGB triples
20 % best (scalar) index for "best" clustering according to
21 % Davies-Boulding index; color(:,:,best) includes the
22 % corresponding color code.
23 % kmeans (cell) output of KMEANS_CLUSTERS in a cell array.
24 %
25 % The function gives a set of color codings according to K-means
26 % clustering. For clustering, it uses function KMEANS_CLUSTERS for map units,
27 % and it calculates color codings for 1,2,...,C clusters.
28 % The idea of coloring is that the color of a cluster is the mean of the
29 % original colors (RGB values) of the map units belonging to that cluster,
30 % see SOM_CLUSTERCOLOR. The original colors are defined by SOM_COLORCODE
31 % by default. Input 'contrast' simply specifies whether or not
32 % to linearly redistribute R,G, and B values so that minimum is 0 and
33 % maximum 1 ('enahanced') or to use directly the output of
34 % SOM_CLUSTERCOLOR ('flat'). KMEANS_CLUSTERS uses certain heuristics to
35 % select the best of 5 trials for each number of clusters. Evaluating the
36 % clustering multiple times may take some time.
37 %
38 % EXAMPLE
39 %
40 % load iris; % or any other map struct sM
41 % [color,b]=som_kmeanscolor(sM,10);
42 % som_show(sM,'color',color,'color',{color(:,:,b),'"Best clustering"');
43 %
44 % See also SOM_SHOW, SOM_COLORCODE, SOM_CLUSTERCOLOR, KMEANS_CLUSTERS
45
46 % Contributed to SOM Toolbox 2.0, April 1st, 2000 by Johan Himberg
47 % Copyright (c) by Johan Himberg
48 % http://www.cis.hut.fi/projects/somtoolbox/
49
50 % corrected help text 11032005 johan
51
52 %%% Check number of inputs
53
54 error(nargchk(2, 4, nargin)); % check no. of input args
55
56 %%% Check input args & set defaults
57
58 if isstruct(sM) & isfield(sM,'type') & strcmp(sM.type,'som_map'),
59 [tmp,lattice,msize]=vis_planeGetArgs(sM);
60 munits=prod(msize);
61 if length(msize)>2
62 error('Does not work with 3D maps.')
63 end
64 else
65 error('Map struct requires for first input argument!');
66 end
67
68 if ~vis_valuetype(C,{'1x1'}),
69 error('Scalar value expect for maximum number of clusters.');
70 end
71
72 % check initial color coding
73 if nargin<3 | isempty(initRGB)
74 initRGB=som_colorcode(sM);
75 end
76
77 % check contrast checking
78 if nargin<4 | isempty(contrast),
79 contrast='enhanced';
80 end
81
82 if ~ischar(contrast),
83 error('String input expected for input arg. ''contrast''.');
84 else
85 switch lower(contrast)
86 case {'flat','enhanced'}
87 ;
88 otherwise
89 error(['''flat'' or ''enhanced'' expected for '...
90 'input argument ''contrast''.']);
91 end
92 end
93
94 if ischar(initRGB),
95 try
96 initRGB=som_colorcode(sM,initRGB);
97 catch
98 error(['Color code ' initRGB ...
99 'was not recognized by SOM_COLORCODE.']);
100 end
101 elseif vis_valuetype(initRGB,{'nx3rgb',[munits 3]},'all'),
102 ;
103 else
104 error(['The initial color code must be a string '...
105 'or an Mx3 matrix of RGB triples.']);
106 end
107
108 %%% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109
110 disp('Wait...');
111 [c,p,err,ind]=kmeans_clusters(sM,C,5,0); % use 5 trials, verbose off
112
113 % Store outputs to kmeans
114 kmeans{1}=c;
115 kmeans{2}=p;
116 kmeans{3}=err;
117 kmeans{4}=ind;
118
119 %%% Build output
120 color=som_clustercolor(sM,cat(2,p{:}),initRGB);
121 [tmp,best]=min(ind);
122
123 switch contrast
124 case 'flat'
125 ;
126 case 'enhanced'
127 warning off;
128 ncolor=maxnorm(color);
129 ncolor(~isfinite(ncolor))=color(~isfinite(ncolor));
130 color=ncolor;
131 warning on;
132 end
133
134 %%% Subfunctions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 function X=maxnorm(x)
136 % normalize columns of x between [0,1]
137
138 x=x-repmat(min(x),[size(x,1) 1 1]);
139 X=x./repmat(max(x),[size(x,1) 1 1]);