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