annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_lininit.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function sMap = som_lininit(D, varargin)
Daniel@0 2
Daniel@0 3 %SOM_LININIT Initialize a Self-Organizing Map linearly.
Daniel@0 4 %
Daniel@0 5 % sMap = som_lininit(D, [[argID,] value, ...])
Daniel@0 6 %
Daniel@0 7 % sMap = som_lininit(D);
Daniel@0 8 % sMap = som_lininit(D,sMap);
Daniel@0 9 % sMap = som_lininit(D,'munits',100,'hexa');
Daniel@0 10 %
Daniel@0 11 % Input and output arguments ([]'s are optional):
Daniel@0 12 % D The training data.
Daniel@0 13 % (struct) data struct
Daniel@0 14 % (matrix) data matrix, size dlen x dim
Daniel@0 15 % [argID, (string) Parameters affecting the map topology are given
Daniel@0 16 % value] (varies) as argument ID - argument value pairs, listed below.
Daniel@0 17 % sMap (struct) map struct
Daniel@0 18 %
Daniel@0 19 % Here are the valid argument IDs and corresponding values. The values
Daniel@0 20 % which are unambiguous (marked with '*') can be given without the
Daniel@0 21 % preceeding argID.
Daniel@0 22 % 'munits' (scalar) number of map units
Daniel@0 23 % 'msize' (vector) map size
Daniel@0 24 % 'lattice' *(string) map lattice: 'hexa' or 'rect'
Daniel@0 25 % 'shape' *(string) map shape: 'sheet', 'cyl' or 'toroid'
Daniel@0 26 % 'topol' *(struct) topology struct
Daniel@0 27 % 'som_topol','sTopol' = 'topol'
Daniel@0 28 % 'map' *(struct) map struct
Daniel@0 29 % 'som_map','sMap' = 'map'
Daniel@0 30 %
Daniel@0 31 % For more help, try 'type som_lininit' or check out online documentation.
Daniel@0 32 % See also SOM_MAP_STRUCT, SOM_RANDINIT, SOM_MAKE.
Daniel@0 33
Daniel@0 34 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 35 %
Daniel@0 36 % som_lininit
Daniel@0 37 %
Daniel@0 38 % PURPOSE
Daniel@0 39 %
Daniel@0 40 % Initializes a SOM linearly along its greatest eigenvectors.
Daniel@0 41 %
Daniel@0 42 % SYNTAX
Daniel@0 43 %
Daniel@0 44 % sMap = som_lininit(D);
Daniel@0 45 % sMap = som_lininit(D,sMap);
Daniel@0 46 % sMap = som_lininit(D,'munits',100,'hexa');
Daniel@0 47 %
Daniel@0 48 % DESCRIPTION
Daniel@0 49 %
Daniel@0 50 % Initializes a SOM linearly. If necessary, a map struct is created
Daniel@0 51 % first. The initialization is made by first calculating the eigenvalues
Daniel@0 52 % and eigenvectors of the training data. Then, the map is initialized
Daniel@0 53 % along the mdim greatest eigenvectors of the training data, where
Daniel@0 54 % mdim is the dimension of the map grid.
Daniel@0 55 %
Daniel@0 56 % REFERENCES
Daniel@0 57 %
Daniel@0 58 % Kohonen, T., "Self-Organizing Map", 2nd ed., Springer-Verlag,
Daniel@0 59 % Berlin, 1995, pp. 106-107.
Daniel@0 60 %
Daniel@0 61 % REQUIRED INPUT ARGUMENTS
Daniel@0 62 %
Daniel@0 63 % D The training data.
Daniel@0 64 % (struct) Data struct. If this is given, its '.comp_names' and
Daniel@0 65 % '.comp_norm' fields are copied to the map struct.
Daniel@0 66 % (matrix) data matrix, size dlen x dim
Daniel@0 67 %
Daniel@0 68 % OPTIONAL INPUT ARGUMENTS
Daniel@0 69 %
Daniel@0 70 % argID (string) Argument identifier string (see below).
Daniel@0 71 % value (varies) Value for the argument (see below).
Daniel@0 72 %
Daniel@0 73 % The optional arguments can be given as 'argID',value -pairs. If an
Daniel@0 74 % argument is given value multiple times, the last one is used.
Daniel@0 75 %
Daniel@0 76 % Here are the valid argument IDs and corresponding values. The values
Daniel@0 77 % which are unambiguous (marked with '*') can be given without the
Daniel@0 78 % preceeding argID.
Daniel@0 79 % 'dlen' (scalar) length of the training data
Daniel@0 80 % 'data' (matrix) the training data
Daniel@0 81 % *(struct) the training data
Daniel@0 82 % 'munits' (scalar) number of map units
Daniel@0 83 % 'msize' (vector) map size
Daniel@0 84 % 'lattice' *(string) map lattice: 'hexa' or 'rect'
Daniel@0 85 % 'shape' *(string) map shape: 'sheet', 'cyl' or 'toroid'
Daniel@0 86 % 'topol' *(struct) topology struct
Daniel@0 87 % 'som_topol','sTopol' = 'topol'
Daniel@0 88 % 'map' *(struct) map struct
Daniel@0 89 % 'som_map','sMap' = 'map'
Daniel@0 90 %
Daniel@0 91 % OUTPUT ARGUMENTS
Daniel@0 92 %
Daniel@0 93 % sMap (struct) The initialized map struct.
Daniel@0 94 %
Daniel@0 95 % EXAMPLES
Daniel@0 96 %
Daniel@0 97 % sMap = som_lininit(D);
Daniel@0 98 % sMap = som_lininit(D,sMap);
Daniel@0 99 % sMap = som_lininit(D,'msize',[10 10]);
Daniel@0 100 % sMap = som_lininit(D,'munits',100,'rect');
Daniel@0 101 %
Daniel@0 102 % SEE ALSO
Daniel@0 103 %
Daniel@0 104 % som_map_struct Create a map struct.
Daniel@0 105 % som_randinit Initialize a map with random values.
Daniel@0 106 % som_make Initialize and train self-organizing map.
Daniel@0 107
Daniel@0 108 % Copyright (c) 1997-2000 by the SOM toolbox programming team.
Daniel@0 109 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 110
Daniel@0 111 % Version 1.0beta ecco 100997
Daniel@0 112 % Version 2.0beta 101199
Daniel@0 113
Daniel@0 114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 115 %% check arguments
Daniel@0 116
Daniel@0 117 % data
Daniel@0 118 if isstruct(D),
Daniel@0 119 data_name = D.name;
Daniel@0 120 comp_names = D.comp_names;
Daniel@0 121 comp_norm = D.comp_norm;
Daniel@0 122 D = D.data;
Daniel@0 123 struct_mode = 1;
Daniel@0 124 else
Daniel@0 125 data_name = inputname(1);
Daniel@0 126 struct_mode = 0;
Daniel@0 127 end
Daniel@0 128 [dlen dim] = size(D);
Daniel@0 129
Daniel@0 130 % varargin
Daniel@0 131 sMap = [];
Daniel@0 132 sTopol = som_topol_struct;
Daniel@0 133 sTopol.msize = 0;
Daniel@0 134 munits = NaN;
Daniel@0 135 i=1;
Daniel@0 136 while i<=length(varargin),
Daniel@0 137 argok = 1;
Daniel@0 138 if ischar(varargin{i}),
Daniel@0 139 switch varargin{i},
Daniel@0 140 case 'munits', i=i+1; munits = varargin{i}; sTopol.msize = 0;
Daniel@0 141 case 'msize', i=i+1; sTopol.msize = varargin{i};
Daniel@0 142 munits = prod(sTopol.msize);
Daniel@0 143 case 'lattice', i=i+1; sTopol.lattice = varargin{i};
Daniel@0 144 case 'shape', i=i+1; sTopol.shape = varargin{i};
Daniel@0 145 case {'som_topol','sTopol','topol'}, i=i+1; sTopol = varargin{i};
Daniel@0 146 case {'som_map','sMap','map'}, i=i+1; sMap = varargin{i}; sTopol = sMap.topol;
Daniel@0 147 case {'hexa','rect'}, sTopol.lattice = varargin{i};
Daniel@0 148 case {'sheet','cyl','toroid'}, sTopol.shape = varargin{i};
Daniel@0 149 otherwise argok=0;
Daniel@0 150 end
Daniel@0 151 elseif isstruct(varargin{i}) & isfield(varargin{i},'type'),
Daniel@0 152 switch varargin{i}.type,
Daniel@0 153 case 'som_topol',
Daniel@0 154 sTopol = varargin{i};
Daniel@0 155 case 'som_map',
Daniel@0 156 sMap = varargin{i};
Daniel@0 157 sTopol = sMap.topol;
Daniel@0 158 otherwise argok=0;
Daniel@0 159 end
Daniel@0 160 else
Daniel@0 161 argok = 0;
Daniel@0 162 end
Daniel@0 163 if ~argok,
Daniel@0 164 disp(['(som_topol_struct) Ignoring invalid argument #' num2str(i)]);
Daniel@0 165 end
Daniel@0 166 i = i+1;
Daniel@0 167 end
Daniel@0 168
Daniel@0 169 if length(sTopol.msize)==1, sTopol.msize = [sTopol.msize 1]; end
Daniel@0 170
Daniel@0 171 if ~isempty(sMap),
Daniel@0 172 [munits dim2] = size(sMap.codebook);
Daniel@0 173 if dim2 ~= dim, error('Map and data must have the same dimension.'); end
Daniel@0 174 end
Daniel@0 175
Daniel@0 176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 177 %% create map
Daniel@0 178
Daniel@0 179 % map struct
Daniel@0 180 if ~isempty(sMap),
Daniel@0 181 sMap = som_set(sMap,'topol',sTopol);
Daniel@0 182 else
Daniel@0 183 if ~prod(sTopol.msize),
Daniel@0 184 if isnan(munits),
Daniel@0 185 sTopol = som_topol_struct('data',D,sTopol);
Daniel@0 186 else
Daniel@0 187 sTopol = som_topol_struct('data',D,'munits',munits,sTopol);
Daniel@0 188 end
Daniel@0 189 end
Daniel@0 190 sMap = som_map_struct(dim, sTopol);
Daniel@0 191 end
Daniel@0 192
Daniel@0 193 if struct_mode,
Daniel@0 194 sMap = som_set(sMap,'comp_names',comp_names,'comp_norm',comp_norm);
Daniel@0 195 end
Daniel@0 196
Daniel@0 197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 198 %% initialization
Daniel@0 199
Daniel@0 200 % train struct
Daniel@0 201 sTrain = som_train_struct('algorithm','lininit');
Daniel@0 202 sTrain = som_set(sTrain,'data_name',data_name);
Daniel@0 203
Daniel@0 204 msize = sMap.topol.msize;
Daniel@0 205 mdim = length(msize);
Daniel@0 206 munits = prod(msize);
Daniel@0 207
Daniel@0 208 [dlen dim] = size(D);
Daniel@0 209 if dlen<2,
Daniel@0 210 %if dlen==1, sMap.codebook = (sMap.codebook - 0.5)*diag(D); end
Daniel@0 211 error(['Linear map initialization requires at least two NaN-free' ...
Daniel@0 212 ' samples.']);
Daniel@0 213 return;
Daniel@0 214 end
Daniel@0 215
Daniel@0 216 % compute principle components
Daniel@0 217 if dim > 1 & sum(msize > 1) > 1,
Daniel@0 218 % calculate mdim largest eigenvalues and their corresponding
Daniel@0 219 % eigenvectors
Daniel@0 220
Daniel@0 221 % autocorrelation matrix
Daniel@0 222 A = zeros(dim);
Daniel@0 223 me = zeros(1,dim);
Daniel@0 224 for i=1:dim,
Daniel@0 225 me(i) = mean(D(isfinite(D(:,i)),i));
Daniel@0 226 D(:,i) = D(:,i) - me(i);
Daniel@0 227 end
Daniel@0 228 for i=1:dim,
Daniel@0 229 for j=i:dim,
Daniel@0 230 c = D(:,i).*D(:,j); c = c(isfinite(c));
Daniel@0 231 A(i,j) = sum(c)/length(c); A(j,i) = A(i,j);
Daniel@0 232 end
Daniel@0 233 end
Daniel@0 234
Daniel@0 235 % take mdim first eigenvectors with the greatest eigenvalues
Daniel@0 236 [V,S] = eig(A);
Daniel@0 237 eigval = diag(S);
Daniel@0 238 [y,ind] = sort(eigval);
Daniel@0 239 eigval = eigval(flipud(ind));
Daniel@0 240 V = V(:,flipud(ind));
Daniel@0 241 V = V(:,1:mdim);
Daniel@0 242 eigval = eigval(1:mdim);
Daniel@0 243
Daniel@0 244 % normalize eigenvectors to unit length and multiply them by
Daniel@0 245 % corresponding (square-root-of-)eigenvalues
Daniel@0 246 for i=1:mdim, V(:,i) = (V(:,i) / norm(V(:,i))) * sqrt(eigval(i)); end
Daniel@0 247
Daniel@0 248 else
Daniel@0 249
Daniel@0 250 me = zeros(1,dim);
Daniel@0 251 V = zeros(1,dim);
Daniel@0 252 for i=1:dim,
Daniel@0 253 inds = find(~isnan(D(:,i)));
Daniel@0 254 me(i) = mean(D(inds,i),1);
Daniel@0 255 V(i) = std(D(inds,i),1);
Daniel@0 256 end
Daniel@0 257
Daniel@0 258 end
Daniel@0 259
Daniel@0 260 % initialize codebook vectors
Daniel@0 261 if dim>1,
Daniel@0 262 sMap.codebook = me(ones(munits,1),:);
Daniel@0 263 Coords = som_unit_coords(msize,'rect','sheet');
Daniel@0 264 cox = Coords(:,1); Coords(:,1) = Coords(:,2); Coords(:,2) = cox;
Daniel@0 265 for i=1:mdim,
Daniel@0 266 ma = max(Coords(:,i)); mi = min(Coords(:,i));
Daniel@0 267 if ma>mi, Coords(:,i) = (Coords(:,i)-mi)/(ma-mi); else Coords(:,i) = 0.5; end
Daniel@0 268 end
Daniel@0 269 Coords = (Coords-0.5)*2;
Daniel@0 270 for n = 1:munits,
Daniel@0 271 for d = 1:mdim,
Daniel@0 272 sMap.codebook(n,:) = sMap.codebook(n,:)+Coords(n,d)*V(:, d)';
Daniel@0 273 end
Daniel@0 274 end
Daniel@0 275 else
Daniel@0 276 sMap.codebook = [0:(munits-1)]'/(munits-1)*(max(D)-min(D))+min(D);
Daniel@0 277 end
Daniel@0 278
Daniel@0 279 % training struct
Daniel@0 280 sTrain = som_set(sTrain,'time',datestr(now,0));
Daniel@0 281 sMap.trainhist = sTrain;
Daniel@0 282
Daniel@0 283 return;
Daniel@0 284
Daniel@0 285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%