Browse code

starting som prediction fine-tuned class-performance visualisation

git-svn-id: https://svn.discofish.de/MATLAB/spmtoolbox/SVMCrossVal@112 83ab2cfd-5345-466c-8aeb-2b2739fb922d

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