Daniel@0: function sS = som_vs2to1(sS) Daniel@0: Daniel@0: %SOM_VS2TO1 Convert version 2 struct to version 1. Daniel@0: % Daniel@0: % sSold = som_vs2to1(sSnew) Daniel@0: % Daniel@0: % sMold = som_vs2to1(sMnew); Daniel@0: % sDold = som_vs2to1(sDnew); Daniel@0: % Daniel@0: % Input and output arguments: Daniel@0: % sSnew (struct) a SOM Toolbox version 2 struct Daniel@0: % sSold (struct) a SOM Toolbox version 1 struct Daniel@0: % Daniel@0: % For more help, try 'type som_vs2to1' or check out online documentation. Daniel@0: % See also SOM_SET, SOM_VS1TO2. Daniel@0: Daniel@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: % Daniel@0: % som_vs2to1 Daniel@0: % Daniel@0: % PURPOSE Daniel@0: % Daniel@0: % Converts SOM Toolbox version 2 structs to version 1 structs. Daniel@0: % Daniel@0: % SYNTAX Daniel@0: % Daniel@0: % sS1 = som_vs2to1(sS2) Daniel@0: % Daniel@0: % DESCRIPTION Daniel@0: % Daniel@0: % This function is offered to allow the change of new map and data structs Daniel@0: % to old ones. There are quite a lot of changes between the versions, Daniel@0: % especially in the map struct, and this function makes it possible to Daniel@0: % use the old functions with new structs. Daniel@0: % Daniel@0: % Note that part of the information is lost in the conversion. Especially, Daniel@0: % training history is lost, and the normalization is, except in the simplest Daniel@0: % cases (like all have 'range' or 'var' normalization) screwed up. Daniel@0: % Daniel@0: % REQUIRED INPUT ARGUMENTS Daniel@0: % Daniel@0: % sS2 (struct) som SOM Toolbox version 2.0 struct (map, data, Daniel@0: % training or normalization struct) Daniel@0: % Daniel@0: % OUTPUT ARGUMENTS Daniel@0: % Daniel@0: % sS1 (struct) the corresponding SOM Toolbox version 2.0 struct Daniel@0: % Daniel@0: % EXAMPLES Daniel@0: % Daniel@0: % sM = som_vs2to1(sMnew); Daniel@0: % sD = som_vs2to1(sDnew); Daniel@0: % sT = som_vs2to1(sMnew.trainhist(1)); Daniel@0: % Daniel@0: % SEE ALSO Daniel@0: % Daniel@0: % som_set Set values and create SOM Toolbox structs. Daniel@0: % som_vs1to2 Transform structs from 1.0 version to 2.0. 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.0beta juuso 101199 Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% check arguments Daniel@0: Daniel@0: error(nargchk(1, 1, nargin)); % check no. of input arguments is correct Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% set field values Daniel@0: Daniel@0: switch sS.type, Daniel@0: case 'som_map', Daniel@0: msize = sS.topol.msize; Daniel@0: [munits dim] = size(sS.codebook); Daniel@0: Daniel@0: % topology Daniel@0: if strcmp(sS.topol.shape,'sheet'), shape = 'rect'; Daniel@0: else shape = sS.shape; Daniel@0: end Daniel@0: Daniel@0: % labels Daniel@0: labels = cell(munits,1); Daniel@0: nl = size(sS.labels,2); Daniel@0: for i=1:munits, Daniel@0: labels{i} = cell(nl,1); Daniel@0: for j=1:nl, labels{i}{j} = sS.labels{i,j}; end Daniel@0: end Daniel@0: Daniel@0: % trainhist Daniel@0: tl = length(sS.trainhist); Daniel@0: if tl==0 | strcmp(sS.trainhist(1).algorithm,'lininit'), Daniel@0: init_type = 'linear'; Daniel@0: else Daniel@0: init_type = 'random'; Daniel@0: end Daniel@0: if tl>1, Daniel@0: for i=2:tl, Daniel@0: train_seq{i-1} = som_vs2to1(sS.trainhist(i)); Daniel@0: end Daniel@0: train_type = sS.trainhist(tl).algorithm; Daniel@0: else Daniel@0: train_seq = []; Daniel@0: train_type = 'batch'; Daniel@0: end Daniel@0: if tl>0, data_name = sS.trainhist(tl).data_name; else data_name = ''; end Daniel@0: Daniel@0: % component normalizations Daniel@0: sN = convert_normalizations(sS.comp_norm); Daniel@0: if strcmp(sN.name,'som_hist_norm'), Daniel@0: sS.codebook = redo_hist_norm(sS.codebook,sS.comp_norm,sN); Daniel@0: end Daniel@0: Daniel@0: % map Daniel@0: sSnew = struct('init_type', 'linear', 'train_type', 'batch', 'lattice' ,... Daniel@0: 'hexa', 'shape', 'rect', 'neigh', 'gaussian', 'msize', msize, ... Daniel@0: 'train_sequence', [], 'codebook', [], 'labels', [], ... Daniel@0: 'mask', [], 'data_name', 'unnamed', 'normalization', [], ... Daniel@0: 'comp_names', [], 'name', 'unnamed'); Daniel@0: sSnew.init_type = init_type; Daniel@0: sSnew.train_type = train_type; Daniel@0: sSnew.lattice = sS.topol.lattice; Daniel@0: sSnew.shape = shape; Daniel@0: sSnew.neigh = sS.neigh; Daniel@0: sSnew.msize = sS.topol.msize; Daniel@0: sSnew.train_sequence = train_seq; Daniel@0: sSnew.codebook = reshape(sS.codebook,[sS.topol.msize dim]); Daniel@0: sSnew.labels = labels; Daniel@0: sSnew.mask = sS.mask; Daniel@0: sSnew.data_name = data_name; Daniel@0: sSnew.normalization = sN; Daniel@0: sSnew.comp_names = sS.comp_names; Daniel@0: sSnew.name = sS.name; Daniel@0: Daniel@0: case 'som_data', Daniel@0: [dlen dim] = size(sS.data); Daniel@0: Daniel@0: % component normalizations Daniel@0: sN = convert_normalizations(sS.comp_norm); Daniel@0: if strcmp(sN.name,'som_hist_norm'), Daniel@0: sS.codebook = redo_hist_norm(sS.codebook,sS.comp_norm,sN); Daniel@0: end Daniel@0: Daniel@0: % data Daniel@0: sSnew = struct('data', [], 'name', '', 'labels' , [], 'comp_names', ... Daniel@0: [], 'normalization', []); Daniel@0: sSnew.data = sS.data; Daniel@0: sSnew.name = sS.name; Daniel@0: sSnew.labels = sS.labels; Daniel@0: sSnew.comp_names = sS.comp_names; Daniel@0: sSnew.normalization = sN; Daniel@0: Daniel@0: case 'som_norm', Daniel@0: sSnew = struct('name','som_var_norm','inv_params',[]); Daniel@0: Daniel@0: switch sS.method, Daniel@0: case 'var', sSnew.name = 'som_var_norm'; Daniel@0: case 'range', sSnew.name = 'som_lin_norm'; Daniel@0: case 'histD', sSnew.name = 'som_hist_norm'; Daniel@0: otherwise, Daniel@0: warning(['Method ' method ' does not exist in version 1.']) Daniel@0: end Daniel@0: Daniel@0: if strcmp(sS.status,'done'), Daniel@0: switch sS.method, Daniel@0: case 'var', Daniel@0: sSnew.inv_params = zeros(2,1); Daniel@0: sSnew.inv_params(1) = sS.params(1); Daniel@0: sSnew.inv_params(2) = sS.params(2); Daniel@0: case 'range', Daniel@0: sSnew.inv_params = zeros(2,1); Daniel@0: sSnew.inv_params(1) = sS.params(1); Daniel@0: sSnew.inv_params(2) = sS.params(2) + sS.params(1);; Daniel@0: case 'histD', Daniel@0: bins = length(sS.params); Daniel@0: sSnew.inv_params = zeros(bins+1,1) + Inf; Daniel@0: sSnew.inv_params(1:bins,i) = sS.params; Daniel@0: sSnew.inv_params(end,i) = bins; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: case 'som_train', Daniel@0: sSnew = struct('algorithm', sS.algorithm, 'radius_ini', ... Daniel@0: sS.radius_ini, 'radius_fin', sS.radius_fin, 'alpha_ini', ... Daniel@0: sS.alpha_ini, 'alpha_type', sS.alpha_type, 'trainlen', sS.trainlen, ... Daniel@0: 'qerror', NaN, 'time', sS.time); Daniel@0: Daniel@0: case 'som_topol', Daniel@0: disp('Version 1 of SOM Toolbox did not have topology structure.\n'); Daniel@0: Daniel@0: otherwise, Daniel@0: Daniel@0: error('Unrecognized struct.'); Daniel@0: end Daniel@0: Daniel@0: sS = sSnew; Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% subfunctions Daniel@0: Daniel@0: function sN = convert_normalizations(cnorm) Daniel@0: Daniel@0: dim = length(cnorm); Daniel@0: sN = struct('name','som_var_norm','inv_params',[]); Daniel@0: Daniel@0: % check that there is exactly one normalization per component Daniel@0: % and that their status and method is the same Daniel@0: ok = 1; Daniel@0: nof = zeros(dim,1); Daniel@0: for i=1:dim, nof(i) = length(cnorm{i}); end Daniel@0: if any(nof>1), ok=0; Daniel@0: elseif any(nof==1) & any(nof==0), ok=0; Daniel@0: elseif any(nof>0), Daniel@0: status = cnorm{1}.status; Daniel@0: method = cnorm{1}.method; Daniel@0: for i=2:dim, Daniel@0: if ~strcmp(cnorm{i}.status,status) | ~strcmp(cnorm{i}.method,method), Daniel@0: ok = 0; Daniel@0: end Daniel@0: end Daniel@0: elseif all(nof==0), Daniel@0: return; Daniel@0: end Daniel@0: if ~ok, Daniel@0: warning(['Normalization could not be converted. All variables can' ... Daniel@0: ' only be normalized with a single, and same, method.']); Daniel@0: return; Daniel@0: end Daniel@0: Daniel@0: % method name Daniel@0: switch method, Daniel@0: case 'var', sN.name = 'som_var_norm'; Daniel@0: case 'range', sN.name = 'som_lin_norm'; Daniel@0: case 'histD', sN.name = 'som_hist_norm'; Daniel@0: otherwise, Daniel@0: warning(['Normalization could not be converted. Method ' method ... Daniel@0: 'does not exist in version 1.']); Daniel@0: return; Daniel@0: end Daniel@0: Daniel@0: % if not done, inv_params is empty Daniel@0: if ~strcmp(status,'done'), return; end Daniel@0: Daniel@0: % ok, make the conversion Daniel@0: switch method, Daniel@0: case 'var', Daniel@0: sN.inv_params = zeros(2,dim); Daniel@0: for i=1:dim, Daniel@0: sN.inv_params(1,i) = cnorm{i}.params(1); Daniel@0: sN.inv_params(2,i) = cnorm{i}.params(2); Daniel@0: end Daniel@0: case 'range', Daniel@0: sN.inv_params = zeros(2,dim); Daniel@0: for i=1:dim, Daniel@0: sN.inv_params(1,i) = cnorm{i}.params(1); Daniel@0: sN.inv_params(2,i) = cnorm{i}.params(2) + cnorm{i}.params(1); Daniel@0: end Daniel@0: case 'histD', Daniel@0: bins = zeros(dim,1); Daniel@0: for i=1:dim, bins(i) = length(cnorm{i}.params); end Daniel@0: m = max(bins); Daniel@0: sN.inv_params = zeros(m+1,dim) + Inf; Daniel@0: for i=1:dim, Daniel@0: sN.inv_params(1:bins(i),i) = cnorm{i}.params; Daniel@0: if bins(i) 0 & ind < n_bins(j), Daniel@0: D(i, j) = ind + 1; Daniel@0: else Daniel@0: D(i, j) = ind; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: D = D * sparse(diag(1 ./ n_bins)); Daniel@0: Daniel@0: