0001 function varargout = auto_classify(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 gui_Singleton = 1;
0031 gui_State = struct('gui_Name', mfilename, ...
0032 'gui_Singleton', gui_Singleton, ...
0033 'gui_OpeningFcn', @auto_classify_OpeningFcn, ...
0034 'gui_OutputFcn', @auto_classify_OutputFcn, ...
0035 'gui_LayoutFcn', [] , ...
0036 'gui_Callback', []);
0037 if nargin && ischar(varargin{1})
0038 gui_State.gui_Callback = str2func(varargin{1});
0039 end
0040
0041 if nargout
0042 [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
0043 else
0044 gui_mainfcn(gui_State, varargin{:});
0045 end
0046
0047
0048
0049
0050 function auto_classify_OpeningFcn(hObject, eventdata, handles, varargin)
0051
0052
0053
0054
0055
0056
0057
0058
0059 if isempty(varargin)
0060 handles.matrix2classify = [];
0061 handles.ncepstral = 5;
0062 else
0063 handles.matrix2classify = varargin{1};
0064 handles.ncepstral = varargin{2};
0065 end
0066
0067 handles.distancemeasure = 'sqEuclidean';
0068
0069 handles.cluster_method = 'kmeans';
0070
0071 set(handles.CepstralPopupMenu,'String',num2str([0:handles.ncepstral]'));
0072 set(handles.CepstralPopupMenu,'Value',handles.ncepstral);
0073
0074
0075
0076
0077
0078
0079 guidata(hObject, handles);
0080
0081
0082
0083
0084 uiwait(handles.figure1);
0085
0086
0087 function varargout = auto_classify_OutputFcn(hObject, eventdata, handles)
0088
0089
0090
0091
0092
0093
0094 varargout{1} = handles.output;
0095
0096 close;
0097
0098 function clusters = clusterdata(X,k,dmeasure)
0099
0100
0101 try
0102 clusters = kmeans(X,k,'distance',dmeasure,'EmptyAction','singleton','replicates',30);
0103 catch
0104 clusters = k;
0105 end
0106
0107
0108 function AutoClassifyButton_Callback(hObject, eventdata, handles)
0109
0110
0111
0112
0113 set(handles.AcceptButton,'Enable','off');
0114 guidata(gcbo,handles);
0115
0116 cncepstral = get(handles.CepstralPopupMenu,'Value');
0117
0118 matrix2classify = handles.matrix2classify;
0119 matrix2classify = madnormalize(matrix2classify,1);
0120 matrix2classify = matrix2classify(:,1:cncepstral+1);
0121
0122 if length(matrix2classify(1,:)) > 3
0123 matrix2classify = matrix2classify(:,[1,3:cncepstral+1]);
0124 end
0125
0126 try
0127 segweight = str2num(get(handles.SegLenWeight,'String'));
0128 catch
0129 segweight = 1;
0130 set(handles.SegLenWeight,'String','1');
0131 end
0132
0133
0134 matrix2classify(:,1) = matrix2classify(:,1)*sqrt(segweight);
0135
0136
0137
0138
0139
0140
0141 contents = get(handles.TransformPopupMenu,'String');
0142 value = get(handles.TransformPopupMenu,'Value');
0143
0144 switch contents{value}
0145 case 'None'
0146 matrix2classify = matrix2classify;
0147 case 'Exclude'
0148 matrix2classify = matrix2classify(:,2:length(matrix2classify(1,:)));
0149 case 'Log'
0150 matrix2classify(:,1) = log(matrix2classify(:,1));
0151 case 'Exp'
0152 matrix2classify(:,1) = exp(matrix2classify(:,1));
0153 end
0154
0155 if length(matrix2classify(1,:)) == 0
0156 return
0157 end
0158
0159 rangestate = get(handles.RangeSpecify,'Value');
0160 diagnosticstate = get(handles.DiagnosticCheckbox,'Value');
0161
0162 if strcmp(handles.cluster_method,'hierarchical')
0163 handles.classification = cluster_hierarchical(matrix2classify);
0164 nclusters = length(unique(handles.classification)) - 1;
0165 set(handles.KClasses,'String',nclusters);
0166
0167 if diagnosticstate
0168 pcaplot(matrix2classify,handles.classification);
0169 end
0170
0171 else if strcmp(handles.cluster_method, 'kmeans')
0172
0173 if not(rangestate)
0174
0175 kclassstr = get(handles.KClasses,'String');
0176
0177 if not(isempty(kclassstr))
0178 try
0179 nclasses = str2num(kclassstr);
0180 catch
0181 return;
0182 end
0183
0184 classification = clusterdata(matrix2classify,nclasses,handles.distancemeasure);
0185
0186
0187
0188
0189 if length(classification) == 1
0190 msgbox('Error in clustering. Try changing the number of target clusters or rerunning.');
0191 else
0192 if diagnosticstate
0193 figure();
0194 [silh h] = silhouette(matrix2classify,classification);
0195 pcaplot(matrix2classify,classification);
0196 end
0197 handles.classification = classification;
0198 end
0199 else
0200 return;
0201 end
0202 else
0203 try
0204 minclust = str2num(get(handles.MinClusters,'String'));
0205 catch
0206 return;
0207 end
0208 try
0209 maxclust = str2num(get(handles.MaxClusters,'String'));
0210 catch
0211 return;
0212 end
0213
0214 resultsclust = cell((1+ maxclust)-minclust,1);
0215
0216 hw = waitbar(0,'Clustering data set ');
0217 for i = 1:(1+maxclust)-minclust
0218 resultsclust{i} = clusterdata(matrix2classify,minclust + (i-1),handles.distancemeasure);
0219 if length(resultsclust{i}) == 1
0220 msgbox(['Clustering failed at ' num2str(resultsclust{i}) ' clusters. Try changing the range of clusters or rerunning.']);
0221 return;
0222 end
0223 waitbar(i/(1+maxclust-minclust));
0224 end
0225 close(hw);
0226
0227 silhmean = zeros((1+ maxclust)-minclust,1);
0228
0229 for i = 1:1+(maxclust)-minclust
0230
0231 if diagnosticstate
0232 figure();
0233 [silh h] = silhouette(matrix2classify,resultsclust{i});
0234 pcaplot(matrix2classify,resultsclust{i});
0235 else
0236 silh = silhouette(matrix2classify,resultsclust{i});
0237 end
0238
0239 silhmean(i) = mean(silh);
0240 end
0241
0242 [x I] = max(silhmean);
0243 handles.classification = resultsclust{I(1)};
0244
0245 set(handles.KClasses,'String',num2str(minclust + (I(1)-1)));
0246 end
0247 end
0248 end
0249
0250
0251 set(handles.AcceptButton,'Enable','on');
0252
0253 guidata(gcbo,handles);
0254
0255
0256 function CancelButton_Callback(hObject, eventdata, handles)
0257
0258
0259
0260
0261 handles.output = 0;
0262 guidata(hObject,handles);
0263 uiresume(handles.figure1);
0264
0265
0266 function AcceptButton_Callback(hObject, eventdata, handles)
0267
0268
0269
0270
0271 handles.output = handles.classification;
0272 guidata(hObject,handles);
0273 uiresume(handles.figure1);
0274
0275
0276 function CepstralEdit_Callback(hObject, eventdata, handles)
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286 function CepstralEdit_CreateFcn(hObject, eventdata, handles)
0287
0288
0289
0290
0291
0292
0293 if ispc
0294 set(hObject,'BackgroundColor','white');
0295 else
0296 set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0297 end
0298
0299
0300
0301 function MinClusters_Callback(hObject, eventdata, handles)
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311 function MinClusters_CreateFcn(hObject, eventdata, handles)
0312
0313
0314
0315
0316
0317
0318 if ispc
0319 set(hObject,'BackgroundColor','white');
0320 else
0321 set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0322 end
0323
0324
0325
0326 function MaxClusters_Callback(hObject, eventdata, handles)
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336 function MaxClusters_CreateFcn(hObject, eventdata, handles)
0337
0338
0339
0340
0341
0342
0343 if ispc
0344 set(hObject,'BackgroundColor','white');
0345 else
0346 set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0347 end
0348
0349
0350
0351 function TransformPopupMenu_Callback(hObject, eventdata, handles)
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368 function TransformPopupMenu_CreateFcn(hObject, eventdata, handles)
0369
0370
0371
0372
0373
0374
0375 if ispc
0376 set(hObject,'BackgroundColor','white');
0377 else
0378 set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0379 end
0380
0381 function KClasses_Callback(hObject, eventdata, handles)
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391 function KClasses_CreateFcn(hObject, eventdata, handles)
0392
0393
0394
0395
0396
0397
0398 if ispc
0399 set(hObject,'BackgroundColor','white');
0400 else
0401 set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0402 end
0403
0404
0405
0406 function CepstralPopupMenu_Callback(hObject, eventdata, handles)
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416 function CepstralPopupMenu_CreateFcn(hObject, eventdata, handles)
0417
0418
0419
0420
0421
0422
0423 if ispc
0424 set(hObject,'BackgroundColor','white');
0425 else
0426 set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0427 end
0428
0429
0430
0431
0432 function RangeSpecify_Callback(hObject, eventdata, handles)
0433
0434
0435
0436
0437
0438
0439 state = get(hObject,'Value');
0440
0441 if state
0442 set(handles.KClasses,'Enable','off');
0443 set(handles.text3,'Visible','on');
0444 set(handles.MinClusters,'Visible','on');
0445 set(handles.text4,'Visible','on');
0446 set(handles.MaxClusters,'Visible','on');
0447 else
0448 set(handles.KClasses,'Enable','on');
0449 set(handles.text3,'Visible','off');
0450 set(handles.MinClusters,'Visible','off');
0451 set(handles.text4,'Visible','off');
0452 set(handles.MaxClusters,'Visible','off');
0453 end
0454
0455
0456
0457 function DistancePopupMenu_Callback(hObject, eventdata, handles)
0458
0459
0460
0461
0462
0463
0464
0465 contents = get(hObject,'String');
0466
0467 dstring = contents{get(hObject,'Value')};
0468
0469 switch dstring
0470 case 'Squared Euclidean'
0471 handles.distancemeasure = 'sqEuclidean'
0472 case 'City Block (L1)'
0473 handles.distancemeasure = 'cityblock'
0474 case 'Cosine'
0475 handles.distancemeasure = 'cosine'
0476 case 'Correlation'
0477 handles.distancemeasure = 'correlation'
0478 end
0479
0480 guidata(gcbo,handles);
0481
0482
0483 function DistancePopupMenu_CreateFcn(hObject, eventdata, handles)
0484
0485
0486
0487
0488
0489
0490 if ispc
0491 set(hObject,'BackgroundColor','white');
0492 else
0493 set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0494 end
0495
0496 handles.distancemeasure = 'sqEuclidean';
0497
0498 guidata(gcbo,handles);
0499
0500
0501 function DiagnosticCheckbox_Callback(hObject, eventdata, handles)
0502
0503
0504
0505
0506
0507
0508 function pcaplot(matrix2classify,classification)
0509
0510
0511
0512 nclusters = length(unique(classification));
0513
0514 if not(isempty(find(classification == 0)))
0515 nclusters = nclusters -1;
0516 end
0517
0518 colors = rand(nclusters,3);
0519 mark = mod(1:nclusters,13)+1;
0520
0521 [coef, score] = princomp(matrix2classify);
0522
0523 figure();
0524
0525 for i = 1:length(classification)
0526 h = line(score(i,1),score(i,2));
0527 markers = set(h,'Marker');
0528 if classification(i) == 0
0529 set(h,'Marker',markers{4});
0530 set(h,'Color',[0 0 0]);
0531 else
0532 set(h,'Marker',markers{mark(classification(i))});
0533 set(h,'Color',colors(classification(i),:));
0534 end
0535 end
0536
0537 xlabel('PCA 1');
0538 ylabel('PCA 2');
0539
0540 function classifymatrix = madnormalize(classifymatrix, cols2normalize)
0541 for i = cols2normalize
0542 classifymatrix(:,i) = (classifymatrix(:,i) - median(classifymatrix(:,i))) / mad(classifymatrix(:,i));
0543 end
0544
0545
0546
0547
0548 function c=cluster_aylin(p1,p2);
0549
0550
0551
0552 nn=size(p1,2);
0553 p=std(p1);g=p(p2);p=p';
0554 z=zeros(nn,1);zz=zeros(nn,1);
0555 n=zeros(1,nn);
0556 for i=1:nn
0557 n(i)=max(max(p1(:,p2(:,i))));
0558 end
0559 gg=[1:nn; max(g); n]';n=find(n<1);
0560 [q1,q1]=sort(gg(n,2)');
0561 g=gg(n(q1),1);
0562 t=cputime;
0563 j=1;m=1
0564 for i=1:length(n)
0565 ii=g(i);b=p2(:,ii);a=b;aa=a(find(gg(a,2)<1));
0566 if length([0; unique(z(aa))])<=2;a1=1;a2=0;
0567 while a1~=a2;a1=length(aa);
0568 a=unique(p2(:,a));a=a(find(gg(a,2)<1));
0569 a=unique(a(find(gg(a,2)<=mean(gg(aa,2))+std(gg(aa,2)))));
0570 a=a(find(ismember(a,aa)==0));
0571 if ~isempty(a);aa=[aa;a];
0572 jj=aa(find(z(aa)));
0573 if ~isempty(jj);;
0574 u=unique(z(jj));
0575 if length(u)==1;
0576 zz(aa(find(~z(aa))))=m;z(aa)=u;m=m+1;
0577 end
0578 break;
0579 end
0580 end;a2=length(aa);
0581 end;a=aa;
0582 jj=a(find(z(a)));
0583 if isempty(jj);
0584 z(a)=j;j=j+1;
0585 zz(a)=m;zz(b)=m-.1;zz(ii)=m-.2;m=m+1;
0586 end
0587 end
0588 end
0589 u=unique(z)
0590 u=u(find(u));v=length(u);vv=floor(1/v);if vv;vv=' is';else vv='s are';end
0591 fprintf([int2str(v) ' cluster' vv ' found in ' num2str(cputime-t) 'sec\n\n'])
0592 q0=[];for i=1:v;qq=find(z==u(i));q0=[q0 length(qq)];end;
0593 [q1,q2]=sort(q0);
0594 c=[];for i=1:v;qq=find(z==u(q2(i)));[j,j]=sort(zz(qq));c(v-i+1).c=qq(j);end;
0595 if isempty(c);fprintf('\tNo cluster was found. \n \tscale the data (step size must be 1)\n');end
0596
0597
0598 function [p1,p2]=hneighbors(e);
0599
0600
0601
0602 s='find(';i='abs(e(:,%d)-e(i,%d))<1&';
0603
0604
0605 for j=1:size(e,2);
0606 s=[s sprintf(i,j,j)];
0607 end;s([end end+1])=');';
0608
0609
0610 nn=size(e,1);m=ceil(nn^(1/4));p1=ones(m,nn);p2=kron(ones(m,1),1:nn);
0611 for i=1:nn;
0612 j=eval(s);
0613 [q,qq]=sort(sqrt(sum((e(j,:)-kron(ones(length(j),1),e(i,:)))'.^2)));q=q(find(q<1));mm=length(q);
0614 qq=j(qq(1:mm))';mn=min([m mm]);
0615 p1(1:mn,i)=q(1:mn)';
0616 p2(1:mn,i)=qq(1:mn)';
0617 end
0618
0619
0620 function classification = cluster_hierarchical(matrix2classify)
0621 [p1,p2] = hneighbors(matrix2classify);
0622 c = cluster_aylin(p1,p2);
0623
0624 nclusters = size(c,2);
0625
0626 total_classified = 0
0627
0628 nsegments = size(matrix2classify,1);
0629
0630 classification = zeros(nsegments,1);
0631
0632 for i = 1:nclusters
0633 for j = 1:length(c(i).c)
0634 classification(c(i).c(j)) = i;
0635 end
0636 end
0637 ;
0638
0639
0640
0641 function HierarchicalRadioButton_Callback(hObject, eventdata, handles)
0642
0643
0644
0645
0646
0647
0648 selected = get(hObject,'Value');
0649
0650 if selected
0651 handles.cluster_method = 'hierarchical';
0652 end
0653
0654 guidata(gcbo,handles);
0655
0656
0657
0658 function KmeansRadioButton_Callback(hObject, eventdata, handles)
0659
0660
0661
0662
0663
0664
0665 selected = get(hObject,'Value');
0666
0667 if selected
0668 handles.cluster_method = 'kmeans';
0669 end
0670
0671 guidata(gcbo,handles);
0672
0673
0674
0675
0676 function SegLenWeight_Callback(hObject, eventdata, handles)
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686 function SegLenWeight_CreateFcn(hObject, eventdata, handles)
0687
0688
0689
0690
0691
0692
0693 if ispc
0694 set(hObject,'BackgroundColor','white');
0695 else
0696 set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0697 end
0698
0699