annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_clplot.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 = som_clplot(sC,varargin)
Daniel@0 2
Daniel@0 3 %SOM_CLPLOT Visualize clustering.
Daniel@0 4 %
Daniel@0 5 % h = som_clplot(sC, [[argID,] value, ...])
Daniel@0 6 % som_clplot(sM, part)
Daniel@0 7 %
Daniel@0 8 % som_clplot(sC);
Daniel@0 9 % som_clplot(som_clstruct(Z))
Daniel@0 10 % som_clplot(sC,sM);
Daniel@0 11 % som_clplot(sC,'coord',P);
Daniel@0 12 % som_clplot(sC,'dendrogram',[1 1 1 1 0 0 1 1 0 0 1]);
Daniel@0 13 % som_clplot(sC,'linewidth',10);
Daniel@0 14 % som_clplot(sC,'size',10);
Daniel@0 15 % som_clplot(sM,part);
Daniel@0 16 %
Daniel@0 17 % Input and output arguments ([]'s are optional):
Daniel@0 18 % sC (struct) clustering struct, as produced by SOM_CLSTRUCT
Daniel@0 19 % [argID, (string) See below. Each pair is the fieldname and
Daniel@0 20 % value] (varies) the value to be given to that field.
Daniel@0 21 % sM (struct) map struct
Daniel@0 22 % part (vector) length = munits, partitioning for the map
Daniel@0 23 %
Daniel@0 24 % h (vector) handles to the arcs between
Daniel@0 25 %
Daniel@0 26 % Here are the valid argument IDs and corresponding values. The values
Daniel@0 27 % which are unambiguous (marked with '*') can be given without the
Daniel@0 28 % preceeding argID.
Daniel@0 29 % 'linecolor' (string) color of the arc lines, 'k' by default
Daniel@0 30 % (vector) size 1 x 3
Daniel@0 31 % 'linewidth' (scalar) width of the arc lines
Daniel@0 32 % 'size' (vector) length 2*clen-1, sizes for each of the
Daniel@0 33 % cluster markers
Daniel@0 34 % (scalar) this size is used for all cluster markers
Daniel@0 35 % 'dendrogram'(vector) size 2*clen-1, indicates which clusters
Daniel@0 36 % are shown in the dendrogram
Daniel@0 37 % *(string) 'on' or 'off' ('on' by default)
Daniel@0 38 % 'coord' (matrix) size dlen x odim, the coordinates
Daniel@0 39 % for the data. If odim<=2, these are used as is.
Daniel@0 40 % Otherwise a 2-dimensional PCA-projection is
Daniel@0 41 % first made (see function PCAPROJ). These
Daniel@0 42 % coordinates are applied also to the clusters.
Daniel@0 43 % *(struct) data struct: as above
Daniel@0 44 % map or topology struct: the coordinates given
Daniel@0 45 % by SOM_VIS_COORDS are used for the data
Daniel@0 46 % 'color' (matrix) size dlen x 3, color for each data. By
Daniel@0 47 % default the colors defined for base
Daniel@0 48 % clusters are used (sC.color(sC.base,:)).
Daniel@0 49 % For ignored data figure background color is used.
Daniel@0 50 % (vector) size dlen x 1, indexed colors are used
Daniel@0 51 %
Daniel@0 52 % See also SOM_CLSTRUCT, SOM_LINKAGE, SOM_CLPRUNE, LINKAGE, DENDROGRAM.
Daniel@0 53
Daniel@0 54 % Copyright (c) 2000 by Juha Vesanto
Daniel@0 55 % Contributed to SOM Toolbox on XXX by Juha Vesanto
Daniel@0 56 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 57
Daniel@0 58 % Version 2.0beta juuso 180600
Daniel@0 59
Daniel@0 60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 61 %% read the arguments
Daniel@0 62
Daniel@0 63 % sC
Daniel@0 64 if strcmp(sC.type,'som_map'),
Daniel@0 65 base = varargin{1};
Daniel@0 66 clen = length(unique(base(isfinite(base))));
Daniel@0 67 Z = ones(clen-1,3);
Daniel@0 68 Z(:,1) = randperm(clen-1)';
Daniel@0 69 Z(:,2) = [clen:2*clen-2]';
Daniel@0 70 Z(:,3) = [1:clen-1]';
Daniel@0 71 sT = sC;
Daniel@0 72 sC = som_clstruct(Z,'base',varargin{1});
Daniel@0 73 h = som_clplot(sC,'coord',sT,'dendrogram','off',varargin{2:end});
Daniel@0 74 return;
Daniel@0 75 end
Daniel@0 76 clen = size(sC.tree,1)+1;
Daniel@0 77
Daniel@0 78 % varargin
Daniel@0 79 show = 'on';
Daniel@0 80 markersize = 10;
Daniel@0 81 linecolor = 'k';
Daniel@0 82 linewidth = 1;
Daniel@0 83 datacoord = [];
Daniel@0 84 datacolor = [];
Daniel@0 85
Daniel@0 86 i=1;
Daniel@0 87 while i<=length(varargin),
Daniel@0 88 argok = 1;
Daniel@0 89 if ischar(varargin{i}),
Daniel@0 90 switch varargin{i},
Daniel@0 91 case 'dendrogram', i=i+1; show = varargin{i};
Daniel@0 92 case 'size', i=i+1; markersize = varargin{i};
Daniel@0 93 case 'linecolor', i=i+1; linecolor = varargin{i};
Daniel@0 94 case 'linewidth', i=i+1; linewidth = varargin{i};
Daniel@0 95 case 'color', i=i+1; datacolor = varargin{i};
Daniel@0 96 case 'coord', i=i+1; datacoord = varargin{i};
Daniel@0 97 case {'on','off'}, show = varargin{i};
Daniel@0 98 otherwise argok=0;
Daniel@0 99 end
Daniel@0 100 elseif isstruct(varargin{i}), datacoord = varargin{i};
Daniel@0 101 else argok = 0;
Daniel@0 102 end
Daniel@0 103 if ~argok, disp(['(som_clplot) Ignoring invalid argument #' num2str(i+1)]); end
Daniel@0 104 i=i+1;
Daniel@0 105 end
Daniel@0 106
Daniel@0 107 % markersize
Daniel@0 108 if length(markersize)==1, markersize = ones(2*clen-1,1)*markersize; end
Daniel@0 109
Daniel@0 110 % datacoord
Daniel@0 111 if ~isempty(datacoord),
Daniel@0 112 if isstruct(datacoord),
Daniel@0 113 switch datacoord.type,
Daniel@0 114 case 'som_map', datacoord = datacoord.topol;
Daniel@0 115 case 'som_topol', %nil
Daniel@0 116 case 'som_data', datacoord = datacoord.data;
Daniel@0 117 otherwise, datacoord = [];
Daniel@0 118 end
Daniel@0 119 end
Daniel@0 120 if isstruct(datacoord),
Daniel@0 121 sC = som_clstruct(sC,'coord',som_vis_coords(datacoord.lattice,datacoord.msize));
Daniel@0 122 else
Daniel@0 123 [dlen dim] = size(datacoord);
Daniel@0 124 if dim>2, datacoord = pcaproj(datacoord,2); end
Daniel@0 125 sC = som_clstruct(sC,'coord',datacoord);
Daniel@0 126 end
Daniel@0 127 end
Daniel@0 128
Daniel@0 129 % show
Daniel@0 130 if ischar(show), show = strcmp(show,'on'); end
Daniel@0 131 if prod(size(show)) == 1, show = ones(2*clen-1,1)*show; end
Daniel@0 132
Daniel@0 133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 134 %% initialize values
Daniel@0 135
Daniel@0 136 % find the children to show for each cluster
Daniel@0 137 sTree0 = struct('parent',0,'children',[]);
Daniel@0 138 sTree = sTree0;
Daniel@0 139 for i=2:(2*clen-1), sTree(i) = sTree0; end
Daniel@0 140 for i=(clen+1):(2*clen-1),
Daniel@0 141 if isfinite(sC.tree(i-clen,3)),
Daniel@0 142 ch = sC.tree(i-clen,1:2);
Daniel@0 143 sTree(i).children = ch;
Daniel@0 144 for j=1:length(ch), sTree(ch(j)).parent = i; end
Daniel@0 145 end
Daniel@0 146 end
Daniel@0 147 if any(show==0), % some clusters are not shown
Daniel@0 148 for i=(clen+1):(2*clen-1),
Daniel@0 149 if ~show(i),
Daniel@0 150 p = sTree(i).parent;
Daniel@0 151 ch = sTree(i).children;
Daniel@0 152 if p,
Daniel@0 153 j = find(sTree(p).children == i);
Daniel@0 154 sTree(p).children = [sTree(p).children([1:(j-1),(j+1):end]), ch];
Daniel@0 155 for j=1:length(ch), sTree(ch(j)).parent = p; end
Daniel@0 156 end
Daniel@0 157 end
Daniel@0 158 end
Daniel@0 159 end
Daniel@0 160
Daniel@0 161 % the arcs
Daniel@0 162 lfrom = []; lto = []; ladd = [];
Daniel@0 163 for i=(clen+1):(2*clen-1),
Daniel@0 164 if show(i),
Daniel@0 165 ch = sTree(i).children';
Daniel@0 166 %ch = ch(find(show(ch)==1));
Daniel@0 167 lfrom = [lfrom; i*ones(length(ch),1)];
Daniel@0 168 lto = [lto; ch];
Daniel@0 169 end
Daniel@0 170 end
Daniel@0 171
Daniel@0 172 % infinite height
Daniel@0 173 %isinf = ~isfinite(sC.height);
Daniel@0 174 %sC.height(isinf) = 2*max(sC.height(~isinf));
Daniel@0 175
Daniel@0 176 % the coordinates of the arcs
Daniel@0 177 Co = [sC.coord, sC.height];
Daniel@0 178 if size(Co,2)==2,
Daniel@0 179 Lx = [Co(lfrom,1), Co(lto,1), Co(lto,1)];
Daniel@0 180 Ly = [Co(lfrom,end), Co(lfrom,end), Co(lto,end)];
Daniel@0 181 Lz = [];
Daniel@0 182 else
Daniel@0 183 Lx = [Co(lfrom,1), Co(lto,1), Co(lto,1)];
Daniel@0 184 Ly = [Co(lfrom,2), Co(lto,2), Co(lto,2)];
Daniel@0 185 Lz = [Co(lfrom,end), Co(lfrom,end), Co(lto,end)];
Daniel@0 186 end
Daniel@0 187
Daniel@0 188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 189 %% plot
Daniel@0 190
Daniel@0 191 washold = ishold;
Daniel@0 192 if ~washold, cla; hold on; end
Daniel@0 193
Daniel@0 194 % plot data
Daniel@0 195 if ~isempty(datacoord),
Daniel@0 196 if isempty(datacolor),
Daniel@0 197 nancolor = get(gcf,'Color');
Daniel@0 198 Col = nancolor(ones(length(sC.base),1),:);
Daniel@0 199 ind = find(isfinite(sC.base));
Daniel@0 200 Col(ind,:) = sC.color(sC.base(ind),:);
Daniel@0 201 elseif size(datacolor,2)==1, Col = som_normcolor(datacolor,jet);
Daniel@0 202 else Col = datacolor;
Daniel@0 203 end
Daniel@0 204 if isstruct(datacoord), som_cplane(datacoord,Col);
Daniel@0 205 else som_grid('rect',[length(sC.base) 1],'line','none',...
Daniel@0 206 'Coord',datacoord,'Markercolor',Col);
Daniel@0 207 end
Daniel@0 208 end
Daniel@0 209
Daniel@0 210 h = [];
Daniel@0 211 if any(show),
Daniel@0 212
Daniel@0 213 % plot the lines
Daniel@0 214 if isempty(Lz),
Daniel@0 215 h = line(Lx',Ly','color',linecolor,'linewidth',linewidth);
Daniel@0 216 else
Daniel@0 217 h = line(Lx',Ly',Lz','color',linecolor,'linewidth',linewidth);
Daniel@0 218 if ~washold, view(3); end
Daniel@0 219 rotate3d on
Daniel@0 220 end
Daniel@0 221
Daniel@0 222 % plot the nodes
Daniel@0 223 inds = find(show);
Daniel@0 224 som_grid('rect',[length(inds) 1],'line','none',...
Daniel@0 225 'Coord',Co(inds,:),...
Daniel@0 226 'Markercolor',sC.color(inds,:),...
Daniel@0 227 'Markersize',markersize(inds));
Daniel@0 228 end
Daniel@0 229
Daniel@0 230 if ~washold, hold off, end
Daniel@0 231
Daniel@0 232 return;
Daniel@0 233
Daniel@0 234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 235
Daniel@0 236
Daniel@0 237
Daniel@0 238