annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_order_cplanes.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 P = som_order_cplanes(sM, varargin)
wolffd@0 2
wolffd@0 3 %SOM_ORDER_CPLANES Orders and shows the SOM component planes.
wolffd@0 4 %
wolffd@0 5 % P = som_order_cplanes(sM, [[argID,] value, ...])
wolffd@0 6 %
wolffd@0 7 % som_order_cplanes(sM);
wolffd@0 8 % som_order_cplanes(sM,'comp',1:30,'simil',C,'pca');
wolffd@0 9 % P = som_order_cplanes(sM);
wolffd@0 10 %
wolffd@0 11 % Input and output arguments ([]'s are optional):
wolffd@0 12 % sM (struct) map or data struct
wolffd@0 13 % (matrix) a data matrix, size * x dim
wolffd@0 14 % [argID, (string) See below. The values which are unambiguous can
wolffd@0 15 % value] (varies) be given without the preceeding argID.
wolffd@0 16 %
wolffd@0 17 % P (matrix) size n x * (typically n x 2), the projection coordinates
wolffd@0 18 %
wolffd@0 19 % Here are the valid argument IDs and corresponding values. The values
wolffd@0 20 % which are unambiguous (marked with '*') can be given without the
wolffd@0 21 % preceeding argID.
wolffd@0 22 % 'comp' (vector) size 1 x n, which components to project, 1:dim by default
wolffd@0 23 % 'simil' *(string) similarity measure to use
wolffd@0 24 % 'corr' linear correlation between component planes
wolffd@0 25 % 'abs(corr)' absolute value of correlation (default)
wolffd@0 26 % 'umat' as 'abs(corr)' but calculated from U-matrices
wolffd@0 27 % 'mutu' mutual information (not implemented yet)
wolffd@0 28 % (matrix) size n x n, a similarity matrix to be used
wolffd@0 29 % 'proj' *(string) projection method to use: 'SOM' (default),
wolffd@0 30 % 'pca', 'sammon', 'cca', 'order', 'ring'
wolffd@0 31 % 'msize' (vector) size of the SOM that is used for projection
wolffd@0 32 % 'show' *(string) how visualization is done: 'planes' (default),
wolffd@0 33 % 'names', or 'none'
wolffd@0 34 % 'mask' (vector) dim x 1, the mask to use, ones(dim,1) by default
wolffd@0 35 % 'comp_names' (cell array) of strings, size dim x 1, the component names
wolffd@0 36 %
wolffd@0 37 % The visualized objects have a callback associated with them: by
wolffd@0 38 % clicking on the object, the index and name of the component are printed
wolffd@0 39 % to the standard output.
wolffd@0 40 %
wolffd@0 41 % See also SOM_SHOW.
wolffd@0 42
wolffd@0 43 % Copyright (c) 2000 by the SOM toolbox programming team.
wolffd@0 44 % Contributed to SOM Toolbox on June 16th, 2000 by Juha Vesanto
wolffd@0 45 % http://www.cis.hut.fi/projects/somtoolbox/
wolffd@0 46
wolffd@0 47 % Version 2.0beta juuso 120600 070601
wolffd@0 48
wolffd@0 49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 50 %% check arguments
wolffd@0 51
wolffd@0 52 % sM
wolffd@0 53 if isstruct(sM),
wolffd@0 54 switch sM.type
wolffd@0 55 case 'som_map',
wolffd@0 56 D = sM.codebook; dim = size(D,2); cnames = sM.comp_names; mask = sM.mask;
wolffd@0 57 ismap = 1;
wolffd@0 58 case 'som_data',
wolffd@0 59 D = sM.data; dim = size(D,2); cnames = sM.comp_names; mask = ones(dim,1);
wolffd@0 60 ismap = 0;
wolffd@0 61 otherwise, error('Invalid first argument.');
wolffd@0 62 end
wolffd@0 63 else
wolffd@0 64 D = sM;
wolffd@0 65 dim = size(D,2); mask = ones(dim,1);
wolffd@0 66 cnames = cell(dim,1);
wolffd@0 67 for i = 1:dim, cnames{i} = sprintf('Variable%d',i); end
wolffd@0 68 ismap = 0;
wolffd@0 69 end
wolffd@0 70
wolffd@0 71 % defaults
wolffd@0 72 comps = 1:dim;
wolffd@0 73 simil = 'abs(corr)';
wolffd@0 74 proj = 'SOM';
wolffd@0 75 show = 'planes';
wolffd@0 76 mapsize = NaN;
wolffd@0 77
wolffd@0 78 % varargin
wolffd@0 79 i=1;
wolffd@0 80 while i<=length(varargin),
wolffd@0 81 argok = 1;
wolffd@0 82 if ischar(varargin{i}),
wolffd@0 83 switch varargin{i},
wolffd@0 84 % argument IDs
wolffd@0 85 case 'mask', i=i+1; mask = varargin{i};
wolffd@0 86 case 'comp_names', i=i+1; cnames = varargin{i};
wolffd@0 87 case 'comp', i=i+1; comps = varargin{i};
wolffd@0 88 case 'proj', i=i+1; proj = varargin{i};
wolffd@0 89 case 'show', i=i+1; show = varargin{i};
wolffd@0 90 case 'simil', i=i+1; simil = varargin{i};
wolffd@0 91 case 'msize', i=i+1; mapsize = varargin{i};
wolffd@0 92 % unambiguous values
wolffd@0 93 case {'corr','abs(corr)','umat','mutu'}, simil = varargin{i};
wolffd@0 94 case {'SOM','pca','sammon','cca','order','ring'}, proj = varargin{i};
wolffd@0 95 case {'planes','names','none'}, show = varargin{i};
wolffd@0 96 otherwise argok=0;
wolffd@0 97 end
wolffd@0 98 else
wolffd@0 99 argok = 0;
wolffd@0 100 end
wolffd@0 101 if ~argok,
wolffd@0 102 disp(['(som_order_cplanes) Ignoring invalid argument #' num2str(i+1)]);
wolffd@0 103 end
wolffd@0 104 i = i+1;
wolffd@0 105 end
wolffd@0 106
wolffd@0 107 if strcmp(show,'planes') & ~ismap,
wolffd@0 108 warning('Given data is not a map: using ''names'' visualization.');
wolffd@0 109 show = 'names';
wolffd@0 110 end
wolffd@0 111
wolffd@0 112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 113 %% similarity matrix
wolffd@0 114
wolffd@0 115 fprintf(1,'Calculating similarity matrix\n');
wolffd@0 116
wolffd@0 117 % use U-matrix
wolffd@0 118 if strcmp(simil,'umat'),
wolffd@0 119 if ~ismap, error('Given data is not a map: cannot use U-matrix similarity.'); end
wolffd@0 120 U = som_umat(sM);
wolffd@0 121 D = zeros(prod(size(U)),dim);
wolffd@0 122 m = zeros(dim,1);
wolffd@0 123 for i=1:dim, m=m*0; m(i)=1; U = som_umat(sM,'mask',m); D(:,i) = U(:); end
wolffd@0 124 end
wolffd@0 125
wolffd@0 126 % components
wolffd@0 127 D = D(:,comps);
wolffd@0 128 cnames = cnames(comps);
wolffd@0 129 mask = mask(comps);
wolffd@0 130 dim = length(comps);
wolffd@0 131
wolffd@0 132 % similarity matrix
wolffd@0 133 if ischar(simil),
wolffd@0 134 switch simil,
wolffd@0 135 case {'corr','abs(corr)','umat'},
wolffd@0 136 A = zeros(dim);
wolffd@0 137 me = zeros(1,dim);
wolffd@0 138 for i=1:dim,
wolffd@0 139 me(i) = mean(D(isfinite(D(:,i)),i)); D(:,i) = D(:,i) - me(i);
wolffd@0 140 end
wolffd@0 141 for i=1:dim,
wolffd@0 142 for j=i:dim,
wolffd@0 143 c = D(:,i).*D(:,j); c = c(isfinite(c));
wolffd@0 144 A(i,j) = sum(c)/length(c); A(j,i) = A(i,j);
wolffd@0 145 end
wolffd@0 146 end
wolffd@0 147 s = diag(A);
wolffd@0 148 A = A./sqrt(s*s');
wolffd@0 149 switch simil,
wolffd@0 150 case {'abs(corr)','umat'}, A = abs(A);
wolffd@0 151 case 'corr', A = A + 1;
wolffd@0 152 end
wolffd@0 153 case 'mutu',
wolffd@0 154 error('Mutual information not implemented yet.');
wolffd@0 155 end
wolffd@0 156 else
wolffd@0 157 A = simil;
wolffd@0 158 end
wolffd@0 159
wolffd@0 160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 161 %% projection
wolffd@0 162
wolffd@0 163 fprintf(1,'Projection\n');
wolffd@0 164
wolffd@0 165 mu = 2*dim;
wolffd@0 166
wolffd@0 167 switch proj,
wolffd@0 168 case 'SOM',
wolffd@0 169
wolffd@0 170 if isnan(mapsize),
wolffd@0 171 sMtmp = som_randinit(A,'munits',mu);
wolffd@0 172 msize = sMtmp.topol.msize;
wolffd@0 173 else
wolffd@0 174 msize = mapsize;
wolffd@0 175 end
wolffd@0 176
wolffd@0 177 sM2 = som_make(A,'msize',msize,'rect','tracking',0);
wolffd@0 178 bm = assign_unique_bm(sM2,A);
wolffd@0 179 Co = som_unit_coords(sM2);
wolffd@0 180 P = Co(bm,:);
wolffd@0 181
wolffd@0 182 case 'ring',
wolffd@0 183
wolffd@0 184 if isnan(mapsize), msize = [1 mu]; else msize = mapsize; end
wolffd@0 185
wolffd@0 186 sM2 = som_make(A,'msize',msize,'cyl','rect','tracking',0);
wolffd@0 187 bm = assign_unique_bm(sM2,A);
wolffd@0 188 Co = som_unit_coords(sM2);
wolffd@0 189 P = Co(bm,[1 3]);
wolffd@0 190
wolffd@0 191 case 'order',
wolffd@0 192
wolffd@0 193 if isnan(mapsize), msize = [1 mu]; else msize = mapsize; end
wolffd@0 194
wolffd@0 195 sM2 = som_make(A,'msize',msize,'tracking',0);
wolffd@0 196 bm = assign_unique_bm(sM2,A);
wolffd@0 197 [dummy,i] = sort(bm);
wolffd@0 198 [dummy,P] = sort(i);
wolffd@0 199 if size(P,2)>1, P = P'; end
wolffd@0 200 if size(P,2)==1, P(:,2) = zeros(length(P),1); end
wolffd@0 201
wolffd@0 202 case {'pca','sammon','cca'},
wolffd@0 203 P = pcaproj(A,2);
wolffd@0 204 if strcmp(proj,'sammon'), P = sammon(A,P,50,'steps');
wolffd@0 205 elseif strcmp(proj,'cca'), P = cca(A,P,50);
wolffd@0 206 end
wolffd@0 207
wolffd@0 208 end
wolffd@0 209
wolffd@0 210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 211 %% visualization
wolffd@0 212
wolffd@0 213 if ~strcmp(show,'none'),
wolffd@0 214 fprintf(1,'Visualization\n');
wolffd@0 215 cla
wolffd@0 216 hold on
wolffd@0 217 if strcmp(show,'planes')
wolffd@0 218 s = findscaling(sM.topol.msize,P);
wolffd@0 219 for i=1:dim,
wolffd@0 220 C = som_normcolor(D(:,i));
wolffd@0 221 if strcmp(simil,'umat'),
wolffd@0 222 h=som_cplane([sM.topol.lattice 'U'],sM.topol.msize,C,1,s*P(i,:));
wolffd@0 223 else
wolffd@0 224 h=som_cplane(sM,C,1,s*P(i,:));
wolffd@0 225 end
wolffd@0 226 set(h,'edgecolor','none','Userdata',sprintf('[%d] %s',i,cnames{i}));
wolffd@0 227 set(h,'ButtonDownFcn','fprintf(1,''%s\n'',get(gco,''UserData''))');
wolffd@0 228 end
wolffd@0 229 else
wolffd@0 230 s=1;
wolffd@0 231 a=[min(P(:,1))-1 max(P(:,1))+1 min(P(:,2))-1-3 max(P(:,2))+1-3];
wolffd@0 232 axis(s*a);
wolffd@0 233 end
wolffd@0 234 h=text(s*P(:,1),s*P(:,2)-3,cnames);
wolffd@0 235 for i=1:length(h), set(h(i),'Userdata',sprintf('[%d] %s',i,cnames{i})); end
wolffd@0 236 set(h,'ButtonDownFcn','fprintf(1,''%s\n'',get(gco,''UserData''))');
wolffd@0 237 hold off
wolffd@0 238
wolffd@0 239 axis on; axis equal; axis tight; set(gca,'XTick',[],'YTick',[],'Box','on');
wolffd@0 240 end
wolffd@0 241
wolffd@0 242 return;
wolffd@0 243
wolffd@0 244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
wolffd@0 245 %% subfunctions
wolffd@0 246
wolffd@0 247 function bm = assign_unique_bm(sM,D)
wolffd@0 248
wolffd@0 249 munits = size(sM.codebook,1);
wolffd@0 250 [dlen dim] = size(D);
wolffd@0 251 margin = max(0,dlen-munits);
wolffd@0 252
wolffd@0 253 [bm,qers] = som_bmus(sM,D);
wolffd@0 254 bmi=ones(dim,1);
wolffd@0 255 hits = som_hits(sM,D);
wolffd@0 256 mult = find(hits>1);
wolffd@0 257 while any(mult) & sum(hits(mult))-length(mult)>margin,
wolffd@0 258 choices = find(bm==mult(1));
wolffd@0 259 while length(choices)>1,
wolffd@0 260 [dummy,mv] = max(qers(choices)); mv = choices(mv);
wolffd@0 261 [mv_to,q] = som_bmus(sM,D(mv,:),bmi(mv));
wolffd@0 262 bmi(mv)=bmi(mv)+1; qers(mv) = q; bm(mv) = mv_to;
wolffd@0 263 choices = find(bm==mv_to);
wolffd@0 264 end
wolffd@0 265 for i=1:length(hits), hits(i)=sum(bm==i); end
wolffd@0 266 mult = find(hits>1);
wolffd@0 267 end
wolffd@0 268 return;
wolffd@0 269
wolffd@0 270 function s = findscaling(msize,P)
wolffd@0 271
wolffd@0 272 d1 = median(abs(diff(unique(sort(P(:,1))))));
wolffd@0 273 d2 = median(abs(diff(unique(sort(P(:,2))))));
wolffd@0 274 if d1>0, s1 = 1.5*msize(2)/d1; else s1 = 0; end
wolffd@0 275 if d2>0, s2 = 1.5*msize(1)/d2; else s2 = 0; end
wolffd@0 276 s = max(s1,s2);
wolffd@0 277 if s==0, s=1; end
wolffd@0 278 return;
wolffd@0 279
wolffd@0 280 function alternative_SOM_plane_vis(sT,bm,simil,D,cnames)
wolffd@0 281
wolffd@0 282 clf
wolffd@0 283 for i=1:size(D,2),
wolffd@0 284 subplot(sT.msize(2),sT.msize(1),bm(i));
wolffd@0 285 if strcmp(simil,'umat'), h=som_cplane([sT.lattice 'U'],sT.msize,D(:,i));
wolffd@0 286 else h=som_cplane(sT,D(:,i));
wolffd@0 287 end
wolffd@0 288 set(h,'edgecolor','none');
wolffd@0 289 title(cnames{i});
wolffd@0 290 axis off
wolffd@0 291 end
wolffd@0 292 return;
wolffd@0 293