Daniel@0: function h=som_cplane(varargin) Daniel@0: Daniel@0: %SOM_CPLANE Visualize one 2D component plane, U-matrix or color plane. Daniel@0: % Daniel@0: % h=som_cplane(lattice, msize, color, [s], [pos]) Daniel@0: % h=som_cplane(topol, color, [s], [pos]) Daniel@0: % Daniel@0: % som_cplane('hexa', [10 5], 'none'); Daniel@0: % som_cplane('rect', [10 5], 'r'); Daniel@0: % som_cplane(sM.topol, sM.codebook(:,1)); Daniel@0: % U = som_umat(sM); som_cplane('hexaU',sM.topol.msize,U(:)); Daniel@0: % Daniel@0: % Input and output arguments ([]'s are optional): Daniel@0: % lattice (string) 'hexa', 'rect' (component planes) Daniel@0: % 'hexaU', 'rectU' (corresponding U-matrices) Daniel@0: % (matrix) defines the patch (see function VIS_PATCH). Daniel@0: % msize (vector) 1x2 vector defines grid size (M=prod(msize)) Daniel@0: % (matrix) Mx2 matrix gives explicit coordinates for each node Daniel@0: % topol (struct) map or topology struct Daniel@0: % color color for the nodes Daniel@0: % (matrix) Mx1 matrix gives indexed colors for the units Daniel@0: % Mx3 matrix of RGB triples gives explicit Daniel@0: % color for each unit Daniel@0: % (Note: in case of U-matrix, the number of color Daniel@0: % values is 4*prod(msize)-2*sum(msize)+1, not prod(msize)) Daniel@0: % (string) ColorSpec gives the same color for each node Daniel@0: % 'none' draws black edges only. Daniel@0: % [s] (matrix) size Mx1, gives individual size scaling for each node Daniel@0: % (scalar) gives the same size for each node, default=1. Daniel@0: % Additional features: see 'type som_cplane' Daniel@0: % This argument is ignored if the lattice is 'rectU' or 'hexaU'. Daniel@0: % [pos] (vector) a 1x2 vector that determines position of origin, Daniel@0: % default is [1 1]. Daniel@0: % Daniel@0: % h (scalar) the object handle for the PATCH object Daniel@0: % Daniel@0: % Axis are set to the 'ij' mode with equal spacing and turned off if Daniel@0: % 'pos' is not given. If 'lattice' is 'rect', 'hexa', 'rectU' or Daniel@0: % 'hexaU' the node (a,b) has coordinates (a,b) (+pos), except on the Daniel@0: % even numbered rows on the 'hexa' and 'hexaU' grids where the Daniel@0: % coordinates are (a,b+0.5) (+pos). Daniel@0: % Daniel@0: % For more help, try 'type som_cplane' or check out online documentation. Daniel@0: % See also SOM_PIEPLANE, SOM_PLOTPLANE, SOM_BARPLANE, VIS_PATCH, Daniel@0: % SOM_VIS_COORDS Daniel@0: Daniel@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: % Daniel@0: % som_cplane Daniel@0: % Daniel@0: % PURPOSE Daniel@0: % Daniel@0: % Visualizes a 2D component plane or u-matrix Daniel@0: % Daniel@0: % SYNTAX Daniel@0: % Daniel@0: % h = som_cplane(topol, color) Daniel@0: % h = som_cplane(lattice, msize, color) Daniel@0: % h = som_cplane(lattice, msize, color) Daniel@0: % h = som_cplane(..., size) Daniel@0: % h = som_cplane(..., size, pos) Daniel@0: % Daniel@0: % DESCRIPTION Daniel@0: % Daniel@0: % Creates some basic visualizations of the SOM grid: the component plane and Daniel@0: % the unified distance matrix. The routine draws the SOM grid as a patch Daniel@0: % object according to the specifications given in the input arguments and Daniel@0: % returns its object handle. Daniel@0: % Daniel@0: % Each unit of the map is presented by a polygon whose color, size, shape Daniel@0: % and location can be specified in various ways. The usual procedure Daniel@0: % is to choose the lattice and map size used in the map training. Then Daniel@0: % the function creates the standard sheet shaped topological Daniel@0: % representation of the map grid with hexagonal or rectangular units. Daniel@0: % When the values from a map codebook component (or from SOM_UMAT) Daniel@0: % are given to the function it produces an indexed coloring for the Daniel@0: % units (as in SURF command). Another possibility is to give a fixed Daniel@0: % RGB color for each unit explicitly. Daniel@0: % Daniel@0: % Special effects (variable unit size, location or shape) can be produced Daniel@0: % giving different types of input variables. Daniel@0: % Daniel@0: % KNOWN BUGS Daniel@0: % Daniel@0: % Using 1x3 or 3x1 grids causes problem, as the MATLAB will treat the color Daniel@0: % information vector 1x3 or 3x1 as a single RGB triple. So, using indexed Daniel@0: % colors is not possible for this particular map size. Daniel@0: % Daniel@0: % It is not possible to specify explicit coordinates for map Daniel@0: % consistig of just one unit as then the msize is interpreted as Daniel@0: % map size. Daniel@0: % Daniel@0: % REQUIRED INPUT ARGUMENTS Daniel@0: % Daniel@0: % Note: M is the number of map units Daniel@0: % Daniel@0: % lattice The basic shape of the map units Daniel@0: % Daniel@0: % (string) 'hexa' or 'rect' creates standard component plane; Daniel@0: % 'hexaU' or 'rectU' creates standard u-matrix. Daniel@0: % (matrix) Lx2 matrix defines the cornes of an arbitary polygon to be used Daniel@0: % as the unit marker. (L is the number of patch vertex: L=6 for Daniel@0: % 'hexa' and L=4 for 'rect') Daniel@0: % Daniel@0: % msize The size of the map grid Daniel@0: % Daniel@0: % (vector) [n1 n2] vector defines the map size (height n1 units, width Daniel@0: % n2 units, total M=n1 x n2 units). The units will be placed to their Daniel@0: % topological locations to form a uniform hexagonal or rectangular grid. Daniel@0: % (matrix) Mx2 matrix defines arbitrary coordinates for the M units Daniel@0: % In this case the argument 'lattice' defines the unit form only. Daniel@0: % Daniel@0: % topol Topology of the map grid Daniel@0: % Daniel@0: % (struct) map or topology struct from which the topology is taken Daniel@0: % Daniel@0: % color Unit colors Daniel@0: % Daniel@0: % (string) (ColorSpec) gives the same color for each unit, 'none' Daniel@0: % draws black unit edges only. Daniel@0: % (vector) Mx1 column vector gives indexed color for each unit using the Daniel@0: % current colormap (see help colormap). Daniel@0: % (matrix) Mx3 matrix of RGB triples as rows gives each unit a fixed color. Daniel@0: % Daniel@0: % OPTIONAL INPUT ARGUMENTS Daniel@0: % Daniel@0: % Note: M is the number of map units. Daniel@0: % Note: if unspecified or given empty values ('' or []) default Daniel@0: % values are used for optional input arguments. Daniel@0: % Daniel@0: % s The size scaling factors for the units Daniel@0: % Daniel@0: % (scalar) scalar gives each unit the same size scaling: Daniel@0: % 0 unit disappears (edges can be seen as a dot). Daniel@0: % 1 by default unit has its normal size (ie. no scaling) Daniel@0: % >1 unit overlaps others Daniel@0: % (matrix) Mx1 double: each unit gets individual size scaling Daniel@0: % Daniel@0: % pos Position of origin Daniel@0: % Daniel@0: % (vector) This argument exists to be able drawing component planes Daniel@0: % in arbitrary locations in a figure. Note the operation: Daniel@0: % if this argument is given, the axis limits setting Daniel@0: % part in the routine is skipped and the limits setting Daniel@0: % will be left to be done by MATLAB's default Daniel@0: % operation. Daniel@0: % Daniel@0: % OUTPUT ARGUMENTS Daniel@0: % Daniel@0: % h (scalar) handle to the created patch object Daniel@0: % Daniel@0: % OBJECT TAGS Daniel@0: % Daniel@0: % One object handle is returned: field Tag is set to Daniel@0: % 'planeC' for component plane Daniel@0: % 'planeU' for U-matrix Daniel@0: % Daniel@0: % FEATURES Daniel@0: % Daniel@0: % There are some extra features in following arguments Daniel@0: % Daniel@0: % size Daniel@0: % - MxL matrix: radial scaling: the distance between Daniel@0: % the center of node m and its kth vertex is scaled by Daniel@0: % s(m,k). Daniel@0: % - Mx1x2 matrix: the uniform scaling is done separately for Daniel@0: % x- and y-directions Daniel@0: % - MxLx2 matrix: the scaling is done separately to x- and y- Daniel@0: % directions for each vertex. Daniel@0: % Daniel@0: % color Daniel@0: % Each vertex may be given individual color. Daniel@0: % The PATCH object interpolates the colors on the Daniel@0: % face if shading is turned to interp. Daniel@0: % - 1xMxL matrix: colormap index for each vertex Daniel@0: % - LxMx3 matrix: RGB color for each vertex Daniel@0: % Daniel@0: % Note: In both cases (size and color) the ordering of the patch Daniel@0: % vertices in the "built-in" patches is the following Daniel@0: % Daniel@0: % 'rect' 'hexa' Daniel@0: % 1 3 1 Daniel@0: % 2 4 5 2 Daniel@0: % 6 3 Daniel@0: % 4 Daniel@0: % Daniel@0: % The color interpolation result seem to depend on the order Daniel@0: % in which the patch vertices are defined. Anyway, it gives Daniel@0: % unfavourable results in our case especially with hexa grid: Daniel@0: % this is a MATLAB feature. Daniel@0: % Daniel@0: % EXAMPLES Daniel@0: % Daniel@0: % m=som_make(rand(100,4),'msize',[6 5]) % make a map Daniel@0: % Daniel@0: % % show the first variable plane using indexed color coding Daniel@0: % Daniel@0: % som_cplane(m.topol.lattice,m.topol.msize,m.codebook(:,1)); Daniel@0: % or som_cplane(m.topol,m.codebook(:,1)); Daniel@0: % or som_cplane(m,m.codebook(:,1)); Daniel@0: % Daniel@0: % % show the first variable using different sized black units Daniel@0: % Daniel@0: % som_cplane(m,'k',m.codebook(:,1)); Daniel@0: % Daniel@0: % % Show the u-matrix. First we have to calculate it. Daniel@0: % % Note: som_umat returns a matrix therefore we write u(:) to get Daniel@0: % % a vector which contains the values in the proper order. Daniel@0: % Daniel@0: % u=som_umat(m); Daniel@0: % som_cplane('hexaU', m.topol.msize, u(:)); Daniel@0: % Daniel@0: % % Show three first variables coded as RGB colors Daniel@0: % % and turn the unit edges off Daniel@0: % Daniel@0: % h=som_cplane(m, m.codebook(:,1:3),1) Daniel@0: % set(h,'edgecolor','none'); Daniel@0: % Daniel@0: % % Try this! (see section FEATURES) Daniel@0: % Daniel@0: % som_cplane('rect',[5 5],'none',rand(25,4)); Daniel@0: % som_cplane('rect',[5 5],rand(1,25,4)); Daniel@0: % Daniel@0: % SEE ALSO Daniel@0: % Daniel@0: % som_barplane Visualize the map prototype vectors as bar diagrams Daniel@0: % som_plotplane Visualize the map prototype vectors as line graphs Daniel@0: % som_pieplane Visualize the map prototype vectors as pie charts Daniel@0: % som_umat Compute unified distance matrix of self-organizing map Daniel@0: % vis_patch Define the basic patches used in som_cplane Daniel@0: % som_vis_coords The default 'hexa' and 'rect' coordinates in visualizations Daniel@0: Daniel@0: % Copyright (c) 1999-2000 by the SOM toolbox programming team. Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % Version 2.0beta Johan 061099 juuso 151199 juuso 070600 Daniel@0: Daniel@0: %%% Check & Init arguments %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: [nargin, lattice, msize, color, s, pos]=vis_planeGetArgs(varargin{:}); Daniel@0: error(nargchk(3, 5, nargin)); % check no. of input args is correct Daniel@0: Daniel@0: %% Translation? Daniel@0: Daniel@0: if nargin < 5 | isempty(pos) Daniel@0: pos=NaN; % "no translation" flag Daniel@0: elseif ~vis_valuetype(pos,{'1x2'}), Daniel@0: error('Position of origin has to be given as an 1x2 vector.'); Daniel@0: end Daniel@0: Daniel@0: %% Patchform %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: switch class(lattice) Daniel@0: case 'char' % built-in patchforms Daniel@0: pos=pos-1; Daniel@0: switch lattice Daniel@0: case {'hexa', 'hexaU'} Daniel@0: patchform=vis_patch('hexa'); Daniel@0: case {'rect', 'rectU'} Daniel@0: patchform=vis_patch('rect'); Daniel@0: otherwise Daniel@0: error([ 'Lattice ' lattice ' not implemented!']); Daniel@0: end Daniel@0: case { 'double', 'sparse'} Daniel@0: if vis_valuetype(lattice,{'nx2'}), Daniel@0: patchform=lattice; % users patchform Daniel@0: lattice='rect'; Daniel@0: else Daniel@0: error('Patchform matrix has wrong size'); Daniel@0: end Daniel@0: otherwise Daniel@0: error('String or matrix expected for lattice.'); Daniel@0: end Daniel@0: Daniel@0: l=size(patchform,1); % number of vertices Daniel@0: planeType=lattice(end); % 'U' if umatrix otherwise something else Daniel@0: Daniel@0: if ~vis_valuetype(msize,{ '1x2', 'nx2'}), Daniel@0: error('msize has to be given as 1x2 or nx2 vectors.'); Daniel@0: end Daniel@0: Daniel@0: %% msize or coordinates %%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: if size(msize,1)>1 Daniel@0: % msize is coordinate matrix Nx2? Daniel@0: Daniel@0: if planeType == 'U', % don't accept u-matrix Daniel@0: error('U-matrix visualization doesn''t work with free coordinates.'); Daniel@0: end Daniel@0: Daniel@0: % set number of map unit and unit coordinates Daniel@0: munits=size(msize,1); Daniel@0: unit_coords=msize; msize=[munits 1]; Daniel@0: Daniel@0: if isnan(pos), % no translation is done here Daniel@0: pos=[0 0]; % using [0 0] in order to prevent Daniel@0: end % axis tightening in Daniel@0: % vis_PlaneAxisProperties (arbitary coords!) Daniel@0: else Daniel@0: % msize is built-in lattice Daniel@0: Daniel@0: unit_coords=som_vis_coords(lattice,msize); Daniel@0: Daniel@0: % Calculate matrices x and y which 'moves' nodes Daniel@0: % to the correct positions: Daniel@0: % For U-matrix, the size has to be recalculated Daniel@0: if planeType == 'U', Daniel@0: xdim=2*msize(1)-1;ydim=2*msize(2)-1; Daniel@0: else Daniel@0: xdim=msize(1);ydim=msize(2); Daniel@0: end Daniel@0: munits=xdim*ydim; Daniel@0: Daniel@0: % Feature warning Daniel@0: if munits == 3 Daniel@0: warning('Problems with 1x3 and 3x1 maps. See documentation.'); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: %% Color matrix %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: if ~isnumeric(color) & ~ischar(color), Daniel@0: error('Color matrix is invalid.'); Daniel@0: else Daniel@0: d=size(color); Daniel@0: switch length(d) Daniel@0: case 2 %% Flat colors Daniel@0: if ischar(color) % Check for string 'none' Daniel@0: if strcmp(color,'none'), Daniel@0: color=NaN; Daniel@0: end Daniel@0: else Daniel@0: if ~(d(1)== 1 & d(2) == 3) & ... Daniel@0: ~(d(1) == munits & (d(2)==1 | d(2)==3)) Daniel@0: error('Color data matrix has wrong size.'); Daniel@0: elseif d(1)~=1 & d(2)==3 Daniel@0: if any(color>1 | color<0) Daniel@0: error('Color data matrix has invalid RGB values.'); Daniel@0: end Daniel@0: color=reshape(color,[1 munits 3]); % RGB colors Daniel@0: elseif d(2)==1 Daniel@0: color=color'; % indexed Daniel@0: end Daniel@0: end Daniel@0: case 3 %% Interpolated colors Daniel@0: if d(1) == 1 & d(2) == munits & d(3) == l, Daniel@0: color=reshape(color, l, munits); Daniel@0: elseif ~(d(1) == l & d(2) == munits & d(3) == 3) Daniel@0: error('Color data matrix has wrong size.'); Daniel@0: end Daniel@0: otherwise Daniel@0: error('Color data matrix has too many dimensions.'); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: %% Size matrix? %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: if nargin < 4 | isempty(s), Daniel@0: s=1; % default value for s (no scaling) Daniel@0: elseif ~isnumeric(s) Daniel@0: error('Size matrix is not numeric.'); Daniel@0: end Daniel@0: Daniel@0: %%Determine the type of size matrix Daniel@0: d=size(s); Daniel@0: switch length(d) Daniel@0: case 2 Daniel@0: if (d(1)==1 & d(2)==1), Daniel@0: % Each node gets the same, uniform scaling. Daniel@0: s=s'; sx=s; sy=s; Daniel@0: elseif (d(1)==munits & d(2)==l), Daniel@0: % Each vertex is scaled radially respetc to the Daniel@0: % node center. Daniel@0: s=s'; sx=s; sy=s; Daniel@0: elseif d(1)==munits & d(2)==1 Daniel@0: % Each node gets an individual uniform scaling. Daniel@0: sx=repmat(s',l,1); sy=sx; Daniel@0: else Daniel@0: error('Size matrix has wrong size.'); Daniel@0: end Daniel@0: case 3 Daniel@0: if d(1)==munits & d(2)==1 & d(3)==2, Daniel@0: % Each node is individually and uniformly Daniel@0: % scaled separately to x- and y-directions. Daniel@0: sx=repmat(shiftdim(s(:,:,1))',l,1); Daniel@0: sy=repmat(shiftdim(s(:,:,2))',l,1); Daniel@0: elseif d(1)==munits & d(2)==l & d(3)==2, Daniel@0: % Each vertex is scaled separately to x- and y-directions Daniel@0: % with respect to the node center. Daniel@0: sx=shiftdim(s(:,:,1))'; Daniel@0: sy=shiftdim(s(:,:,2))'; Daniel@0: else Daniel@0: error('Size matrix has wrong size.'); Daniel@0: end Daniel@0: otherwise Daniel@0: error('Size matrix has too many dimensions.'); Daniel@0: end Daniel@0: Daniel@0: % Size zero would cause division by zero. eps is as good (node disappears) Daniel@0: % I tried first NaN, it works well otherwise, but the node is Daniel@0: % then not on the axis and some commands may the work oddly. Daniel@0: % The edge may be visible, though. Daniel@0: Daniel@0: sx(sx==0)=eps; Daniel@0: sy(sy==0)=eps; Daniel@0: Daniel@0: % Rescale sizes for u-matrix Daniel@0: if planeType=='U', Daniel@0: sx=sx/2;sy=sy/2; Daniel@0: end Daniel@0: Daniel@0: %%%% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: % Making grid. %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: % Translation for each patch Daniel@0: Daniel@0: x=repmat(unit_coords(:,1)',l,1); Daniel@0: y=repmat(unit_coords(:,2)',l,1); Daniel@0: Daniel@0: % patch vertex coordiantes Daniel@0: Daniel@0: nx=repmat(patchform(:,1),1,munits); Daniel@0: ny=repmat(patchform(:,2),1,munits); Daniel@0: Daniel@0: % NB: The hexagons are not uniform in order to get even Daniel@0: % y-coordinates for the nodes. This is handled by setting _axis scaling_ Daniel@0: % so that the hexa-nodes look like uniform hexagonals. See Daniel@0: % vis_PlaneAxisProperties Daniel@0: Daniel@0: %% Make and scale the final input for PATCH: Daniel@0: Daniel@0: % 1: combine translation and scaling of each patch Daniel@0: x=(x./sx+nx).*sx; y=(y./sy+ny).*sy; Daniel@0: Daniel@0: %% 2: translation of origin (pos) Daniel@0: if ~isnan(pos) Daniel@0: x=x+pos(1);y=y+pos(2); % move upper left corner Daniel@0: end % to pos Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: %% Set axes properties Daniel@0: %% Command view([0 90]) shows the map in 2D properly oriented Daniel@0: Daniel@0: ax=newplot; % set new plot Daniel@0: vis_PlaneAxisProperties(ax,lattice,msize,pos); Daniel@0: Daniel@0: %% Draw the map! Daniel@0: Daniel@0: if ~isnan(color) Daniel@0: h_=patch(x,y,color); Daniel@0: else Daniel@0: h_=patch(x,y,'k'); % empty grid Daniel@0: set(h_,'FaceColor','none'); Daniel@0: end Daniel@0: Daniel@0: %% Set object tag Daniel@0: Daniel@0: if planeType=='U' Daniel@0: set(h_,'Tag','planeU'); Daniel@0: else Daniel@0: set(h_,'Tag','planeC'); Daniel@0: end Daniel@0: Daniel@0: %%% Build output %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: if nargout>0, h=h_; end % Set h only, Daniel@0: % if there really is output Daniel@0: