Mercurial > hg > camir-aes2014
comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_cplane.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_cplane(varargin) | |
2 | |
3 %SOM_CPLANE Visualize one 2D component plane, U-matrix or color plane. | |
4 % | |
5 % h=som_cplane(lattice, msize, color, [s], [pos]) | |
6 % h=som_cplane(topol, color, [s], [pos]) | |
7 % | |
8 % som_cplane('hexa', [10 5], 'none'); | |
9 % som_cplane('rect', [10 5], 'r'); | |
10 % som_cplane(sM.topol, sM.codebook(:,1)); | |
11 % U = som_umat(sM); som_cplane('hexaU',sM.topol.msize,U(:)); | |
12 % | |
13 % Input and output arguments ([]'s are optional): | |
14 % lattice (string) 'hexa', 'rect' (component planes) | |
15 % 'hexaU', 'rectU' (corresponding U-matrices) | |
16 % (matrix) defines the patch (see function VIS_PATCH). | |
17 % msize (vector) 1x2 vector defines grid size (M=prod(msize)) | |
18 % (matrix) Mx2 matrix gives explicit coordinates for each node | |
19 % topol (struct) map or topology struct | |
20 % color color for the nodes | |
21 % (matrix) Mx1 matrix gives indexed colors for the units | |
22 % Mx3 matrix of RGB triples gives explicit | |
23 % color for each unit | |
24 % (Note: in case of U-matrix, the number of color | |
25 % values is 4*prod(msize)-2*sum(msize)+1, not prod(msize)) | |
26 % (string) ColorSpec gives the same color for each node | |
27 % 'none' draws black edges only. | |
28 % [s] (matrix) size Mx1, gives individual size scaling for each node | |
29 % (scalar) gives the same size for each node, default=1. | |
30 % Additional features: see 'type som_cplane' | |
31 % This argument is ignored if the lattice is 'rectU' or 'hexaU'. | |
32 % [pos] (vector) a 1x2 vector that determines position of origin, | |
33 % default is [1 1]. | |
34 % | |
35 % h (scalar) the object handle for the PATCH object | |
36 % | |
37 % Axis are set to the 'ij' mode with equal spacing and turned off if | |
38 % 'pos' is not given. If 'lattice' is 'rect', 'hexa', 'rectU' or | |
39 % 'hexaU' the node (a,b) has coordinates (a,b) (+pos), except on the | |
40 % even numbered rows on the 'hexa' and 'hexaU' grids where the | |
41 % coordinates are (a,b+0.5) (+pos). | |
42 % | |
43 % For more help, try 'type som_cplane' or check out online documentation. | |
44 % See also SOM_PIEPLANE, SOM_PLOTPLANE, SOM_BARPLANE, VIS_PATCH, | |
45 % SOM_VIS_COORDS | |
46 | |
47 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
48 % | |
49 % som_cplane | |
50 % | |
51 % PURPOSE | |
52 % | |
53 % Visualizes a 2D component plane or u-matrix | |
54 % | |
55 % SYNTAX | |
56 % | |
57 % h = som_cplane(topol, color) | |
58 % h = som_cplane(lattice, msize, color) | |
59 % h = som_cplane(lattice, msize, color) | |
60 % h = som_cplane(..., size) | |
61 % h = som_cplane(..., size, pos) | |
62 % | |
63 % DESCRIPTION | |
64 % | |
65 % Creates some basic visualizations of the SOM grid: the component plane and | |
66 % the unified distance matrix. The routine draws the SOM grid as a patch | |
67 % object according to the specifications given in the input arguments and | |
68 % returns its object handle. | |
69 % | |
70 % Each unit of the map is presented by a polygon whose color, size, shape | |
71 % and location can be specified in various ways. The usual procedure | |
72 % is to choose the lattice and map size used in the map training. Then | |
73 % the function creates the standard sheet shaped topological | |
74 % representation of the map grid with hexagonal or rectangular units. | |
75 % When the values from a map codebook component (or from SOM_UMAT) | |
76 % are given to the function it produces an indexed coloring for the | |
77 % units (as in SURF command). Another possibility is to give a fixed | |
78 % RGB color for each unit explicitly. | |
79 % | |
80 % Special effects (variable unit size, location or shape) can be produced | |
81 % giving different types of input variables. | |
82 % | |
83 % KNOWN BUGS | |
84 % | |
85 % Using 1x3 or 3x1 grids causes problem, as the MATLAB will treat the color | |
86 % information vector 1x3 or 3x1 as a single RGB triple. So, using indexed | |
87 % colors is not possible for this particular map size. | |
88 % | |
89 % It is not possible to specify explicit coordinates for map | |
90 % consistig of just one unit as then the msize is interpreted as | |
91 % map size. | |
92 % | |
93 % REQUIRED INPUT ARGUMENTS | |
94 % | |
95 % Note: M is the number of map units | |
96 % | |
97 % lattice The basic shape of the map units | |
98 % | |
99 % (string) 'hexa' or 'rect' creates standard component plane; | |
100 % 'hexaU' or 'rectU' creates standard u-matrix. | |
101 % (matrix) Lx2 matrix defines the cornes of an arbitary polygon to be used | |
102 % as the unit marker. (L is the number of patch vertex: L=6 for | |
103 % 'hexa' and L=4 for 'rect') | |
104 % | |
105 % msize The size of the map grid | |
106 % | |
107 % (vector) [n1 n2] vector defines the map size (height n1 units, width | |
108 % n2 units, total M=n1 x n2 units). The units will be placed to their | |
109 % topological locations to form a uniform hexagonal or rectangular grid. | |
110 % (matrix) Mx2 matrix defines arbitrary coordinates for the M units | |
111 % In this case the argument 'lattice' defines the unit form only. | |
112 % | |
113 % topol Topology of the map grid | |
114 % | |
115 % (struct) map or topology struct from which the topology is taken | |
116 % | |
117 % color Unit colors | |
118 % | |
119 % (string) (ColorSpec) gives the same color for each unit, 'none' | |
120 % draws black unit edges only. | |
121 % (vector) Mx1 column vector gives indexed color for each unit using the | |
122 % current colormap (see help colormap). | |
123 % (matrix) Mx3 matrix of RGB triples as rows gives each unit a fixed color. | |
124 % | |
125 % OPTIONAL INPUT ARGUMENTS | |
126 % | |
127 % Note: M is the number of map units. | |
128 % Note: if unspecified or given empty values ('' or []) default | |
129 % values are used for optional input arguments. | |
130 % | |
131 % s The size scaling factors for the units | |
132 % | |
133 % (scalar) scalar gives each unit the same size scaling: | |
134 % 0 unit disappears (edges can be seen as a dot). | |
135 % 1 by default unit has its normal size (ie. no scaling) | |
136 % >1 unit overlaps others | |
137 % (matrix) Mx1 double: each unit gets individual size scaling | |
138 % | |
139 % pos Position of origin | |
140 % | |
141 % (vector) This argument exists to be able drawing component planes | |
142 % in arbitrary locations in a figure. Note the operation: | |
143 % if this argument is given, the axis limits setting | |
144 % part in the routine is skipped and the limits setting | |
145 % will be left to be done by MATLAB's default | |
146 % operation. | |
147 % | |
148 % OUTPUT ARGUMENTS | |
149 % | |
150 % h (scalar) handle to the created patch object | |
151 % | |
152 % OBJECT TAGS | |
153 % | |
154 % One object handle is returned: field Tag is set to | |
155 % 'planeC' for component plane | |
156 % 'planeU' for U-matrix | |
157 % | |
158 % FEATURES | |
159 % | |
160 % There are some extra features in following arguments | |
161 % | |
162 % size | |
163 % - MxL matrix: radial scaling: the distance between | |
164 % the center of node m and its kth vertex is scaled by | |
165 % s(m,k). | |
166 % - Mx1x2 matrix: the uniform scaling is done separately for | |
167 % x- and y-directions | |
168 % - MxLx2 matrix: the scaling is done separately to x- and y- | |
169 % directions for each vertex. | |
170 % | |
171 % color | |
172 % Each vertex may be given individual color. | |
173 % The PATCH object interpolates the colors on the | |
174 % face if shading is turned to interp. | |
175 % - 1xMxL matrix: colormap index for each vertex | |
176 % - LxMx3 matrix: RGB color for each vertex | |
177 % | |
178 % Note: In both cases (size and color) the ordering of the patch | |
179 % vertices in the "built-in" patches is the following | |
180 % | |
181 % 'rect' 'hexa' | |
182 % 1 3 1 | |
183 % 2 4 5 2 | |
184 % 6 3 | |
185 % 4 | |
186 % | |
187 % The color interpolation result seem to depend on the order | |
188 % in which the patch vertices are defined. Anyway, it gives | |
189 % unfavourable results in our case especially with hexa grid: | |
190 % this is a MATLAB feature. | |
191 % | |
192 % EXAMPLES | |
193 % | |
194 % m=som_make(rand(100,4),'msize',[6 5]) % make a map | |
195 % | |
196 % % show the first variable plane using indexed color coding | |
197 % | |
198 % som_cplane(m.topol.lattice,m.topol.msize,m.codebook(:,1)); | |
199 % or som_cplane(m.topol,m.codebook(:,1)); | |
200 % or som_cplane(m,m.codebook(:,1)); | |
201 % | |
202 % % show the first variable using different sized black units | |
203 % | |
204 % som_cplane(m,'k',m.codebook(:,1)); | |
205 % | |
206 % % Show the u-matrix. First we have to calculate it. | |
207 % % Note: som_umat returns a matrix therefore we write u(:) to get | |
208 % % a vector which contains the values in the proper order. | |
209 % | |
210 % u=som_umat(m); | |
211 % som_cplane('hexaU', m.topol.msize, u(:)); | |
212 % | |
213 % % Show three first variables coded as RGB colors | |
214 % % and turn the unit edges off | |
215 % | |
216 % h=som_cplane(m, m.codebook(:,1:3),1) | |
217 % set(h,'edgecolor','none'); | |
218 % | |
219 % % Try this! (see section FEATURES) | |
220 % | |
221 % som_cplane('rect',[5 5],'none',rand(25,4)); | |
222 % som_cplane('rect',[5 5],rand(1,25,4)); | |
223 % | |
224 % SEE ALSO | |
225 % | |
226 % som_barplane Visualize the map prototype vectors as bar diagrams | |
227 % som_plotplane Visualize the map prototype vectors as line graphs | |
228 % som_pieplane Visualize the map prototype vectors as pie charts | |
229 % som_umat Compute unified distance matrix of self-organizing map | |
230 % vis_patch Define the basic patches used in som_cplane | |
231 % som_vis_coords The default 'hexa' and 'rect' coordinates in visualizations | |
232 | |
233 % Copyright (c) 1999-2000 by the SOM toolbox programming team. | |
234 % http://www.cis.hut.fi/projects/somtoolbox/ | |
235 | |
236 % Version 2.0beta Johan 061099 juuso 151199 juuso 070600 | |
237 | |
238 %%% Check & Init arguments %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
239 | |
240 [nargin, lattice, msize, color, s, pos]=vis_planeGetArgs(varargin{:}); | |
241 error(nargchk(3, 5, nargin)); % check no. of input args is correct | |
242 | |
243 %% Translation? | |
244 | |
245 if nargin < 5 | isempty(pos) | |
246 pos=NaN; % "no translation" flag | |
247 elseif ~vis_valuetype(pos,{'1x2'}), | |
248 error('Position of origin has to be given as an 1x2 vector.'); | |
249 end | |
250 | |
251 %% Patchform %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
252 | |
253 switch class(lattice) | |
254 case 'char' % built-in patchforms | |
255 pos=pos-1; | |
256 switch lattice | |
257 case {'hexa', 'hexaU'} | |
258 patchform=vis_patch('hexa'); | |
259 case {'rect', 'rectU'} | |
260 patchform=vis_patch('rect'); | |
261 otherwise | |
262 error([ 'Lattice ' lattice ' not implemented!']); | |
263 end | |
264 case { 'double', 'sparse'} | |
265 if vis_valuetype(lattice,{'nx2'}), | |
266 patchform=lattice; % users patchform | |
267 lattice='rect'; | |
268 else | |
269 error('Patchform matrix has wrong size'); | |
270 end | |
271 otherwise | |
272 error('String or matrix expected for lattice.'); | |
273 end | |
274 | |
275 l=size(patchform,1); % number of vertices | |
276 planeType=lattice(end); % 'U' if umatrix otherwise something else | |
277 | |
278 if ~vis_valuetype(msize,{ '1x2', 'nx2'}), | |
279 error('msize has to be given as 1x2 or nx2 vectors.'); | |
280 end | |
281 | |
282 %% msize or coordinates %%%%%%%%%%%%%%%%%%%%%%% | |
283 | |
284 if size(msize,1)>1 | |
285 % msize is coordinate matrix Nx2? | |
286 | |
287 if planeType == 'U', % don't accept u-matrix | |
288 error('U-matrix visualization doesn''t work with free coordinates.'); | |
289 end | |
290 | |
291 % set number of map unit and unit coordinates | |
292 munits=size(msize,1); | |
293 unit_coords=msize; msize=[munits 1]; | |
294 | |
295 if isnan(pos), % no translation is done here | |
296 pos=[0 0]; % using [0 0] in order to prevent | |
297 end % axis tightening in | |
298 % vis_PlaneAxisProperties (arbitary coords!) | |
299 else | |
300 % msize is built-in lattice | |
301 | |
302 unit_coords=som_vis_coords(lattice,msize); | |
303 | |
304 % Calculate matrices x and y which 'moves' nodes | |
305 % to the correct positions: | |
306 % For U-matrix, the size has to be recalculated | |
307 if planeType == 'U', | |
308 xdim=2*msize(1)-1;ydim=2*msize(2)-1; | |
309 else | |
310 xdim=msize(1);ydim=msize(2); | |
311 end | |
312 munits=xdim*ydim; | |
313 | |
314 % Feature warning | |
315 if munits == 3 | |
316 warning('Problems with 1x3 and 3x1 maps. See documentation.'); | |
317 end | |
318 end | |
319 | |
320 %% Color matrix %%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
321 | |
322 if ~isnumeric(color) & ~ischar(color), | |
323 error('Color matrix is invalid.'); | |
324 else | |
325 d=size(color); | |
326 switch length(d) | |
327 case 2 %% Flat colors | |
328 if ischar(color) % Check for string 'none' | |
329 if strcmp(color,'none'), | |
330 color=NaN; | |
331 end | |
332 else | |
333 if ~(d(1)== 1 & d(2) == 3) & ... | |
334 ~(d(1) == munits & (d(2)==1 | d(2)==3)) | |
335 error('Color data matrix has wrong size.'); | |
336 elseif d(1)~=1 & d(2)==3 | |
337 if any(color>1 | color<0) | |
338 error('Color data matrix has invalid RGB values.'); | |
339 end | |
340 color=reshape(color,[1 munits 3]); % RGB colors | |
341 elseif d(2)==1 | |
342 color=color'; % indexed | |
343 end | |
344 end | |
345 case 3 %% Interpolated colors | |
346 if d(1) == 1 & d(2) == munits & d(3) == l, | |
347 color=reshape(color, l, munits); | |
348 elseif ~(d(1) == l & d(2) == munits & d(3) == 3) | |
349 error('Color data matrix has wrong size.'); | |
350 end | |
351 otherwise | |
352 error('Color data matrix has too many dimensions.'); | |
353 end | |
354 end | |
355 | |
356 %% Size matrix? %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
357 | |
358 if nargin < 4 | isempty(s), | |
359 s=1; % default value for s (no scaling) | |
360 elseif ~isnumeric(s) | |
361 error('Size matrix is not numeric.'); | |
362 end | |
363 | |
364 %%Determine the type of size matrix | |
365 d=size(s); | |
366 switch length(d) | |
367 case 2 | |
368 if (d(1)==1 & d(2)==1), | |
369 % Each node gets the same, uniform scaling. | |
370 s=s'; sx=s; sy=s; | |
371 elseif (d(1)==munits & d(2)==l), | |
372 % Each vertex is scaled radially respetc to the | |
373 % node center. | |
374 s=s'; sx=s; sy=s; | |
375 elseif d(1)==munits & d(2)==1 | |
376 % Each node gets an individual uniform scaling. | |
377 sx=repmat(s',l,1); sy=sx; | |
378 else | |
379 error('Size matrix has wrong size.'); | |
380 end | |
381 case 3 | |
382 if d(1)==munits & d(2)==1 & d(3)==2, | |
383 % Each node is individually and uniformly | |
384 % scaled separately to x- and y-directions. | |
385 sx=repmat(shiftdim(s(:,:,1))',l,1); | |
386 sy=repmat(shiftdim(s(:,:,2))',l,1); | |
387 elseif d(1)==munits & d(2)==l & d(3)==2, | |
388 % Each vertex is scaled separately to x- and y-directions | |
389 % with respect to the node center. | |
390 sx=shiftdim(s(:,:,1))'; | |
391 sy=shiftdim(s(:,:,2))'; | |
392 else | |
393 error('Size matrix has wrong size.'); | |
394 end | |
395 otherwise | |
396 error('Size matrix has too many dimensions.'); | |
397 end | |
398 | |
399 % Size zero would cause division by zero. eps is as good (node disappears) | |
400 % I tried first NaN, it works well otherwise, but the node is | |
401 % then not on the axis and some commands may the work oddly. | |
402 % The edge may be visible, though. | |
403 | |
404 sx(sx==0)=eps; | |
405 sy(sy==0)=eps; | |
406 | |
407 % Rescale sizes for u-matrix | |
408 if planeType=='U', | |
409 sx=sx/2;sy=sy/2; | |
410 end | |
411 | |
412 %%%% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
413 | |
414 % Making grid. %%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
415 | |
416 % Translation for each patch | |
417 | |
418 x=repmat(unit_coords(:,1)',l,1); | |
419 y=repmat(unit_coords(:,2)',l,1); | |
420 | |
421 % patch vertex coordiantes | |
422 | |
423 nx=repmat(patchform(:,1),1,munits); | |
424 ny=repmat(patchform(:,2),1,munits); | |
425 | |
426 % NB: The hexagons are not uniform in order to get even | |
427 % y-coordinates for the nodes. This is handled by setting _axis scaling_ | |
428 % so that the hexa-nodes look like uniform hexagonals. See | |
429 % vis_PlaneAxisProperties | |
430 | |
431 %% Make and scale the final input for PATCH: | |
432 | |
433 % 1: combine translation and scaling of each patch | |
434 x=(x./sx+nx).*sx; y=(y./sy+ny).*sy; | |
435 | |
436 %% 2: translation of origin (pos) | |
437 if ~isnan(pos) | |
438 x=x+pos(1);y=y+pos(2); % move upper left corner | |
439 end % to pos | |
440 | |
441 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
442 | |
443 %% Set axes properties | |
444 %% Command view([0 90]) shows the map in 2D properly oriented | |
445 | |
446 ax=newplot; % set new plot | |
447 vis_PlaneAxisProperties(ax,lattice,msize,pos); | |
448 | |
449 %% Draw the map! | |
450 | |
451 if ~isnan(color) | |
452 h_=patch(x,y,color); | |
453 else | |
454 h_=patch(x,y,'k'); % empty grid | |
455 set(h_,'FaceColor','none'); | |
456 end | |
457 | |
458 %% Set object tag | |
459 | |
460 if planeType=='U' | |
461 set(h_,'Tag','planeU'); | |
462 else | |
463 set(h_,'Tag','planeC'); | |
464 end | |
465 | |
466 %%% Build output %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
467 | |
468 if nargout>0, h=h_; end % Set h only, | |
469 % if there really is output | |
470 |