annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_coloring.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 Col = som_coloring(sM,ncol,chaingap,dw)
Daniel@0 2
Daniel@0 3 % SOM_COLORING Make a SOM-based coloring for given data/map.
Daniel@0 4 %
Daniel@0 5 % Col = som_coloring(sM,[ncol],[chaingap],[dw])
Daniel@0 6 %
Daniel@0 7 % Col = som_coloring(sM,5);
Daniel@0 8 % som_show(sM,'color',Col);
Daniel@0 9 %
Daniel@0 10 % Input and output arguments ([]'s are optional):
Daniel@0 11 % sM (struct) map or data struct
Daniel@0 12 % (matrix) data matrix
Daniel@0 13 % [ncol] (scalar) number of colors to use
Daniel@0 14 % [chaingap] (scalar) size of gap in the color circle (see below),
Daniel@0 15 % 0.1 by default
Daniel@0 16 % [dw] (scalar) 1 = use input space distances to stretch
Daniel@0 17 % the color circle (default)
Daniel@0 18 % 0 = don't use
Daniel@0 19 %
Daniel@0 20 % Col (matrix) color for each data/codebook vector
Daniel@0 21 %
Daniel@0 22 % This function trains a 1-dimensional SOM using the input data
Daniel@0 23 % (codebook of a SOM, or a set of data vectors). A color from the
Daniel@0 24 % color circle (see HSV function) is associated with each map unit,
Daniel@0 25 % and each data/codebook vector of the input data picks its color
Daniel@0 26 % from its BMU on the 1-dimensional SOM.
Daniel@0 27 %
Daniel@0 28 % If the chaingap argument == 0, the 1-dimensional map has a cylinder
Daniel@0 29 % (in effect, a ring) topology. Otherwise, the topology is rectangular
Daniel@0 30 % (in effect, a chain).
Daniel@0 31 %
Daniel@0 32 % The colors are mapped to the 1-dimensional SOM simply by picking colors
Daniel@0 33 % from the color circle. If chaingap>0, a slice of the color circle is
Daniel@0 34 % removed before map units pick their colors from it. This creates a
Daniel@0 35 % discontiuity in the coloring at the ends of the 1-dimensional SOM.
Daniel@0 36 %
Daniel@0 37 % If the dw argument == 0, the colors are picked from the color circle
Daniel@0 38 % equidistantly. If not, the distances between the prototype vectors
Daniel@0 39 % in the 1-dimensional SOM are taken into account.
Daniel@0 40 %
Daniel@0 41 % See also SOM_KMEANSCOLOR, SOM_KMEANSCOLOR2, SOM_FUZZYCOLOR.
Daniel@0 42
Daniel@0 43 % Contributed to SOM Toolbox 2.0, December 21st, 2001 by Juha Vesanto
Daniel@0 44 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 45
Daniel@0 46 % Version 2.0beta juuso 211201
Daniel@0 47
Daniel@0 48 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 49
Daniel@0 50 if isstruct(sM),
Daniel@0 51 if strcmp(sM.type,'som_map'), ismap = 1; D = sM.codebook;
Daniel@0 52 else ismap = 0; D = sM.data;
Daniel@0 53 end
Daniel@0 54 else ismap = 0; D = sM;
Daniel@0 55 end
Daniel@0 56
Daniel@0 57 if nargin<2 | isempty(ncol) | isnan(ncol), ncol = min(64,size(D,1)); end
Daniel@0 58 if nargin<3 | isempty(chaingap) | isnan(chaingap), chaingap = 0.1; end
Daniel@0 59 if nargin<4 | isempty(dw) | isnan(dw), dw = 1; end
Daniel@0 60
Daniel@0 61 if chaingap == 0, lattice = 'sheet'; else lattice = 'cyl'; end
Daniel@0 62 sMring = som_make(D,'msize',[1,ncol],lattice,'tracking',0);
Daniel@0 63 b = som_bmus(sMring,D);
Daniel@0 64
Daniel@0 65 Colmap = hsv(ceil(ncol*(1+chaingap)));
Daniel@0 66 Colmap = Colmap(1:ncol,:);
Daniel@0 67
Daniel@0 68 if dw, % take distances in input space into account
Daniel@0 69 dist = sqrt(sum((sMring.codebook-sMring.codebook([2:end 1],:)).^2,2));
Daniel@0 70 ind = round([0; cumsum(dist)/sum(dist)]*(ncol-1)) + 1;
Daniel@0 71 Colmap = Colmap(ind,:);
Daniel@0 72 end
Daniel@0 73 Col = Colmap(b,:);
Daniel@0 74
Daniel@0 75 return;
Daniel@0 76
Daniel@0 77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 78
Daniel@0 79 % visualization
Daniel@0 80 if ismap,
Daniel@0 81 a = som_bmus(sM.codebook,sMring.codebook);
Daniel@0 82 if chaingap==0, a(end+1) = a(1); end
Daniel@0 83 som_show(sM,'color',Col);
Daniel@0 84 som_show_add('traj',a)
Daniel@0 85 else
Daniel@0 86 i = find(sum(isnan(D),2)==0);
Daniel@0 87 [P,V,me] = pcaproj(D(i,:),2);
Daniel@0 88 Pr = pcaproj(sMring.codebook,V,me);
Daniel@0 89 a = som_bmus(D(i,:),sMring.codebook); % Pr = P(a,:);
Daniel@0 90 som_grid({'rect',[length(i) 1]},'line','none',...
Daniel@0 91 'coord',P,'markercolor',Col(i,:));
Daniel@0 92 hold on
Daniel@0 93 if chaingap==0, Pr(end+1,:) = Pr(1,:); end
Daniel@0 94 som_grid({'rect',[size(Pr,1) 1]},'linecolor','k',...
Daniel@0 95 'linewidth',2,'markercolor','k','coord',Pr);
Daniel@0 96 end
Daniel@0 97
Daniel@0 98