wolffd@0: function h = som_clplot(sC,varargin) wolffd@0: wolffd@0: %SOM_CLPLOT Visualize clustering. wolffd@0: % wolffd@0: % h = som_clplot(sC, [[argID,] value, ...]) wolffd@0: % som_clplot(sM, part) wolffd@0: % wolffd@0: % som_clplot(sC); wolffd@0: % som_clplot(som_clstruct(Z)) wolffd@0: % som_clplot(sC,sM); wolffd@0: % som_clplot(sC,'coord',P); wolffd@0: % som_clplot(sC,'dendrogram',[1 1 1 1 0 0 1 1 0 0 1]); wolffd@0: % som_clplot(sC,'linewidth',10); wolffd@0: % som_clplot(sC,'size',10); wolffd@0: % som_clplot(sM,part); wolffd@0: % wolffd@0: % Input and output arguments ([]'s are optional): wolffd@0: % sC (struct) clustering struct, as produced by SOM_CLSTRUCT wolffd@0: % [argID, (string) See below. Each pair is the fieldname and wolffd@0: % value] (varies) the value to be given to that field. wolffd@0: % sM (struct) map struct wolffd@0: % part (vector) length = munits, partitioning for the map wolffd@0: % wolffd@0: % h (vector) handles to the arcs between wolffd@0: % wolffd@0: % Here are the valid argument IDs and corresponding values. The values wolffd@0: % which are unambiguous (marked with '*') can be given without the wolffd@0: % preceeding argID. wolffd@0: % 'linecolor' (string) color of the arc lines, 'k' by default wolffd@0: % (vector) size 1 x 3 wolffd@0: % 'linewidth' (scalar) width of the arc lines wolffd@0: % 'size' (vector) length 2*clen-1, sizes for each of the wolffd@0: % cluster markers wolffd@0: % (scalar) this size is used for all cluster markers wolffd@0: % 'dendrogram'(vector) size 2*clen-1, indicates which clusters wolffd@0: % are shown in the dendrogram wolffd@0: % *(string) 'on' or 'off' ('on' by default) wolffd@0: % 'coord' (matrix) size dlen x odim, the coordinates wolffd@0: % for the data. If odim<=2, these are used as is. wolffd@0: % Otherwise a 2-dimensional PCA-projection is wolffd@0: % first made (see function PCAPROJ). These wolffd@0: % coordinates are applied also to the clusters. wolffd@0: % *(struct) data struct: as above wolffd@0: % map or topology struct: the coordinates given wolffd@0: % by SOM_VIS_COORDS are used for the data wolffd@0: % 'color' (matrix) size dlen x 3, color for each data. By wolffd@0: % default the colors defined for base wolffd@0: % clusters are used (sC.color(sC.base,:)). wolffd@0: % For ignored data figure background color is used. wolffd@0: % (vector) size dlen x 1, indexed colors are used wolffd@0: % wolffd@0: % See also SOM_CLSTRUCT, SOM_LINKAGE, SOM_CLPRUNE, LINKAGE, DENDROGRAM. wolffd@0: wolffd@0: % Copyright (c) 2000 by Juha Vesanto wolffd@0: % Contributed to SOM Toolbox on XXX by Juha Vesanto wolffd@0: % http://www.cis.hut.fi/projects/somtoolbox/ wolffd@0: wolffd@0: % Version 2.0beta juuso 180600 wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% read the arguments wolffd@0: wolffd@0: % sC wolffd@0: if strcmp(sC.type,'som_map'), wolffd@0: base = varargin{1}; wolffd@0: clen = length(unique(base(isfinite(base)))); wolffd@0: Z = ones(clen-1,3); wolffd@0: Z(:,1) = randperm(clen-1)'; wolffd@0: Z(:,2) = [clen:2*clen-2]'; wolffd@0: Z(:,3) = [1:clen-1]'; wolffd@0: sT = sC; wolffd@0: sC = som_clstruct(Z,'base',varargin{1}); wolffd@0: h = som_clplot(sC,'coord',sT,'dendrogram','off',varargin{2:end}); wolffd@0: return; wolffd@0: end wolffd@0: clen = size(sC.tree,1)+1; wolffd@0: wolffd@0: % varargin wolffd@0: show = 'on'; wolffd@0: markersize = 10; wolffd@0: linecolor = 'k'; wolffd@0: linewidth = 1; wolffd@0: datacoord = []; wolffd@0: datacolor = []; wolffd@0: wolffd@0: i=1; wolffd@0: while i<=length(varargin), wolffd@0: argok = 1; wolffd@0: if ischar(varargin{i}), wolffd@0: switch varargin{i}, wolffd@0: case 'dendrogram', i=i+1; show = varargin{i}; wolffd@0: case 'size', i=i+1; markersize = varargin{i}; wolffd@0: case 'linecolor', i=i+1; linecolor = varargin{i}; wolffd@0: case 'linewidth', i=i+1; linewidth = varargin{i}; wolffd@0: case 'color', i=i+1; datacolor = varargin{i}; wolffd@0: case 'coord', i=i+1; datacoord = varargin{i}; wolffd@0: case {'on','off'}, show = varargin{i}; wolffd@0: otherwise argok=0; wolffd@0: end wolffd@0: elseif isstruct(varargin{i}), datacoord = varargin{i}; wolffd@0: else argok = 0; wolffd@0: end wolffd@0: if ~argok, disp(['(som_clplot) Ignoring invalid argument #' num2str(i+1)]); end wolffd@0: i=i+1; wolffd@0: end wolffd@0: wolffd@0: % markersize wolffd@0: if length(markersize)==1, markersize = ones(2*clen-1,1)*markersize; end wolffd@0: wolffd@0: % datacoord wolffd@0: if ~isempty(datacoord), wolffd@0: if isstruct(datacoord), wolffd@0: switch datacoord.type, wolffd@0: case 'som_map', datacoord = datacoord.topol; wolffd@0: case 'som_topol', %nil wolffd@0: case 'som_data', datacoord = datacoord.data; wolffd@0: otherwise, datacoord = []; wolffd@0: end wolffd@0: end wolffd@0: if isstruct(datacoord), wolffd@0: sC = som_clstruct(sC,'coord',som_vis_coords(datacoord.lattice,datacoord.msize)); wolffd@0: else wolffd@0: [dlen dim] = size(datacoord); wolffd@0: if dim>2, datacoord = pcaproj(datacoord,2); end wolffd@0: sC = som_clstruct(sC,'coord',datacoord); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: % show wolffd@0: if ischar(show), show = strcmp(show,'on'); end wolffd@0: if prod(size(show)) == 1, show = ones(2*clen-1,1)*show; end wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% initialize values wolffd@0: wolffd@0: % find the children to show for each cluster wolffd@0: sTree0 = struct('parent',0,'children',[]); wolffd@0: sTree = sTree0; wolffd@0: for i=2:(2*clen-1), sTree(i) = sTree0; end wolffd@0: for i=(clen+1):(2*clen-1), wolffd@0: if isfinite(sC.tree(i-clen,3)), wolffd@0: ch = sC.tree(i-clen,1:2); wolffd@0: sTree(i).children = ch; wolffd@0: for j=1:length(ch), sTree(ch(j)).parent = i; end wolffd@0: end wolffd@0: end wolffd@0: if any(show==0), % some clusters are not shown wolffd@0: for i=(clen+1):(2*clen-1), wolffd@0: if ~show(i), wolffd@0: p = sTree(i).parent; wolffd@0: ch = sTree(i).children; wolffd@0: if p, wolffd@0: j = find(sTree(p).children == i); wolffd@0: sTree(p).children = [sTree(p).children([1:(j-1),(j+1):end]), ch]; wolffd@0: for j=1:length(ch), sTree(ch(j)).parent = p; end wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: % the arcs wolffd@0: lfrom = []; lto = []; ladd = []; wolffd@0: for i=(clen+1):(2*clen-1), wolffd@0: if show(i), wolffd@0: ch = sTree(i).children'; wolffd@0: %ch = ch(find(show(ch)==1)); wolffd@0: lfrom = [lfrom; i*ones(length(ch),1)]; wolffd@0: lto = [lto; ch]; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: % infinite height wolffd@0: %isinf = ~isfinite(sC.height); wolffd@0: %sC.height(isinf) = 2*max(sC.height(~isinf)); wolffd@0: wolffd@0: % the coordinates of the arcs wolffd@0: Co = [sC.coord, sC.height]; wolffd@0: if size(Co,2)==2, wolffd@0: Lx = [Co(lfrom,1), Co(lto,1), Co(lto,1)]; wolffd@0: Ly = [Co(lfrom,end), Co(lfrom,end), Co(lto,end)]; wolffd@0: Lz = []; wolffd@0: else wolffd@0: Lx = [Co(lfrom,1), Co(lto,1), Co(lto,1)]; wolffd@0: Ly = [Co(lfrom,2), Co(lto,2), Co(lto,2)]; wolffd@0: Lz = [Co(lfrom,end), Co(lfrom,end), Co(lto,end)]; wolffd@0: end wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% plot wolffd@0: wolffd@0: washold = ishold; wolffd@0: if ~washold, cla; hold on; end wolffd@0: wolffd@0: % plot data wolffd@0: if ~isempty(datacoord), wolffd@0: if isempty(datacolor), wolffd@0: nancolor = get(gcf,'Color'); wolffd@0: Col = nancolor(ones(length(sC.base),1),:); wolffd@0: ind = find(isfinite(sC.base)); wolffd@0: Col(ind,:) = sC.color(sC.base(ind),:); wolffd@0: elseif size(datacolor,2)==1, Col = som_normcolor(datacolor,jet); wolffd@0: else Col = datacolor; wolffd@0: end wolffd@0: if isstruct(datacoord), som_cplane(datacoord,Col); wolffd@0: else som_grid('rect',[length(sC.base) 1],'line','none',... wolffd@0: 'Coord',datacoord,'Markercolor',Col); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: h = []; wolffd@0: if any(show), wolffd@0: wolffd@0: % plot the lines wolffd@0: if isempty(Lz), wolffd@0: h = line(Lx',Ly','color',linecolor,'linewidth',linewidth); wolffd@0: else wolffd@0: h = line(Lx',Ly',Lz','color',linecolor,'linewidth',linewidth); wolffd@0: if ~washold, view(3); end wolffd@0: rotate3d on wolffd@0: end wolffd@0: wolffd@0: % plot the nodes wolffd@0: inds = find(show); wolffd@0: som_grid('rect',[length(inds) 1],'line','none',... wolffd@0: 'Coord',Co(inds,:),... wolffd@0: 'Markercolor',sC.color(inds,:),... wolffd@0: 'Markersize',markersize(inds)); wolffd@0: end wolffd@0: wolffd@0: if ~washold, hold off, end wolffd@0: wolffd@0: return; wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: wolffd@0: wolffd@0: