annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_kmeanscolor2.m @ 0:cc4b1211e677 tip

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