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