annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_pieplane.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function h=som_pieplane(varargin)
wolffd@0 2
wolffd@0 3 %SOM_PIEPLANE Visualize the map prototype vectors as pie charts
wolffd@0 4 %
wolffd@0 5 % h=som_pieplane(lattice, msize, data, [color], [s], [pos])
wolffd@0 6 % h=som_pieplane(topol, data, [color], [s], [pos])
wolffd@0 7 %
wolffd@0 8 % som_pieplane('hexa',[5 5], rand(25,4), jet(4), rand(25,1))
wolffd@0 9 % som_pieplane(sM, sM.codebook);
wolffd@0 10 %
wolffd@0 11 % Input and output arguments ([]'s are optional):
wolffd@0 12 % lattice (string) grid 'hexa' or 'rect'
wolffd@0 13 % msize (vector) size 1x2, defines the grid, M=msize(1)*msize(2)
wolffd@0 14 % (matrix) size Mx2, gives explicit coordinates for each node: in
wolffd@0 15 % this case the lattice does not matter.
wolffd@0 16 % topol (struct) map or topology struct
wolffd@0 17 % data (matrix) size Mxd, Mth row is the data for Mth pie. The
wolffd@0 18 % values will be normalized to have unit sum in each row.
wolffd@0 19 % [color] (matrix) size dx3, RGB triples. The first row is the
wolffd@0 20 % color of the first slice in each pie etc. Default is hsv(d).
wolffd@0 21 % (string) ColorSpec or 'none' gives the same color for each slice.
wolffd@0 22 % [s] (matrix) size Mx1, gives an individual size scaling for each node.
wolffd@0 23 % (scalar) gives the same size for each node. Default is 0.8.
wolffd@0 24 % [pos] (vectors) a 1x2 vector that determines position for the
wolffd@0 25 % origin, i.e. upper left corner. Default is no translation.
wolffd@0 26 %
wolffd@0 27 % h (scalar) the object handle to the PATCH object
wolffd@0 28 %
wolffd@0 29 % The data will be linearly scaled so that its sum is 1 in each unit.
wolffd@0 30 % Negative values are invalid. Axis are set as in som_cplane.
wolffd@0 31 %
wolffd@0 32 % For more help, try 'type som_pieplane' or check out online documentation.
wolffd@0 33 % See also SOM_CPLANE, SOM_PLOTPLANE, SOM_BARPLANE
wolffd@0 34
wolffd@0 35 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 36 %
wolffd@0 37 % som_pieplane
wolffd@0 38 %
wolffd@0 39 % PURPOSE
wolffd@0 40 %
wolffd@0 41 % Visualizes the map prototype vectors as pie charts.
wolffd@0 42 %
wolffd@0 43 % SYNTAX
wolffd@0 44 %
wolffd@0 45 % h = som_pieplane(topol, data)
wolffd@0 46 % h = som_pieplane(lattice, msize, data)
wolffd@0 47 % h = som_pieplane(..., color)
wolffd@0 48 % h = som_pieplane(..., color, s)
wolffd@0 49 % h = som_pieplane(..., color, s, pos)
wolffd@0 50 %
wolffd@0 51 % DESCRIPTION
wolffd@0 52 %
wolffd@0 53 % Visualizes the map prototype vectors as pie charts.
wolffd@0 54 %
wolffd@0 55 % KNOWN BUGS
wolffd@0 56 %
wolffd@0 57 % It is not possible to specify explicit coordinates for map
wolffd@0 58 % consisting of just one unit as then the msize is interpreted as
wolffd@0 59 % map size.
wolffd@0 60 %
wolffd@0 61 % FEATURES
wolffd@0 62 %
wolffd@0 63 % - negative values in data cause an error
wolffd@0 64 %
wolffd@0 65 % - the colors are fixed: changing colormap in the figure (see help
wolffd@0 66 % colormap) will not affect the coloring of the slices.
wolffd@0 67 %
wolffd@0 68 % - if input variable s has size Nxd it gives each slice an individual
wolffd@0 69 % scaling factor. This may be used to create a glyph where
wolffd@0 70 % the radius of the slice, not the angle, shows the variable
wolffd@0 71 % try, e.g., som_pieplane('rect',[5 4],ones(20,4),'w',rand(20,4));
wolffd@0 72 %
wolffd@0 73 % REQUIRED INPUT ARGUMENTS
wolffd@0 74 %
wolffd@0 75 % lattice The basic shape of the map units
wolffd@0 76 %
wolffd@0 77 % (string) 'hexa' or 'rect' positions the pies according to hexagonal or
wolffd@0 78 % rectangular map lattice.
wolffd@0 79 %
wolffd@0 80 % msize The size of the map grid
wolffd@0 81 %
wolffd@0 82 % (vector) [n1 n2] vector defines the map size (height n1 units,
wolffd@0 83 % width n2 units, total M=n1xn2 units). The units will
wolffd@0 84 % be placed to their topological locations to form a
wolffd@0 85 % uniform hexagonal or rectangular grid.
wolffd@0 86 % (matrix) Mx2 matrix defines arbitary coordinates for the M units. In
wolffd@0 87 % this case the argument 'lattice' has no effect.
wolffd@0 88 %
wolffd@0 89 % topol Topology of the map grid
wolffd@0 90 %
wolffd@0 91 % (struct) map or topology struct from which the topology is taken
wolffd@0 92 %
wolffd@0 93 % data The data to be visualized
wolffd@0 94 %
wolffd@0 95 % (matrix) Mxd matrix of data vectors. Negative values are invalid.
wolffd@0 96 %
wolffd@0 97 % OPTIONAL INPUT ARGUMENTS
wolffd@0 98 %
wolffd@0 99 % If value is unspecified or empty ([] or ''), the default values
wolffd@0 100 % are used for optional input arguments.
wolffd@0 101 %
wolffd@0 102 % s The size scaling factors for the units
wolffd@0 103 %
wolffd@0 104 % (scalar) gives each unit the same size scaling:
wolffd@0 105 % 0 unit disappears (edges can be seen as a dot)
wolffd@0 106 % ... default size is 0.8
wolffd@0 107 % >1 unit overlaps others
wolffd@0 108 % (matrix) Mx1 double: each unit gets individual size scaling
wolffd@0 109 %
wolffd@0 110 % color The color of the slices in each pie
wolffd@0 111 %
wolffd@0 112 % (string) ColorSpec or 'none' gives the same color for each slice
wolffd@0 113 % (matrix) dx3 matrix assigns an RGB color determined by the dth row of
wolffd@0 114 % the matrix to the dth slice (variable) in each pie plot
wolffd@0 115 %
wolffd@0 116 % pos Position of origin
wolffd@0 117 %
wolffd@0 118 % (vector) size 1x2: this is meant for drawing the plane in arbitary
wolffd@0 119 % location in a figure. Note the operation: if this argument is
wolffd@0 120 % given, the axis limits setting part in the routine is skipped and
wolffd@0 121 % the limits setting will be left to be done by
wolffd@0 122 % MATLAB's defaults. Default is no translation.
wolffd@0 123 %
wolffd@0 124 % OUTPUT ARGUMENTS
wolffd@0 125 %
wolffd@0 126 % h (scalar) Handle to the created patch object.
wolffd@0 127 %
wolffd@0 128 % OBJECT TAGS
wolffd@0 129 %
wolffd@0 130 % One object handle is returned: field Tag is set to 'planePie'
wolffd@0 131 %
wolffd@0 132 % EXAMPLES
wolffd@0 133 %
wolffd@0 134 % %%% Create the data and make a map
wolffd@0 135 %
wolffd@0 136 % data=rand(100,5); map=som_make(data);
wolffd@0 137 %
wolffd@0 138 % %%% Create a 'jet' colormap that has as many rows as the data has variables
wolffd@0 139 %
wolffd@0 140 % colors=jet(5);
wolffd@0 141 %
wolffd@0 142 % %%% Draw pies
wolffd@0 143 %
wolffd@0 144 % som_pieplane(map, map.codebook, colors);
wolffd@0 145 %
wolffd@0 146 % %%% Calculate the hits of data on the map and normalize them between [0,1]
wolffd@0 147 %
wolffd@0 148 % hit=som_hits(map,data); hit=hit./max(max(hit));
wolffd@0 149 %
wolffd@0 150 % %%% Draw the pies so that their size tells the hit count
wolffd@0 151 %
wolffd@0 152 % som_pieplane(map, map.codebook, colors, hit);
wolffd@0 153 %
wolffd@0 154 % %%% Try this! (see section FEATURES)
wolffd@0 155 %
wolffd@0 156 % som_pieplane('rect',[5 4],ones(20,4),'w',rand(20,4));
wolffd@0 157 %
wolffd@0 158 % SEE ALSO
wolffd@0 159 %
wolffd@0 160 % som_cplane Visualize a 2D component plane, u-matrix or color plane
wolffd@0 161 % som_barplane Visualize the map prototype vectors as bar diagrams
wolffd@0 162 % som_plotplane Visualize the map prototype vectors as line graphs
wolffd@0 163
wolffd@0 164 % Copyright (c) 1999-2000 by the SOM toolbox programming team.
wolffd@0 165 % http://www.cis.hut.fi/projects/somtoolbox/
wolffd@0 166
wolffd@0 167 % Version 2.0beta Johan 140799 juuso 310300 070600
wolffd@0 168
wolffd@0 169 %%% Check & Init arguments %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 170
wolffd@0 171 [nargin, lattice, msize, data, color, s, pos] = vis_planeGetArgs(varargin{:});
wolffd@0 172 error(nargchk(3, 6, nargin)); % check no. of input args is correct
wolffd@0 173
wolffd@0 174 % check pos
wolffd@0 175
wolffd@0 176 if nargin < 6 | isempty(pos)
wolffd@0 177 pos=NaN; % default value for pos (no translation)
wolffd@0 178 elseif ~vis_valuetype(pos,{'1x2'})
wolffd@0 179 error('Position of origin has to be given as an 1x2 vector');
wolffd@0 180 end
wolffd@0 181
wolffd@0 182 % check msize
wolffd@0 183
wolffd@0 184 if ~vis_valuetype(msize,{'1x2','nx2'}),
wolffd@0 185 error('msize has to be 1x2 grid size vector or a Nx2 coordinate matrix.');
wolffd@0 186 end
wolffd@0 187
wolffd@0 188 % check data
wolffd@0 189
wolffd@0 190 if ~isnumeric(data),
wolffd@0 191 error('Data matrix must be numeric.');
wolffd@0 192 elseif length(size((data)))>2
wolffd@0 193 error('Data matrix has too many dimensions!');
wolffd@0 194 else
wolffd@0 195 d=size(data,2);
wolffd@0 196 N=size(data,1);
wolffd@0 197 end
wolffd@0 198
wolffd@0 199 if any(data(:)<0)
wolffd@0 200 error('Negative data values not allowed in pie plots!');
wolffd@0 201 end
wolffd@0 202
wolffd@0 203 % Check lattice
wolffd@0 204 if ~ischar(lattice) | ~any(strcmp(lattice,{'hexa','rect'})),
wolffd@0 205 error('Invalid lattice.');
wolffd@0 206 end
wolffd@0 207
wolffd@0 208 %% Calculate patch coordinates for slices
wolffd@0 209
wolffd@0 210 for i=1:N,
wolffd@0 211 [nx,ny]=vis_piepatch(data(i,:));
wolffd@0 212 piesx(:,(1+(i-1)*d):(i*d))=nx;
wolffd@0 213 piesy(:,(1+(i-1)*d):(i*d))=ny;
wolffd@0 214 end
wolffd@0 215 l=size(piesx,1);
wolffd@0 216
wolffd@0 217 if size(msize,1) == 1,
wolffd@0 218 if prod(msize) ~= N
wolffd@0 219 error('Data matrix has wrong size.');
wolffd@0 220 else
wolffd@0 221 coord=som_vis_coords(lattice, msize);
wolffd@0 222 end
wolffd@0 223 else
wolffd@0 224 if N ~= size(msize,1),
wolffd@0 225 error('Data matrix has wrong size.');
wolffd@0 226 end
wolffd@0 227 coord=msize;
wolffd@0 228 % This turns the axis tightening off,
wolffd@0 229 % as now we don't now the limits (no fixed grid)
wolffd@0 230 if isnan(pos); pos=[0 0]; end
wolffd@0 231 end
wolffd@0 232 x=reshape(repmat(coord(:,1),1,l*d)',l,d*N);
wolffd@0 233 y=reshape(repmat(coord(:,2),1,l*d)',l,d*N);
wolffd@0 234
wolffd@0 235 % Check size
wolffd@0 236
wolffd@0 237 if nargin < 5 | isempty(s),
wolffd@0 238 s=0.8; % default value for scaling
wolffd@0 239 elseif ~vis_valuetype(s, {'1x1', [N 1], [N d]}),
wolffd@0 240 error('Size matrix does not match with the data matrix.');
wolffd@0 241 elseif size(s) == [N 1],
wolffd@0 242 s=reshape(repmat(s,1,l*d)',l,d*N);
wolffd@0 243 elseif all(size(s) ~= [1 1]),
wolffd@0 244 s=reshape(repmat(reshape(s',d*N,1),1,l)',l,d*N);
wolffd@0 245 end
wolffd@0 246
wolffd@0 247 % Check color
wolffd@0 248 % C_FLAG is a flag for color 'none'
wolffd@0 249
wolffd@0 250 if nargin < 4 | isempty(color)
wolffd@0 251 color=hsv(d); C_FLAG=0; % default n hsv colors
wolffd@0 252 end
wolffd@0 253
wolffd@0 254 if ~(vis_valuetype(color, {[d 3], 'nx3rgb'},'all')) & ...
wolffd@0 255 ~vis_valuetype(color,{'colorstyle','1x3rgb'}),
wolffd@0 256 error('The color matrix has wrong size or contains invalid values.');
wolffd@0 257 elseif ischar(color) & strcmp(color,'none'),
wolffd@0 258 C_FLAG=1; % check for color 'none'
wolffd@0 259 color='w';
wolffd@0 260 else
wolffd@0 261 C_FLAG=0; % valid color string or colormap
wolffd@0 262 end
wolffd@0 263
wolffd@0 264 %% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 265
wolffd@0 266 % Size zero would cause division by zero. eps is as good (node disappears)
wolffd@0 267 % The edge may be visible, though. (NaN causes some other problems)
wolffd@0 268
wolffd@0 269 s(s==0)=eps;
wolffd@0 270
wolffd@0 271 %% 1. Scaling
wolffd@0 272 x=(x./s+piesx).*s; y=(y./s+piesy).*s;
wolffd@0 273
wolffd@0 274 %% 2. Translation
wolffd@0 275 if ~isnan(pos)
wolffd@0 276 x=x+pos(1);y=y+pos(2);
wolffd@0 277 end
wolffd@0 278
wolffd@0 279 %% 3. Rearrange dx3 color matrix
wolffd@0 280
wolffd@0 281 if ~isstr(color) & size(color,1)~=1,
wolffd@0 282 color=reshape(repmat(color,N,1),[1 N*d 3]);
wolffd@0 283 end
wolffd@0 284
wolffd@0 285 %% Set axes properties
wolffd@0 286 ax=newplot; % get current axis
wolffd@0 287 vis_PlaneAxisProperties(ax,lattice, msize, pos);
wolffd@0 288
wolffd@0 289 %% Draw the plane!
wolffd@0 290
wolffd@0 291 h_=patch(x,y,color);
wolffd@0 292
wolffd@0 293 if C_FLAG
wolffd@0 294 set(h_,'FaceColor','none');
wolffd@0 295 end
wolffd@0 296
wolffd@0 297 set(h_,'Tag','planePie'); % tag the object
wolffd@0 298
wolffd@0 299 %%% Build output %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 300
wolffd@0 301 if nargout>0, h=h_; end % Set h only if
wolffd@0 302 % there really is output
wolffd@0 303 %%% Subfunctions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 304
wolffd@0 305 function [x,y]=vis_piepatch(v)
wolffd@0 306
wolffd@0 307 % Do a pie (see e.g. the MathWorks function PIE).
wolffd@0 308 % Origin is at (0,0) and the radius is .5.
wolffd@0 309
wolffd@0 310 N=25;
wolffd@0 311
wolffd@0 312 if sum(v)==0, v_is_zero = 1; v(1) = 1; else v_is_zero = 0; end
wolffd@0 313
wolffd@0 314 v(v==0) = eps; % Matlab 5.2 version of linspace doesn't work otherwise
wolffd@0 315
wolffd@0 316 phi=[0 2*pi*cumsum(v./sum(v))];
wolffd@0 317
wolffd@0 318 for i=2:length(phi),
wolffd@0 319 [xi,yi]=pol2cart(linspace(phi(i-1),phi(i),N),0.5);
wolffd@0 320 x(:,i-1)=[0 xi 0]';
wolffd@0 321 y(:,i-1)=[0 yi 0]';
wolffd@0 322 end
wolffd@0 323
wolffd@0 324 if v_is_zero, x = x*0; y = y*0; end
wolffd@0 325