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