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,362 @@
1
+function U = som_umat(sMap, varargin)
2
+
3
+%SOM_UMAT Compute unified distance matrix of self-organizing map.
4
+%
5
+% U = som_umat(sMap, [argID, value, ...])
6
+%
7
+%  U = som_umat(sMap);  
8
+%  U = som_umat(M,sTopol,'median','mask',[1 1 0 1]);
9
+%
10
+%  Input and output arguments ([]'s are optional): 
11
+%   sMap     (struct) map struct or
12
+%            (matrix) the codebook matrix of the map
13
+%   [argID,  (string) See below. The values which are unambiguous can 
14
+%    value]  (varies) be given without the preceeding argID.
15
+%
16
+%   U        (matrix) u-matrix of the self-organizing map 
17
+%
18
+% Here are the valid argument IDs and corresponding values. The values which
19
+% are unambiguous (marked with '*') can be given without the preceeding argID.
20
+%   'mask'       (vector) size dim x 1, weighting factors for different 
21
+%                         components (same as BMU search mask)
22
+%   'msize'      (vector) map grid size
23
+%   'topol'     *(struct) topology struct
24
+%   'som_topol','sTopol' = 'topol'
25
+%   'lattice'   *(string) map lattice, 'hexa' or 'rect'
26
+%   'mode'      *(string) 'min','mean','median','max', default is 'median'
27
+%
28
+% NOTE! the U-matrix is always calculated for 'sheet'-shaped map and
29
+% the map grid must be at most 2-dimensional.
30
+% 
31
+% For more help, try 'type som_umat' or check out online documentation.
32
+% See also SOM_SHOW, SOM_CPLANE.
33
+
34
+%%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35
+%
36
+% som_umat
37
+%
38
+% PURPOSE
39
+%
40
+% Computes the unified distance matrix of a SOM.
41
+%
42
+% SYNTAX
43
+%
44
+%  U = som_umat(sM)  
45
+%  U = som_umat(...,'argID',value,...)
46
+%  U = som_umat(...,value,...)
47
+%
48
+% DESCRIPTION
49
+%
50
+% Compute and return the unified distance matrix of a SOM. 
51
+% For example a case of 5x1 -sized map:
52
+%            m(1) m(2) m(3) m(4) m(5)
53
+% where m(i) denotes one map unit. The u-matrix is a 9x1 vector:
54
+%    u(1) u(1,2) u(2) u(2,3) u(3) u(3,4) u(4) u(4,5) u(5) 
55
+% where u(i,j) is the distance between map units m(i) and m(j)
56
+% and u(k) is the mean (or minimum, maximum or median) of the 
57
+% surrounding values, e.g. u(3) = (u(2,3) + u(3,4))/2. 
58
+%
59
+% Note that the u-matrix is always calculated for 'sheet'-shaped map and
60
+% the map grid must be at most 2-dimensional.
61
+%
62
+% REFERENCES
63
+%
64
+% Ultsch, A., Siemon, H.P., "Kohonen's Self-Organizing Feature Maps
65
+%   for Exploratory Data Analysis", in Proc. of INNC'90,
66
+%   International Neural Network Conference, Dordrecht,
67
+%   Netherlands, 1990, pp. 305-308.
68
+% Kohonen, T., "Self-Organizing Map", 2nd ed., Springer-Verlag, 
69
+%    Berlin, 1995, pp. 117-119. 
70
+% Iivarinen, J., Kohonen, T., Kangas, J., Kaski, S., "Visualizing 
71
+%   the Clusters on the Self-Organizing Map", in proceedings of
72
+%   Conference on Artificial Intelligence Research in Finland,
73
+%   Helsinki, Finland, 1994, pp. 122-126.
74
+% Kraaijveld, M.A., Mao, J., Jain, A.K., "A Nonlinear Projection
75
+%   Method Based on Kohonen's Topology Preserving Maps", IEEE
76
+%   Transactions on Neural Networks, vol. 6, no. 3, 1995, pp. 548-559.
77
+% 
78
+% REQUIRED INPUT ARGUMENTS
79
+%
80
+%  sM (struct) SOM Toolbox struct or the codebook matrix of the map.
81
+%     (matrix) The matrix may be 3-dimensional in which case the first 
82
+%              two dimensions are taken for the map grid dimensions (msize).
83
+%
84
+% OPTIONAL INPUT ARGUMENTS
85
+%
86
+%  argID (string) Argument identifier string (see below).
87
+%  value (varies) Value for the argument (see below).
88
+%
89
+%  The optional arguments are given as 'argID',value -pairs. If the 
90
+%  value is unambiguous, it can be given without the preceeding argID.
91
+%  If an argument is given value multiple times, the last one is used. 
92
+%
93
+%  Below is the list of valid arguments: 
94
+%   'mask'      (vector) mask to be used in calculating
95
+%                        the interunit distances, size [dim  1]. Default is 
96
+%                        the one in sM (field sM.mask) or a vector of
97
+%                        ones if only a codebook matrix was given.
98
+%   'topol'     (struct) topology of the map. Default is the one
99
+%                        in sM (field sM.topol).
100
+%   'sTopol','som_topol' (struct) = 'topol'
101
+%   'msize'     (vector) map grid dimensions
102
+%   'lattice'   (string) map lattice 'rect' or 'hexa'
103
+%   'mode'      (string) 'min', 'mean', 'median' or 'max'
104
+%                        Map unit value computation method. In fact, 
105
+%                        eval-function is used to evaluate this, so 
106
+%                        you can give other computation methods as well.
107
+%                        Default is 'median'. 
108
+%
109
+% OUTPUT ARGUMENTS
110
+%
111
+%  U   (matrix) the unified distance matrix of the SOM 
112
+%               size 2*n1-1 x 2*n2-1, where n1 = msize(1) and n2 = msize(2)
113
+%
114
+% EXAMPLES
115
+%
116
+%  U = som_umat(sM);  
117
+%  U = som_umat(sM.codebook,sM.topol,'median','mask',[1 1 0 1]);
118
+%  U = som_umat(rand(10,10,4),'hexa','rect'); 
119
+% 
120
+% SEE ALSO
121
+%
122
+%  som_show    show the selected component planes and the u-matrix
123
+%  som_cplane  draw a 2D unified distance matrix
124
+
125
+% Copyright (c) 1997-2000 by the SOM toolbox programming team.
126
+% http://www.cis.hut.fi/projects/somtoolbox/
127
+
128
+% Version 1.0beta juuso 260997
129
+% Version 2.0beta juuso 151199, 151299, 200900
130
+
131
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132
+%% check arguments
133
+
134
+error(nargchk(1, Inf, nargin));  % check no. of input arguments is correct
135
+
136
+% sMap
137
+if isstruct(sMap), 
138
+  M = sMap.codebook;
139
+  sTopol = sMap.topol; 
140
+  mask = sMap.mask;
141
+elseif isnumeric(sMap),
142
+  M = sMap; 
143
+  si = size(M);
144
+  dim = si(end);
145
+  if length(si)>2, msize = si(1:end-1);
146
+  else msize = [si(1) 1];
147
+  end
148
+  munits = prod(msize);
149
+  sTopol = som_set('som_topol','msize',msize,'lattice','rect','shape','sheet'); 
150
+  mask = ones(dim,1);
151
+  M = reshape(M,[munits,dim]);
152
+end
153
+mode = 'median';
154
+
155
+% varargin
156
+i=1; 
157
+while i<=length(varargin), 
158
+  argok = 1; 
159
+  if ischar(varargin{i}), 
160
+    switch varargin{i}, 
161
+      % argument IDs
162
+     case 'mask',       i=i+1; mask = varargin{i}; 
163
+     case 'msize',      i=i+1; sTopol.msize = varargin{i}; 
164
+     case 'lattice',    i=i+1; sTopol.lattice = varargin{i};
165
+     case {'topol','som_topol','sTopol'}, i=i+1; sTopol = varargin{i};
166
+     case 'mode',       i=i+1; mode = varargin{i};
167
+      % unambiguous values
168
+     case {'hexa','rect'}, sTopol.lattice = varargin{i};
169
+     case {'min','mean','median','max'}, mode = varargin{i};
170
+     otherwise argok=0; 
171
+    end
172
+  elseif isstruct(varargin{i}) & isfield(varargin{i},'type'), 
173
+    switch varargin{i}(1).type, 
174
+     case 'som_topol', sTopol = varargin{i};
175
+     case 'som_map',   sTopol = varargin{i}.topol;
176
+     otherwise argok=0; 
177
+    end
178
+  else
179
+    argok = 0; 
180
+  end
181
+  if ~argok, 
182
+    disp(['(som_umat) Ignoring invalid argument #' num2str(i+1)]); 
183
+  end
184
+  i = i+1; 
185
+end
186
+
187
+% check
188
+[munits dim] = size(M);
189
+if prod(sTopol.msize)~=munits, 
190
+  error('Map grid size does not match the number of map units.')
191
+end
192
+if length(sTopol.msize)>2, 
193
+  error('Can only handle 1- and 2-dimensional map grids.')
194
+end
195
+if prod(sTopol.msize)==1,
196
+  warning('Only one codebook vector.'); U = []; return;
197
+end
198
+if ~strcmp(sTopol.shape,'sheet'), 
199
+  disp(['The ' sTopol.shape ' shape of the map ignored. Using sheet instead.']);
200
+end
201
+
202
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203
+%% initialize variables
204
+
205
+y = sTopol.msize(1);
206
+x = sTopol.msize(2);
207
+lattice = sTopol.lattice;
208
+shape = sTopol.shape;
209
+M = reshape(M,[y x dim]);
210
+
211
+ux = 2 * x - 1; 
212
+uy = 2 * y - 1;
213
+U  = zeros(uy, ux);
214
+
215
+calc = sprintf('%s(a)',mode);
216
+
217
+if size(mask,2)>1, mask = mask'; end
218
+
219
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220
+%% u-matrix computation
221
+
222
+% distances between map units
223
+
224
+if strcmp(lattice, 'rect'), % rectangular lattice
225
+  
226
+  for j=1:y, for i=1:x,
227
+      if i<x, 
228
+	dx = (M(j,i,:) - M(j,i+1,:)).^2; % horizontal
229
+	U(2*j-1,2*i) = sqrt(mask'*dx(:));
230
+      end 
231
+      if j<y, 
232
+	dy = (M(j,i,:) - M(j+1,i,:)).^2; % vertical
233
+	U(2*j,2*i-1) = sqrt(mask'*dy(:));
234
+      end
235
+      if j<y & i<x,	
236
+	dz1 = (M(j,i,:) - M(j+1,i+1,:)).^2; % diagonals
237
+	dz2 = (M(j+1,i,:) - M(j,i+1,:)).^2;
238
+	U(2*j,2*i) = (sqrt(mask'*dz1(:))+sqrt(mask'*dz2(:)))/(2 * sqrt(2));
239
+      end
240
+    end
241
+  end
242
+
243
+elseif strcmp(lattice, 'hexa') % hexagonal lattice
244
+
245
+  for j=1:y, 
246
+    for i=1:x,
247
+      if i<x,
248
+	dx = (M(j,i,:) - M(j,i+1,:)).^2; % horizontal
249
+	U(2*j-1,2*i) = sqrt(mask'*dx(:));
250
+      end
251
+      
252
+      if j<y, % diagonals
253
+	dy = (M(j,i,:) - M(j+1,i,:)).^2;
254
+	U(2*j,2*i-1) = sqrt(mask'*dy(:));	
255
+	
256
+	if rem(j,2)==0 & i<x,
257
+	  dz= (M(j,i,:) - M(j+1,i+1,:)).^2; 
258
+	  U(2*j,2*i) = sqrt(mask'*dz(:));
259
+	elseif rem(j,2)==1 & i>1,
260
+	  dz = (M(j,i,:) - M(j+1,i-1,:)).^2; 
261
+	  U(2*j,2*i-2) = sqrt(mask'*dz(:));
262
+	end
263
+      end
264
+    end
265
+  end
266
+  
267
+end
268
+
269
+% values on the units
270
+
271
+if (uy == 1 | ux == 1),
272
+  % in 1-D case, mean is equal to median 
273
+
274
+  ma = max([ux uy]);
275
+  for i = 1:2:ma,
276
+    if i>1 & i<ma, 
277
+      a = [U(i-1) U(i+1)]; 
278
+      U(i) = eval(calc);
279
+    elseif i==1, U(i) = U(i+1); 
280
+    else U(i) = U(i-1); % i==ma
281
+    end
282
+  end    
283
+
284
+elseif strcmp(lattice, 'rect')
285
+
286
+  for j=1:2:uy, 
287
+    for i=1:2:ux,
288
+      if i>1 & j>1 & i<ux & j<uy,    % middle part of the map
289
+	a = [U(j,i-1) U(j,i+1) U(j-1,i) U(j+1,i)];        
290
+      elseif j==1 & i>1 & i<ux,        % upper edge
291
+	a = [U(j,i-1) U(j,i+1) U(j+1,i)];
292
+      elseif j==uy & i>1 & i<ux,       % lower edge
293
+	a = [U(j,i-1) U(j,i+1) U(j-1,i)];
294
+      elseif i==1 & j>1 & j<uy,        % left edge
295
+	a = [U(j,i+1) U(j-1,i) U(j+1,i)];
296
+      elseif i==ux & j>1 & j<uy,       % right edge
297
+	a = [U(j,i-1) U(j-1,i) U(j+1,i)];
298
+      elseif i==1 & j==1,              % top left corner
299
+	a = [U(j,i+1) U(j+1,i)];
300
+      elseif i==ux & j==1,             % top right corner
301
+	a = [U(j,i-1) U(j+1,i)];
302
+      elseif i==1 & j==uy,             % bottom left corner
303
+	a = [U(j,i+1) U(j-1,i)];
304
+      elseif i==ux & j==uy,            % bottom right corner
305
+	a = [U(j,i-1) U(j-1,i)];
306
+      else
307
+	a = 0;
308
+      end
309
+      U(j,i) = eval(calc);
310
+    end
311
+  end
312
+
313
+elseif strcmp(lattice, 'hexa')
314
+  
315
+  for j=1:2:uy, 
316
+    for i=1:2:ux,
317
+      if i>1 & j>1 & i<ux & j<uy,      % middle part of the map
318
+	a = [U(j,i-1) U(j,i+1)];
319
+	if rem(j-1,4)==0, a = [a, U(j-1,i-1) U(j-1,i) U(j+1,i-1) U(j+1,i)];
320
+	else a = [a, U(j-1,i) U(j-1,i+1) U(j+1,i) U(j+1,i+1)]; end       
321
+      elseif j==1 & i>1 & i<ux,        % upper edge
322
+	a = [U(j,i-1) U(j,i+1) U(j+1,i-1) U(j+1,i)];
323
+      elseif j==uy & i>1 & i<ux,       % lower edge
324
+	a = [U(j,i-1) U(j,i+1)];
325
+	if rem(j-1,4)==0, a = [a, U(j-1,i-1) U(j-1,i)];
326
+	else a = [a, U(j-1,i) U(j-1,i+1)]; end
327
+      elseif i==1 & j>1 & j<uy,        % left edge
328
+	a = U(j,i+1);
329
+	if rem(j-1,4)==0, a = [a, U(j-1,i) U(j+1,i)];
330
+	else a = [a, U(j-1,i) U(j-1,i+1) U(j+1,i) U(j+1,i+1)]; end
331
+      elseif i==ux & j>1 & j<uy,       % right edge
332
+	a = U(j,i-1);
333
+	if rem(j-1,4)==0, a=[a, U(j-1,i) U(j-1,i-1) U(j+1,i) U(j+1,i-1)];
334
+	else a = [a, U(j-1,i) U(j+1,i)]; end
335
+      elseif i==1 & j==1,              % top left corner
336
+	a = [U(j,i+1) U(j+1,i)];
337
+      elseif i==ux & j==1,             % top right corner
338
+	a = [U(j,i-1) U(j+1,i-1) U(j+1,i)];
339
+      elseif i==1 & j==uy,             % bottom left corner
340
+	if rem(j-1,4)==0, a = [U(j,i+1) U(j-1,i)];
341
+	else a = [U(j,i+1) U(j-1,i) U(j-1,i+1)]; end
342
+      elseif i==ux & j==uy,            % bottom right corner
343
+	if rem(j-1,4)==0, a = [U(j,i-1) U(j-1,i) U(j-1,i-1)];
344
+	else a = [U(j,i-1) U(j-1,i)]; end
345
+      else
346
+	a=0;
347
+      end
348
+      U(j,i) = eval(calc);
349
+    end
350
+  end
351
+end
352
+
353
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354
+%% normalization between [0,1]
355
+
356
+% U = U - min(min(U)); 
357
+% ma = max(max(U)); if ma > 0, U = U / ma; end
358
+
359
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360
+
361
+
362
+