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