comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_coloring.m @ 0:e9a9cd732c1e tip

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