Daniel@0: function sTopol = som_topol_struct(varargin) Daniel@0: Daniel@0: %SOM_TOPOL_STRUCT Default values for SOM topology. Daniel@0: % Daniel@0: % sT = som_topol_struct([[argID,] value, ...]) Daniel@0: % Daniel@0: % sTopol = som_topol_struct('data',D); Daniel@0: % sTopol = som_topol_struct('data',D,'munits',200); Daniel@0: % sTopol = som_topol_struct(sTopol); Daniel@0: % sTopol = som_topol_struct; Daniel@0: % Daniel@0: % Input and output arguments ([]'s are optional): Daniel@0: % [argID, (string) Default map topology depends on a number of Daniel@0: % value] (varies) factors (see below). These are given as a Daniel@0: % argument ID - argument value pairs, listed below. Daniel@0: % Daniel@0: % sT (struct) The ready topology struct. Daniel@0: % Daniel@0: % Topology struct contains values for map size, lattice (default is 'hexa') Daniel@0: % and shape (default is 'sheet'). Map size depends on training data and the Daniel@0: % number of map units. The number of map units depends on number of training Daniel@0: % samples. Daniel@0: % Daniel@0: % Here are the valid argument IDs and corresponding values. The values which Daniel@0: % are unambiguous (marked with '*') can be given without the preceeding argID. Daniel@0: % 'dlen' (scalar) length of the training data Daniel@0: % 'data' (matrix) the training data Daniel@0: % *(struct) the training data Daniel@0: % 'munits' (scalar) number of map units Daniel@0: % 'msize' (vector) map size Daniel@0: % 'lattice' *(string) map lattice: 'hexa' or 'rect' Daniel@0: % 'shape' *(string) map shape: 'sheet', 'cyl' or 'toroid' Daniel@0: % 'topol' *(struct) incomplete topology struct: its empty fields Daniel@0: % will be given values Daniel@0: % 'som_topol','sTopol' = 'topol' Daniel@0: % Daniel@0: % For more help, try 'type som_topol_struct' or check out online documentation. Daniel@0: % See also SOM_SET, SOM_TRAIN_STRUCT, SOM_MAKE. Daniel@0: Daniel@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: % Daniel@0: % som_topol_struct Daniel@0: % Daniel@0: % PURPOSE Daniel@0: % Daniel@0: % Default values for map topology and training parameters. Daniel@0: % Daniel@0: % SYNTAX Daniel@0: % Daniel@0: % sT = som_topol_struct('argID',value,...); Daniel@0: % sT = som_topol_struct(value,...); Daniel@0: % Daniel@0: % DESCRIPTION Daniel@0: % Daniel@0: % This function is used to give sensible values for map topology (ie. map Daniel@0: % size). The topology struct is returned. Daniel@0: % Daniel@0: % The topology struct has three fields: '.msize', '.lattice' and Daniel@0: % '.shape'. Of these, default value for '.lattice' is 'hexa' and for Daniel@0: % '.shape' 'sheet'. Only the '.msize' field depends on the optional Daniel@0: % arguments: 'dlen', 'munits' and 'data'. The value for '.msize' field is Daniel@0: % determined as follows. Daniel@0: % Daniel@0: % First, the number of map units is determined (unless it is given). A Daniel@0: % heuristic formula of 'munits = 5*sqrt(dlen)' is used to calculate Daniel@0: % it. After this, the map size is determined. Basically, the two biggest Daniel@0: % eigenvalues of the training data are calculated and the ratio between Daniel@0: % sidelengths of the map grid is set to the square root of this ratio. The Daniel@0: % actual sidelengths are then set so that their product is as close to the Daniel@0: % desired number of map units as possible. If the lattice of the grid is Daniel@0: % 'hexa', the ratio is modified a bit to take it into account. If the Daniel@0: % lattice is 'hexa' and shape is 'toroid', the map size along the first axis Daniel@0: % must be even. Daniel@0: % Daniel@0: % OPTIONAL INPUT ARGUMENTS Daniel@0: % Daniel@0: % argID (string) Argument identifier string (see below). Daniel@0: % value (varies) Value for the argument (see below). Daniel@0: % Daniel@0: % The optional arguments can be given as 'argID',value -pairs. If an Daniel@0: % argument is given value multiple times, the last one is Daniel@0: % used. The valid IDs and corresponding values are listed below. The values Daniel@0: % which are unambiguous (marked with '*') can be given without the Daniel@0: % preceeding argID. Daniel@0: % Daniel@0: % 'dlen' (scalar) length of the training data Daniel@0: % 'data' (matrix) the training data Daniel@0: % *(struct) the training data Daniel@0: % 'munits' (scalar) number of map units Daniel@0: % 'msize' (vector) map size Daniel@0: % 'lattice' *(string) map lattice: 'hexa' or 'rect' Daniel@0: % 'shape' *(string) map shape: 'sheet', 'cyl' or 'toroid' Daniel@0: % 'topol' *(struct) incomplete topology struct: its empty fields Daniel@0: % will be given values Daniel@0: % 'som_topol','sTopol' = 'topol' Daniel@0: % Daniel@0: % OUTPUT ARGUMENTS Daniel@0: % Daniel@0: % sT (struct) The topology struct. Daniel@0: % Daniel@0: % EXAMPLES Daniel@0: % Daniel@0: % The most important optional argument for the default topology is 'data'. Daniel@0: % To get a default topology (given data) use: Daniel@0: % Daniel@0: % sTopol = som_topol_struct('data',D); Daniel@0: % Daniel@0: % This sets lattice to its default value 'hexa'. If you want to have a Daniel@0: % 'rect' lattice instead: Daniel@0: % Daniel@0: % sTopol = som_topol_struct('data',D,'lattice','rect'); Daniel@0: % or Daniel@0: % sTopol = som_topol_struct('data',D,'rect'); Daniel@0: % Daniel@0: % If you want to have (close to) a specific number of map units, e.g. 100: Daniel@0: % Daniel@0: % sTopol = som_topol_struct('data',D,'munits',100); Daniel@0: % Daniel@0: % SEE ALSO Daniel@0: % Daniel@0: % som_make Initialize and train a map using default parameters. Daniel@0: % som_train_struct Default training parameters. Daniel@0: % som_randinint Random initialization algorithm. Daniel@0: % som_lininit Linear initialization algorithm. Daniel@0: % som_seqtrain Sequential training algorithm. Daniel@0: % som_batchtrain Batch training algorithm. Daniel@0: Daniel@0: % Copyright (c) 1999-2000 by the SOM toolbox programming team. Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % Version 2.0alpha juuso 060898 250399 070499 050899 240801 Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% check arguments Daniel@0: Daniel@0: % initialize Daniel@0: sTopol = som_set('som_topol','lattice','hexa','shape','sheet'); Daniel@0: D = []; Daniel@0: dlen = NaN; Daniel@0: dim = 2; Daniel@0: munits = NaN; Daniel@0: Daniel@0: % varargin Daniel@0: i=1; Daniel@0: while i<=length(varargin), Daniel@0: argok = 1; Daniel@0: if ischar(varargin{i}), Daniel@0: switch varargin{i}, Daniel@0: case 'dlen', i=i+1; dlen = varargin{i}; Daniel@0: case 'munits', i=i+1; munits = varargin{i}; sTopol.msize = 0; Daniel@0: case 'msize', i=i+1; sTopol.msize = varargin{i}; Daniel@0: case 'lattice', i=i+1; sTopol.lattice = varargin{i}; Daniel@0: case 'shape', i=i+1; sTopol.shape = varargin{i}; Daniel@0: case 'data', Daniel@0: i=i+1; Daniel@0: if isstruct(varargin{i}), D = varargin{i}.data; Daniel@0: else D = varargin{i}; Daniel@0: end Daniel@0: [dlen dim] = size(D); Daniel@0: case {'hexa','rect'}, sTopol.lattice = varargin{i}; Daniel@0: case {'sheet','cyl','toroid'}, sTopol.shape = varargin{i}; Daniel@0: case {'som_topol','sTopol','topol'}, Daniel@0: i=i+1; Daniel@0: if ~isempty(varargin{i}.msize) & prod(varargin{i}.msize), Daniel@0: sTopol.msize = varargin{i}.msize; Daniel@0: end Daniel@0: if ~isempty(varargin{i}.lattice), sTopol.lattice = varargin{i}.lattice; end Daniel@0: if ~isempty(varargin{i}.shape), sTopol.shape = varargin{i}.shape; end Daniel@0: otherwise argok=0; Daniel@0: end Daniel@0: elseif isstruct(varargin{i}) & isfield(varargin{i},'type'), Daniel@0: switch varargin{i}.type, Daniel@0: case 'som_topol', Daniel@0: if ~isempty(varargin{i}.msize) & prod(varargin{i}.msize), Daniel@0: sTopol.msize = varargin{i}.msize; Daniel@0: end Daniel@0: if ~isempty(varargin{i}.lattice), sTopol.lattice = varargin{i}.lattice; end Daniel@0: if ~isempty(varargin{i}.shape), sTopol.shape = varargin{i}.shape; end Daniel@0: case 'som_data', Daniel@0: D = varargin{i}.data; Daniel@0: [dlen dim] = size(D); Daniel@0: otherwise argok=0; Daniel@0: end Daniel@0: else Daniel@0: argok = 0; Daniel@0: end Daniel@0: if ~argok, Daniel@0: disp(['(som_topol_struct) Ignoring invalid argument #' num2str(i)]); Daniel@0: end Daniel@0: i = i+1; Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% action - topology struct Daniel@0: Daniel@0: % lattice and shape set already, so if msize is also set, there's Daniel@0: % nothing else to do Daniel@0: if prod(sTopol.msize) & ~isempty(sTopol.msize), return; end Daniel@0: Daniel@0: % otherwise, decide msize Daniel@0: % first (if necessary) determine the number of map units (munits) Daniel@0: if isnan(munits), Daniel@0: if ~isnan(dlen), Daniel@0: munits = ceil(5 * dlen^0.5); % this is just one way to make a guess... Daniel@0: else Daniel@0: munits = 100; % just a convenient value Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % then determine the map size (msize) Daniel@0: if dim == 1, % 1-D data Daniel@0: Daniel@0: sTopol.msize = [1 ceil(munits)]; Daniel@0: Daniel@0: elseif size(D,1)<2, % eigenvalues cannot be determined since there's no data Daniel@0: Daniel@0: sTopol.msize = round(sqrt(munits)); Daniel@0: sTopol.msize(2) = round(munits/sTopol.msize(1)); Daniel@0: Daniel@0: else % determine map size based on eigenvalues Daniel@0: Daniel@0: % initialize xdim/ydim ratio using principal components of the input Daniel@0: % space; the ratio is the square root of ratio of two largest eigenvalues Daniel@0: Daniel@0: % autocorrelation matrix Daniel@0: A = zeros(dim)+Inf; Daniel@0: for i=1:dim, D(:,i) = D(:,i) - mean(D(isfinite(D(:,i)),i)); end Daniel@0: for i=1:dim, Daniel@0: for j=i:dim, Daniel@0: c = D(:,i).*D(:,j); c = c(isfinite(c)); Daniel@0: A(i,j) = sum(c)/length(c); A(j,i) = A(i,j); Daniel@0: end Daniel@0: end Daniel@0: % take mdim first eigenvectors with the greatest eigenvalues Daniel@0: [V,S] = eig(A); Daniel@0: eigval = diag(S); Daniel@0: [y,ind] = sort(eigval); Daniel@0: eigval = eigval(ind); Daniel@0: Daniel@0: %me = mean(D); Daniel@0: %D = D - me(ones(length(ind),1),:); % remove mean from data Daniel@0: %eigval = sort(eig((D'*D)./size(D,1))); Daniel@0: if eigval(end)==0 | eigval(end-1)*munits