annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_colorcode.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function colors=som_colorcode(m, colorcode, scaling)
Daniel@0 2
Daniel@0 3 %SOM_COLORCODE Calculates a heuristic color coding for the SOM grid
Daniel@0 4 %
Daniel@0 5 % colors = som_colorcode(m, colorcode, scaling)
Daniel@0 6 %
Daniel@0 7 % Input and output arguments ([]'s are optional):
Daniel@0 8 % m (struct) map or topol struct
Daniel@0 9 % (cell array) of form {str,[m1 m2]} where
Daniel@0 10 % str = 'hexa' or 'rect' and [m1 m2] = msize
Daniel@0 11 % (matrix) size N x 2, unit coordinates
Daniel@0 12 % [colorcode] (string) 'rgb1' (default),'rgb2','rgb3','rgb4','hsv'
Daniel@0 13 % [scaling] (scalar) 1=on (default), 0=off. Has effect only
Daniel@0 14 % if m is a Nx2 matrix of coordinates:
Daniel@0 15 % controls whether these are scaled to
Daniel@0 16 % range [0,1] or not.
Daniel@0 17 %
Daniel@0 18 % colors (matrix) size N x 3, RGB colors for each unit (or point)
Daniel@0 19 %
Daniel@0 20 % The function gives a color coding by location for the map grid
Daniel@0 21 % (or arbitrary set of points). Map grid coordinates are always linearly
Daniel@0 22 % normalized to a unit square (x and y coordinates between [0,1]), except
Daniel@0 23 % if m is a Nx2 matrix and scaling=0. In that case too, the coordinates
Daniel@0 24 % must be in range [0,1].
Daniel@0 25 %
Daniel@0 26 % Following heuristic color codings are available:
Daniel@0 27 %
Daniel@0 28 % 'rgb1' slice of RGB-cube so that green - yellow
Daniel@0 29 % the corners have colors: | |
Daniel@0 30 % blue - magenta
Daniel@0 31 %
Daniel@0 32 % 'rgb2' slice of RGB-cube so that red - yellow
Daniel@0 33 % the corners have colors: | |
Daniel@0 34 % blue - cyan
Daniel@0 35 %
Daniel@0 36 % 'rgb3' slice of RGB-cube so that mixed_green - orange
Daniel@0 37 % the corners have colors: | |
Daniel@0 38 % light_blue - pink
Daniel@0 39 %
Daniel@0 40 % 'rgb4' has 'rgb1' on the diagonal + additional colors in corners
Daniel@0 41 % (more resolution but visually strongly discontinuous)
Daniel@0 42 %
Daniel@0 43 % 'hsv' angle and radius from map centre are coded by hue and
Daniel@0 44 % intensity (more resoluton but visually discontinuous)
Daniel@0 45 %
Daniel@0 46 % See also SOM_CPLANE, SOM_SHOW, SOM_CLUSTERCOLOR, SOM_KMEANSCOLOR,
Daniel@0 47 % SOM_BMUCOLOR.
Daniel@0 48
Daniel@0 49 % Contributed to SOM Toolbox 2.0, February 11th, 2000 by Johan Himberg
Daniel@0 50 % Copyright (c) by Johan Himberg
Daniel@0 51 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 52
Daniel@0 53 % Version 2.0 Johan 140799
Daniel@0 54
Daniel@0 55 %%% Check arguments %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 56
Daniel@0 57 error(nargchk(1, 3, nargin)); % check no. of input args is correct
Daniel@0 58
Daniel@0 59 %% Check m: map, topol, cell or data?
Daniel@0 60
Daniel@0 61 if vis_valuetype(m,{'nx2'}),
Daniel@0 62 p=m; % explicit coordinates
Daniel@0 63
Daniel@0 64 else
Daniel@0 65
Daniel@0 66 % map, topol, cell
Daniel@0 67
Daniel@0 68 [tmp,ok,tmp]=som_set(m);
Daniel@0 69 if isstruct(m) & all(ok)
Daniel@0 70 switch m.type
Daniel@0 71 case 'som_topol' % topol
Daniel@0 72 msize=m.msize;
Daniel@0 73 lattice=m.lattice;
Daniel@0 74 case 'som_map'
Daniel@0 75 msize=m.topol.msize; % map
Daniel@0 76 lattice=m.topol.lattice;
Daniel@0 77 otherwise
Daniel@0 78 error('Invalid map or topol struct.');
Daniel@0 79 end
Daniel@0 80
Daniel@0 81 % cell
Daniel@0 82
Daniel@0 83 elseif iscell(m) & vis_valuetype(size(m),{[1 2]}),
Daniel@0 84 if vis_valuetype(m{2},{[1 2]}) & vis_valuetype(m{1},{'string'}),
Daniel@0 85 lattice=m{1};
Daniel@0 86 msize=m{2};
Daniel@0 87 else
Daniel@0 88 error('Invalid map size information.');
Daniel@0 89 end
Daniel@0 90 end
Daniel@0 91
Daniel@0 92 %% Check map parameters
Daniel@0 93
Daniel@0 94 switch lattice % lattice
Daniel@0 95 case 'hexa'
Daniel@0 96 ;
Daniel@0 97 case 'rect'
Daniel@0 98 ;
Daniel@0 99 otherwise
Daniel@0 100 error('Unknown lattice type');
Daniel@0 101 end
Daniel@0 102
Daniel@0 103 if length(msize)>2 % dimension
Daniel@0 104 error('Only 2D maps allowed!');
Daniel@0 105 end
Daniel@0 106
Daniel@0 107
Daniel@0 108 % Calculate coordinates
Daniel@0 109 p=som_unit_coords(msize,lattice,'sheet');
Daniel@0 110
Daniel@0 111 % Set scaling to 1 as it is done always in this case
Daniel@0 112 scaling=1;
Daniel@0 113 end
Daniel@0 114
Daniel@0 115 % Check colorcode
Daniel@0 116
Daniel@0 117 if nargin < 2 | isempty(colorcode),
Daniel@0 118 colorcode='rgb1';
Daniel@0 119 end
Daniel@0 120 if ~ischar(colorcode)
Daniel@0 121 error('String value for colorcode mode expected.');
Daniel@0 122 else
Daniel@0 123 switch colorcode
Daniel@0 124 case { 'rgb1', 'rgb2', 'rgb3' , 'rgb4' ,'hsv'}
Daniel@0 125 otherwise
Daniel@0 126 error([ 'Colorcode mode ' colorcode ' not implemented.']);
Daniel@0 127 end
Daniel@0 128 end
Daniel@0 129
Daniel@0 130 % Check scaling
Daniel@0 131
Daniel@0 132 if nargin < 3 | isempty(scaling)
Daniel@0 133 scaling=1;
Daniel@0 134 end
Daniel@0 135
Daniel@0 136 if ~vis_valuetype(scaling,{'1x1'})
Daniel@0 137 error('Scaling should be 0 (off) or 1 (on).');
Daniel@0 138 end
Daniel@0 139
Daniel@0 140 %% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 141
Daniel@0 142 % scale coordintes between [0,1]
Daniel@0 143
Daniel@0 144 if scaling
Daniel@0 145 n=size(p,1);
Daniel@0 146 mn=min(p);
Daniel@0 147 e=max(p)-mn;
Daniel@0 148 p=(p-repmat(mn,n,1))./repmat(e,n,1);
Daniel@0 149 elseif sum(p(:,1)>1+p(:,1)<0+p(:,2)>1+p(:,2)<0),
Daniel@0 150 error('Coordinates out of range [0,1].');
Daniel@0 151 end
Daniel@0 152
Daniel@0 153 switch colorcode
Daniel@0 154 case 'rgb1'
Daniel@0 155 h(:,1)=p(:,1);
Daniel@0 156 h(:,2)=1-p(:,2);
Daniel@0 157 h(:,3)=p(:,2);
Daniel@0 158 case 'rgb2'
Daniel@0 159 h(:,1)=p(:,1);
Daniel@0 160 h(:,2)=1-p(:,2);
Daniel@0 161 h(:,3)=1-p(:,1);
Daniel@0 162 case 'rgb3'
Daniel@0 163 h(:,1)=p(:,1);
Daniel@0 164 h(:,2)=.5;
Daniel@0 165 h(:,3)=p(:,2);
Daniel@0 166 case 'rgb4'
Daniel@0 167 p=rgb4(p);
Daniel@0 168 h(:,1)=p(:,1);
Daniel@0 169 h(:,2)=1-p(:,2);
Daniel@0 170 h(:,3)=p(:,3);
Daniel@0 171 case 'hsv'
Daniel@0 172 munits = n;
Daniel@0 173 Hsv = zeros(munits,3);
Daniel@0 174 for i=1:n,
Daniel@0 175 dx = .5-p(i,1);
Daniel@0 176 dy = .5-p(i,2);
Daniel@0 177 r = sqrt(dx^2+dy^2);
Daniel@0 178 if r==0,
Daniel@0 179 h=1;
Daniel@0 180 elseif dx==0,
Daniel@0 181 h=.5; %h=ay;
Daniel@0 182 elseif dy==0,
Daniel@0 183 h=.5; %h=ax;
Daniel@0 184 else
Daniel@0 185 h = min(abs(.5/(dx/r)),abs(.5/(dy/r)));
Daniel@0 186 end
Daniel@0 187
Daniel@0 188 if r==0,
Daniel@0 189 angle = 0;
Daniel@0 190 else
Daniel@0 191 angle = acos(dx/r);
Daniel@0 192 if dy<0,
Daniel@0 193 angle = 2*pi-angle;
Daniel@0 194 end
Daniel@0 195 end
Daniel@0 196
Daniel@0 197 Hsv(i,1) = 1-sin(angle/4);
Daniel@0 198 Hsv(i,2) = 1;
Daniel@0 199 Hsv(i,3) = r/h;
Daniel@0 200 h = hsv2rgb(Hsv);
Daniel@0 201 end
Daniel@0 202 end
Daniel@0 203
Daniel@0 204
Daniel@0 205 %% Build output %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 206
Daniel@0 207 colors=h;
Daniel@0 208
Daniel@0 209 %% Subfunctions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% juha %%%%
Daniel@0 210
Daniel@0 211 function p=rgb4(coord)
Daniel@0 212
Daniel@0 213 for i=1:size(coord,1);
Daniel@0 214 p(i,:)=get_coords(coord(i,:))';
Daniel@0 215 end
Daniel@0 216
Daniel@0 217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 218
Daniel@0 219 function coords=get_coords(coords)
Daniel@0 220
Daniel@0 221 %GET_COORDS
Daniel@0 222 %
Daniel@0 223 % get_coords(coords)
Daniel@0 224 %
Daniel@0 225 % ARGUMENTS
Daniel@0 226 %
Daniel@0 227 % coords (1x2 or 2x1 vector) coords(1) is an x-coordinate and coords(2)
Daniel@0 228 % y-coordinate.
Daniel@0 229 %
Daniel@0 230 %
Daniel@0 231 % RETURNS
Daniel@0 232 %
Daniel@0 233 % coords (3x1 vector) x,y and z-coordinates.
Daniel@0 234 %
Daniel@0 235
Daniel@0 236 if ~(all(size(coords) == [1 2]) | all(size(coords) == [2 1]))
Daniel@0 237 error('Argument ''coords'' must be an 2x1 or 1x2 vector.');
Daniel@0 238 end
Daniel@0 239
Daniel@0 240 if all(size(coords) == [1 2])
Daniel@0 241 coords=coords';
Daniel@0 242 end
Daniel@0 243
Daniel@0 244 if any(coords > 1) any(coords < 0)
Daniel@0 245 error('Coordinates must lay inside the interval [0,1].');
Daniel@0 246 end
Daniel@0 247
Daniel@0 248 if coords(1) <= 1/(sqrt(2)+1),
Daniel@0 249 if coords(2) <= line3(coords(1))
Daniel@0 250 coords=coords_in_base(4,coords);
Daniel@0 251 elseif coords(2) <= line2(coords(1))
Daniel@0 252 coords=coords_in_base(1,coords);
Daniel@0 253 else
Daniel@0 254 coords=coords_in_base(2,coords);
Daniel@0 255 end
Daniel@0 256 elseif coords(1) <= sqrt(2)/(sqrt(2)+1)
Daniel@0 257 if coords(2) <= line1(coords(1))
Daniel@0 258 coords=coords_in_base(3,coords);
Daniel@0 259 elseif coords(2) <= line2(coords(1))
Daniel@0 260 coords=coords_in_base(1,coords);
Daniel@0 261 else
Daniel@0 262 coords=coords_in_base(2,coords);
Daniel@0 263 end
Daniel@0 264 else
Daniel@0 265 if coords(2) <= line1(coords(1)),
Daniel@0 266 coords=coords_in_base(3,coords);
Daniel@0 267 elseif coords(2) <= line4(coords(1))
Daniel@0 268 coords=coords_in_base(1,coords);
Daniel@0 269 else
Daniel@0 270 coords=coords_in_base(5,coords);
Daniel@0 271 end
Daniel@0 272 end
Daniel@0 273
Daniel@0 274
Daniel@0 275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 276
Daniel@0 277 function coords=coords_in_base(base_no,coords)
Daniel@0 278
Daniel@0 279 A=[0;1/(sqrt(2)+1)];
Daniel@0 280 E=[1;1];
Daniel@0 281 F=[0;0];
Daniel@0 282 G=[1;0];
Daniel@0 283 H=[0;1];
Daniel@0 284
Daniel@0 285 const=1+1/sqrt(2);
Daniel@0 286
Daniel@0 287 switch base_no
Daniel@0 288 case 1
Daniel@0 289 x=(coords-A)*const;
Daniel@0 290 coords=[(1/sqrt(2))*(x(1)-x(2));0.5*(x(1)+x(2));0.5*(x(1)+x(2))];
Daniel@0 291 case 2
Daniel@0 292 x=(coords-H)*const;
Daniel@0 293 coords=[0;x(1);1+x(2)];
Daniel@0 294 case 3
Daniel@0 295 x=(coords-G)*const;
Daniel@0 296 coords=[1;1+x(1);x(2)];
Daniel@0 297 case 4
Daniel@0 298 x=(coords-F)*const;
Daniel@0 299 coords=[0.5+(1/sqrt(2))*(x(1)-x(2));...
Daniel@0 300 0.5-(1/sqrt(2))*(x(1)+x(2));...
Daniel@0 301 0];
Daniel@0 302 case 5
Daniel@0 303 x=(coords-E)*const;
Daniel@0 304 coords=[0.5+(1/sqrt(2))*(x(1)-x(2));...
Daniel@0 305 0.5-(1/sqrt(2))*(x(1)+x(2));...
Daniel@0 306 1];
Daniel@0 307 end
Daniel@0 308
Daniel@0 309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 310
Daniel@0 311 function y=line1(x)
Daniel@0 312
Daniel@0 313 y = x-1/(sqrt(2)+1);
Daniel@0 314
Daniel@0 315 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 316
Daniel@0 317 function y=line2(x)
Daniel@0 318
Daniel@0 319 y = x+1/(sqrt(2)+1);
Daniel@0 320
Daniel@0 321 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 322
Daniel@0 323 function y=line3(x)
Daniel@0 324
Daniel@0 325 y = -x+1/(sqrt(2)+1);
Daniel@0 326
Daniel@0 327 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 328
Daniel@0 329 function y= line4(x)
Daniel@0 330
Daniel@0 331 y = -x+(2*sqrt(2)+1)/(sqrt(2)+1);
Daniel@0 332
Daniel@0 333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%