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

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