annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_dendrogram.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 [h,Coord,Color,height] = som_dendrogram(Z,varargin)
Daniel@0 2
Daniel@0 3 %SOM_DENDROGRAM Visualize a dendrogram.
Daniel@0 4 %
Daniel@0 5 % [h,Coord,Color,height] = som_dendrogram(Z, [[argID,] value, ...])
Daniel@0 6 %
Daniel@0 7 % Z = som_linkage(sM);
Daniel@0 8 % som_dendrogram(Z);
Daniel@0 9 % som_dendrogram(Z,sM);
Daniel@0 10 % som_dendrogram(Z,'coord',co);
Daniel@0 11 %
Daniel@0 12 % Input and output arguments ([]'s are optional):
Daniel@0 13 % h (vector) handle to the arc lines
Daniel@0 14 % Z (matrix) size n-1 x 1, the hierarchical cluster matrix
Daniel@0 15 % returned by functions like LINKAGE and SOM_LINKAGE
Daniel@0 16 % n is the number of original data samples.
Daniel@0 17 % [argID, (string) See below. The values which are unambiguous can
Daniel@0 18 % value] (varies) be given without the preceeding argID.
Daniel@0 19 % Coord (matrix) size 2*n-1 x {1,2}, the coordinates of the
Daniel@0 20 % original data samples and cluster nodes used
Daniel@0 21 % in the visualization
Daniel@0 22 % Color (matrix) size 2*n-1 x 3, the colors of ...
Daniel@0 23 % height (vector) size 2*n-1 x 1, the heights of ...
Daniel@0 24 %
Daniel@0 25 % Here are the valid argument IDs and corresponding values. The values
Daniel@0 26 % which are unambiguous (marked with '*') can be given without the
Daniel@0 27 % preceeding argID.
Daniel@0 28 % 'data' *(struct) map or data struct: many other optional
Daniel@0 29 % arguments require this
Daniel@0 30 % (matrix) data matrix
Daniel@0 31 % 'coord' (matrix) size n x 1 or n x 2, the coordinates of
Daniel@0 32 % the original data samples either in 1D or 2D
Daniel@0 33 % (matrix) size 2*n-1 x {1,2}, the coordinates of both
Daniel@0 34 % original data samples and each cluster
Daniel@0 35 % *(string) 'SOM', 'pca#', 'sammon#', or 'cca#': the coordinates
Daniel@0 36 % are calculated using the given data and the
Daniel@0 37 % required projection algorithm. The '#' at the
Daniel@0 38 % end of projection algorithms refers to the
Daniel@0 39 % desired output dimension and can be either 1 or 2
Daniel@0 40 % (2 by default). In case of 'SOM', the unit
Daniel@0 41 % coordinates (given by SOM_VIS_COORDS) are used.
Daniel@0 42 % 'color' (matrix) size n x 3, the color of the original data samples
Daniel@0 43 % (matrix) size 2*n-1 x 3, the colors of both original
Daniel@0 44 % data samples and each cluster
Daniel@0 45 % (string) color specification, e.g. 'r.', used for each node
Daniel@0 46 % 'height' (vector) size n-1 x 1, the heights used for each cluster
Daniel@0 47 % (vector) size 2*n-1 x 1, the heights used for both original
Daniel@0 48 % data samples and each cluster
Daniel@0 49 % *(string) 'order', the order of combination determines height
Daniel@0 50 % 'depth', the depth at which the combination
Daniel@0 51 % happens determines height
Daniel@0 52 % 'linecolor' (string) color specification for the arc color, 'k' by default
Daniel@0 53 % (vector) size 1 x 3
Daniel@0 54 %
Daniel@0 55 % See also SOM_LINKAGE, DENDROGRAM.
Daniel@0 56
Daniel@0 57 % Copyright (c) 2000 by Juha Vesanto
Daniel@0 58 % Contributed to SOM Toolbox on June 16th, 2000 by Juha Vesanto
Daniel@0 59 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 60
Daniel@0 61 % Version 2.0beta juuso 160600
Daniel@0 62
Daniel@0 63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 64 %% read the arguments
Daniel@0 65
Daniel@0 66 % Z
Daniel@0 67 nd = size(Z,1)+1;
Daniel@0 68 nc = size(Z,1);
Daniel@0 69
Daniel@0 70 % varargin
Daniel@0 71 Coordtype = 'natural'; Coord = []; codim = 1;
Daniel@0 72 Colortype = 'none'; Color = [];
Daniel@0 73 height = [zeros(nd,1); Z(:,3)];
Daniel@0 74 M = [];
Daniel@0 75 linecol = 'k';
Daniel@0 76
Daniel@0 77 i=1;
Daniel@0 78 while i<=length(varargin),
Daniel@0 79 argok = 1;
Daniel@0 80 if ischar(varargin{i}),
Daniel@0 81 switch varargin{i},
Daniel@0 82 case 'data', i = i + 1; M = varargin{i};
Daniel@0 83 case 'coord',
Daniel@0 84 i=i+1;
Daniel@0 85 if isnumeric(varargin{i}), Coord = varargin{i}; Coordtype = 'given';
Daniel@0 86 else
Daniel@0 87 if strcmp(varargin{i},'SOM'), Coordtype = 'SOM';
Daniel@0 88 else Coordtype = 'projection'; Coord = varargin{i};
Daniel@0 89 end
Daniel@0 90 end
Daniel@0 91 case 'color',
Daniel@0 92 i=i+1;
Daniel@0 93 if isempty(varargin{i}), Colortype = 'none';
Daniel@0 94 elseif ischar(varargin{i}), Colortype = 'colorspec'; Color = varargin{i};
Daniel@0 95 else Colortype = 'given'; Color = varargin{i};
Daniel@0 96 end
Daniel@0 97 case 'height', i=i+1; height = varargin{i};
Daniel@0 98 case 'linecolor', i=i+1; linecol = varargin{i};
Daniel@0 99 case 'SOM',
Daniel@0 100 Coordtype = 'SOM';
Daniel@0 101 case {'pca','pca1','pca2','sammon','sammon1','sammon2','cca','cca1','cca2'},
Daniel@0 102 Coordtype = 'projection'; Coord = varargin{i};
Daniel@0 103 case {'order','depth'}, height = varargin{i};
Daniel@0 104 end
Daniel@0 105 elseif isstruct(varargin{i}), M = varargin{i};
Daniel@0 106 else
Daniel@0 107 argok = 0;
Daniel@0 108 end
Daniel@0 109 if ~argok,
Daniel@0 110 disp(['(som_dendrogram) Ignoring invalid argument #' num2str(i+1)]);
Daniel@0 111 end
Daniel@0 112 i = i+1;
Daniel@0 113 end
Daniel@0 114
Daniel@0 115 switch Coordtype,
Daniel@0 116 case 'SOM',
Daniel@0 117 if isempty(M) | ~any(strcmp(M.type,{'som_map','som_topol'})) ,
Daniel@0 118 error('Cannot determine SOM coordinates without a SOM.');
Daniel@0 119 end
Daniel@0 120 if strcmp(M.type,'som_map'), M = M.topol; end
Daniel@0 121 case 'projection',
Daniel@0 122 if isempty(M), error('Cannot do projection without the data.'); end
Daniel@0 123 if isstruct(M),
Daniel@0 124 if strcmp(M.type,'som_data'), M = M.data;
Daniel@0 125 elseif strcmp(M.type,'som_map'), M = M.codebook;
Daniel@0 126 end
Daniel@0 127 end
Daniel@0 128 if size(M,1) ~= nd,
Daniel@0 129 error('Given data must be equal in length to the number of original data samples.')
Daniel@0 130 end
Daniel@0 131 case 'given',
Daniel@0 132 if size(Coord,1) ~= nd & size(Coord,1) ~= nd+nc,
Daniel@0 133 error('Size of given coordinate matrix does not match the cluster hierarchy.');
Daniel@0 134 end
Daniel@0 135 end
Daniel@0 136
Daniel@0 137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 138 %% initialization
Daniel@0 139
Daniel@0 140 % Coordinates
Daniel@0 141 switch Coordtype,
Daniel@0 142 case 'natural', o = leavesorder(Z)'; [dummy,Coord] = sort(o); codim = 1;
Daniel@0 143 case 'SOM', Coord = som_vis_coords(M.lattice,M.msize); codim = 2;
Daniel@0 144 case 'projection',
Daniel@0 145 switch Coord,
Daniel@0 146 case {'pca','pca2'}, Coord = pcaproj(M,2); codim = 2;
Daniel@0 147 case 'pca1', Coord = pcaproj(M,1); codim = 1;
Daniel@0 148 case {'cca','cca2'}, Coord = cca(M,2,20); codim = 2;
Daniel@0 149 case 'cca1', Coord = cca(M,1,20); codim = 1;
Daniel@0 150 case {'sammon','sammon2'}, Coord = sammon(M,2,50); codim = 2;
Daniel@0 151 case 'sammon1', Coord = sammon(M,1,50); codim = 1;
Daniel@0 152 end
Daniel@0 153 case 'given', codim = min(size(Coord,2),2); % nill
Daniel@0 154 end
Daniel@0 155
Daniel@0 156 if size(Coord,1) == nd,
Daniel@0 157 Coord = [Coord; zeros(nc,size(Coord,2))];
Daniel@0 158 for i=(nd+1):(nd+nc),
Daniel@0 159 leaves = leafnodes(Z,i,nd);
Daniel@0 160 if any(leaves), Coord(i,:) = mean(Coord(leaves,:),1); else Coord(i,:) = Inf; end
Daniel@0 161 end
Daniel@0 162 end
Daniel@0 163
Daniel@0 164 % Colors
Daniel@0 165 switch Colortype,
Daniel@0 166 case 'colorspec', % nill
Daniel@0 167 case 'none', Color = '';
Daniel@0 168 case 'given',
Daniel@0 169 if size(Color,1) == nd,
Daniel@0 170 Color = [Color; zeros(nc,3)];
Daniel@0 171 for i=(nd+1):(nd+nc),
Daniel@0 172 leaves = leafnodes(Z,i,nd);
Daniel@0 173 if any(leaves), Color(i,:) = mean(Color(leaves,:),1);
Daniel@0 174 else Color(i,:) = 0.8;
Daniel@0 175 end
Daniel@0 176 end
Daniel@0 177 end
Daniel@0 178 end
Daniel@0 179
Daniel@0 180 % height
Daniel@0 181 if ischar(height),
Daniel@0 182 switch height,
Daniel@0 183 case 'order', height = [zeros(nd,1); [1:nc]'];
Daniel@0 184 case 'depth', height = nodedepth(Z); height = max(height) - height;
Daniel@0 185 end
Daniel@0 186 else
Daniel@0 187 if length(height)==nc, height = [zeros(nd,1); height]; end
Daniel@0 188 end
Daniel@0 189
Daniel@0 190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 191 %% draw
Daniel@0 192
Daniel@0 193 % the arcs
Daniel@0 194 lfrom = []; lto = [];
Daniel@0 195 for i=1:nd+nc,
Daniel@0 196 if i<=nd, ch = [];
Daniel@0 197 elseif ~isfinite(Z(i-nd,3)), ch = [];
Daniel@0 198 else ch = Z(i-nd,1:2)';
Daniel@0 199 end
Daniel@0 200 if any(ch),
Daniel@0 201 lfrom = [lfrom; i*ones(length(ch),1)];
Daniel@0 202 lto = [lto; ch];
Daniel@0 203 end
Daniel@0 204 end
Daniel@0 205
Daniel@0 206 % the coordinates of the arcs
Daniel@0 207 if codim == 1,
Daniel@0 208 Lx = [Coord(lfrom), Coord(lto), Coord(lto)];
Daniel@0 209 Ly = [height(lfrom), height(lfrom), height(lto)];
Daniel@0 210 Lz = [];
Daniel@0 211 else
Daniel@0 212 Lx = [Coord(lfrom,1), Coord(lto,1), Coord(lto,1)];
Daniel@0 213 Ly = [Coord(lfrom,2), Coord(lto,2), Coord(lto,2)];
Daniel@0 214 Lz = [height(lfrom), height(lfrom), height(lto)];
Daniel@0 215 end
Daniel@0 216
Daniel@0 217 washold = ishold;
Daniel@0 218 if ~washold, cla; end
Daniel@0 219
Daniel@0 220 % plot the lines
Daniel@0 221 if isempty(Lz),
Daniel@0 222 h = line(Lx',Ly','color',linecol);
Daniel@0 223 else
Daniel@0 224 h = line(Lx',Ly',Lz','color',linecol);
Daniel@0 225 if ~washold, view(3); end
Daniel@0 226 rotate3d on
Daniel@0 227 end
Daniel@0 228
Daniel@0 229 % plot the nodes
Daniel@0 230 hold on
Daniel@0 231 switch Colortype,
Daniel@0 232 case 'none', % nill
Daniel@0 233 case 'colorspec',
Daniel@0 234 if codim == 1, plot(Coord,height,Color);
Daniel@0 235 else plot3(Coord(:,1), Coord(:,2), height, Color);
Daniel@0 236 end
Daniel@0 237 case 'given',
Daniel@0 238 som_grid('rect',[nd+nc 1],'line','none','Coord',[Coord, height],...
Daniel@0 239 'Markersize',10,'Markercolor',Color);
Daniel@0 240 end
Daniel@0 241 if ~washold, hold off, end
Daniel@0 242
Daniel@0 243 return;
Daniel@0 244
Daniel@0 245
Daniel@0 246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 247 %% subfunctions
Daniel@0 248
Daniel@0 249 function depth = nodedepth(Z)
Daniel@0 250
Daniel@0 251 nd = size(Z,1)+1;
Daniel@0 252 nc = size(Z,1);
Daniel@0 253 depth = zeros(nd+nc,1);
Daniel@0 254 ch = nc+nd-1;
Daniel@0 255 while any(ch),
Daniel@0 256 c = ch(1); ch = ch(2:end);
Daniel@0 257 if c>nd & isfinite(Z(c-nd,3)),
Daniel@0 258 chc = Z(c-nd,1:2);
Daniel@0 259 depth(chc) = depth(c) + 1;
Daniel@0 260 ch = [ch, chc];
Daniel@0 261 end
Daniel@0 262 end
Daniel@0 263 return;
Daniel@0 264
Daniel@0 265 function inds = leafnodes(Z,i,nd)
Daniel@0 266
Daniel@0 267 inds = [];
Daniel@0 268 ch = i;
Daniel@0 269 while any(ch),
Daniel@0 270 c = ch(1); ch = ch(2:end);
Daniel@0 271 if c>nd & isfinite(Z(c-nd,3)), ch = [ch, Z(c-nd,1:2)]; end
Daniel@0 272 if c<=nd, inds(end+1) = c; end
Daniel@0 273 end
Daniel@0 274 return;
Daniel@0 275
Daniel@0 276 function order = leavesorder(Z)
Daniel@0 277
Daniel@0 278 nd = size(Z,1)+1;
Daniel@0 279 order = 2*nd-1;
Daniel@0 280 nonleaves = 1;
Daniel@0 281 while any(nonleaves),
Daniel@0 282 j = nonleaves(1);
Daniel@0 283 ch = Z(order(j)-nd,1:2);
Daniel@0 284 if j==1, oleft = []; else oleft = order(1:(j-1)); end
Daniel@0 285 if j==length(order), oright = []; else oright = order((j+1):length(order)); end
Daniel@0 286 order = [oleft, ch, oright];
Daniel@0 287 nonleaves = find(order>nd);
Daniel@0 288 end
Daniel@0 289 return;
Daniel@0 290
Daniel@0 291
Daniel@0 292