comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_clplot.m @ 0:e9a9cd732c1e tip

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