Daniel@0: function h=som_pieplane(varargin) Daniel@0: Daniel@0: %SOM_PIEPLANE Visualize the map prototype vectors as pie charts Daniel@0: % Daniel@0: % h=som_pieplane(lattice, msize, data, [color], [s], [pos]) Daniel@0: % h=som_pieplane(topol, data, [color], [s], [pos]) Daniel@0: % Daniel@0: % som_pieplane('hexa',[5 5], rand(25,4), jet(4), rand(25,1)) Daniel@0: % som_pieplane(sM, sM.codebook); Daniel@0: % Daniel@0: % Input and output arguments ([]'s are optional): Daniel@0: % lattice (string) grid 'hexa' or 'rect' Daniel@0: % msize (vector) size 1x2, defines the grid, M=msize(1)*msize(2) Daniel@0: % (matrix) size Mx2, gives explicit coordinates for each node: in Daniel@0: % this case the lattice does not matter. Daniel@0: % topol (struct) map or topology struct Daniel@0: % data (matrix) size Mxd, Mth row is the data for Mth pie. The Daniel@0: % values will be normalized to have unit sum in each row. Daniel@0: % [color] (matrix) size dx3, RGB triples. The first row is the Daniel@0: % color of the first slice in each pie etc. Default is hsv(d). Daniel@0: % (string) ColorSpec or 'none' gives the same color for each slice. Daniel@0: % [s] (matrix) size Mx1, gives an individual size scaling for each node. Daniel@0: % (scalar) gives the same size for each node. Default is 0.8. Daniel@0: % [pos] (vectors) a 1x2 vector that determines position for the Daniel@0: % origin, i.e. upper left corner. Default is no translation. Daniel@0: % Daniel@0: % h (scalar) the object handle to the PATCH object Daniel@0: % Daniel@0: % The data will be linearly scaled so that its sum is 1 in each unit. Daniel@0: % Negative values are invalid. Axis are set as in som_cplane. Daniel@0: % Daniel@0: % For more help, try 'type som_pieplane' or check out online documentation. Daniel@0: % See also SOM_CPLANE, SOM_PLOTPLANE, SOM_BARPLANE Daniel@0: Daniel@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: % Daniel@0: % som_pieplane Daniel@0: % Daniel@0: % PURPOSE Daniel@0: % Daniel@0: % Visualizes the map prototype vectors as pie charts. Daniel@0: % Daniel@0: % SYNTAX Daniel@0: % Daniel@0: % h = som_pieplane(topol, data) Daniel@0: % h = som_pieplane(lattice, msize, data) Daniel@0: % h = som_pieplane(..., color) Daniel@0: % h = som_pieplane(..., color, s) Daniel@0: % h = som_pieplane(..., color, s, pos) Daniel@0: % Daniel@0: % DESCRIPTION Daniel@0: % Daniel@0: % Visualizes the map prototype vectors as pie charts. Daniel@0: % Daniel@0: % KNOWN BUGS Daniel@0: % Daniel@0: % It is not possible to specify explicit coordinates for map Daniel@0: % consisting of just one unit as then the msize is interpreted as Daniel@0: % map size. Daniel@0: % Daniel@0: % FEATURES Daniel@0: % Daniel@0: % - negative values in data cause an error Daniel@0: % Daniel@0: % - the colors are fixed: changing colormap in the figure (see help Daniel@0: % colormap) will not affect the coloring of the slices. Daniel@0: % Daniel@0: % - if input variable s has size Nxd it gives each slice an individual Daniel@0: % scaling factor. This may be used to create a glyph where Daniel@0: % the radius of the slice, not the angle, shows the variable Daniel@0: % try, e.g., som_pieplane('rect',[5 4],ones(20,4),'w',rand(20,4)); Daniel@0: % Daniel@0: % REQUIRED INPUT ARGUMENTS Daniel@0: % Daniel@0: % lattice The basic shape of the map units Daniel@0: % Daniel@0: % (string) 'hexa' or 'rect' positions the pies according to hexagonal or Daniel@0: % rectangular map lattice. 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, Daniel@0: % width n2 units, total M=n1xn2 units). The units will Daniel@0: % be placed to their topological locations to form a Daniel@0: % uniform hexagonal or rectangular grid. Daniel@0: % (matrix) Mx2 matrix defines arbitary coordinates for the M units. In Daniel@0: % this case the argument 'lattice' has no effect. 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: % data The data to be visualized Daniel@0: % Daniel@0: % (matrix) Mxd matrix of data vectors. Negative values are invalid. Daniel@0: % Daniel@0: % OPTIONAL INPUT ARGUMENTS Daniel@0: % Daniel@0: % If value is unspecified or empty ([] or ''), the default values Daniel@0: % are used for optional input arguments. Daniel@0: % Daniel@0: % s The size scaling factors for the units Daniel@0: % Daniel@0: % (scalar) gives each unit the same size scaling: Daniel@0: % 0 unit disappears (edges can be seen as a dot) Daniel@0: % ... default size is 0.8 Daniel@0: % >1 unit overlaps others Daniel@0: % (matrix) Mx1 double: each unit gets individual size scaling Daniel@0: % Daniel@0: % color The color of the slices in each pie Daniel@0: % Daniel@0: % (string) ColorSpec or 'none' gives the same color for each slice Daniel@0: % (matrix) dx3 matrix assigns an RGB color determined by the dth row of Daniel@0: % the matrix to the dth slice (variable) in each pie plot Daniel@0: % Daniel@0: % pos Position of origin Daniel@0: % Daniel@0: % (vector) size 1x2: this is meant for drawing the plane in arbitary Daniel@0: % location in a figure. Note the operation: if this argument is Daniel@0: % given, the axis limits setting part in the routine is skipped and Daniel@0: % the limits setting will be left to be done by Daniel@0: % MATLAB's defaults. Default is no translation. 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 'planePie' Daniel@0: % Daniel@0: % EXAMPLES Daniel@0: % Daniel@0: % %%% Create the data and make a map Daniel@0: % Daniel@0: % data=rand(100,5); map=som_make(data); Daniel@0: % Daniel@0: % %%% Create a 'jet' colormap that has as many rows as the data has variables Daniel@0: % Daniel@0: % colors=jet(5); Daniel@0: % Daniel@0: % %%% Draw pies Daniel@0: % Daniel@0: % som_pieplane(map, map.codebook, colors); Daniel@0: % Daniel@0: % %%% Calculate the hits of data on the map and normalize them between [0,1] Daniel@0: % Daniel@0: % hit=som_hits(map,data); hit=hit./max(max(hit)); Daniel@0: % Daniel@0: % %%% Draw the pies so that their size tells the hit count Daniel@0: % Daniel@0: % som_pieplane(map, map.codebook, colors, hit); Daniel@0: % Daniel@0: % %%% Try this! (see section FEATURES) Daniel@0: % Daniel@0: % som_pieplane('rect',[5 4],ones(20,4),'w',rand(20,4)); Daniel@0: % Daniel@0: % SEE ALSO Daniel@0: % Daniel@0: % som_cplane Visualize a 2D component plane, u-matrix or color plane 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: 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 140799 juuso 310300 070600 Daniel@0: Daniel@0: %%% Check & Init arguments %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: [nargin, lattice, msize, data, color, s, pos] = vis_planeGetArgs(varargin{:}); Daniel@0: error(nargchk(3, 6, nargin)); % check no. of input args is correct Daniel@0: Daniel@0: % check pos Daniel@0: Daniel@0: if nargin < 6 | isempty(pos) Daniel@0: pos=NaN; % default value for pos (no translation) 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: % check msize Daniel@0: Daniel@0: if ~vis_valuetype(msize,{'1x2','nx2'}), Daniel@0: error('msize has to be 1x2 grid size vector or a Nx2 coordinate matrix.'); Daniel@0: end Daniel@0: Daniel@0: % check data Daniel@0: Daniel@0: if ~isnumeric(data), Daniel@0: error('Data matrix must be numeric.'); Daniel@0: elseif length(size((data)))>2 Daniel@0: error('Data matrix has too many dimensions!'); Daniel@0: else Daniel@0: d=size(data,2); Daniel@0: N=size(data,1); Daniel@0: end Daniel@0: Daniel@0: if any(data(:)<0) Daniel@0: error('Negative data values not allowed in pie plots!'); Daniel@0: end Daniel@0: Daniel@0: % Check lattice Daniel@0: if ~ischar(lattice) | ~any(strcmp(lattice,{'hexa','rect'})), Daniel@0: error('Invalid lattice.'); Daniel@0: end Daniel@0: Daniel@0: %% Calculate patch coordinates for slices Daniel@0: Daniel@0: for i=1:N, Daniel@0: [nx,ny]=vis_piepatch(data(i,:)); Daniel@0: piesx(:,(1+(i-1)*d):(i*d))=nx; Daniel@0: piesy(:,(1+(i-1)*d):(i*d))=ny; Daniel@0: end Daniel@0: l=size(piesx,1); Daniel@0: Daniel@0: if size(msize,1) == 1, Daniel@0: if prod(msize) ~= N Daniel@0: error('Data matrix has wrong size.'); Daniel@0: else Daniel@0: coord=som_vis_coords(lattice, msize); Daniel@0: end Daniel@0: else Daniel@0: if N ~= size(msize,1), Daniel@0: error('Data matrix has wrong size.'); Daniel@0: end Daniel@0: coord=msize; Daniel@0: % This turns the axis tightening off, Daniel@0: % as now we don't now the limits (no fixed grid) Daniel@0: if isnan(pos); pos=[0 0]; end Daniel@0: end Daniel@0: x=reshape(repmat(coord(:,1),1,l*d)',l,d*N); Daniel@0: y=reshape(repmat(coord(:,2),1,l*d)',l,d*N); Daniel@0: Daniel@0: % Check size Daniel@0: Daniel@0: if nargin < 5 | isempty(s), Daniel@0: s=0.8; % default value for scaling Daniel@0: elseif ~vis_valuetype(s, {'1x1', [N 1], [N d]}), Daniel@0: error('Size matrix does not match with the data matrix.'); Daniel@0: elseif size(s) == [N 1], Daniel@0: s=reshape(repmat(s,1,l*d)',l,d*N); Daniel@0: elseif all(size(s) ~= [1 1]), Daniel@0: s=reshape(repmat(reshape(s',d*N,1),1,l)',l,d*N); Daniel@0: end Daniel@0: Daniel@0: % Check color Daniel@0: % C_FLAG is a flag for color 'none' Daniel@0: Daniel@0: if nargin < 4 | isempty(color) Daniel@0: color=hsv(d); C_FLAG=0; % default n hsv colors Daniel@0: end Daniel@0: Daniel@0: if ~(vis_valuetype(color, {[d 3], 'nx3rgb'},'all')) & ... Daniel@0: ~vis_valuetype(color,{'colorstyle','1x3rgb'}), Daniel@0: error('The color matrix has wrong size or contains invalid values.'); Daniel@0: elseif ischar(color) & strcmp(color,'none'), Daniel@0: C_FLAG=1; % check for color 'none' Daniel@0: color='w'; Daniel@0: else Daniel@0: C_FLAG=0; % valid color string or colormap Daniel@0: end Daniel@0: Daniel@0: %% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: % Size zero would cause division by zero. eps is as good (node disappears) Daniel@0: % The edge may be visible, though. (NaN causes some other problems) Daniel@0: Daniel@0: s(s==0)=eps; Daniel@0: Daniel@0: %% 1. Scaling Daniel@0: x=(x./s+piesx).*s; y=(y./s+piesy).*s; Daniel@0: Daniel@0: %% 2. Translation Daniel@0: if ~isnan(pos) Daniel@0: x=x+pos(1);y=y+pos(2); Daniel@0: end Daniel@0: Daniel@0: %% 3. Rearrange dx3 color matrix Daniel@0: Daniel@0: if ~isstr(color) & size(color,1)~=1, Daniel@0: color=reshape(repmat(color,N,1),[1 N*d 3]); Daniel@0: end Daniel@0: Daniel@0: %% Set axes properties Daniel@0: ax=newplot; % get current axis Daniel@0: vis_PlaneAxisProperties(ax,lattice, msize, pos); Daniel@0: Daniel@0: %% Draw the plane! Daniel@0: Daniel@0: h_=patch(x,y,color); Daniel@0: Daniel@0: if C_FLAG Daniel@0: set(h_,'FaceColor','none'); Daniel@0: end Daniel@0: Daniel@0: set(h_,'Tag','planePie'); % tag the object Daniel@0: Daniel@0: %%% Build output %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: if nargout>0, h=h_; end % Set h only if Daniel@0: % there really is output Daniel@0: %%% Subfunctions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: function [x,y]=vis_piepatch(v) Daniel@0: Daniel@0: % Do a pie (see e.g. the MathWorks function PIE). Daniel@0: % Origin is at (0,0) and the radius is .5. Daniel@0: Daniel@0: N=25; Daniel@0: Daniel@0: if sum(v)==0, v_is_zero = 1; v(1) = 1; else v_is_zero = 0; end Daniel@0: Daniel@0: v(v==0) = eps; % Matlab 5.2 version of linspace doesn't work otherwise Daniel@0: Daniel@0: phi=[0 2*pi*cumsum(v./sum(v))]; Daniel@0: Daniel@0: for i=2:length(phi), Daniel@0: [xi,yi]=pol2cart(linspace(phi(i-1),phi(i),N),0.5); Daniel@0: x(:,i-1)=[0 xi 0]'; Daniel@0: y(:,i-1)=[0 yi 0]'; Daniel@0: end Daniel@0: Daniel@0: if v_is_zero, x = x*0; y = y*0; end Daniel@0: