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