Browse code

starting som prediction fine-tuned class-performance visualisation

git-svn-id: https://svn.discofish.de/MATLAB/spmtoolbox/SVMCrossVal@112 83ab2cfd-5345-466c-8aeb-2b2739fb922d

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