annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_clstruct.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 sC = som_clstruct(Z,varargin)
Daniel@0 2
Daniel@0 3 %SOM_CLSTRUCT Create a clustering struct or set its field values.
Daniel@0 4 %
Daniel@0 5 % sC = som_clstruct(Z, [argID, value, ...])
Daniel@0 6 %
Daniel@0 7 % Z = linkage(pdist(sM.codebook));
Daniel@0 8 % sC = som_clstruct(Z);
Daniel@0 9 % sC = som_clstruct(sC,'coord',som_vis_coords(lattice,msize));
Daniel@0 10 % sC = som_clstruct(sC,'color',som_colorcode(sM));
Daniel@0 11 % sC = som_clstruct(sC,'base',sC.base(som_bmus(sM,sD)));
Daniel@0 12 %
Daniel@0 13 % Input and output arguments ([]'s are optional):
Daniel@0 14 % Z (matrix) size clen-1 x 3, where clen is the number of
Daniel@0 15 % base clusters. This is a clustering matrix
Daniel@0 16 % similar to that produced by LINKAGE in
Daniel@0 17 % Statistical Toolbox. See SOM_LINKAGE.
Daniel@0 18 % (struct) clustering struct (as produced by this function)
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 %
Daniel@0 22 % sC (struct) clustering struct
Daniel@0 23 %
Daniel@0 24 % The clustering struct is based on the assumption that there
Daniel@0 25 % is a base partitioning of the SOM (or data) which is saved in
Daniel@0 26 % the .base field of the struct. Then a hierarchical clustering
Daniel@0 27 % is applied to this base partitioning. The results are saved to
Daniel@0 28 % .tree field of the struct. Each cluster (base and combined)
Daniel@0 29 % has also three properties: height, coordinate and color, which
Daniel@0 30 % are used in the visualizations. The fields of the struct are:
Daniel@0 31 % .type (string) 'som_clustering'
Daniel@0 32 % .name (string) Identifier for the clustering.
Daniel@0 33 % .tree (matrix) Size clen-1 x 3, as argument Z above.
Daniel@0 34 % .base (vector) Size dlen x 1, the basic groups of data
Daniel@0 35 % forming the base clusters, e.g. as a result
Daniel@0 36 % of partitive clustering. Allowed values are
Daniel@0 37 % 1:clen indicating the base cluster
Daniel@0 38 % to which the data belongs to.
Daniel@0 39 % NaN indicating that the data has
Daniel@0 40 % been ignored in the clustering
Daniel@0 41 % By default [1:clen].
Daniel@0 42 % .height (vector) Size 2*clen-1 x 1, (clustering) height for each
Daniel@0 43 % cluster. By default 0 for each base cluster and
Daniel@0 44 % .tree(:,3) for the others.
Daniel@0 45 % .coord (matrix) Size 2*clen-1 x *, coordinate for each cluster,
Daniel@0 46 % By default the coordinates are set so that
Daniel@0 47 % the base clusters are ordered on a line, and the
Daniel@0 48 % position of each combined cluster is average of
Daniel@0 49 % the base clusters that constitute it.
Daniel@0 50 % .color (matrix) Size 2*clen-1 x 3, color for each cluster.
Daniel@0 51 % By default the colors are set so that the
Daniel@0 52 % base clusters are ordered on a line, like above,
Daniel@0 53 % and then colors are assigned from the 'hsv'
Daniel@0 54 % colormap to the base clusters. The color
Daniel@0 55 % of each combined cluster is average as above.
Daniel@0 56 %
Daniel@0 57 % Height, coord and color can also be specified in alternate forms:
Daniel@0 58 % 'height' (vector) size 2*clen-1 x 1, if given explicitly
Daniel@0 59 % size clen-1 x 1, specified heights of the
Daniel@0 60 % combined clusters (the base cluster heights
Daniel@0 61 % are all = 0)
Daniel@0 62 % size 0 x 0, default value is used
Daniel@0 63 % 'coord' (matrix) size 2*clen-1 x *, if given explicitly
Daniel@0 64 % size clen x *, to give coordinates for base
Daniel@0 65 % clusters; the coordinate of combined clusters
Daniel@0 66 % are averaged from these
Daniel@0 67 % size dlen x *, to give coordinates of the
Daniel@0 68 % original data: the cluster coordinates are
Daniel@0 69 % averaged from these based on base clusters
Daniel@0 70 % size 0 x 0, default value is used
Daniel@0 71 % 'color' (matrix) as 'coord'
Daniel@0 72 %
Daniel@0 73 % See also SOM_CLPLOT, SOM_CLVALIDITY, SOM_CLGET, SOM_CLLINKAGE.
Daniel@0 74
Daniel@0 75 % Copyright (c) 2000 by the SOM toolbox programming team.
Daniel@0 76 % Contributed to SOM Toolbox on XXX by Juha Vesanto
Daniel@0 77 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 78
Daniel@0 79 % Version 2.0beta juuso 180800
Daniel@0 80
Daniel@0 81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 82
Daniel@0 83 if isstruct(Z),
Daniel@0 84 base = Z.base;
Daniel@0 85 color = Z.color;
Daniel@0 86 coord = Z.coord;
Daniel@0 87 height = Z.height;
Daniel@0 88 name = Z.name;
Daniel@0 89 Z = Z.tree;
Daniel@0 90 else
Daniel@0 91 base = [];
Daniel@0 92 color = [];
Daniel@0 93 coord = [];
Daniel@0 94 height = [];
Daniel@0 95 name = '';
Daniel@0 96 end
Daniel@0 97 clen = size(Z,1)+1;
Daniel@0 98
Daniel@0 99 i=1;
Daniel@0 100 while i<=length(varargin),
Daniel@0 101 argok = 1;
Daniel@0 102 if ischar(varargin{i}),
Daniel@0 103 switch varargin{i},
Daniel@0 104 case 'tree', i=i+1; Z = varargin{i}; clen = size(Z,1)+1;
Daniel@0 105 case 'base', i=i+1; base = varargin{i};
Daniel@0 106 case 'color', i=i+1; color = varargin{i};
Daniel@0 107 case 'coord', i=i+1; coord = varargin{i};
Daniel@0 108 case 'height', i=i+1; height = varargin{i};
Daniel@0 109 case 'name', i=i+1; name = varargin{i};
Daniel@0 110 otherwise argok=0;
Daniel@0 111 end
Daniel@0 112 else argok = 0;
Daniel@0 113 end
Daniel@0 114 if ~argok, disp(['(som_clstruct) Ignoring invalid argument #' num2str(i+1)]); end
Daniel@0 115 i = i+1;
Daniel@0 116 end
Daniel@0 117
Daniel@0 118 if isempty(base),
Daniel@0 119 dlen = clen;
Daniel@0 120 base = 1:dlen;
Daniel@0 121 else
Daniel@0 122 dlen = length(base);
Daniel@0 123 if any(base)>clen | any(base)<1, error('Incorrect base partition vector.'); end
Daniel@0 124 end
Daniel@0 125
Daniel@0 126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 127 %% analysis of hierarchy
Daniel@0 128
Daniel@0 129 % order of base clusters
Daniel@0 130 order = 2*clen-1;
Daniel@0 131 nonleaves = 1;
Daniel@0 132 while any(nonleaves),
Daniel@0 133 j = nonleaves(1);
Daniel@0 134 ch = Z(order(j)-clen,1:2);
Daniel@0 135 if j==1, oleft = []; else oleft = order(1:(j-1)); end
Daniel@0 136 if j==length(order), oright = []; else oright = order((j+1):length(order)); end
Daniel@0 137 order = [oleft, ch, oright];
Daniel@0 138 nonleaves = find(order>clen);
Daniel@0 139 end
Daniel@0 140
Daniel@0 141 % base cluster indeces for each non-base cluster
Daniel@0 142 basecl = cell(clen-1,1);
Daniel@0 143 for i=1:clen-1,
Daniel@0 144 c1 = Z(i,1); if c1>clen, c1 = basecl{c1-clen}; end
Daniel@0 145 c2 = Z(i,2); if c2>clen, c2 = basecl{c2-clen}; end
Daniel@0 146 basecl{i} = [c1 c2];
Daniel@0 147 end
Daniel@0 148
Daniel@0 149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 150 %% set coordinates, color and height and make the struct
Daniel@0 151
Daniel@0 152 % coordinates
Daniel@0 153 if size(coord,1)==2*clen-1, % this is ok already
Daniel@0 154 else
Daniel@0 155 if size(coord,1)==0, % the default
Daniel@0 156 [dummy,coord] = sort(order);
Daniel@0 157 coord = coord';
Daniel@0 158 elseif size(coord,1)==dlen & dlen>clen, % coordinates given for original data
Daniel@0 159 codata = coord;
Daniel@0 160 coord = zeros(clen,size(coord,2));
Daniel@0 161 for i=1:clen, coord(i,:) = mean(codata(find(base==i),:),1); end
Daniel@0 162 end
Daniel@0 163 if size(coord,1)==clen, % average from base clusters
Daniel@0 164 coord = [coord; zeros(clen-1,size(coord,2))];
Daniel@0 165 for i=1:clen-1, coord(i+clen,:) = mean(coord(basecl{i},:),1); end
Daniel@0 166 else
Daniel@0 167 error('Incorrect coordinate matrix.');
Daniel@0 168 end
Daniel@0 169 end
Daniel@0 170
Daniel@0 171 % color
Daniel@0 172 if size(color,1)==2*clen-1, % this is ok already
Daniel@0 173 else
Daniel@0 174 if size(color,1)==0, % the default
Daniel@0 175 color(order,:) = hsv(length(order));
Daniel@0 176 elseif size(color,1)==dlen & dlen>clen, % colors given for original data
Daniel@0 177 codata = color;
Daniel@0 178 color = zeros(clen,3);
Daniel@0 179 for i=1:clen, color(i,:) = mean(codata(find(base==i),:),1); end
Daniel@0 180 end
Daniel@0 181 if size(color,1)==clen, % average from base clusters
Daniel@0 182 color = [color; zeros(clen-1,3)];
Daniel@0 183 for i=1:clen-1, color(i+clen,:) = mean(color(basecl{i},:),1); end
Daniel@0 184 else
Daniel@0 185 error('Incorrect color matrix.');
Daniel@0 186 end
Daniel@0 187 end
Daniel@0 188
Daniel@0 189 % height
Daniel@0 190 if isempty(height),
Daniel@0 191 height = [zeros(clen,1); Z(:,3)];
Daniel@0 192 elseif length(height)==clen-1,
Daniel@0 193 if size(height,2)==clen-1, height = height'; end
Daniel@0 194 height = [zeros(clen,1); height];
Daniel@0 195 elseif length(height)~=2*clen-1,
Daniel@0 196 error('Incorrect height vector.');
Daniel@0 197 end
Daniel@0 198
Daniel@0 199 % make the struct
Daniel@0 200 sC = struct('type','som_clustering',...
Daniel@0 201 'name',name,'base',base,'tree',Z,...
Daniel@0 202 'color',color,'coord',coord,'height',height);
Daniel@0 203 return;
Daniel@0 204
Daniel@0 205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 206