annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_clustercolor.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function color=som_clustercolor(m, class, colorcode)
wolffd@0 2
wolffd@0 3 % SOM_CLUSTERCOLOR Sets map unit coloring according to classification
wolffd@0 4 %
wolffd@0 5 % syntax 1: color = som_clustercolor(m, class, [colorcode])
wolffd@0 6 % syntax 2: color = som_clustercolor(class, colormatrix)
wolffd@0 7 %
wolffd@0 8 % Input and output arguments ([]'s are optional):
wolffd@0 9 % m (struct) map or topol struct
wolffd@0 10 % (cell array) of form {str,[m1 m2]} where str = 'hexa'
wolffd@0 11 % or 'rect' and [m1 m2] = msize.
wolffd@0 12 % class (matrix) Mxn matrix of integers (class labels)
wolffd@0 13 % where M is the number of map units and each
wolffd@0 14 % column gives some classification for the units.
wolffd@0 15 % colorcode (string) 'rgb1', 'rgb2' (default), 'rgb3', 'rgb4', 'hsv'.
wolffd@0 16 % colormatrix (matrix) Mx3 matrix of RGB triplets giving the
wolffd@0 17 % initial color code for each unit.
wolffd@0 18 % color (matrix) size Mx3xn of RGB triplets giving the
wolffd@0 19 % resulting color code for each unit
wolffd@0 20 %
wolffd@0 21 % The function gives a color coding by class and location for the
wolffd@0 22 % map units. The color is determined by calculating the mean of the
wolffd@0 23 % initial RGB values of units belonging to the same class.
wolffd@0 24 %
wolffd@0 25 % Function has two syntaxes:
wolffd@0 26 %
wolffd@0 27 % * If first argument gives the map topology, i.e. is map or topol struct
wolffd@0 28 % or cell indicating the topology, the initial color coding of the
wolffd@0 29 % units may be given by a string ('rgb1','rgb2','rgb3','rgb4', or 'hsv')
wolffd@0 30 % which describe a predefined coloring scheme. (see SOM_COLORCODE).
wolffd@0 31 % or an initial color matrix of size Mx3 with RGB triplets as rows.
wolffd@0 32 % * Another possibility is to give just the classification vector
wolffd@0 33 % of size Mx1 and an initial color matrix of size Mx3 with RGB
wolffd@0 34 % triplets as rows.
wolffd@0 35 %
wolffd@0 36 % EXAMPLE (requires Matlab Statistics Toolbox)
wolffd@0 37 %
wolffd@0 38 % % Do a 10-cluster single linkage hierachical clustering for SOM units
wolffd@0 39 % class=cluster(linkage(pdist(sM.codebook),'single'),10);
wolffd@0 40 % % Color code the clusters
wolffd@0 41 % C=som_clustercolor(sM, class, 'rgb2');
wolffd@0 42 % % Visualize
wolffd@0 43 % som_show(sM,'color',C);
wolffd@0 44 %
wolffd@0 45 % See also SOM_COLORCODE, SOM_KMEANSCOLOR, SOM_CPLANE, SOM_SHOW
wolffd@0 46
wolffd@0 47 % Contributed to SOM Toolbox 2.0, February 11th, 2000 by Johan Himberg
wolffd@0 48 % Copyright (c) by Johan Himberg
wolffd@0 49 % http://www.cis.hut.fi/projects/somtoolbox/
wolffd@0 50
wolffd@0 51 % Version 2.0beta Johan 100200
wolffd@0 52
wolffd@0 53 %%% Check arguments %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 54
wolffd@0 55 error(nargchk(2, 3, nargin)); % check no. of input args is correct
wolffd@0 56
wolffd@0 57 % Check 1s argument
wolffd@0 58
wolffd@0 59 % Class matrix?
wolffd@0 60 if vis_valuetype(m, {'nxm'});
wolffd@0 61 colorcode=class;
wolffd@0 62 class=m;
wolffd@0 63 if ~vis_valuetype(colorcode,{'nx3rgb',[size(class,1) 3]},'all'),
wolffd@0 64 error(['If map or topol is not specified the colorcode must be a' ...
wolffd@0 65 ' [size(class,1) 3] sized RGB matrix.']);
wolffd@0 66 end
wolffd@0 67 else
wolffd@0 68 [tmp,ok,tmp]=som_set(m);
wolffd@0 69 if isstruct(m) & all(ok)
wolffd@0 70 switch m.type
wolffd@0 71 case 'som_topol' % topol?
wolffd@0 72 msize=m.msize;
wolffd@0 73 lattice=m.lattice;
wolffd@0 74 case 'som_map'
wolffd@0 75 msize=m.topol.msize; % map?
wolffd@0 76 lattice=m.topol.lattice;
wolffd@0 77 otherwise
wolffd@0 78 error('Invalid map or topol struct.');
wolffd@0 79 end
wolffd@0 80 % cell?
wolffd@0 81 elseif iscell(m) & vis_valuetype(size(m),{[1 2]}),
wolffd@0 82 if vis_valuetype(m{2},{[1 2]}) & vis_valuetype(m{1},{'string'}),
wolffd@0 83 lattice=m{1};
wolffd@0 84 msize=m{2};
wolffd@0 85 else
wolffd@0 86 error('Invalid map size information.');
wolffd@0 87 end
wolffd@0 88 else
wolffd@0 89 % not known type
wolffd@0 90 error('Invalid first argument!');
wolffd@0 91 end
wolffd@0 92 % Check map parameters
wolffd@0 93 switch lattice % lattice
wolffd@0 94 case 'hexa'
wolffd@0 95 ;
wolffd@0 96 case 'rect'
wolffd@0 97 ;
wolffd@0 98 otherwise
wolffd@0 99 error('Unknown lattice type');
wolffd@0 100 end
wolffd@0 101 if length(msize)>2 % dimension
wolffd@0 102 error('Only 2D maps allowed!');
wolffd@0 103 end
wolffd@0 104 % Check colorcode
wolffd@0 105 if nargin<3 | isempty(colorcode)
wolffd@0 106 colorcode='rgb2';
wolffd@0 107 end
wolffd@0 108 end
wolffd@0 109
wolffd@0 110 % Check class
wolffd@0 111 if any(class~=round(class))
wolffd@0 112 error('Class labels must be integer numbers.');
wolffd@0 113 end
wolffd@0 114
wolffd@0 115 if min(class)<=0
wolffd@0 116 error('Class numbers should be greater than 0');
wolffd@0 117 end
wolffd@0 118
wolffd@0 119 if ischar(colorcode),
wolffd@0 120 switch colorcode
wolffd@0 121 case{'rgb1','rgb2','rgb3','rgb4','hsv'}
wolffd@0 122 colorcode=som_colorcode(m, colorcode);
wolffd@0 123 otherwise
wolffd@0 124 error(['Color code not known: should be ''rgb1'',''rgb2'',' ...
wolffd@0 125 ' ''rgb3'',''rgb4'' or ''hsv''.']);
wolffd@0 126 end
wolffd@0 127 elseif ~vis_valuetype(colorcode,{'nx3rgb',[size(class,1) 3]},'all');
wolffd@0 128 error(['Invalid colorcode matrix: should be a ' ...
wolffd@0 129 '[length(class) 3] sized RGB matrix.']);
wolffd@0 130 end
wolffd@0 131
wolffd@0 132 %% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 133
wolffd@0 134 % Go through all i classifications (columns)
wolffd@0 135 for i=1:size(class,2),
wolffd@0 136 % Get unique class labels in ith classification
wolffd@0 137 c=unique(class(:,i))'; % row vector for loop indexing
wolffd@0 138 % Go through all class in ith classification
wolffd@0 139 for j=c;
wolffd@0 140 index=(class(:,i)==j);
wolffd@0 141 N=sum(index);
wolffd@0 142 colors=colorcode(index,:);
wolffd@0 143 % Calculate the mean color
wolffd@0 144 meancolor=repmat(mean(colors,1),N,1);
wolffd@0 145 % Select the original color that is closest to this mean
wolffd@0 146 dist=sum((meancolor-colors).^2,2);
wolffd@0 147 [tmp,min_dist_index]=min(dist);
wolffd@0 148 best_color=repmat(colors(min_dist_index,:),N,1);
wolffd@0 149 % Set the color to output variable
wolffd@0 150 color(index,:,i)=best_color;
wolffd@0 151 end
wolffd@0 152 end