Mercurial > hg > camir-aes2014
comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_order_cplanes.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 P = som_order_cplanes(sM, varargin) | |
2 | |
3 %SOM_ORDER_CPLANES Orders and shows the SOM component planes. | |
4 % | |
5 % P = som_order_cplanes(sM, [[argID,] value, ...]) | |
6 % | |
7 % som_order_cplanes(sM); | |
8 % som_order_cplanes(sM,'comp',1:30,'simil',C,'pca'); | |
9 % P = som_order_cplanes(sM); | |
10 % | |
11 % Input and output arguments ([]'s are optional): | |
12 % sM (struct) map or data struct | |
13 % (matrix) a data matrix, size * x dim | |
14 % [argID, (string) See below. The values which are unambiguous can | |
15 % value] (varies) be given without the preceeding argID. | |
16 % | |
17 % P (matrix) size n x * (typically n x 2), the projection coordinates | |
18 % | |
19 % Here are the valid argument IDs and corresponding values. The values | |
20 % which are unambiguous (marked with '*') can be given without the | |
21 % preceeding argID. | |
22 % 'comp' (vector) size 1 x n, which components to project, 1:dim by default | |
23 % 'simil' *(string) similarity measure to use | |
24 % 'corr' linear correlation between component planes | |
25 % 'abs(corr)' absolute value of correlation (default) | |
26 % 'umat' as 'abs(corr)' but calculated from U-matrices | |
27 % 'mutu' mutual information (not implemented yet) | |
28 % (matrix) size n x n, a similarity matrix to be used | |
29 % 'proj' *(string) projection method to use: 'SOM' (default), | |
30 % 'pca', 'sammon', 'cca', 'order', 'ring' | |
31 % 'msize' (vector) size of the SOM that is used for projection | |
32 % 'show' *(string) how visualization is done: 'planes' (default), | |
33 % 'names', or 'none' | |
34 % 'mask' (vector) dim x 1, the mask to use, ones(dim,1) by default | |
35 % 'comp_names' (cell array) of strings, size dim x 1, the component names | |
36 % | |
37 % The visualized objects have a callback associated with them: by | |
38 % clicking on the object, the index and name of the component are printed | |
39 % to the standard output. | |
40 % | |
41 % See also SOM_SHOW. | |
42 | |
43 % Copyright (c) 2000 by the SOM toolbox programming team. | |
44 % Contributed to SOM Toolbox on June 16th, 2000 by Juha Vesanto | |
45 % http://www.cis.hut.fi/projects/somtoolbox/ | |
46 | |
47 % Version 2.0beta juuso 120600 070601 | |
48 | |
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
50 %% check arguments | |
51 | |
52 % sM | |
53 if isstruct(sM), | |
54 switch sM.type | |
55 case 'som_map', | |
56 D = sM.codebook; dim = size(D,2); cnames = sM.comp_names; mask = sM.mask; | |
57 ismap = 1; | |
58 case 'som_data', | |
59 D = sM.data; dim = size(D,2); cnames = sM.comp_names; mask = ones(dim,1); | |
60 ismap = 0; | |
61 otherwise, error('Invalid first argument.'); | |
62 end | |
63 else | |
64 D = sM; | |
65 dim = size(D,2); mask = ones(dim,1); | |
66 cnames = cell(dim,1); | |
67 for i = 1:dim, cnames{i} = sprintf('Variable%d',i); end | |
68 ismap = 0; | |
69 end | |
70 | |
71 % defaults | |
72 comps = 1:dim; | |
73 simil = 'abs(corr)'; | |
74 proj = 'SOM'; | |
75 show = 'planes'; | |
76 mapsize = NaN; | |
77 | |
78 % varargin | |
79 i=1; | |
80 while i<=length(varargin), | |
81 argok = 1; | |
82 if ischar(varargin{i}), | |
83 switch varargin{i}, | |
84 % argument IDs | |
85 case 'mask', i=i+1; mask = varargin{i}; | |
86 case 'comp_names', i=i+1; cnames = varargin{i}; | |
87 case 'comp', i=i+1; comps = varargin{i}; | |
88 case 'proj', i=i+1; proj = varargin{i}; | |
89 case 'show', i=i+1; show = varargin{i}; | |
90 case 'simil', i=i+1; simil = varargin{i}; | |
91 case 'msize', i=i+1; mapsize = varargin{i}; | |
92 % unambiguous values | |
93 case {'corr','abs(corr)','umat','mutu'}, simil = varargin{i}; | |
94 case {'SOM','pca','sammon','cca','order','ring'}, proj = varargin{i}; | |
95 case {'planes','names','none'}, show = varargin{i}; | |
96 otherwise argok=0; | |
97 end | |
98 else | |
99 argok = 0; | |
100 end | |
101 if ~argok, | |
102 disp(['(som_order_cplanes) Ignoring invalid argument #' num2str(i+1)]); | |
103 end | |
104 i = i+1; | |
105 end | |
106 | |
107 if strcmp(show,'planes') & ~ismap, | |
108 warning('Given data is not a map: using ''names'' visualization.'); | |
109 show = 'names'; | |
110 end | |
111 | |
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
113 %% similarity matrix | |
114 | |
115 fprintf(1,'Calculating similarity matrix\n'); | |
116 | |
117 % use U-matrix | |
118 if strcmp(simil,'umat'), | |
119 if ~ismap, error('Given data is not a map: cannot use U-matrix similarity.'); end | |
120 U = som_umat(sM); | |
121 D = zeros(prod(size(U)),dim); | |
122 m = zeros(dim,1); | |
123 for i=1:dim, m=m*0; m(i)=1; U = som_umat(sM,'mask',m); D(:,i) = U(:); end | |
124 end | |
125 | |
126 % components | |
127 D = D(:,comps); | |
128 cnames = cnames(comps); | |
129 mask = mask(comps); | |
130 dim = length(comps); | |
131 | |
132 % similarity matrix | |
133 if ischar(simil), | |
134 switch simil, | |
135 case {'corr','abs(corr)','umat'}, | |
136 A = zeros(dim); | |
137 me = zeros(1,dim); | |
138 for i=1:dim, | |
139 me(i) = mean(D(isfinite(D(:,i)),i)); D(:,i) = D(:,i) - me(i); | |
140 end | |
141 for i=1:dim, | |
142 for j=i:dim, | |
143 c = D(:,i).*D(:,j); c = c(isfinite(c)); | |
144 A(i,j) = sum(c)/length(c); A(j,i) = A(i,j); | |
145 end | |
146 end | |
147 s = diag(A); | |
148 A = A./sqrt(s*s'); | |
149 switch simil, | |
150 case {'abs(corr)','umat'}, A = abs(A); | |
151 case 'corr', A = A + 1; | |
152 end | |
153 case 'mutu', | |
154 error('Mutual information not implemented yet.'); | |
155 end | |
156 else | |
157 A = simil; | |
158 end | |
159 | |
160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
161 %% projection | |
162 | |
163 fprintf(1,'Projection\n'); | |
164 | |
165 mu = 2*dim; | |
166 | |
167 switch proj, | |
168 case 'SOM', | |
169 | |
170 if isnan(mapsize), | |
171 sMtmp = som_randinit(A,'munits',mu); | |
172 msize = sMtmp.topol.msize; | |
173 else | |
174 msize = mapsize; | |
175 end | |
176 | |
177 sM2 = som_make(A,'msize',msize,'rect','tracking',0); | |
178 bm = assign_unique_bm(sM2,A); | |
179 Co = som_unit_coords(sM2); | |
180 P = Co(bm,:); | |
181 | |
182 case 'ring', | |
183 | |
184 if isnan(mapsize), msize = [1 mu]; else msize = mapsize; end | |
185 | |
186 sM2 = som_make(A,'msize',msize,'cyl','rect','tracking',0); | |
187 bm = assign_unique_bm(sM2,A); | |
188 Co = som_unit_coords(sM2); | |
189 P = Co(bm,[1 3]); | |
190 | |
191 case 'order', | |
192 | |
193 if isnan(mapsize), msize = [1 mu]; else msize = mapsize; end | |
194 | |
195 sM2 = som_make(A,'msize',msize,'tracking',0); | |
196 bm = assign_unique_bm(sM2,A); | |
197 [dummy,i] = sort(bm); | |
198 [dummy,P] = sort(i); | |
199 if size(P,2)>1, P = P'; end | |
200 if size(P,2)==1, P(:,2) = zeros(length(P),1); end | |
201 | |
202 case {'pca','sammon','cca'}, | |
203 P = pcaproj(A,2); | |
204 if strcmp(proj,'sammon'), P = sammon(A,P,50,'steps'); | |
205 elseif strcmp(proj,'cca'), P = cca(A,P,50); | |
206 end | |
207 | |
208 end | |
209 | |
210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
211 %% visualization | |
212 | |
213 if ~strcmp(show,'none'), | |
214 fprintf(1,'Visualization\n'); | |
215 cla | |
216 hold on | |
217 if strcmp(show,'planes') | |
218 s = findscaling(sM.topol.msize,P); | |
219 for i=1:dim, | |
220 C = som_normcolor(D(:,i)); | |
221 if strcmp(simil,'umat'), | |
222 h=som_cplane([sM.topol.lattice 'U'],sM.topol.msize,C,1,s*P(i,:)); | |
223 else | |
224 h=som_cplane(sM,C,1,s*P(i,:)); | |
225 end | |
226 set(h,'edgecolor','none','Userdata',sprintf('[%d] %s',i,cnames{i})); | |
227 set(h,'ButtonDownFcn','fprintf(1,''%s\n'',get(gco,''UserData''))'); | |
228 end | |
229 else | |
230 s=1; | |
231 a=[min(P(:,1))-1 max(P(:,1))+1 min(P(:,2))-1-3 max(P(:,2))+1-3]; | |
232 axis(s*a); | |
233 end | |
234 h=text(s*P(:,1),s*P(:,2)-3,cnames); | |
235 for i=1:length(h), set(h(i),'Userdata',sprintf('[%d] %s',i,cnames{i})); end | |
236 set(h,'ButtonDownFcn','fprintf(1,''%s\n'',get(gco,''UserData''))'); | |
237 hold off | |
238 | |
239 axis on; axis equal; axis tight; set(gca,'XTick',[],'YTick',[],'Box','on'); | |
240 end | |
241 | |
242 return; | |
243 | |
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5 | |
245 %% subfunctions | |
246 | |
247 function bm = assign_unique_bm(sM,D) | |
248 | |
249 munits = size(sM.codebook,1); | |
250 [dlen dim] = size(D); | |
251 margin = max(0,dlen-munits); | |
252 | |
253 [bm,qers] = som_bmus(sM,D); | |
254 bmi=ones(dim,1); | |
255 hits = som_hits(sM,D); | |
256 mult = find(hits>1); | |
257 while any(mult) & sum(hits(mult))-length(mult)>margin, | |
258 choices = find(bm==mult(1)); | |
259 while length(choices)>1, | |
260 [dummy,mv] = max(qers(choices)); mv = choices(mv); | |
261 [mv_to,q] = som_bmus(sM,D(mv,:),bmi(mv)); | |
262 bmi(mv)=bmi(mv)+1; qers(mv) = q; bm(mv) = mv_to; | |
263 choices = find(bm==mv_to); | |
264 end | |
265 for i=1:length(hits), hits(i)=sum(bm==i); end | |
266 mult = find(hits>1); | |
267 end | |
268 return; | |
269 | |
270 function s = findscaling(msize,P) | |
271 | |
272 d1 = median(abs(diff(unique(sort(P(:,1)))))); | |
273 d2 = median(abs(diff(unique(sort(P(:,2)))))); | |
274 if d1>0, s1 = 1.5*msize(2)/d1; else s1 = 0; end | |
275 if d2>0, s2 = 1.5*msize(1)/d2; else s2 = 0; end | |
276 s = max(s1,s2); | |
277 if s==0, s=1; end | |
278 return; | |
279 | |
280 function alternative_SOM_plane_vis(sT,bm,simil,D,cnames) | |
281 | |
282 clf | |
283 for i=1:size(D,2), | |
284 subplot(sT.msize(2),sT.msize(1),bm(i)); | |
285 if strcmp(simil,'umat'), h=som_cplane([sT.lattice 'U'],sT.msize,D(:,i)); | |
286 else h=som_cplane(sT,D(:,i)); | |
287 end | |
288 set(h,'edgecolor','none'); | |
289 title(cnames{i}); | |
290 axis off | |
291 end | |
292 return; | |
293 |