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