annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_vs2to1.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function sS = som_vs2to1(sS)
wolffd@0 2
wolffd@0 3 %SOM_VS2TO1 Convert version 2 struct to version 1.
wolffd@0 4 %
wolffd@0 5 % sSold = som_vs2to1(sSnew)
wolffd@0 6 %
wolffd@0 7 % sMold = som_vs2to1(sMnew);
wolffd@0 8 % sDold = som_vs2to1(sDnew);
wolffd@0 9 %
wolffd@0 10 % Input and output arguments:
wolffd@0 11 % sSnew (struct) a SOM Toolbox version 2 struct
wolffd@0 12 % sSold (struct) a SOM Toolbox version 1 struct
wolffd@0 13 %
wolffd@0 14 % For more help, try 'type som_vs2to1' or check out online documentation.
wolffd@0 15 % See also SOM_SET, SOM_VS1TO2.
wolffd@0 16
wolffd@0 17 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 18 %
wolffd@0 19 % som_vs2to1
wolffd@0 20 %
wolffd@0 21 % PURPOSE
wolffd@0 22 %
wolffd@0 23 % Converts SOM Toolbox version 2 structs to version 1 structs.
wolffd@0 24 %
wolffd@0 25 % SYNTAX
wolffd@0 26 %
wolffd@0 27 % sS1 = som_vs2to1(sS2)
wolffd@0 28 %
wolffd@0 29 % DESCRIPTION
wolffd@0 30 %
wolffd@0 31 % This function is offered to allow the change of new map and data structs
wolffd@0 32 % to old ones. There are quite a lot of changes between the versions,
wolffd@0 33 % especially in the map struct, and this function makes it possible to
wolffd@0 34 % use the old functions with new structs.
wolffd@0 35 %
wolffd@0 36 % Note that part of the information is lost in the conversion. Especially,
wolffd@0 37 % training history is lost, and the normalization is, except in the simplest
wolffd@0 38 % cases (like all have 'range' or 'var' normalization) screwed up.
wolffd@0 39 %
wolffd@0 40 % REQUIRED INPUT ARGUMENTS
wolffd@0 41 %
wolffd@0 42 % sS2 (struct) som SOM Toolbox version 2.0 struct (map, data,
wolffd@0 43 % training or normalization struct)
wolffd@0 44 %
wolffd@0 45 % OUTPUT ARGUMENTS
wolffd@0 46 %
wolffd@0 47 % sS1 (struct) the corresponding SOM Toolbox version 2.0 struct
wolffd@0 48 %
wolffd@0 49 % EXAMPLES
wolffd@0 50 %
wolffd@0 51 % sM = som_vs2to1(sMnew);
wolffd@0 52 % sD = som_vs2to1(sDnew);
wolffd@0 53 % sT = som_vs2to1(sMnew.trainhist(1));
wolffd@0 54 %
wolffd@0 55 % SEE ALSO
wolffd@0 56 %
wolffd@0 57 % som_set Set values and create SOM Toolbox structs.
wolffd@0 58 % som_vs1to2 Transform structs from 1.0 version to 2.0.
wolffd@0 59
wolffd@0 60 % Copyright (c) 1999-2000 by the SOM toolbox programming team.
wolffd@0 61 % http://www.cis.hut.fi/projects/somtoolbox/
wolffd@0 62
wolffd@0 63 % Version 2.0beta juuso 101199
wolffd@0 64
wolffd@0 65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 66 %% check arguments
wolffd@0 67
wolffd@0 68 error(nargchk(1, 1, nargin)); % check no. of input arguments is correct
wolffd@0 69
wolffd@0 70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 71 %% set field values
wolffd@0 72
wolffd@0 73 switch sS.type,
wolffd@0 74 case 'som_map',
wolffd@0 75 msize = sS.topol.msize;
wolffd@0 76 [munits dim] = size(sS.codebook);
wolffd@0 77
wolffd@0 78 % topology
wolffd@0 79 if strcmp(sS.topol.shape,'sheet'), shape = 'rect';
wolffd@0 80 else shape = sS.shape;
wolffd@0 81 end
wolffd@0 82
wolffd@0 83 % labels
wolffd@0 84 labels = cell(munits,1);
wolffd@0 85 nl = size(sS.labels,2);
wolffd@0 86 for i=1:munits,
wolffd@0 87 labels{i} = cell(nl,1);
wolffd@0 88 for j=1:nl, labels{i}{j} = sS.labels{i,j}; end
wolffd@0 89 end
wolffd@0 90
wolffd@0 91 % trainhist
wolffd@0 92 tl = length(sS.trainhist);
wolffd@0 93 if tl==0 | strcmp(sS.trainhist(1).algorithm,'lininit'),
wolffd@0 94 init_type = 'linear';
wolffd@0 95 else
wolffd@0 96 init_type = 'random';
wolffd@0 97 end
wolffd@0 98 if tl>1,
wolffd@0 99 for i=2:tl,
wolffd@0 100 train_seq{i-1} = som_vs2to1(sS.trainhist(i));
wolffd@0 101 end
wolffd@0 102 train_type = sS.trainhist(tl).algorithm;
wolffd@0 103 else
wolffd@0 104 train_seq = [];
wolffd@0 105 train_type = 'batch';
wolffd@0 106 end
wolffd@0 107 if tl>0, data_name = sS.trainhist(tl).data_name; else data_name = ''; end
wolffd@0 108
wolffd@0 109 % component normalizations
wolffd@0 110 sN = convert_normalizations(sS.comp_norm);
wolffd@0 111 if strcmp(sN.name,'som_hist_norm'),
wolffd@0 112 sS.codebook = redo_hist_norm(sS.codebook,sS.comp_norm,sN);
wolffd@0 113 end
wolffd@0 114
wolffd@0 115 % map
wolffd@0 116 sSnew = struct('init_type', 'linear', 'train_type', 'batch', 'lattice' ,...
wolffd@0 117 'hexa', 'shape', 'rect', 'neigh', 'gaussian', 'msize', msize, ...
wolffd@0 118 'train_sequence', [], 'codebook', [], 'labels', [], ...
wolffd@0 119 'mask', [], 'data_name', 'unnamed', 'normalization', [], ...
wolffd@0 120 'comp_names', [], 'name', 'unnamed');
wolffd@0 121 sSnew.init_type = init_type;
wolffd@0 122 sSnew.train_type = train_type;
wolffd@0 123 sSnew.lattice = sS.topol.lattice;
wolffd@0 124 sSnew.shape = shape;
wolffd@0 125 sSnew.neigh = sS.neigh;
wolffd@0 126 sSnew.msize = sS.topol.msize;
wolffd@0 127 sSnew.train_sequence = train_seq;
wolffd@0 128 sSnew.codebook = reshape(sS.codebook,[sS.topol.msize dim]);
wolffd@0 129 sSnew.labels = labels;
wolffd@0 130 sSnew.mask = sS.mask;
wolffd@0 131 sSnew.data_name = data_name;
wolffd@0 132 sSnew.normalization = sN;
wolffd@0 133 sSnew.comp_names = sS.comp_names;
wolffd@0 134 sSnew.name = sS.name;
wolffd@0 135
wolffd@0 136 case 'som_data',
wolffd@0 137 [dlen dim] = size(sS.data);
wolffd@0 138
wolffd@0 139 % component normalizations
wolffd@0 140 sN = convert_normalizations(sS.comp_norm);
wolffd@0 141 if strcmp(sN.name,'som_hist_norm'),
wolffd@0 142 sS.codebook = redo_hist_norm(sS.codebook,sS.comp_norm,sN);
wolffd@0 143 end
wolffd@0 144
wolffd@0 145 % data
wolffd@0 146 sSnew = struct('data', [], 'name', '', 'labels' , [], 'comp_names', ...
wolffd@0 147 [], 'normalization', []);
wolffd@0 148 sSnew.data = sS.data;
wolffd@0 149 sSnew.name = sS.name;
wolffd@0 150 sSnew.labels = sS.labels;
wolffd@0 151 sSnew.comp_names = sS.comp_names;
wolffd@0 152 sSnew.normalization = sN;
wolffd@0 153
wolffd@0 154 case 'som_norm',
wolffd@0 155 sSnew = struct('name','som_var_norm','inv_params',[]);
wolffd@0 156
wolffd@0 157 switch sS.method,
wolffd@0 158 case 'var', sSnew.name = 'som_var_norm';
wolffd@0 159 case 'range', sSnew.name = 'som_lin_norm';
wolffd@0 160 case 'histD', sSnew.name = 'som_hist_norm';
wolffd@0 161 otherwise,
wolffd@0 162 warning(['Method ' method ' does not exist in version 1.'])
wolffd@0 163 end
wolffd@0 164
wolffd@0 165 if strcmp(sS.status,'done'),
wolffd@0 166 switch sS.method,
wolffd@0 167 case 'var',
wolffd@0 168 sSnew.inv_params = zeros(2,1);
wolffd@0 169 sSnew.inv_params(1) = sS.params(1);
wolffd@0 170 sSnew.inv_params(2) = sS.params(2);
wolffd@0 171 case 'range',
wolffd@0 172 sSnew.inv_params = zeros(2,1);
wolffd@0 173 sSnew.inv_params(1) = sS.params(1);
wolffd@0 174 sSnew.inv_params(2) = sS.params(2) + sS.params(1);;
wolffd@0 175 case 'histD',
wolffd@0 176 bins = length(sS.params);
wolffd@0 177 sSnew.inv_params = zeros(bins+1,1) + Inf;
wolffd@0 178 sSnew.inv_params(1:bins,i) = sS.params;
wolffd@0 179 sSnew.inv_params(end,i) = bins;
wolffd@0 180 end
wolffd@0 181 end
wolffd@0 182
wolffd@0 183 case 'som_train',
wolffd@0 184 sSnew = struct('algorithm', sS.algorithm, 'radius_ini', ...
wolffd@0 185 sS.radius_ini, 'radius_fin', sS.radius_fin, 'alpha_ini', ...
wolffd@0 186 sS.alpha_ini, 'alpha_type', sS.alpha_type, 'trainlen', sS.trainlen, ...
wolffd@0 187 'qerror', NaN, 'time', sS.time);
wolffd@0 188
wolffd@0 189 case 'som_topol',
wolffd@0 190 disp('Version 1 of SOM Toolbox did not have topology structure.\n');
wolffd@0 191
wolffd@0 192 otherwise,
wolffd@0 193
wolffd@0 194 error('Unrecognized struct.');
wolffd@0 195 end
wolffd@0 196
wolffd@0 197 sS = sSnew;
wolffd@0 198
wolffd@0 199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 200 %% subfunctions
wolffd@0 201
wolffd@0 202 function sN = convert_normalizations(cnorm)
wolffd@0 203
wolffd@0 204 dim = length(cnorm);
wolffd@0 205 sN = struct('name','som_var_norm','inv_params',[]);
wolffd@0 206
wolffd@0 207 % check that there is exactly one normalization per component
wolffd@0 208 % and that their status and method is the same
wolffd@0 209 ok = 1;
wolffd@0 210 nof = zeros(dim,1);
wolffd@0 211 for i=1:dim, nof(i) = length(cnorm{i}); end
wolffd@0 212 if any(nof>1), ok=0;
wolffd@0 213 elseif any(nof==1) & any(nof==0), ok=0;
wolffd@0 214 elseif any(nof>0),
wolffd@0 215 status = cnorm{1}.status;
wolffd@0 216 method = cnorm{1}.method;
wolffd@0 217 for i=2:dim,
wolffd@0 218 if ~strcmp(cnorm{i}.status,status) | ~strcmp(cnorm{i}.method,method),
wolffd@0 219 ok = 0;
wolffd@0 220 end
wolffd@0 221 end
wolffd@0 222 elseif all(nof==0),
wolffd@0 223 return;
wolffd@0 224 end
wolffd@0 225 if ~ok,
wolffd@0 226 warning(['Normalization could not be converted. All variables can' ...
wolffd@0 227 ' only be normalized with a single, and same, method.']);
wolffd@0 228 return;
wolffd@0 229 end
wolffd@0 230
wolffd@0 231 % method name
wolffd@0 232 switch method,
wolffd@0 233 case 'var', sN.name = 'som_var_norm';
wolffd@0 234 case 'range', sN.name = 'som_lin_norm';
wolffd@0 235 case 'histD', sN.name = 'som_hist_norm';
wolffd@0 236 otherwise,
wolffd@0 237 warning(['Normalization could not be converted. Method ' method ...
wolffd@0 238 'does not exist in version 1.']);
wolffd@0 239 return;
wolffd@0 240 end
wolffd@0 241
wolffd@0 242 % if not done, inv_params is empty
wolffd@0 243 if ~strcmp(status,'done'), return; end
wolffd@0 244
wolffd@0 245 % ok, make the conversion
wolffd@0 246 switch method,
wolffd@0 247 case 'var',
wolffd@0 248 sN.inv_params = zeros(2,dim);
wolffd@0 249 for i=1:dim,
wolffd@0 250 sN.inv_params(1,i) = cnorm{i}.params(1);
wolffd@0 251 sN.inv_params(2,i) = cnorm{i}.params(2);
wolffd@0 252 end
wolffd@0 253 case 'range',
wolffd@0 254 sN.inv_params = zeros(2,dim);
wolffd@0 255 for i=1:dim,
wolffd@0 256 sN.inv_params(1,i) = cnorm{i}.params(1);
wolffd@0 257 sN.inv_params(2,i) = cnorm{i}.params(2) + cnorm{i}.params(1);
wolffd@0 258 end
wolffd@0 259 case 'histD',
wolffd@0 260 bins = zeros(dim,1);
wolffd@0 261 for i=1:dim, bins(i) = length(cnorm{i}.params); end
wolffd@0 262 m = max(bins);
wolffd@0 263 sN.inv_params = zeros(m+1,dim) + Inf;
wolffd@0 264 for i=1:dim,
wolffd@0 265 sN.inv_params(1:bins(i),i) = cnorm{i}.params;
wolffd@0 266 if bins(i)<m, sN.inv_params(bins(i)+1,i) = NaN; end
wolffd@0 267 sN.inv_params(end,i) = bins(i);
wolffd@0 268 end
wolffd@0 269 end
wolffd@0 270
wolffd@0 271 function D = redo_hist_norm(D,cnorm,sN)
wolffd@0 272
wolffd@0 273 dim = size(D,2);
wolffd@0 274
wolffd@0 275 % first - undo the new way
wolffd@0 276 for i=1:dim,
wolffd@0 277 bins = length(cnorm{i}.params);
wolffd@0 278 D(:,i) = round(D(:,i)*(bins-1)+1);
wolffd@0 279 inds = find(~isnan(D(:,i)) & ~isinf(D(:,i)));
wolffd@0 280 D(inds,i) = cnorm{i}.params(D(inds,i));
wolffd@0 281 end
wolffd@0 282 % then - redo the old way
wolffd@0 283 n_bins = sN.inv_params(size(sN.inv_params,1),:);
wolffd@0 284 for j = 1:dim,
wolffd@0 285 for i = 1:size(D, 1)
wolffd@0 286 if ~isnan(D(i, j)),
wolffd@0 287 [d ind] = min(abs(D(i, j) - sN.inv_params(1:n_bins(j), j)));
wolffd@0 288 if (D(i, j) - sN.inv_params(ind, j)) > 0 & ind < n_bins(j),
wolffd@0 289 D(i, j) = ind + 1;
wolffd@0 290 else
wolffd@0 291 D(i, j) = ind;
wolffd@0 292 end
wolffd@0 293 end
wolffd@0 294 end
wolffd@0 295 end
wolffd@0 296 D = D * sparse(diag(1 ./ n_bins));
wolffd@0 297
wolffd@0 298