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