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