annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_vs1to2.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_vs1to2(sS)
wolffd@0 2
wolffd@0 3 %SOM_VS1TO2 Convert version 1 structure to version 2.
wolffd@0 4 %
wolffd@0 5 % sSnew = som_vs1to2(sSold)
wolffd@0 6 %
wolffd@0 7 % sMnew = som_vs1to2(sMold);
wolffd@0 8 % sDnew = som_vs1to2(sDold);
wolffd@0 9 %
wolffd@0 10 % Input and output arguments:
wolffd@0 11 % sSold (struct) a SOM Toolbox version 1 structure
wolffd@0 12 % sSnew (struct) a SOM Toolbox version 2 structure
wolffd@0 13 %
wolffd@0 14 % For more help, try 'type som_vs1to2' or check out online documentation.
wolffd@0 15 % See also SOM_SET, SOM_VS2TO1.
wolffd@0 16
wolffd@0 17 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 18 %
wolffd@0 19 % som_vs1to2
wolffd@0 20 %
wolffd@0 21 % PURPOSE
wolffd@0 22 %
wolffd@0 23 % Transforms SOM Toolbox 1 version structs from to 2 version structs.
wolffd@0 24 %
wolffd@0 25 % SYNTAX
wolffd@0 26 %
wolffd@0 27 % sS2 = som_vs1to2(sS1)
wolffd@0 28 %
wolffd@0 29 % DESCRIPTION
wolffd@0 30 %
wolffd@0 31 % This function is offered to allow the change of old map and data structs
wolffd@0 32 % to new ones. There are quite a lot of changes between the versions,
wolffd@0 33 % especially in the map struct, and this function makes it easy to update
wolffd@0 34 % the structs.
wolffd@0 35 %
wolffd@0 36 % WARNING!
wolffd@0 37 %
wolffd@0 38 % 'som_unit_norm' normalization type is not supported by version 2,
wolffd@0 39 % so this type of normalization will be lost.
wolffd@0 40 %
wolffd@0 41 % REQUIRED INPUT ARGUMENTS
wolffd@0 42 %
wolffd@0 43 % sS1 (struct) any SOM Toolbox version 1 struct (map, data,
wolffd@0 44 % training or normalization struct)
wolffd@0 45 %
wolffd@0 46 % OUTPUT ARGUMENTS
wolffd@0 47 %
wolffd@0 48 % sS2 (struct) the corresponding SOM Toolbox 2 version struct
wolffd@0 49 %
wolffd@0 50 % EXAMPLES
wolffd@0 51 %
wolffd@0 52 % sM = som_vs1to2(sMold);
wolffd@0 53 % sD = som_vs1to2(sDold);
wolffd@0 54 % sT = som_vs1to2(sMold.train_sequence{1});
wolffd@0 55 % sN = som_vs1to2(sDold.normalization);
wolffd@0 56 %
wolffd@0 57 % SEE ALSO
wolffd@0 58 %
wolffd@0 59 % som_set Set values and create SOM Toolbox structs.
wolffd@0 60 % som_vs2to1 Transform structs from version 2.0 to 1.0.
wolffd@0 61
wolffd@0 62 % Copyright (c) 1999-2000 by the SOM toolbox programming team.
wolffd@0 63 % http://www.cis.hut.fi/projects/somtoolbox/
wolffd@0 64
wolffd@0 65 % Version 2.0beta juuso 101199
wolffd@0 66
wolffd@0 67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 68 %% check arguments
wolffd@0 69
wolffd@0 70 error(nargchk(1, 1, nargin)); % check no. of input arguments is correct
wolffd@0 71
wolffd@0 72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 73 %% set field values
wolffd@0 74
wolffd@0 75 if isfield(sS,'codebook'), type='som_map';
wolffd@0 76 elseif isfield(sS,'data'), type='som_data';
wolffd@0 77 elseif isfield(sS,'algorithm'), type = 'som_train';
wolffd@0 78 elseif isfield(sS,'inv_params'), type = 'som_norm';
wolffd@0 79 else
wolffd@0 80 error('Unrecognized input struct.');
wolffd@0 81 end
wolffd@0 82
wolffd@0 83 switch type,
wolffd@0 84 case 'som_map',
wolffd@0 85 msize = sS.msize; munits = prod(msize); dim = prod(size(sS.codebook))/munits;
wolffd@0 86 M = reshape(sS.codebook,[munits dim]);
wolffd@0 87
wolffd@0 88 % topology
wolffd@0 89 if strcmp(sS.shape,'rect'), shape = 'sheet'; else shape = sS.shape; end
wolffd@0 90 sTopol = struct('type','som_topol','msize',msize,'lattice',sS.lattice,'shape',shape);
wolffd@0 91
wolffd@0 92 % labels
wolffd@0 93 labels = cell(munits,1);
wolffd@0 94 for i=1:munits,
wolffd@0 95 for j=1:length(sS.labels{i}), labels{i,j} = sS.labels{i}{j}; end
wolffd@0 96 end
wolffd@0 97
wolffd@0 98 % trainhist
wolffd@0 99 tl = length(sS.train_sequence);
wolffd@0 100 if strcmp(sS.init_type,'linear'); alg = 'lininit'; else alg = 'randinit'; end
wolffd@0 101 trh = struct('type','som_train');
wolffd@0 102 trh.algorithm = alg;
wolffd@0 103 trh.neigh = sS.neigh;
wolffd@0 104 trh.mask = sS.mask;
wolffd@0 105 trh.data_name = sS.data_name;
wolffd@0 106 trh.radius_ini = NaN;
wolffd@0 107 trh.radius_fin = NaN;
wolffd@0 108 trh.alpha_ini = NaN;
wolffd@0 109 trh.alpha_type = '';
wolffd@0 110 trh.trainlen = NaN;
wolffd@0 111 trh.time = '';
wolffd@0 112 for i=1:tl,
wolffd@0 113 trh(i+1) = som_vs1to2(sS.train_sequence{i});
wolffd@0 114 trh(i+1).mask = sS.mask;
wolffd@0 115 trh(i+1).neigh = sS.neigh;
wolffd@0 116 trh(i+1).data_name = sS.data_name;
wolffd@0 117 end
wolffd@0 118
wolffd@0 119 % component normalizations
wolffd@0 120 cnorm = som_vs1to2(sS.normalization);
wolffd@0 121 if isempty(cnorm),
wolffd@0 122 cnorm = cell(dim,1);
wolffd@0 123 elseif length(cnorm) ~= dim,
wolffd@0 124 warning('Incorrect number of normalizations. Normalizations ignored.\n');
wolffd@0 125 cnorm = cell(dim,1);
wolffd@0 126 else
wolffd@0 127 if strcmp(cnorm{1}.method,'histD'),
wolffd@0 128 M = redo_hist_norm(M,sS.normalization.inv_params,cnorm);
wolffd@0 129 end
wolffd@0 130 end
wolffd@0 131
wolffd@0 132 % map
wolffd@0 133 sSnew = struct('type','som_map');
wolffd@0 134 sSnew.codebook = M;
wolffd@0 135 sSnew.topol = sTopol;
wolffd@0 136 sSnew.labels = labels;
wolffd@0 137 sSnew.neigh = sS.neigh;
wolffd@0 138 sSnew.mask = sS.mask;
wolffd@0 139 sSnew.trainhist = trh;
wolffd@0 140 sSnew.name = sS.name;
wolffd@0 141 sSnew.comp_norm = cnorm;
wolffd@0 142 sSnew.comp_names = sS.comp_names;
wolffd@0 143
wolffd@0 144 case 'som_data',
wolffd@0 145 [dlen dim] = size(sS.data);
wolffd@0 146
wolffd@0 147 % component normalizations
wolffd@0 148 cnorm = som_vs1to2(sS.normalization);
wolffd@0 149 if isempty(cnorm),
wolffd@0 150 cnorm = cell(dim,1);
wolffd@0 151 elseif length(cnorm) ~= dim,
wolffd@0 152 warning('Incorrect number of normalizations. Normalizations ignored.\n');
wolffd@0 153 cnorm = cell(dim,1);
wolffd@0 154 else
wolffd@0 155 if strcmp(cnorm{1}.method,'histD'),
wolffd@0 156 sS.data = redo_hist_norm(sS.data,sS.normalization.inv_params,cnorm);
wolffd@0 157 end
wolffd@0 158 end
wolffd@0 159
wolffd@0 160 % data
wolffd@0 161 sSnew = struct('type','som_data');
wolffd@0 162 sSnew.data = sS.data;
wolffd@0 163 sSnew.name = sS.name;
wolffd@0 164 sSnew.labels = sS.labels;
wolffd@0 165 sSnew.comp_names = sS.comp_names;
wolffd@0 166 sSnew.comp_norm = cnorm;
wolffd@0 167 sSnew.label_names = [];
wolffd@0 168
wolffd@0 169 case 'som_norm',
wolffd@0 170 if isempty(sS.inv_params),
wolffd@0 171 sSnew = [];
wolffd@0 172 else
wolffd@0 173 dim = size(sS.inv_params,2);
wolffd@0 174 sSnew = cell(dim,1);
wolffd@0 175 switch sS.name,
wolffd@0 176 case 'som_var_norm', method = 'var';
wolffd@0 177 case 'som_lin_norm', method = 'range';
wolffd@0 178 case 'som_hist_norm', method = 'histD';
wolffd@0 179 case 'som_unit_norm', method = '';
wolffd@0 180 warning(['Normalization method ''som_unit_norm'' is not available' ...
wolffd@0 181 ' in version 2 of SOM Toolbox.\n']);
wolffd@0 182 end
wolffd@0 183 if ~isempty(method),
wolffd@0 184 for i=1:dim,
wolffd@0 185 sSnew{i} = struct('type','som_norm');
wolffd@0 186 sSnew{i}.method = method;
wolffd@0 187 sSnew{i}.params = [];
wolffd@0 188 sSnew{i}.status = 'done';
wolffd@0 189 switch method,
wolffd@0 190 case 'var',
wolffd@0 191 me = sS.inv_params(1,i); st = sS.inv_params(2,i);
wolffd@0 192 sSnew{i}.params = [me, st];
wolffd@0 193 case 'range',
wolffd@0 194 mi = sS.inv_params(1,i); ma = sS.inv_params(2,i);
wolffd@0 195 sSnew{i}.params = [mi, ma-mi];
wolffd@0 196 case 'histD',
wolffd@0 197 vals = sS.inv_params(1:(end-1),i);
wolffd@0 198 bins = sum(isfinite(vals));
wolffd@0 199 vals = vals(1:bins);
wolffd@0 200 sSnew{i}.params = vals;
wolffd@0 201 end
wolffd@0 202 end
wolffd@0 203 end
wolffd@0 204 end
wolffd@0 205
wolffd@0 206 case 'som_train',
wolffd@0 207 sSnew = struct('type','som_train');
wolffd@0 208 sSnew.algorithm = sS.algorithm;
wolffd@0 209 sSnew.neigh = 'gaussian';
wolffd@0 210 sSnew.mask = [];
wolffd@0 211 sSnew.data_name = 'unknown';
wolffd@0 212 sSnew.radius_ini = sS.radius_ini;
wolffd@0 213 sSnew.radius_fin = sS.radius_fin;
wolffd@0 214 sSnew.alpha_ini = sS.alpha_ini;
wolffd@0 215 sSnew.alpha_type = sS.alpha_type;
wolffd@0 216 sSnew.trainlen = sS.trainlen;
wolffd@0 217 sSnew.time = sS.time;
wolffd@0 218
wolffd@0 219 case 'som_topol',
wolffd@0 220 disp('Version 1.0 of SOM Toolbox did not have topology structure.\n');
wolffd@0 221
wolffd@0 222 case {'som_grid','som_vis'}
wolffd@0 223 disp('Version 1.0 of SOM Toolbox did not have visualization structs.\n');
wolffd@0 224
wolffd@0 225 otherwise,
wolffd@0 226
wolffd@0 227 error('Unrecognized struct.');
wolffd@0 228 end
wolffd@0 229
wolffd@0 230 sS = sSnew;
wolffd@0 231
wolffd@0 232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 233 %% subfunctions
wolffd@0 234
wolffd@0 235 function D = redo_hist_norm(D,inv_params,cnorm)
wolffd@0 236
wolffd@0 237 dim = size(D,2);
wolffd@0 238
wolffd@0 239 % first - undo the old way
wolffd@0 240 n_bins = inv_params(end,:);
wolffd@0 241 D = round(D * sparse(diag(n_bins)));
wolffd@0 242 for i = 1:dim,
wolffd@0 243 if any(isnan(D(:, i))), D(isnan(D(:, i)), i) = n_bins(i); end
wolffd@0 244 D(:, i) = inv_params(D(:, i), i);
wolffd@0 245 end
wolffd@0 246 % then - redo the new way
wolffd@0 247 for i=1:dim,
wolffd@0 248 bins = length(cnorm{i}.params);
wolffd@0 249 x = D(:,i);
wolffd@0 250 inds = find(~isnan(x) & ~isinf(x))';
wolffd@0 251 for j = inds,
wolffd@0 252 [dummy ind] = min(abs(x(j) - cnorm{i}.params));
wolffd@0 253 if x(j) > cnorm{i}.params(ind) & ind < bins, x(j) = ind + 1;
wolffd@0 254 else x(j) = ind;
wolffd@0 255 end
wolffd@0 256 end
wolffd@0 257 D(:,i) = (x-1)/(bins-1);
wolffd@0 258 end
wolffd@0 259
wolffd@0 260
wolffd@0 261
wolffd@0 262