comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_topol_struct.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 sTopol = som_topol_struct(varargin)
2
3 %SOM_TOPOL_STRUCT Default values for SOM topology.
4 %
5 % sT = som_topol_struct([[argID,] value, ...])
6 %
7 % sTopol = som_topol_struct('data',D);
8 % sTopol = som_topol_struct('data',D,'munits',200);
9 % sTopol = som_topol_struct(sTopol);
10 % sTopol = som_topol_struct;
11 %
12 % Input and output arguments ([]'s are optional):
13 % [argID, (string) Default map topology depends on a number of
14 % value] (varies) factors (see below). These are given as a
15 % argument ID - argument value pairs, listed below.
16 %
17 % sT (struct) The ready topology struct.
18 %
19 % Topology struct contains values for map size, lattice (default is 'hexa')
20 % and shape (default is 'sheet'). Map size depends on training data and the
21 % number of map units. The number of map units depends on number of training
22 % samples.
23 %
24 % Here are the valid argument IDs and corresponding values. The values which
25 % are unambiguous (marked with '*') can be given without the preceeding argID.
26 % 'dlen' (scalar) length of the training data
27 % 'data' (matrix) the training data
28 % *(struct) the training data
29 % 'munits' (scalar) number of map units
30 % 'msize' (vector) map size
31 % 'lattice' *(string) map lattice: 'hexa' or 'rect'
32 % 'shape' *(string) map shape: 'sheet', 'cyl' or 'toroid'
33 % 'topol' *(struct) incomplete topology struct: its empty fields
34 % will be given values
35 % 'som_topol','sTopol' = 'topol'
36 %
37 % For more help, try 'type som_topol_struct' or check out online documentation.
38 % See also SOM_SET, SOM_TRAIN_STRUCT, SOM_MAKE.
39
40 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 %
42 % som_topol_struct
43 %
44 % PURPOSE
45 %
46 % Default values for map topology and training parameters.
47 %
48 % SYNTAX
49 %
50 % sT = som_topol_struct('argID',value,...);
51 % sT = som_topol_struct(value,...);
52 %
53 % DESCRIPTION
54 %
55 % This function is used to give sensible values for map topology (ie. map
56 % size). The topology struct is returned.
57 %
58 % The topology struct has three fields: '.msize', '.lattice' and
59 % '.shape'. Of these, default value for '.lattice' is 'hexa' and for
60 % '.shape' 'sheet'. Only the '.msize' field depends on the optional
61 % arguments: 'dlen', 'munits' and 'data'. The value for '.msize' field is
62 % determined as follows.
63 %
64 % First, the number of map units is determined (unless it is given). A
65 % heuristic formula of 'munits = 5*sqrt(dlen)' is used to calculate
66 % it. After this, the map size is determined. Basically, the two biggest
67 % eigenvalues of the training data are calculated and the ratio between
68 % sidelengths of the map grid is set to the square root of this ratio. The
69 % actual sidelengths are then set so that their product is as close to the
70 % desired number of map units as possible. If the lattice of the grid is
71 % 'hexa', the ratio is modified a bit to take it into account. If the
72 % lattice is 'hexa' and shape is 'toroid', the map size along the first axis
73 % must be even.
74 %
75 % OPTIONAL INPUT ARGUMENTS
76 %
77 % argID (string) Argument identifier string (see below).
78 % value (varies) Value for the argument (see below).
79 %
80 % The optional arguments can be given as 'argID',value -pairs. If an
81 % argument is given value multiple times, the last one is
82 % used. The valid IDs and corresponding values are listed below. The values
83 % which are unambiguous (marked with '*') can be given without the
84 % preceeding argID.
85 %
86 % 'dlen' (scalar) length of the training data
87 % 'data' (matrix) the training data
88 % *(struct) the training data
89 % 'munits' (scalar) number of map units
90 % 'msize' (vector) map size
91 % 'lattice' *(string) map lattice: 'hexa' or 'rect'
92 % 'shape' *(string) map shape: 'sheet', 'cyl' or 'toroid'
93 % 'topol' *(struct) incomplete topology struct: its empty fields
94 % will be given values
95 % 'som_topol','sTopol' = 'topol'
96 %
97 % OUTPUT ARGUMENTS
98 %
99 % sT (struct) The topology struct.
100 %
101 % EXAMPLES
102 %
103 % The most important optional argument for the default topology is 'data'.
104 % To get a default topology (given data) use:
105 %
106 % sTopol = som_topol_struct('data',D);
107 %
108 % This sets lattice to its default value 'hexa'. If you want to have a
109 % 'rect' lattice instead:
110 %
111 % sTopol = som_topol_struct('data',D,'lattice','rect');
112 % or
113 % sTopol = som_topol_struct('data',D,'rect');
114 %
115 % If you want to have (close to) a specific number of map units, e.g. 100:
116 %
117 % sTopol = som_topol_struct('data',D,'munits',100);
118 %
119 % SEE ALSO
120 %
121 % som_make Initialize and train a map using default parameters.
122 % som_train_struct Default training parameters.
123 % som_randinint Random initialization algorithm.
124 % som_lininit Linear initialization algorithm.
125 % som_seqtrain Sequential training algorithm.
126 % som_batchtrain Batch training algorithm.
127
128 % Copyright (c) 1999-2000 by the SOM toolbox programming team.
129 % http://www.cis.hut.fi/projects/somtoolbox/
130
131 % Version 2.0alpha juuso 060898 250399 070499 050899 240801
132
133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134 %% check arguments
135
136 % initialize
137 sTopol = som_set('som_topol','lattice','hexa','shape','sheet');
138 D = [];
139 dlen = NaN;
140 dim = 2;
141 munits = NaN;
142
143 % varargin
144 i=1;
145 while i<=length(varargin),
146 argok = 1;
147 if ischar(varargin{i}),
148 switch varargin{i},
149 case 'dlen', i=i+1; dlen = varargin{i};
150 case 'munits', i=i+1; munits = varargin{i}; sTopol.msize = 0;
151 case 'msize', i=i+1; sTopol.msize = varargin{i};
152 case 'lattice', i=i+1; sTopol.lattice = varargin{i};
153 case 'shape', i=i+1; sTopol.shape = varargin{i};
154 case 'data',
155 i=i+1;
156 if isstruct(varargin{i}), D = varargin{i}.data;
157 else D = varargin{i};
158 end
159 [dlen dim] = size(D);
160 case {'hexa','rect'}, sTopol.lattice = varargin{i};
161 case {'sheet','cyl','toroid'}, sTopol.shape = varargin{i};
162 case {'som_topol','sTopol','topol'},
163 i=i+1;
164 if ~isempty(varargin{i}.msize) & prod(varargin{i}.msize),
165 sTopol.msize = varargin{i}.msize;
166 end
167 if ~isempty(varargin{i}.lattice), sTopol.lattice = varargin{i}.lattice; end
168 if ~isempty(varargin{i}.shape), sTopol.shape = varargin{i}.shape; end
169 otherwise argok=0;
170 end
171 elseif isstruct(varargin{i}) & isfield(varargin{i},'type'),
172 switch varargin{i}.type,
173 case 'som_topol',
174 if ~isempty(varargin{i}.msize) & prod(varargin{i}.msize),
175 sTopol.msize = varargin{i}.msize;
176 end
177 if ~isempty(varargin{i}.lattice), sTopol.lattice = varargin{i}.lattice; end
178 if ~isempty(varargin{i}.shape), sTopol.shape = varargin{i}.shape; end
179 case 'som_data',
180 D = varargin{i}.data;
181 [dlen dim] = size(D);
182 otherwise argok=0;
183 end
184 else
185 argok = 0;
186 end
187 if ~argok,
188 disp(['(som_topol_struct) Ignoring invalid argument #' num2str(i)]);
189 end
190 i = i+1;
191 end
192
193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 %% action - topology struct
195
196 % lattice and shape set already, so if msize is also set, there's
197 % nothing else to do
198 if prod(sTopol.msize) & ~isempty(sTopol.msize), return; end
199
200 % otherwise, decide msize
201 % first (if necessary) determine the number of map units (munits)
202 if isnan(munits),
203 if ~isnan(dlen),
204 munits = ceil(5 * dlen^0.5); % this is just one way to make a guess...
205 else
206 munits = 100; % just a convenient value
207 end
208 end
209
210 % then determine the map size (msize)
211 if dim == 1, % 1-D data
212
213 sTopol.msize = [1 ceil(munits)];
214
215 elseif size(D,1)<2, % eigenvalues cannot be determined since there's no data
216
217 sTopol.msize = round(sqrt(munits));
218 sTopol.msize(2) = round(munits/sTopol.msize(1));
219
220 else % determine map size based on eigenvalues
221
222 % initialize xdim/ydim ratio using principal components of the input
223 % space; the ratio is the square root of ratio of two largest eigenvalues
224
225 % autocorrelation matrix
226 A = zeros(dim)+Inf;
227 for i=1:dim, D(:,i) = D(:,i) - mean(D(isfinite(D(:,i)),i)); end
228 for i=1:dim,
229 for j=i:dim,
230 c = D(:,i).*D(:,j); c = c(isfinite(c));
231 A(i,j) = sum(c)/length(c); A(j,i) = A(i,j);
232 end
233 end
234 % take mdim first eigenvectors with the greatest eigenvalues
235 [V,S] = eig(A);
236 eigval = diag(S);
237 [y,ind] = sort(eigval);
238 eigval = eigval(ind);
239
240 %me = mean(D);
241 %D = D - me(ones(length(ind),1),:); % remove mean from data
242 %eigval = sort(eig((D'*D)./size(D,1)));
243 if eigval(end)==0 | eigval(end-1)*munits<eigval(end),
244 ratio = 1;
245 else
246 ratio = sqrt(eigval(end)/eigval(end-1)); % ratio between map sidelengths
247 end
248
249 % in hexagonal lattice, the sidelengths are not directly
250 % proportional to the number of units since the units on the
251 % y-axis are squeezed together by a factor of sqrt(0.75)
252 if strcmp(sTopol.lattice,'hexa'),
253 sTopol.msize(2) = min(munits, round(sqrt(munits / ratio * sqrt(0.75))));
254 else
255 sTopol.msize(2) = min(munits, round(sqrt(munits / ratio)));
256 end
257 sTopol.msize(1) = round(munits / sTopol.msize(2));
258
259 % if actual dimension of the data is 1, make the map 1-D
260 if min(sTopol.msize) == 1, sTopol.msize = [1 max(sTopol.msize)]; end;
261
262 % a special case: if the map is toroid with hexa lattice,
263 % size along first axis must be even
264 if strcmp(sTopol.lattice,'hexa') & strcmp(sTopol.shape,'toroid'),
265 if mod(sTopol.msize(1),2), sTopol.msize(1) = sTopol.msize(1) + 1; end
266 end
267
268 end
269
270 return;
271
272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%