annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_barplane.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_barplane(varargin)
wolffd@0 2
wolffd@0 3 %SOM_BARPLANE Visualize the map prototype vectors as bar charts
wolffd@0 4 %
wolffd@0 5 % h = som_barplane(lattice, msize, data, [color], [scaling], [gap], [pos])
wolffd@0 6 % h = som_barplane(topol, data, [color], [scaling], [gap], [pos])
wolffd@0 7 %
wolffd@0 8 % som_barplane('hexa',[5 5], rand(25,4), jet(4))
wolffd@0 9 % som_barplane(sM, sM.codebook,'none')
wolffd@0 10 %
wolffd@0 11 % Input and output argumetns ([]'s are optional):
wolffd@0 12 % lattice (string) grid 'hexa' or 'rect'
wolffd@0 13 % msize (vector) size 1x2, defines the map grid size msize, M=prod(msize)
wolffd@0 14 % (matrix) size Mx2, gives explicit coordinates for each node:
wolffd@0 15 % in this case the first argument does not matter.
wolffd@0 16 % topol (struct) map or topology struct
wolffd@0 17 % data (matrix) size Mxd, each row defines heights of the bars
wolffd@0 18 % [color] (matrix) size dx3, of RGB triples. The rows define colors
wolffd@0 19 % for each bar in a node. Default is hsv(d). A ColorSpec or
wolffd@0 20 % (string) A ColorSpec or 'none' gives each bar the same color.
wolffd@0 21 % [scaling] (string) 'none', 'unitwise' or 'varwise'. The scaling
wolffd@0 22 % mode for the values. Default is 'varwise'.
wolffd@0 23 % [gap] (scalar) Defines the gap between bars, limits: 0 <= gap <= 1
wolffd@0 24 % where 0=no gap, 1=bars are thin lines. Default is 0.25.
wolffd@0 25 % [pos] (vector) 1x2 vector defines the position of origin.
wolffd@0 26 % Default is [1 1].
wolffd@0 27 %
wolffd@0 28 % h (scalar) the object handle to the PATCH object
wolffd@0 29 %
wolffd@0 30 % Axis are set as in SOM_CPLANE.
wolffd@0 31 %
wolffd@0 32 % For more help, try 'type som_barplane' or check out online documentation.
wolffd@0 33 % See also SOM_CPLANE, SOM_PLOTPLANE, SOM_PIEPLANE.
wolffd@0 34
wolffd@0 35 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 36 %
wolffd@0 37 % som_barplane
wolffd@0 38 %
wolffd@0 39 % PURPOSE
wolffd@0 40 %
wolffd@0 41 % Visualizes the map prototype vectors as bar charts.
wolffd@0 42 %
wolffd@0 43 % SYNTAX
wolffd@0 44 %
wolffd@0 45 % h = som_barplane(topol, data)
wolffd@0 46 % h = som_barplane(lattice, msize, data)
wolffd@0 47 % h = som_barplane(..., color)
wolffd@0 48 % h = som_barplane(..., color, scaling)
wolffd@0 49 % h = som_barplane(..., color, scaling, gap)
wolffd@0 50 % h = som_barplane(..., color, scaling, gap, pos)
wolffd@0 51 %
wolffd@0 52 % DESCRIPTION
wolffd@0 53 %
wolffd@0 54 % Visualizes the map prototype vectors as bar charts.
wolffd@0 55 %
wolffd@0 56 % REQUIRED INPUT ARGUMENTS
wolffd@0 57 %
wolffd@0 58 % lattice The basic shape of the map units
wolffd@0 59 % (string) 'hexa' or 'rect' positions the bar charts according to
wolffd@0 60 % hexagonal or rectangular map lattice
wolffd@0 61 %
wolffd@0 62 % msize The size of the map grid
wolffd@0 63 % (vector) [n1 n2] vector defines the map size (height: n1 units widht: n2
wolffd@0 64 % units, total: M=n1xn2 units). The units will be placed to their
wolffd@0 65 % topological locations in order to form a uniform hexagonal or
wolffd@0 66 % rectangular grid.
wolffd@0 67 % (matrix) Mx2 matrix defines arbitary coordinates for the N units. In
wolffd@0 68 % this case the argument 'lattice' has no effect
wolffd@0 69 %
wolffd@0 70 % topol Topology of the map grid
wolffd@0 71 %
wolffd@0 72 % (struct) map or topology struct from which the topology is taken
wolffd@0 73 %
wolffd@0 74 % data The data to use when constructing the bar charts.
wolffd@0 75 % Typically, the map codebook or some of its components.
wolffd@0 76 % (matrix) Mxd matrix. A row defines heights of the bars.
wolffd@0 77 %
wolffd@0 78 % OPTIONAL INPUT ARGUMENTS
wolffd@0 79 %
wolffd@0 80 % Note: if unspecified or given an empty value ('' or []), default
wolffd@0 81 % values are used for optional input arguments.
wolffd@0 82 %
wolffd@0 83 % color The color of the bars in each pie
wolffd@0 84 % (ColorSpec) or (string) 'none' gives the same color for each slice.
wolffd@0 85 % (matrix) dx3 matrix assigns an RGB color determined by the dth row of
wolffd@0 86 % the matrix to the dth bar (variable) in each bar plot.
wolffd@0 87 % Default value is hsv(d).
wolffd@0 88 %
wolffd@0 89 % scaling How to scale the values
wolffd@0 90 % (string) 'none', 'unitwise' or 'varwise'. This determines the
wolffd@0 91 % scaling of codebook values when drawing the bars.
wolffd@0 92 %
wolffd@0 93 % 'none' don't scale at all. The bars are not limited
wolffd@0 94 % to remain inside he units' area: That is, if value of
wolffd@0 95 % some variable exceeds [-.625,.625] for 'rect' (and
wolffd@0 96 % in "worst case" [-.5,-.5] for 'hexa') the bars may
wolffd@0 97 % overlap other units.
wolffd@0 98 %
wolffd@0 99 % Base line (zero value line)
wolffd@0 100 % - is in the middle of the unit if data (codebook) contains both
wolffd@0 101 % negative and positive values (or is completely zero).
wolffd@0 102 % - is in the top the unit if data (codebook) contains only
wolffd@0 103 % non-positive values (everything <=0).
wolffd@0 104 % - is in the bottom the unit if data (codebook) contains only
wolffd@0 105 % non-negative values (everything >=0).
wolffd@0 106 %
wolffd@0 107 % 'varwise' scales values so that each variable is scaled separately
wolffd@0 108 % so that when it gets its overall maximum value, the
wolffd@0 109 % corresponding bar gets maximum range and for minimum value
wolffd@0 110 % it gets the minimum range. Baseline: see scaling 'none'
wolffd@0 111 % This is the default.
wolffd@0 112 %
wolffd@0 113 % 'unitwise' scales values in each unit individually so that the
wolffd@0 114 % bars for variables having minimum and maximum values have minimum
wolffd@0 115 % and maximum range inside each unit, respectively.
wolffd@0 116 % In this case the zero value line may move depending on the values.
wolffd@0 117 %
wolffd@0 118
wolffd@0 119 % gap The gap between bars
wolffd@0 120 % (scalar) 0: no gap: bars are glued together
wolffd@0 121 % ... default value is 0.25
wolffd@0 122 % 1: maximum gap: bars are thin lines
wolffd@0 123 %
wolffd@0 124 % pos Position of origin
wolffd@0 125 % (vector) size 1x2. This is meant for drawing the plane in arbitrary
wolffd@0 126 % location in a figure. Note the operation: if this argument is
wolffd@0 127 % given, the axis limits setting part in the routine is skipped and
wolffd@0 128 % the limits setting will be left to be done by MATLAB's defaults.
wolffd@0 129 % Default is [1 1].
wolffd@0 130 %
wolffd@0 131 % OUTPUT ARGUMENTS
wolffd@0 132 %
wolffd@0 133 % h (scalar) handle to the created patch object
wolffd@0 134 %
wolffd@0 135 % OBJECT TAGS
wolffd@0 136 %
wolffd@0 137 % One object handle is returned: field Tag is set to 'planeBar'
wolffd@0 138 %
wolffd@0 139 % FEATURES
wolffd@0 140 %
wolffd@0 141 % - The colors are fixed: changing colormap in the figure (see help
wolffd@0 142 % colormap) will not change the coloring of the bars.
wolffd@0 143 %
wolffd@0 144 % EXAMPLES
wolffd@0 145 %
wolffd@0 146 % %%% Create the data and make a map
wolffd@0 147 %
wolffd@0 148 % data=rand(100,5); map=som_make(data);
wolffd@0 149 %
wolffd@0 150 % %%% Create a 'jet' colormap that has as many rows as the data has variables
wolffd@0 151 %
wolffd@0 152 % colors=jet(5);
wolffd@0 153 %
wolffd@0 154 % %%% Draw bars
wolffd@0 155 %
wolffd@0 156 % som_barplane(map.topol.lattice, map.topol.msize, map.codebook, colors);
wolffd@0 157 % or som_barplane(map.topol, map.codebook, colors);
wolffd@0 158 % or som_barplane(map, map.codebook, colors);
wolffd@0 159 %
wolffd@0 160 % %%% Draw the bars so that the gap between the bars is bigger and all
wolffd@0 161 % bars are black
wolffd@0 162 %
wolffd@0 163 % som_barplane(map, map.codebook, 'k', '', 0.6);
wolffd@0 164 %
wolffd@0 165 % SEE ALSO
wolffd@0 166 %
wolffd@0 167 % som_cplane Visualize a 2D component plane, u-matrix or color plane
wolffd@0 168 % som_plotplane Visualize the map prototype vectors as line graphs
wolffd@0 169 % som_pieplane Visualize the map prototype vectors as pie charts
wolffd@0 170
wolffd@0 171 % Copyright (c) 1999-2000 by the SOM toolbox programming team.
wolffd@0 172 % http://www.cis.hut.fi/projects/somtoolbox/
wolffd@0 173
wolffd@0 174 % Version 2.0beta Juha P 110599, Johan 140799, juuso 151199 140300 070600
wolffd@0 175
wolffd@0 176 %%% Check & Init arguments %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 177
wolffd@0 178 [nargin, lattice, msize, data, color, scaling, gap, pos] = vis_planeGetArgs(varargin{:});
wolffd@0 179 error(nargchk(3, 7, nargin)) % check that no. of input args is correct
wolffd@0 180
wolffd@0 181 % Check pos
wolffd@0 182
wolffd@0 183 if nargin < 7 | isempty(pos)
wolffd@0 184 pos=NaN; % default value for pos (no translation)
wolffd@0 185 elseif ~vis_valuetype(pos,{'1x2'})
wolffd@0 186 error('Position of origin has to be given as an 1x2 vector');
wolffd@0 187 end
wolffd@0 188
wolffd@0 189 % Check gap
wolffd@0 190
wolffd@0 191 if nargin < 6 | isempty(gap),
wolffd@0 192 gap=0.25; % default value for gap
wolffd@0 193 elseif ~vis_valuetype(gap, {'1x1'}),
wolffd@0 194 error('Gap value must be scalar.');
wolffd@0 195 elseif ~(gap >= 0 & gap<=1)
wolffd@0 196 error('Gap value must be in interval [0,1].')
wolffd@0 197 end
wolffd@0 198
wolffd@0 199 % Check scaling
wolffd@0 200
wolffd@0 201 if nargin < 5 | isempty(scaling),
wolffd@0 202 scaling='varwise';
wolffd@0 203 elseif ~vis_valuetype(scaling,{'string'}) | ...
wolffd@0 204 ~any(strcmp(scaling,{'none','unitwise','varwise'})),
wolffd@0 205 error('scaling sholud be ''none'', ''unitwise'' or ''varwise''.');
wolffd@0 206 end
wolffd@0 207
wolffd@0 208 % Check msize
wolffd@0 209
wolffd@0 210 if ~vis_valuetype(msize,{'1x2','nx2'}),
wolffd@0 211 error('msize has to be 1x2 grid size vector or a Nx2 coordinate matrix.');
wolffd@0 212 end
wolffd@0 213
wolffd@0 214 % Check data
wolffd@0 215
wolffd@0 216 if ~isnumeric(data),
wolffd@0 217 error('Data matrix has to be numeric.');
wolffd@0 218 elseif length(size((data)))>2
wolffd@0 219 error('Data matrix has too many dimensions!');
wolffd@0 220 else
wolffd@0 221 d=size(data,2);
wolffd@0 222 N=size(data,1);
wolffd@0 223 end
wolffd@0 224
wolffd@0 225 s=.8; % patch size scaling factor
wolffd@0 226
wolffd@0 227 switch scaling,
wolffd@0 228 case 'none'
wolffd@0 229 % no scaling: don't scale
wolffd@0 230 % Check data max and min values
wolffd@0 231 positive=any(data(:)>0); negative=any(data(:)<0);
wolffd@0 232 if (positive & negative) | (~positive & ~negative),
wolffd@0 233 % Data contains both negative and positive values (or is
wolffd@0 234 % completely zero) baseline to centre
wolffd@0 235 zeroline='zero';
wolffd@0 236 elseif positive & ~negative
wolffd@0 237 % Data contains only positive values: baseline to bottom
wolffd@0 238 zeroline='bottom';
wolffd@0 239 elseif ~positive & negative
wolffd@0 240 % Data contains only negative values: baseline to top
wolffd@0 241 zeroline='top';
wolffd@0 242 end
wolffd@0 243 case 'unitwise'
wolffd@0 244 % scale the variables so that the bar for variable with the maximum
wolffd@0 245 % value in the unit spans to the upper edge of the unit
wolffd@0 246 % and the bar for the variable with minimum value spans to the lower edge,
wolffd@0 247 % respectively.
wolffd@0 248 zeroline='moving';
wolffd@0 249 case 'varwise'
wolffd@0 250 % Check data max and min values
wolffd@0 251 positive=any(data(:)>0); negative=any(data(:)<0);
wolffd@0 252 if (positive & negative) | (~positive & ~negative),
wolffd@0 253 % Data contains both negative and positive values (or is
wolffd@0 254 % completely zero) baseline to
wolffd@0 255 % centre, scale data so that it doesn't overflow
wolffd@0 256 data=data./repmat(max(abs([max(data); min(data)])),N,1)*.5;
wolffd@0 257 zeroline='zero';
wolffd@0 258 elseif positive & ~negative
wolffd@0 259 % Data contains only positive values: baseline to
wolffd@0 260 % bottom, scale data so that it doesn't overflow
wolffd@0 261 data=data./repmat(max(abs([max(data); min(data)])),N,1)*.5;
wolffd@0 262 zeroline='bottom';
wolffd@0 263 elseif ~positive & negative
wolffd@0 264 % Data contains only negative values: baseline to
wolffd@0 265 % top, scale data so that it doesn't overflow
wolffd@0 266 zeroline='top';
wolffd@0 267 data=data./repmat(max(abs([max(data); min(data)])),N,1)*.5;
wolffd@0 268 end
wolffd@0 269 otherwise
wolffd@0 270 error('Unknown scaling mode?');
wolffd@0 271 end
wolffd@0 272
wolffd@0 273 for i=1:N, % calculate patch coordinates for
wolffd@0 274 v=data(i,:);
wolffd@0 275 [nx,ny]=vis_barpatch(v,gap,zeroline); % bars
wolffd@0 276 barx(:,(1+(i-1)*d):(i*d))=s*nx;
wolffd@0 277 bary(:,(1+(i-1)*d):(i*d))=s*ny;
wolffd@0 278 end
wolffd@0 279 l=size(barx,1);
wolffd@0 280
wolffd@0 281 if size(msize,1) == 1,
wolffd@0 282 xdim=msize(2);
wolffd@0 283 ydim=msize(1);
wolffd@0 284 if xdim*ydim~=N
wolffd@0 285 error('Data matrix has wrong size.');
wolffd@0 286 else
wolffd@0 287 y=reshape(repmat(1:ydim,d,1),1,d*ydim); y=repmat(repmat(y,l,1),1,xdim);
wolffd@0 288 x=reshape(repmat(1:xdim,l*ydim*d,1),l,N*d);
wolffd@0 289 end
wolffd@0 290 else
wolffd@0 291 x=reshape(repmat(msize(:,1),1,l*d)',l,d*N);
wolffd@0 292 y=reshape(repmat(msize(:,2),1,l*d)',l,d*N);
wolffd@0 293 if N ~= size(msize,1),
wolffd@0 294 error('Data matrix has wrong size.');
wolffd@0 295 else
wolffd@0 296 lattice='rect';
wolffd@0 297 if isnan(pos),
wolffd@0 298 pos=[0 0];
wolffd@0 299 end
wolffd@0 300 end
wolffd@0 301 end
wolffd@0 302
wolffd@0 303 % Check lattice
wolffd@0 304
wolffd@0 305 if ~ischar(lattice)
wolffd@0 306 error('Invalid lattice.');
wolffd@0 307 end
wolffd@0 308
wolffd@0 309 switch lattice
wolffd@0 310 case {'hexa','rect'}
wolffd@0 311 pos=pos-1;
wolffd@0 312 otherwise
wolffd@0 313 error([ 'Lattice' lattice ' not implemented!']);
wolffd@0 314 end
wolffd@0 315
wolffd@0 316 % Check color
wolffd@0 317 % C_FLAG is for color 'none'
wolffd@0 318
wolffd@0 319 if nargin < 4 | isempty(color)
wolffd@0 320 color=hsv(d); % default n hsv colors
wolffd@0 321 end
wolffd@0 322 if ~vis_valuetype(color, {[d 3],'nx3rgb'},'all') & ...
wolffd@0 323 ~vis_valuetype(color,{'colorstyle','1x3rgb'})
wolffd@0 324 error('The color matrix has wrong size or has invalid values.');
wolffd@0 325 elseif ischar(color) & strcmp(color,'none')
wolffd@0 326 C_FLAG=1;
wolffd@0 327 color='w';
wolffd@0 328 else
wolffd@0 329 C_FLAG=0;
wolffd@0 330 end
wolffd@0 331
wolffd@0 332 %% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 333
wolffd@0 334 % Making lattice.
wolffd@0 335 % Command view([0 90]) shows the map in 2D properly oriented
wolffd@0 336
wolffd@0 337 switch lattice
wolffd@0 338 case 'hexa'
wolffd@0 339 t=find(rem(y(1,:),2)); % move even rows by .5
wolffd@0 340 x(:,t)=x(:,t)-.5;
wolffd@0 341 x=x+barx+.5;
wolffd@0 342 y=y+bary;
wolffd@0 343 case 'rect'
wolffd@0 344 x=x+barx;
wolffd@0 345 y=y+bary;
wolffd@0 346 end
wolffd@0 347
wolffd@0 348 % NB: The coordinates in hexa are not uniform in order to get even
wolffd@0 349 % y-coordinates for the nodes. This is handled by setting _axis scaling_
wolffd@0 350 % so that the hexa-nodes look like uniform hexagonals. See
wolffd@0 351 % vis_PlaneAxisProperties
wolffd@0 352
wolffd@0 353 if ~isnan(pos)
wolffd@0 354 x=x+pos(1);y=y+pos(2); % move upper left corner
wolffd@0 355 end % to pos
wolffd@0 356
wolffd@0 357 %% Set axes properties
wolffd@0 358
wolffd@0 359 ax=newplot; % get current axis
wolffd@0 360 vis_PlaneAxisProperties(ax,lattice, msize, pos);
wolffd@0 361
wolffd@0 362 %% Rearrange dx3 color matrix
wolffd@0 363
wolffd@0 364 if ~isstr(color) & size(color,1)~=1,
wolffd@0 365 color=reshape(repmat(color,N,1),[1 N*d 3]);
wolffd@0 366 end
wolffd@0 367
wolffd@0 368 %% Draw the plane!
wolffd@0 369
wolffd@0 370 if isnumeric(color),
wolffd@0 371 % explicit color settings by RGB-triplets won't work with
wolffd@0 372 % patch in 'painters' mode, unless there only a single triplet
wolffd@0 373 si = size(color);
wolffd@0 374 if length(si)~=2 | any(si==[1 3]), set(gcf,'renderer','zbuffer'); end
wolffd@0 375 end
wolffd@0 376
wolffd@0 377 h_=patch(x,y,color);
wolffd@0 378
wolffd@0 379 if C_FLAG
wolffd@0 380 set(h_,'FaceColor','none');
wolffd@0 381 end
wolffd@0 382
wolffd@0 383 set(h_,'Tag','planeBar'); % tag the object
wolffd@0 384
wolffd@0 385 %%% Build output %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 386
wolffd@0 387 if nargout>0, h=h_; end % Set h only if
wolffd@0 388 % there really is output
wolffd@0 389
wolffd@0 390 %%% Subfunctions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 391
wolffd@0 392 function [xcoord,ycoord]=vis_barpatch(y,gap,zeroline)
wolffd@0 393
wolffd@0 394 x = length(y);
wolffd@0 395 d = gap/(2*(x-1)+2);
wolffd@0 396 step= -.5:1/x:.5;
wolffd@0 397
wolffd@0 398 miny=min(y);
wolffd@0 399 maxy=max(y);
wolffd@0 400
wolffd@0 401 switch(zeroline)
wolffd@0 402 case 'moving'
wolffd@0 403 if miny < 0
wolffd@0 404 if maxy > 0
wolffd@0 405 zl = .5 - (abs(miny)/(maxy-miny)); %reverse mode
wolffd@0 406 y= .5 - ((y-miny*ones(1,x))./(maxy-miny));
wolffd@0 407 else
wolffd@0 408 zl = -.5;
wolffd@0 409 y=-.5+abs(y./miny);
wolffd@0 410 end
wolffd@0 411 else
wolffd@0 412 zl = .5; %reverse mode
wolffd@0 413 y=.5-y./maxy;
wolffd@0 414 end
wolffd@0 415 case 'moveNotScale'
wolffd@0 416 if miny < 0
wolffd@0 417 if maxy > 0
wolffd@0 418 zl = 0.5+miny;
wolffd@0 419 y = zl - y;
wolffd@0 420 else
wolffd@0 421 zl=-.5;
wolffd@0 422 y=-.5+abs(y);
wolffd@0 423 end
wolffd@0 424 else
wolffd@0 425 zl=.5;
wolffd@0 426 y =.5-y;
wolffd@0 427 end
wolffd@0 428 case 'zero'
wolffd@0 429 zl=0; y=zl-y;
wolffd@0 430 case 'top'
wolffd@0 431 zl=-.5; y=zl-2*y;
wolffd@0 432 case 'bottom'
wolffd@0 433 zl=.5; y=zl-2*y;
wolffd@0 434 end
wolffd@0 435
wolffd@0 436 for i=1:x
wolffd@0 437 xcoord(:,i) = [d+step(i);d+step(i);step(i+1)-d;step(i+1)-d;d+step(i)];
wolffd@0 438 ycoord(:,i) = [zl;y(i);y(i);zl;zl];
wolffd@0 439 end
wolffd@0 440
wolffd@0 441
wolffd@0 442
wolffd@0 443
wolffd@0 444