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,164 @@
1
+function Ne1 = som_unit_neighs(topol,lattice,shape)
2
+
3
+%SOM_UNIT_NEIGHS Matrix indicating units in 1-neighborhood for each map unit.
4
+%
5
+% Ne1 = som_unit_neighs(topol,[lattice],[shape])
6
+% 
7
+%  Ne1 = som_unit_neighs(sTopol);
8
+%  Ne1 = som_unit_neighs(sMap.topol);
9
+%  Ne1 = som_unit_neighs([10 4], 'hexa', 'cyl');
10
+%  Ne1 = som_unit_neighs(msize, 'rect', 'toroid');
11
+%
12
+%  Input and output arguments ([]'s are optional): 
13
+%   topol              topology of the SOM grid
14
+%             (struct) topology or map struct
15
+%             (vector) the 'msize' field of topology struct
16
+%   [lattice] (string) map lattice, 'rect' by default
17
+%   [shape]   (string) map shape, 'sheet' by default
18
+%
19
+%   Ne1       (matrix, size [munits munits]) a sparse matrix
20
+%                      indicating the map units in 1-neighborhood
21
+%                      by value 1 (note: the unit itself also has value 0)
22
+%
23
+% For more help, try 'type som_unit_neighs' or check out online documentation.
24
+% See also SOM_NEIGHBORHOOD, SOM_UNIT_DISTS, SOM_UNIT_COORDS, SOM_CONNECTION.
25
+
26
+%%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
27
+%
28
+% som_unit_neighs
29
+%
30
+% PURPOSE
31
+%
32
+% Find the adjacent (in 1-neighborhood) units for each map unit of a SOM
33
+% based on given topology.
34
+%
35
+% SYNTAX
36
+%
37
+%  Ne1 = som_unit_neighs(sMap);
38
+%  Ne1 = som_unit_neighs(sM.topol);
39
+%  Ne1 = som_unit_neighs(msize);
40
+%  Ne1 = som_unit_neighs(msize,'hexa');
41
+%  Ne1 = som_unit_neighs(msize,'rect','toroid');
42
+%
43
+% DESCRIPTION
44
+%
45
+% For each map unit, find the units the distance of which from 
46
+% the map unit is equal to 1. The distances are calculated
47
+% along the map grid. Consider, for example, the case of a 4x3 map. 
48
+% The unit ('1' to 'C') positions for 'rect' and 'hexa' lattice (and
49
+% 'sheet' shape) are depicted below: 
50
+% 
51
+%   'rect' lattice           'hexa' lattice
52
+%   --------------           --------------
53
+%      1  5  9                  1  5  9
54
+%      2  6  a                   2  6  a
55
+%      3  7  b                  3  7  b
56
+%      4  8  c                   4  8  c
57
+%
58
+% The units in 1-neighborhood (adjacent units) for unit '6' are '2','5','7'
59
+% and 'a' in the 'rect' case and '5','2','7','9','a' and 'b' in the 'hexa'
60
+% case. The function returns a sparse matrix having value 1 for these units.  
61
+% Notice that not all units have equal number of neighbors. Unit '1' has only 
62
+% units '2' and '5' in its 1-neighborhood. 
63
+% 
64
+% REQUIRED INPUT ARGUMENTS
65
+% 
66
+%  topol          Map grid dimensions.
67
+%        (struct) topology struct or map struct, the topology 
68
+%                 (msize, lattice, shape) of the map is taken from 
69
+%                 the appropriate fields (see e.g. SOM_SET)
70
+%        (vector) the vector which gives the size of the map grid
71
+%                 (msize-field of the topology struct).
72
+%  
73
+% OPTIONAL INPUT ARGUMENTS 
74
+% 
75
+%  lattice (string) The map lattice, either 'rect' or 'hexa'. Default
76
+%                   is 'rect'. 'hexa' can only be used with 1- or 
77
+%                   2-dimensional map grids.
78
+%  shape   (string) The map shape, either 'sheet', 'cyl' or 'toroid'. 
79
+%                   Default is 'sheet'. 
80
+%
81
+% OUTPUT ARGUMENTS
82
+%
83
+%  Ne1   (matrix) sparse matrix indicating units in 1-neighborhood
84
+%                 by 1, all others have value 0 (including the unit itself!),
85
+%                 size is [munits munits]
86
+%
87
+% EXAMPLES
88
+%
89
+% Simplest case:
90
+%  Ne1 = som_unit_neighs(sTopol);
91
+%  Ne1 = som_unit_neighs(sMap.topol);
92
+%  Ne1 = som_unit_neighs(msize);
93
+%  Ne1 = som_unit_neighs([10 10]);
94
+%
95
+% If topology is given as vector, lattice is 'rect' and shape is 'sheet'
96
+% by default. To change these, you can use the optional arguments:
97
+%  Ne1 = som_unit_neighs(msize, 'hexa', 'toroid');
98
+%
99
+% The neighbors can also be calculated for high-dimensional grids:
100
+%  Ne1 = som_unit_neighs([4 4 4 4 4 4]);
101
+%
102
+% SEE ALSO
103
+% 
104
+%  som_neighborhood  Calculate N-neighborhoods of map units.
105
+%  som_unit_coords   Calculate grid coordinates.
106
+%  som_unit_dists    Calculate interunit distances.
107
+%  som_connection    Connection matrix.
108
+
109
+% Copyright (c) 1997-2000 by the SOM toolbox programming team.
110
+% http://www.cis.hut.fi/projects/somtoolbox/
111
+
112
+% Version 1.0beta juuso 141097
113
+% Version 2.0beta juuso 101199
114
+
115
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116
+%% Check arguments 
117
+
118
+error(nargchk(1, 3, nargin));
119
+
120
+% default values
121
+sTopol = som_set('som_topol','lattice','rect');
122
+
123
+% topol
124
+if isstruct(topol), 
125
+  switch topol.type, 
126
+  case 'som_map', sTopol = topol.topol;
127
+  case 'som_topol', sTopol = topol;
128
+  end
129
+elseif iscell(topol), 
130
+  for i=1:length(topol), 
131
+    if isnumeric(topol{i}), sTopol.msize = topol{i}; 
132
+    elseif ischar(topol{i}),  
133
+      switch topol{i}, 
134
+      case {'rect','hexa'}, sTopol.lattice = topol{i}; 
135
+      case {'sheet','cyl','toroid'}, sTopol.shape = topol{i}; 
136
+      end
137
+    end
138
+  end
139
+else
140
+  sTopol.msize = topol;
141
+end
142
+if prod(sTopol.msize)==0, error('Map size is 0.'); end
143
+
144
+% lattice
145
+if nargin>1 & ~isempty(lattice) & ~isnan(lattice), sTopol.lattice = lattice; end
146
+
147
+% shape 
148
+if nargin>2 & ~isempty(shape) & ~isnan(shape), sTopol.shape = shape; end
149
+
150
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151
+%% Action
152
+
153
+% distances between each map unit
154
+Ud = som_unit_dists(sTopol);
155
+
156
+% 1-neighborhood are those units the distance of which is equal to 1
157
+munits = prod(sTopol.msize);
158
+Ne1 = sparse(zeros(munits));
159
+for i=1:munits, 
160
+  inds = find(Ud(i,:)<1.01 & Ud(i,:)>0); % allow for rounding error
161
+  Ne1(i,inds) = 1;
162
+end
163
+
164
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%