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