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,351 @@
1
+function h=som_recolorbar(p, ticks, scale, labels)
2
+
3
+%SOM_RECOLORBAR Refresh and  rescale colorbars in the current SOM_SHOW fig.
4
+%
5
+% h = som_recolorbar([p], [ticks], [scaling], [labels])
6
+%
7
+%   colormap(jet); som_recolorbar   
8
+%
9
+% Input and output arguments ([]'s are optional) 
10
+%  [p]      (vector) subplot number vector 
11
+%           (string) 'all' (the default), 'comp' to process only
12
+%                    component planes        
13
+%  [ticks]  (string) 'auto' or 'border', default: 'auto'
14
+%           (cell array) p x 1 cell array of p row vectors
15
+%           (vector) the same ticks are applied to all given subplots
16
+%           (scalar) value is at least 2: the number of ticks to show, 
17
+%                    evenly spaced between and including minimum and maximum 
18
+%  [scale]  (string) 'denormalized' or 'normalized' (the default)
19
+%  [labels] (cell array) p x 1 cell array of cells containing strings
20
+%
21
+%  h        (vector) handles to the colorbar objects.
22
+%
23
+% This function refreshes the colorbars in the figure created by SOM_SHOW.
24
+% Refreshing  is necessary if you have changed the colormap.
25
+% Each colorbar has letter 'd' or 'n' and possibly 'u' as label. Letter 'd' means
26
+% that the scale is denormalized, letter 'n' that the scale is
27
+% normalized, and 'u' is for user specified labels.
28
+%
29
+% For more help, try 'type som_recolorbar' or check out online documentation.
30
+% See also SOM_SHOW
31
+ 
32
+%%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33
+%
34
+% som_recolorbar
35
+%
36
+% PURPOSE
37
+% 
38
+% Refreshes the the colorbars in the figure.
39
+%
40
+% SYNTAX
41
+%
42
+%  h = som_recolorbar
43
+%  h = som_recolorbar(p)
44
+%  h = som_recolorbar(p, ticks)
45
+%  h = som_recolorbar(p, ticks, scaling)
46
+%  h = som_recolorbar(p, ticks, scaling, labels)
47
+%
48
+% DESCRIPTION
49
+%
50
+% This function refreshes the colorbars in the figure created by SOM_SHOW.
51
+% Refreshing is necessary if you have changed the colormap.  Each colorbar
52
+% has letter 'd' or 'n' and possibly 'u' as label. Letter 'd' means that the
53
+% scale is denormalized, letter 'n' that the scale is normalized, and 'u' is
54
+% for user specified labels.
55
+%
56
+% Different argument combinations:
57
+%
58
+% 1. Argument 'ticks' has string values:
59
+%  - 'auto' for input argument ticks sets the automatic tick
60
+%     marking on (factory default). 
61
+%  - 'border' sets the tick marks to the color borders. This is 
62
+%     convenient if there are only few colors in use. 
63
+%
64
+%  Argument scale controls the scaling of the tick mark label values. 
65
+%  'normalized' means that the tick mark labels are directly the values 
66
+%  of the ticks, that is, they refer to the map codebook values. 
67
+%  Value 'denormalized' scales the tick mark label values back to the original
68
+%  data scaling. This is made using som_denormalize_data.
69
+%
70
+% 2. Argument 'ticks' is a cell array of vectors:
71
+%  The values are set to be the tick marks to the colorbar specified by p.
72
+%  - if arg. scale is 'normalized' the ticks are set directly to the colorbar.
73
+%  - if arg. scale is 'denormalized' the tick values are first normalized 
74
+%    in the same way as the data.
75
+%
76
+% 3. Argument 'ticks' is a vector
77
+%  As above, but the same values are used for all (given) subplots.
78
+%  
79
+% 4. Argument 'ticks' is a scalar
80
+%  The ticks are set to equally spaced values between (and including)
81
+%  minimum and maximum.
82
+%     
83
+% Argument 'labels' specify user defined labels to the tick marks
84
+%
85
+% NOTE: ticks are rounded to contain three significant digits.
86
+%
87
+% OPTIONAL INPUT ARGUMENTS
88
+% 
89
+%  p        (vector) subplot number vector 
90
+%           (string) 'all' (the default), 'comp' to effect only 
91
+%                    component planes
92
+%
93
+%  ticks    (string) 'auto' or 'border', default: 'auto'
94
+%           (cell array) p x 1 cell array of p row vectors
95
+%           (vector) as the cell array, but the same vector is 
96
+%                    applied to all given subplots
97
+%           (scalar) the number of ticks to show: these are 
98
+%                    evenly space between minimum and maximum
99
+%
100
+%  scale    (string) 'denormalized' or 'normalized' (the default)
101
+%
102
+%  labels   (cell array) p x 1 cell array of cells containing strings
103
+%
104
+% OUTPUT ARGUMENTS
105
+%
106
+%  h        (vector) handles to the colorbar objects.
107
+%
108
+% EXAMPLE
109
+%
110
+%  colormap(jet(5)); som_recolorbar('all','border','denormalized')
111
+%      % Uses five colors and sets the ticks on the color borders.
112
+%      % Tick label values are denormalized back to the original data scaling
113
+%
114
+%  colormap(copper(64));som_recolorbar
115
+%      % changes to colormap copper and resets default ticking and labeling
116
+%
117
+%  som_recolorbar('all',3)
118
+%      % To put 3 ticks to each colorbar so that minimum, mean and
119
+%      % maximum values on the colorbar are shown.
120
+% 
121
+%  som_recolorbar([1 3],{[0.1 0.2 0.3];[0.2 0.4]},'denormalized')
122
+%      % Ticks colorbar 1 by first normalizing values 0.1, 0.2, 0.3 and
123
+%      % then setting the ticks to the colorbar. Labels are of course 
124
+%      % 0.1, 0.2 and 0.3. Ticks colorbar 3 in the same way using values
125
+%      % 0.2 and 0.4.
126
+%
127
+%  som_recolorbar([2 4],{[0.1 0.2];[-1.2 3]},'normalized',{{'1' '2'};{'a' 'b'}})
128
+%      % Ticks colorbar 2 and 4 directly to the specified values. Sets labels
129
+%      % '1' '2' and 'a' 'b' to the ticks.
130
+%
131
+%  som_recolorbar([2 4],{[0.1 0.2];[-1.2 3]},'normalized',{{'1' '2'};{'a' 'b'}})
132
+%      % as previous one, but normalizes tick values first
133
+%
134
+% SEE ALSO
135
+% 
136
+%  som_show        Basic SOM visualization.
137
+%  som_normalize   Normalization operations.
138
+%  som_denormalize Denormalization operations.
139
+
140
+% Copyright (c) 1997-2000 by the SOM toolbox programming team.
141
+% http://www.cis.hut.fi/projects/somtoolbox/             
142
+
143
+% Version 1.0beta Johan 061197 
144
+% Version 2.0beta juuso 151199 130300 160600 181101
145
+
146
+%% Init & check %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147
+
148
+error(nargchk(0, 4, nargin))    % check no. of input args
149
+
150
+% Check the subplot vector p and  get the handles, exit if error
151
+% Default subplot vector is 'all'
152
+
153
+if nargin < 1 | isempty(p)                       % default p
154
+  p= 'all';
155
+end
156
+
157
+% check SOM_SHOW and get the figure data. Exit, if error
158
+
159
+[handles, msg, lattice, msize, dim, normalization, comps]= ...
160
+    vis_som_show_data(p, gcf);
161
+error(msg);                                       
162
+
163
+if nargin < 2 | isempty(ticks)                   % default tick mode is 'auto'
164
+  ticks = 'auto';
165
+elseif isa(ticks,'cell')                         % check for cell
166
+  tickValues = ticks; 
167
+  ticks= 'explicit';
168
+elseif isa(ticks,'double') & length(ticks)>1,
169
+  tickValues = {ticks}; 
170
+  ticks = 'explicit'; 
171
+elseif isa(ticks,'double') & length(ticks)==1,
172
+  tickValues = max(2,round(ticks)); 
173
+  ticks = 'evenspace'; 
174
+end
175
+if ~ischar(ticks)                                % invalid argument
176
+  error('The second argument should be a string or a cell array of vectors.');
177
+end
178
+
179
+switch ticks                                     % check ticks
180
+ case {'auto','border'}, % nill
181
+ case 'evenspace', 
182
+  tickValues_tmp = cell(length(handles),1); 
183
+  for i=1:length(handles), tickValues_tmp{i} = tickValues; end
184
+  tickValues = tickValues_tmp; 
185
+ case 'explicit', 
186
+  if length(tickValues)==1 & length(handles)>1, 
187
+    tickValues_tmp = cell(length(handles),1); 
188
+    for i=1:length(handles), tickValues_tmp{i} = tickValues{1}; end
189
+    tickValues = tickValues_tmp; 
190
+  end
191
+  if length(tickValues) ~= length(handles), 
192
+    error('Cell containing the ticks has wrong size.')
193
+  end
194
+otherwise
195
+  error('''auto'' or ''border'' expected for the second argument.');
196
+end
197
+
198
+if nargin < 3 | isempty(scale)                   % default mode is normalized
199
+  scale= 'normalized';
200
+end
201
+if ~ischar(scale)                                % check scale type
202
+  error('The third argument should be a string.'); 
203
+end
204
+switch scale                                     % check the string
205
+ case { 'normalized', 'denormalized'} % ok
206
+ case 'n', scale = 'normalized'; 
207
+ case 'd', scale = 'denormalized'; 
208
+ otherwise   
209
+  error('''normalized'' or ''denormalized'' expected for the third argument.')
210
+end
211
+
212
+if nargin < 4 | isempty(labels)                  % default is autolabeling
213
+  labels = 'auto';
214
+elseif ~isa(labels,'cell')                       % check type
215
+  error('The fourth argument should be a cell array of cells containing strings.')
216
+else
217
+  labelValues=labels;                            % set labels
218
+  labels = 'explicit';
219
+  if length(labelValues) == length(handles)      % check size
220
+    ;
221
+  else
222
+    error('Cell containing the labels has wrong size')
223
+  end
224
+end
225
+
226
+%% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227
+
228
+n = size(colormap,1)+1;                      % number of colors+1
229
+h_ = zeros(length(handles),1);  
230
+
231
+for i=1:length(handles),                   % MAIN LOOP BEGINS
232
+  axes(handles(i));                        % set axes, refres colorbar and  
233
+  if comps(i)>=0,   
234
+    h_(i)=colorbar;                          % get colorbar handles
235
+
236
+    colorbardir=get(h_(i),'YaxisLocation');
237
+    switch colorbardir                     % get colorbar direction &
238
+     case 'left'                            % set some strings
239
+      Tick='Xtick'; Lim='Xlim'; LabelMode='XTickLabelMode'; Label='XtickLabel';
240
+     case 'right'
241
+      Tick='Ytick'; Lim='Ylim'; LabelMode='YTickLabelMode'; Label='YtickLabel';
242
+     otherwise
243
+      error('Internal error: unknown value for YaxisLocation'); % fatal
244
+    end                                                         
245
+    
246
+    switch ticks                         
247
+     case 'auto'
248
+      set(h_(i),LabelMode,'auto');        % factory default ticking
249
+      tickValues{i}=get(h_(i),Tick);       % get tick values
250
+     case 'border' 
251
+      limit=caxis;                        
252
+      t=linspace(limit(1),limit(2),n);    % set n ticks between min and max 
253
+      t([1 length(t)])=get(h_(i),Lim); % <- caxis is not necerraily the same 
254
+      tickValues{i}=t;                    % as the colorbar min & max values
255
+     case 'evenspace'
256
+      limit = caxis; 
257
+      t = linspace(limit(1),limit(2),tickValues{i}); 
258
+      t([1 length(t)])=get(h_(i),Lim);
259
+      tickValues{i}=t; 
260
+     case 'explicit'
261
+      if comps(i)>0, 
262
+	if strcmp(scale,'normalized')     % normalize tick values
263
+	  tickValues{i} = som_normalize(tickValues{i},normalization{comps(i)});
264
+	end
265
+      end
266
+      
267
+     otherwise 
268
+      error('Internal error: unknown tick type')   % this shouldn't happen
269
+    end
270
+
271
+    %tickValues{i} = epsto0(tickValues{i});
272
+
273
+    switch labels
274
+     case 'auto'
275
+      switch scale                         
276
+       case 'normalized'
277
+	labelValues{i} = round2(tickValues{i});     % use the raw ones 
278
+       case 'denormalized'                 % denormalize tick values
279
+	if comps(i)>0, 
280
+	  labelValues{i} = som_denormalize(tickValues{i},normalization{comps(i)});
281
+	  labelValues{i} = round2(labelValues{i});     % round the scale
282
+	else
283
+	  labelValues{i} = round2(tickValues{i});
284
+	end
285
+       otherwise
286
+	error('Internal error: unknown scale type'); % this shouldn't happen
287
+      end
288
+     case 'explicit'
289
+      ;                                            % they are there already
290
+     otherwise
291
+      error('Internal error: unknown label type'); % this shouldn't happen
292
+    end
293
+
294
+    set(h_(i),Tick,tickValues{i});                 % set ticks and labels
295
+    set(h_(i),Label,labelValues{i});            
296
+    
297
+    if comps(i)>0, 
298
+      % Label the colorbar with letter 'n' if normalized, with letter 'd' 
299
+      % if denormalized and 'u' if the labels are user specified  
300
+      mem_axes=gca; axes(h_(i));
301
+      ch='  ';
302
+      if strcmp(scale,'normalized'),   ch(1)='n'; end
303
+      if strcmp(scale,'denormalized'), ch(1)='d'; end
304
+      if strcmp(labels,'explicit'),    ch(2)='u'; end
305
+      xlabel(ch); 
306
+      axes(mem_axes);
307
+    end
308
+  end
309
+end                                              % MAIN LOOP ENDS 
310
+
311
+
312
+%% Build output %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313
+
314
+if nargout>0
315
+  h=h_;
316
+end
317
+
318
+return; 
319
+
320
+%% Subfunction: ROUND2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321
+% ROUND2 rounds the labels to tol significant digits
322
+
323
+function r=round2(d)
324
+
325
+tol=3;
326
+
327
+zero=(d==0);
328
+d(zero)=1;
329
+k=floor(log10(abs(d)))-(tol-1);
330
+r=round(d./10.^k).*10.^k;
331
+r(zero)=0;
332
+%r=epsto0(r);
333
+
334
+%% Subfunction: ISVECTOR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335
+
336
+function t=isvector(v)
337
+% ISVECTOR checks if a matrix is a vector or not
338
+
339
+t=(ndims(v) == 2 & min(size(v)) == 1) & isnumeric(v);
340
+
341
+%% Subfunction: EPSTO0
342
+
343
+function t=epsto0(t)
344
+% EPSTO0 checks whether first tick value is *very* close to zero, 
345
+% if so sets it to zero.
346
+
347
+if (t(end)-t(1))/t(end) > 1-0.005 & abs(t(1))<1, t(1) = 0; end
348
+
349
+
350
+
351
+