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