00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "common.h"
00020 #include "NormCut.h"
00021 #include "NumericLib.h"
00022
00023 #include <iostream>
00024 #include <cmath>
00025
00026
00027
00028 using std::ostringstream;
00029 using std::cout;
00030 using std::min;
00031
00032 using std::endl;
00033
00034 using namespace Marsyas;
00035
00036 NormCut::NormCut(mrs_string name):MarSystem("NormCut", name)
00037 {
00038 numClusters_ = -1;
00039 paramOffset_ = 0.5;
00040 paramVerbose_ = 3;
00041 paramMaxiterations_ = 20;
00042 paramEigsErrorTolerance_ = 0.000001;
00043
00044 addControls();
00045 }
00046
00047 NormCut::NormCut(const NormCut& a) : MarSystem(a)
00048 {
00049 numClusters_ = a.numClusters_;
00050 paramOffset_ = a.paramOffset_;
00051 paramVerbose_ = a.paramVerbose_;
00052 paramMaxiterations_ = a.paramMaxiterations_;
00053 paramEigsErrorTolerance_ = a.paramEigsErrorTolerance_;
00054
00055 ctrl_numClusters_ = getctrl("mrs_natural/numClusters");
00056 }
00057
00058 NormCut::~NormCut()
00059 {
00060
00061 }
00062
00063 MarSystem*
00064 NormCut::clone() const
00065 {
00066 return new NormCut(*this);
00067 }
00068
00069 void
00070 NormCut::addControls()
00071 {
00072 addctrl("mrs_natural/numClusters", 2, ctrl_numClusters_);
00073 setctrlState("mrs_natural/numClusters", true);
00074
00075 addctrl("mrs_real/offset", 0.5);
00076 setctrlState("mrs_real/offset", true);
00077
00078 addctrl("mrs_natural/verbose", 3);
00079 setctrlState("mrs_natural/verbose", true);
00080
00081 addctrl("mrs_natural/maxIters", 20);
00082 setctrlState("mrs_natural/maxIters", true);
00083
00084 addctrl("mrs_real/eigsErrorTol", 0.000001);
00085 setctrlState("mrs_real/eigsErrorTol", true);
00086 }
00087
00088 void
00089 NormCut::myUpdate(MarControlPtr sender)
00090 {
00091 (void) sender;
00092 ctrl_onSamples_->setValue(ctrl_inSamples_, NOUPDATE);
00093 ctrl_onObservations_->setValue(1, NOUPDATE);
00094 ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00095 ctrl_onObsNames_->setValue("PeakLabels", NOUPDATE);
00096
00097
00098
00099
00100
00101 if(numClusters_ != ctrl_numClusters_->to<mrs_natural>() || onSamples_ != inSamples_)
00102 {
00103 numClusters_ = ctrl_numClusters_->to<mrs_natural>();
00104
00105 nCutDiscrete_.stretch(numClusters_ * ctrl_inObservations_->to<mrs_natural>());
00106 nCutEigVectors_.stretch(numClusters_ * ctrl_inObservations_->to<mrs_natural>());
00107 nCutEigValues_.stretch(numClusters_);
00108 }
00109 }
00110
00111 void
00112 NormCut::myProcess(realvec& in, realvec& out)
00113 {
00114 mrs_natural t,o;
00115
00116 #ifdef MARSYAS_MATLAB
00117 #ifdef MTLB_DBG_LOG
00118 MATLAB_PUT(in, "in");
00119 MATLAB_EVAL("figure(71),imagesc(in',[0 1]),colorbar");
00120 #endif
00121 #endif
00122
00123 if(in.getSize() == 0 || numClusters_ == 0)
00124 {
00125
00126 out.setval(-1.0);
00127 return;
00128 }
00129 if(in.getSize() == 1 || numClusters_ == 0)
00130 {
00131
00132 out.setval(0);
00133 return;
00134 }
00135
00136 out.setval(0);
00137
00138
00139
00140
00141 ncut(inObservations_, in, numClusters_, nCutEigVectors_, nCutDiscrete_ );
00142 discretisation(inObservations_, numClusters_, nCutEigVectors_, nCutDiscrete_ );
00143
00144 for( o=0 ; o<inObservations_ ; o++ )
00145 {
00146 for( t=0 ; t<numClusters_ ; t++ )
00147 {
00148
00149 if( nCutDiscrete_(o*(numClusters_)+t) == 1.0 )
00150 out(0,o) = t;
00151
00152 }
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 }
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 void
00199 NormCut::ncut(mrs_natural n, realvec &W, mrs_natural nbcluster, realvec &NcutEigenvectors, realvec &NcutEigenvalues)
00200 {
00201
00202 realvec dinvsqrt(n);
00203 mrs_real ulp;
00204 realvec P(n*(n));
00205
00206 mrs_real norm;
00207 mrs_real sqrtn = sqrt((mrs_real) n);
00208
00209 nbcluster = min(nbcluster,n);
00210
00211
00212 realvec evals(n);
00213 realvec mrs_naturalerm(n);
00214
00215 mrs_natural i,j;
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 for( i=0 ; i<n ; ++i )
00230 for( j=0 ; j<n ; j++ ){
00231 if( W(i*(n)+j) > 1.0 )
00232 {
00233
00234
00235 MRSWARN("NormCut::ncut() - input values should be <= 1 : delta @(" << i << "," << j << ") = " << W(i*(n)+j)-1.0);
00236 }
00237 if( W(i*(n)+j) != W(j*(n)+i) )
00238 {
00239
00240
00241 MRSWARN("NormCut::ncut - input matrix is not symmetric!");
00242 }
00243 P(i*(n)+j)=0.;
00244 }
00245
00246
00247 ulp = std::numeric_limits<double>::epsilon() * std::numeric_limits<double>::radix;
00248
00249
00250 for( i=0 ; i<n ; ++i )
00251 {
00252
00253 dinvsqrt(i) = 2.*paramOffset_;
00254 for( j=0 ; j<n ; j++ )
00255 dinvsqrt(i) += W(i*(n)+j);
00256 dinvsqrt(i) = 1./sqrt(dinvsqrt(i)+ulp);
00257 MRSASSERT(dinvsqrt(i) == dinvsqrt(i));
00258 }
00259
00260
00261 for( i=0 ; i<n ; ++i )
00262 for( j=i ; j<n ; j++ )
00263 P(i*(n)+j) = dinvsqrt(i)*dinvsqrt(j);
00264
00265
00266
00267 for( j=0 ; j<n ; j++ ){
00268 P(j*(n)+j) = P(j*(n)+j)*( W(j*(n)+j) + paramOffset_ );
00269 for( i=j+1 ; i<n ; ++i ){
00270 P(j*(n)+i) = P(j*(n)+i)*W(j*(n)+i);
00271 }
00272 }
00273
00274 NumericLib::tred2(P, n, evals, mrs_naturalerm );
00275 NumericLib::tqli( evals, mrs_naturalerm, n, P );
00276
00277
00278
00279
00280
00281
00282 for( j=0 ; j<nbcluster ; j++ )
00283 {
00284 for( i=0 ; i<n ; ++i )
00285 {
00286 NcutEigenvectors(j*(n)+i) = P((n-j-1)*(n)+i);
00287 MRSASSERT(NcutEigenvectors(j*n + i) == NcutEigenvectors(j*n + i));
00288 }
00289 NcutEigenvalues(j) = evals(n-j-1);
00290 }
00291
00292 for( j=0 ; j<nbcluster ; j++ )
00293 for( i=0 ; i<n ; ++i )
00294 {
00295 NcutEigenvectors(j*(n)+i) = NcutEigenvectors(j*(n)+i)*dinvsqrt(i);
00296 MRSASSERT(NcutEigenvectors(j*n + i) == NcutEigenvectors(j*n + i));
00297 }
00298
00299
00300 for( j=0 ; j<nbcluster ; j++ ){
00301 norm=0.;
00302 for( i=0 ; i<n ; ++i )
00303 norm += NcutEigenvectors(j*(n) + i)*NcutEigenvectors(j*(n) + i);
00304 norm = sqrt(norm);
00305 norm = sqrtn / norm;
00306 if( NcutEigenvectors(j*(n)) >= 0. )
00307 for( i=0 ; i<n ; ++i )
00308 {
00309 NcutEigenvectors(j*(n) + i) *= -norm;
00310 MRSASSERT(NcutEigenvectors(j*n + i) == NcutEigenvectors(j*n + i));
00311 }
00312
00313 else
00314 for( i=0 ; i<n ; ++i )
00315 {
00316 NcutEigenvectors(j*(n) + i) *= norm;
00317 MRSASSERT(NcutEigenvectors(j*n + i) == NcutEigenvectors(j*n + i));
00318 }
00319
00320 }
00321
00322 }
00323
00324 void NormCut::discretisation(mrs_natural n, mrs_natural nbcluster, realvec &NcutEigenvectors, realvec &NcutDiscrete)
00325 {
00326 realvec vm(n);
00327 realvec R(nbcluster*(nbcluster));
00328 realvec EVtimesR(n*(nbcluster));
00329 realvec c(n);
00330 realvec tmp(n);
00331 double minval;
00332 int mini = 0;
00333 realvec EVDtimesEV(nbcluster*(nbcluster));
00334 double NcutValue;
00335 double lastObjectiveValue=0;
00336
00337
00338 realvec s(nbcluster+1);
00339 realvec U(nbcluster*(nbcluster));
00340 realvec V(nbcluster*(nbcluster));
00341
00342
00343 double ulp = std::numeric_limits<double>::epsilon() * std::numeric_limits<double>::radix;
00344
00345 int nbIterDiscrete = 0;
00346 bool exitLoop = false;
00347
00348 int randn;
00349 int i,j,k;
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363 for( i=0 ; i<n ; ++i ){
00364 vm(i) = 0;
00365 for( j=0 ; j<nbcluster ; j++ )
00366 {
00367 vm(i) += NcutEigenvectors(j*(n)+i)*NcutEigenvectors(j*(n)+i);
00368 MRSASSERT(vm(i) == vm(i));
00369 }
00370 vm(i) = sqrt(vm(i));
00371 for( j=0 ; j<nbcluster ; j++ ){
00372 if (vm(i) > 0)
00373 NcutEigenvectors(j*(n)+i) /= vm(i);
00374 else
00375 NcutEigenvectors(j*(n)+i) = 0.;
00376 MRSASSERT(NcutEigenvectors(j*(n)+i) == NcutEigenvectors(j*(n)+i));
00377 }
00378 c(i) = 0;
00379 }
00380
00381
00382 randn = 0;
00383
00384 for( i=0 ; i<nbcluster ; ++i ){
00385 R(i) = NcutEigenvectors(i*(n)+randn);
00386 MRSASSERT(R(i) == R(i));
00387 for( j=0 ; j<nbcluster ; j++ )
00388 U(i*(nbcluster)+j) = 0.0;
00389 }
00390
00391 for( j=1 ; j<nbcluster ; j++ )
00392 {
00393 minval = MAXREAL;
00394
00395 for( i=0 ; i<n ; ++i )
00396 {
00397 tmp(i) = 0.;
00398 for( k=0 ; k<nbcluster ; k++ )
00399 tmp(i) += NcutEigenvectors(k*(n)+i)*R((j-1)*(nbcluster)+k);
00400 }
00401
00402 for( i=0 ; i<n ; ++i ){
00403 c(i) += fabs(tmp(i));
00404 if( c(i) < minval )
00405 {
00406 minval = c(i);
00407 mini = i;
00408 }
00409 }
00410
00411 for( i=0 ; i<nbcluster ; ++i ){
00412 R(j*(nbcluster)+i) = NcutEigenvectors(i*(n) + mini);
00413 MRSASSERT(R(j*(nbcluster)+i) == R(j*(nbcluster)+i));
00414 }
00415 }
00416
00417
00418
00419 while( !exitLoop )
00420 {
00421 nbIterDiscrete += 1;
00422
00423 for( i=0 ; i<n ; ++i ){
00424 for( j=0 ; j<nbcluster ; j++ ){
00425 EVtimesR(j*(n)+i) = 0.;
00426 for( k=0 ; k<nbcluster ; k++)
00427 {
00428 EVtimesR(j*(n)+i) += NcutEigenvectors(k*(n)+i)*R(j*(nbcluster)+k);
00429 MRSASSERT(EVtimesR(j*(n)+i) == EVtimesR(j*(n)+i));
00430 }
00431 }
00432 }
00433
00434 discretisationEigenvectorData(n, nbcluster, EVtimesR,NcutDiscrete);
00435
00436 for( i=0 ; i<nbcluster ; ++i ){
00437 for( j=0 ; j<nbcluster ; j++ ){
00438 EVDtimesEV(j*(nbcluster)+i) = 0.;
00439 for( k=0 ; k<n ; k++)
00440 {
00441 EVDtimesEV(j*(nbcluster)+i) += NcutDiscrete(k*(nbcluster)+i)*NcutEigenvectors(j*(n)+k);
00442 MRSASSERT(EVDtimesEV(j*(nbcluster)+i) == EVDtimesEV(j*(nbcluster)+i));
00443 }
00444 }
00445 }
00446
00447
00448
00449
00450
00451 NumericLib::svd( nbcluster, nbcluster, EVDtimesEV, U, V, s );
00452
00453
00454 NcutValue = 0.;
00455 for( i=0 ; i<nbcluster ; ++i )
00456 NcutValue += s(i);
00457 NcutValue = 2*( n - NcutValue );
00458
00459 if( fabs(NcutValue - lastObjectiveValue) < ulp || nbIterDiscrete > paramMaxiterations_ )
00460 {
00461 exitLoop = 1;
00462 }
00463 else
00464 {
00465 lastObjectiveValue = NcutValue;
00466
00467 for( i=0 ; i<nbcluster ; ++i ){
00468 for( j=0 ; j<nbcluster ; j++ ){
00469 R(j*(nbcluster)+i) = 0.;
00470 for( k=0 ; k<nbcluster ; k++)
00471 R(j*(nbcluster)+i) += V(k*(nbcluster)+i)*U(k*(nbcluster)+j);
00472 }
00473 }
00474
00475 }
00476 }
00477 }
00478
00479 void
00480 NormCut::discretisationEigenvectorData(mrs_natural n, mrs_natural nbcluster, realvec &V, realvec &Vdiscrete)
00481 {
00482 mrs_real maxval;
00483 mrs_natural maxi = 0;
00484
00485 mrs_natural i,j;
00486
00487 for( i=0 ; i<n ; ++i ){
00488 maxval = -MAXREAL;
00489
00490
00491 for( j=0 ; j<nbcluster ; j++ )
00492 {
00493
00494 Vdiscrete(i*(nbcluster)+j) = 0.0;
00495
00496 if( V(j*(n)+i) > maxval )
00497 {
00498 maxval = V(j*(n)+i);
00499 maxi = j;
00500 }
00501 }
00502
00503 Vdiscrete(i*(nbcluster)+maxi) = 1.0;
00504
00505 }
00506 }
00507
00508
00509
00510
00511
00512
00513
00514 void
00515 NormCut::prmrs_natural( realvec &A, mrs_natural m , mrs_natural n )
00516 {
00517 mrs_natural i,j;
00518
00519 if( n > 0 )
00520 {
00521 for(i=0; i < m; ++i)
00522 {
00523 for (j=0; j < n; j++)
00524 cout << A(j*m+i) << "\t";
00525 cout << endl;
00526 }
00527 }
00528 else if( n == -1 )
00529 {
00530 for( i=0 ; i<m ; ++i )
00531 cout << A(i) << "\t";
00532 cout << endl; }
00533 }
00534
00535
00536
00537
00538
00539
00540
00541 void
00542 NormCut::print( realvec &A, int m , int n )
00543 {
00544 int i,j;
00545
00546 if( n > 0 )
00547 {
00548 for(i=0; i < m; ++i)
00549 {
00550 for (j=0; j < n; j++)
00551 cout << A(j*m+i) << "\t";
00552 cout << endl;
00553 }
00554 }
00555 else if( n == -1 )
00556 {
00557 for( i=0 ; i<m ; ++i )
00558 cout << A(i) << "\t";
00559 cout << endl; }
00560 }
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705