comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_clspread.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 base = som_clspread(sM,base,cldist,Ne,verbosity)
2
3 % SOM_CLSPREAD Partition the given data by flooding.
4 %
5 % part = som_clspread(sM,part,cldist,[Ne],[verbos])
6 %
7 % Input and output arguments ([]'s are optional):
8 % sM (struct) map or data struct
9 % (matrix) size dlen x dim, the data set
10 % base (vector) initial partition, where if base(i) is
11 % 0 i should be assigned to some cluster
12 % NaN i should not be assigned to any cluster
13 % otherwise i belongs to cluster base(i)
14 % cldist (string) cluster distance measure: 'single', 'average',
15 % 'complete', 'neighf', 'ward', 'centroid', 'BMU'
16 % [Ne] (scalar) 0 = not constrined to neighborhood
17 % 1 = constrained
18 % (matrix) size dlen x dlen, indicating possible connections
19 % [verbos] (scalar) 1 (default) = show status bar
20 % 0 = don't
21 %
22 % See also SOM_CLDIST.
23
24 % Copyright (c) 2000 by Juha Vesanto
25 % Contributed to SOM Toolbox on XXX by Juha Vesanto
26 % http://www.cis.hut.fi/projects/somtoolbox/
27
28 % Version 2.0beta juuso 220800
29
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31 %% input arguments
32
33 q = 2;
34
35 % map/data
36 if isstruct(sM),
37 switch sM.type,
38 case 'som_map', M = sM.codebook; mask = sM.mask; sT = sM.topol;
39 case 'som_data', M = sM.data; mask = []; sT = [];
40 end
41 else M = sM; mask = []; sT = [];
42 end
43 [dlen dim] = size(M);
44 if isempty(mask), mask = ones(dim,1); end
45
46 % simple option
47 if any(strcmp(cldist,{'closest','BMU'})),
48 i0 = find(base==0);
49 i1 = find(base>0);
50 bmus = som_bmus(M(i1,:),M(i0,:));
51 base(i0) = base(i1(bmus));
52 return;
53 end
54
55 % constrained clustering
56 if nargin<4, Ne = []; end
57 if prod(size(Ne))==1,
58 if Ne & isempty(sT),
59 warning('Cannot use constrained clustering.'); Ne = 0;
60 end
61 if Ne, Ne = som_unit_neighs(sT); else Ne = []; end
62 end
63 if ~isempty(Ne),
64 Ne([0:dlen-1]*dlen+[1:dlen]) = 1; % set diagonal elements = 1
65 if all(Ne(:)>0), Ne = []; end
66 end
67
68 if nargin<5, verbosity = 1; end
69
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 %% initialize
72
73 if size(base,1)==1, base = base'; end
74
75 cid = unique(base(isfinite(base) & base~=0)); % cluster IDs
76 nc = length(cid);
77 uind = find(base==0); % unclustered points
78 nu = length(uind);
79 if nu==0, return; end
80
81 % initial clusters
82 clinds = cell(nc,1); for i=1:nc, clinds{i} = find(base==i); end
83 clinds2 = cell(nu,1); for i=1:nu, clinds2{i} = uind(i); end
84
85 % neighborhood function values
86 if strcmp(cldist,'neighf')
87 if isempty(sT), error('Cannot use neighf linkage.'); end
88 q = som_unit_dists(sT).^2;
89 r = sM.trainhist(end).radius_fin^2;
90 if isnan(r) | isempty(r), r = 1; end
91 switch sM.neigh,
92 case 'bubble', q = (q <= r);
93 case 'gaussian', q = exp(-q/(2*r));
94 case 'cutgauss', q = exp(-q/(2*r)) .* (q <= r);
95 case 'ep', q = (1-q/r) .* (q <= r);
96 end
97 end
98
99 % distance of each cluster to the unclustered points
100 if any(strcmp(cldist,{'single','average','complete','neighf'})),
101 M = som_mdist(M,2,mask,Ne);
102 end
103 Cd = som_cldist(M,clinds,clinds2,cldist,q,mask);
104
105 % check out from Ne which of the clusters are not connected
106 if ~isempty(Ne) & any(strcmp(cldist,{'centroid','ward'})),
107 Clconn = sparse(nc,nu);
108 for i=1:nc, for j=1:nu, Clconn(i,j) = any(any(Ne(clinds{i},uind(j)))); end, end
109 Cd(Clconn==0) = Inf;
110 else
111 Clconn = [];
112 end
113
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 %% action
116
117 if verbosity,
118 nu0 = nu;
119 h = waitbar(1-nu/nu0,'Assigning unclustered points'); % tracking
120 end
121
122 while 1,
123
124 % find closest unclustered point
125 [dk,k] = min(Cd,[],2); % min distance from each unclustered point
126 [d,c] = min(dk); % cluster to which it is assigned
127 k = k(c);
128
129 if ~isfinite(d),
130 break;
131 end
132
133 % add k to cluster c
134 base(uind(k)) = cid(c);
135 clinds{c} = [clinds{c}; uind(k)];
136
137 % remove point k
138 notk = [1:k-1,k+1:nu];
139 nu = nu-1; if nu<=0, break; end
140 Cd = Cd(:,notk);
141 uind = uind(notk);
142 clinds2 = clinds2(notk);
143 if ~isempty(Clconn), Clconn = Clconn(:,notk); end
144
145 % update cluster distances to c
146 Cd(c,:) = som_cldist(M,clinds(c),clinds2,cldist,q,mask);
147 if ~isempty(Clconn),
148 for j=1:nu, Clconn(c,j) = any(any(Ne(clinds{c},uind(j)))); end
149 Cd(c,find(Clconn(c,:)==0)) = Inf;
150 end
151
152 if verbosity, waitbar(1-nu/nu0,h); end % tracking
153
154 end
155 if verbosity, close(h); end
156
157 return;
158
159 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160
161