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