/usr/include/vigra/hierarchical_clustering.hxx is in libvigraimpex-dev 1.10.0+git20160211.167be93+dfsg-5ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 | /************************************************************************/
/* */
/* Copyright 2011 by Ullrich Koethe */
/* */
/* This file is part of the VIGRA computer vision library. */
/* The VIGRA Website is */
/* http://hci.iwr.uni-heidelberg.de/vigra/ */
/* Please direct questions, bug reports, and contributions to */
/* ullrich.koethe@iwr.uni-heidelberg.de or */
/* vigra@informatik.uni-hamburg.de */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
#ifndef VIGRA_HIERARCHICAL_CLUSTERING_HXX
#define VIGRA_HIERARCHICAL_CLUSTERING_HXX
/*std*/
#include <queue>
#include <iomanip>
/*vigra*/
#include "priority_queue.hxx"
#include "metrics.hxx"
namespace vigra{
namespace cluster_operators{
template<
class MERGE_GRAPH,
class EDGE_INDICATOR_MAP,
class EDGE_SIZE_MAP,
class NODE_SIZE_MAP,
class MIN_WEIGHT_MAP
>
class EdgeWeightedUcm
{
typedef EdgeWeightedUcm<
MERGE_GRAPH,
EDGE_INDICATOR_MAP,
EDGE_SIZE_MAP,
NODE_SIZE_MAP,
MIN_WEIGHT_MAP
> SelfType;
public:
typedef typename EDGE_INDICATOR_MAP::Value ValueType;
typedef ValueType WeightType;
typedef MERGE_GRAPH MergeGraph;
typedef typename MergeGraph::Graph Graph;
typedef typename Graph::Edge BaseGraphEdge;
typedef typename Graph::Node BaseGraphNode;
typedef typename MergeGraph::Edge Edge;
typedef typename MergeGraph::Node Node;
typedef typename MergeGraph::EdgeIt EdgeIt;
typedef typename MergeGraph::NodeIt NodeIt;
typedef typename MergeGraph::IncEdgeIt IncEdgeIt;
typedef typename MergeGraph::index_type index_type;
typedef MergeGraphItemHelper<MergeGraph,Edge> EdgeHelper;
typedef MergeGraphItemHelper<MergeGraph,Node> NodeHelper;
typedef typename MergeGraph::MergeNodeCallBackType MergeNodeCallBackType;
typedef typename MergeGraph::MergeEdgeCallBackType MergeEdgeCallBackType;
typedef typename MergeGraph::EraseEdgeCallBackType EraseEdgeCallBackType;
typedef typename EDGE_INDICATOR_MAP::Reference EdgeIndicatorReference;
/// \brief construct cluster operator
EdgeWeightedUcm(
MergeGraph & mergeGraph,
EDGE_INDICATOR_MAP edgeIndicatorMap,
EDGE_SIZE_MAP edgeSizeMap,
NODE_SIZE_MAP nodeSizeMap,
MIN_WEIGHT_MAP minWeightEdgeMap,
const ValueType wardness=1.0
)
: mergeGraph_(mergeGraph),
edgeIndicatorMap_(edgeIndicatorMap),
edgeSizeMap_(edgeSizeMap),
nodeSizeMap_(nodeSizeMap),
minWeightEdgeMap_(minWeightEdgeMap),
pq_(mergeGraph.maxEdgeId()+1),
wardness_(wardness)
{
MergeNodeCallBackType cbMn(MergeNodeCallBackType:: template from_method<SelfType,&SelfType::mergeNodes>(this));
MergeEdgeCallBackType cbMe(MergeEdgeCallBackType:: template from_method<SelfType,&SelfType::mergeEdges>(this));
EraseEdgeCallBackType cbEe(EraseEdgeCallBackType:: template from_method<SelfType,&SelfType::eraseEdge>(this));
mergeGraph_.registerMergeNodeCallBack(cbMn);
mergeGraph_.registerMergeEdgeCallBack(cbMe);
mergeGraph_.registerEraseEdgeCallBack(cbEe);
for(EdgeIt e(mergeGraph_);e!=lemon::INVALID;++e){
const Edge edge = *e;
const BaseGraphEdge graphEdge=EdgeHelper::itemToGraphItem(mergeGraph_,edge);
const index_type edgeId = mergeGraph_.id(edge);
const ValueType currentWeight = this->getEdgeWeight(edge);
pq_.push(edgeId,currentWeight);
minWeightEdgeMap_[graphEdge]=currentWeight;
}
}
void setWardness(const float w){
wardness_ = w;
}
void resetMgAndPq(){
mergeGraph_.reset();
MergeNodeCallBackType cbMn(MergeNodeCallBackType:: template from_method<SelfType,&SelfType::mergeNodes>(this));
MergeEdgeCallBackType cbMe(MergeEdgeCallBackType:: template from_method<SelfType,&SelfType::mergeEdges>(this));
EraseEdgeCallBackType cbEe(EraseEdgeCallBackType:: template from_method<SelfType,&SelfType::eraseEdge>(this));
mergeGraph_.registerMergeNodeCallBack(cbMn);
mergeGraph_.registerMergeEdgeCallBack(cbMe);
mergeGraph_.registerEraseEdgeCallBack(cbEe);
pq_.reset();
for(EdgeIt e(mergeGraph_);e!=lemon::INVALID;++e){
const Edge edge = *e;
const BaseGraphEdge graphEdge=EdgeHelper::itemToGraphItem(mergeGraph_,edge);
const index_type edgeId = mergeGraph_.id(edge);
const ValueType currentWeight = this->getEdgeWeight(edge);
pq_.push(edgeId,currentWeight);
minWeightEdgeMap_[graphEdge]=currentWeight;
}
}
/// \brief will be called via callbacks from mergegraph
void mergeEdges(const Edge & a,const Edge & b){
// update features / weigts etc
const BaseGraphEdge aa=EdgeHelper::itemToGraphItem(mergeGraph_,a);
const BaseGraphEdge bb=EdgeHelper::itemToGraphItem(mergeGraph_,b);
EdgeIndicatorReference va=edgeIndicatorMap_[aa];
EdgeIndicatorReference vb=edgeIndicatorMap_[bb];
va*=edgeSizeMap_[aa];
vb*=edgeSizeMap_[bb];
va+=vb;
edgeSizeMap_[aa]+=edgeSizeMap_[bb];
va/=(edgeSizeMap_[aa]);
vb/=edgeSizeMap_[bb];
// delete b from pq
pq_.deleteItem(b.id());
}
/// \brief will be called via callbacks from mergegraph
void mergeNodes(const Node & a,const Node & b){
const BaseGraphNode aa=NodeHelper::itemToGraphItem(mergeGraph_,a);
const BaseGraphNode bb=NodeHelper::itemToGraphItem(mergeGraph_,b);
nodeSizeMap_[aa]+=nodeSizeMap_[bb];
}
/// \brief will be called via callbacks from mergegraph
void eraseEdge(const Edge & edge){
//std::cout<<"start to erase edge "<<mergeGraph_.id(edge)<<"\n";
// delete edge from pq
pq_.deleteItem(edge.id());
// get the new region the edge is in
// (since the edge is no any more an active edge)
//std::cout<<"get the new node \n";
const Node newNode = mergeGraph_.inactiveEdgesNode(edge);
//std::cout<<"new node "<<mergeGraph_.id(newNode)<<"\n";
// iterate over all edges of this node
for (IncEdgeIt e(mergeGraph_,newNode);e!=lemon::INVALID;++e)
{
//std::cout<<"get inc edge\n";
const Edge incEdge(*e);
//std::cout<<"get inc graph edge\n";
const BaseGraphEdge incGraphEdge = EdgeHelper::itemToGraphItem(mergeGraph_,incEdge);
//std::cout<<"get inc edge weight"<<counter<<"\n";
// compute the new weight for this edge
// (this should involve region differences)
const ValueType newWeight = getEdgeWeight(incEdge);
// change the weight in pq by repushing
pq_.push(incEdge.id(),newWeight);
minWeightEdgeMap_[incGraphEdge]=newWeight;
}
//std::cout<<"done\n";
}
/// \brief get the edge which should be contracted next
Edge contractionEdge(){
index_type minLabel = pq_.top();
while(mergeGraph_.hasEdgeId(minLabel)==false){
eraseEdge(Edge(minLabel));
minLabel = pq_.top();
}
return Edge(minLabel);
}
/// \brief get the edge weight of the edge which should be contracted next
WeightType contractionWeight(){
index_type minLabel = pq_.top();
while(mergeGraph_.hasEdgeId(minLabel)==false){
eraseEdge(Edge(minLabel));
minLabel = pq_.top();
}
return pq_.topPriority();
}
/// \brief get a reference to the merge
MergeGraph & mergeGraph(){
return mergeGraph_;
}
bool done(){
index_type minLabel = pq_.top();
while(mergeGraph_.hasEdgeId(minLabel)==false){
eraseEdge(Edge(minLabel));
minLabel = pq_.top();
}
return mergeGraph_.edgeNum()==0 || mergeGraph_.nodeNum()==1;
}
private:
ValueType getEdgeWeight(const Edge & e){
const Node u = mergeGraph_.u(e);
const Node v = mergeGraph_.v(e);
const size_t dU = mergeGraph_.degree(u);
const size_t dV = mergeGraph_.degree(u);
const BaseGraphEdge ee=EdgeHelper::itemToGraphItem(mergeGraph_,e);
const BaseGraphNode uu=NodeHelper::itemToGraphItem(mergeGraph_,u);
const BaseGraphNode vv=NodeHelper::itemToGraphItem(mergeGraph_,v);
const float sizeU = nodeSizeMap_[uu] ;
const float sizeV = nodeSizeMap_[vv] ;
const ValueType wardFac = 2.0 / ( 1.0/std::pow(sizeU,wardness_) + 1/std::pow(sizeV,wardness_) );
//const ValueType wardFac = (wardFacRaw*wardness_) + (1.0-wardness_);
const ValueType fromEdgeIndicator = edgeIndicatorMap_[ee];
const ValueType totalWeight = fromEdgeIndicator*wardFac;
return totalWeight;
}
MergeGraph & mergeGraph_;
EDGE_INDICATOR_MAP edgeIndicatorMap_;
EDGE_SIZE_MAP edgeSizeMap_;
NODE_SIZE_MAP nodeSizeMap_;
MIN_WEIGHT_MAP minWeightEdgeMap_;
vigra::ChangeablePriorityQueue< ValueType > pq_;
ValueType wardness_;;
};
/// \brief This Cluster Operator is a MONSTER.
/// It can really do a lot.
///
/// Each edge has a single scalar weight w_e.
/// Each node has a feature vector f_n.
/// (all f_n's have the same length).
/// Edges and nodes have a length / size
///
/// The total edge weight is computed via a complicated formula
///
/// The main idea is the following.
/// Use a mixture between the edge weights w_e,
/// and node based edge weights which are computed
/// via a metric which measures the 'difference' between
/// the u/v feature vectors f_n.
///
/// Furthermore a 'Ward'-like regularization can be applied.
/// This is useful if one have clusters with sizes
/// in the same magnitude (or 'similar' sizes).
/// The amount of 'ward'-regularization is controlled
/// with the 'wardness' parameter.
///
/// Also labels (in the sense of seeds) can be attached to get a 'watershed-ish'
/// behavior (nodes with different labels will never be merged)
/// The '0'-Label is used to indicate that there is no label at all.
/// If certain connected regions share the same seed/label
/// it is not guaranteed that they will merge. But a certain prior / multiplier
/// must be specified. The total weight of an edge where the u/v node have
/// the same label is multiplied with this very multiplier.
template<
class MERGE_GRAPH,
class EDGE_INDICATOR_MAP,
class EDGE_SIZE_MAP,
class NODE_FEATURE_MAP,
class NODE_SIZE_MAP,
class MIN_WEIGHT_MAP,
class NODE_LABEL_MAP
>
class EdgeWeightNodeFeatures{
typedef EdgeWeightNodeFeatures<
MERGE_GRAPH,
EDGE_INDICATOR_MAP,
EDGE_SIZE_MAP,
NODE_FEATURE_MAP,
NODE_SIZE_MAP,
MIN_WEIGHT_MAP,
NODE_LABEL_MAP
> SelfType;
public:
typedef typename EDGE_INDICATOR_MAP::Value ValueType;
typedef ValueType WeightType;
typedef MERGE_GRAPH MergeGraph;
typedef typename MergeGraph::Graph Graph;
typedef typename Graph::Edge BaseGraphEdge;
typedef typename Graph::Node BaseGraphNode;
typedef typename MergeGraph::Edge Edge;
typedef typename MergeGraph::Node Node;
typedef typename MergeGraph::EdgeIt EdgeIt;
typedef typename MergeGraph::NodeIt NodeIt;
typedef typename MergeGraph::IncEdgeIt IncEdgeIt;
typedef typename MergeGraph::index_type index_type;
typedef MergeGraphItemHelper<MergeGraph,Edge> EdgeHelper;
typedef MergeGraphItemHelper<MergeGraph,Node> NodeHelper;
typedef typename EDGE_INDICATOR_MAP::Reference EdgeIndicatorReference;
typedef typename NODE_FEATURE_MAP::Reference NodeFeatureReference;
/// \brief construct cluster operator
EdgeWeightNodeFeatures(
MergeGraph & mergeGraph,
EDGE_INDICATOR_MAP edgeIndicatorMap,
EDGE_SIZE_MAP edgeSizeMap,
NODE_FEATURE_MAP nodeFeatureMap,
NODE_SIZE_MAP nodeSizeMap,
MIN_WEIGHT_MAP minWeightEdgeMap,
NODE_LABEL_MAP nodeLabelMap,
const ValueType beta,
const metrics::MetricType metricType,
const ValueType wardness=1.0,
const ValueType gamma = 10000000.0,
const ValueType sameLabelMultiplier = 0.8
)
: mergeGraph_(mergeGraph),
edgeIndicatorMap_(edgeIndicatorMap),
edgeSizeMap_(edgeSizeMap),
nodeFeatureMap_(nodeFeatureMap),
nodeSizeMap_(nodeSizeMap),
minWeightEdgeMap_(minWeightEdgeMap),
nodeLabelMap_(nodeLabelMap),
pq_(mergeGraph.maxEdgeId()+1),
beta_(beta),
wardness_(wardness),
gamma_(gamma),
sameLabelMultiplier_(sameLabelMultiplier),
metric_(metricType)
{
typedef typename MergeGraph::MergeNodeCallBackType MergeNodeCallBackType;
typedef typename MergeGraph::MergeEdgeCallBackType MergeEdgeCallBackType;
typedef typename MergeGraph::EraseEdgeCallBackType EraseEdgeCallBackType;
MergeNodeCallBackType cbMn(MergeNodeCallBackType:: template from_method<SelfType,&SelfType::mergeNodes>(this));
MergeEdgeCallBackType cbMe(MergeEdgeCallBackType:: template from_method<SelfType,&SelfType::mergeEdges>(this));
EraseEdgeCallBackType cbEe(EraseEdgeCallBackType:: template from_method<SelfType,&SelfType::eraseEdge>(this));
mergeGraph_.registerMergeNodeCallBack(cbMn);
mergeGraph_.registerMergeEdgeCallBack(cbMe);
mergeGraph_.registerEraseEdgeCallBack(cbEe);
for(EdgeIt e(mergeGraph);e!=lemon::INVALID;++e){
const Edge edge = *e;
const BaseGraphEdge graphEdge=EdgeHelper::itemToGraphItem(mergeGraph_,edge);
const index_type edgeId = mergeGraph_.id(edge);
const ValueType currentWeight = this->getEdgeWeight(edge);
pq_.push(edgeId,currentWeight);
minWeightEdgeMap_[graphEdge]=currentWeight;
}
}
/// \brief will be called via callbacks from mergegraph
void mergeEdges(const Edge & a,const Edge & b){
// update features / weigts etc
const BaseGraphEdge aa=EdgeHelper::itemToGraphItem(mergeGraph_,a);
const BaseGraphEdge bb=EdgeHelper::itemToGraphItem(mergeGraph_,b);
EdgeIndicatorReference va=edgeIndicatorMap_[aa];
EdgeIndicatorReference vb=edgeIndicatorMap_[bb];
va*=edgeSizeMap_[aa];
vb*=edgeSizeMap_[bb];
va+=vb;
edgeSizeMap_[aa]+=edgeSizeMap_[bb];
va/=(edgeSizeMap_[aa]);
vb/=edgeSizeMap_[bb];
// delete b from pq
pq_.deleteItem(b.id());
}
/// \brief will be called via callbacks from mergegraph
void mergeNodes(const Node & a,const Node & b){
const BaseGraphNode aa=NodeHelper::itemToGraphItem(mergeGraph_,a);
const BaseGraphNode bb=NodeHelper::itemToGraphItem(mergeGraph_,b);
NodeFeatureReference va=nodeFeatureMap_[aa];
NodeFeatureReference vb=nodeFeatureMap_[bb];
va*=nodeSizeMap_[aa];
vb*=nodeSizeMap_[bb];
va+=vb;
nodeSizeMap_[aa]+=nodeSizeMap_[bb];
va/=(nodeSizeMap_[aa]);
vb/=nodeSizeMap_[bb];
// update labels
const UInt32 labelA = nodeLabelMap_[aa];
const UInt32 labelB = nodeLabelMap_[bb];
if(labelA!=0 && labelB!=0 && labelA!=labelB){
throw std::runtime_error("both nodes have labels");
}
else{
const UInt32 newLabel = std::max(labelA, labelB);
nodeLabelMap_[aa] = newLabel;
}
}
/// \brief will be called via callbacks from mergegraph
void eraseEdge(const Edge & edge){
//std::cout<<"start to erase edge "<<mergeGraph_.id(edge)<<"\n";
// delete edge from pq
pq_.deleteItem(edge.id());
// get the new region the edge is in
// (since the edge is no any more an active edge)
//std::cout<<"get the new node \n";
const Node newNode = mergeGraph_.inactiveEdgesNode(edge);
//std::cout<<"new node "<<mergeGraph_.id(newNode)<<"\n";
// iterate over all edges of this node
for (IncEdgeIt e(mergeGraph_,newNode);e!=lemon::INVALID;++e){
//std::cout<<"get inc edge\n";
const Edge incEdge(*e);
//std::cout<<"get inc graph edge\n";
const BaseGraphEdge incGraphEdge = EdgeHelper::itemToGraphItem(mergeGraph_,incEdge);
//std::cout<<"get inc edge weight"<<counter<<"\n";
// compute the new weight for this edge
// (this should involve region differences)
const ValueType newWeight = getEdgeWeight(incEdge);
// change the weight in pq by repushing
//std::cout<<"push\n";
pq_.push(incEdge.id(),newWeight);
minWeightEdgeMap_[incGraphEdge]=newWeight;
}
//std::cout<<"done\n";
}
/// \brief get the edge which should be contracted next
Edge contractionEdge(){
index_type minLabel = pq_.top();
while(mergeGraph_.hasEdgeId(minLabel)==false){
pq_.deleteItem(minLabel);
minLabel = pq_.top();
}
return Edge(minLabel);
}
/// \brief get the edge weight of the edge which should be contracted next
WeightType contractionWeight(){
index_type minLabel = pq_.top();
while(mergeGraph_.hasEdgeId(minLabel)==false){
pq_.deleteItem(minLabel);
minLabel = pq_.top();
}
return pq_.topPriority();
}
/// \brief get a reference to the merge
MergeGraph & mergeGraph(){
return mergeGraph_;
}
bool done(){
index_type minLabel = pq_.top();
while(mergeGraph_.hasEdgeId(minLabel)==false){
pq_.deleteItem(minLabel);
minLabel = pq_.top();
}
const ValueType p = pq_.topPriority();
return p>= gamma_;
}
private:
ValueType getEdgeWeight(const Edge & e){
const Node u = mergeGraph_.u(e);
const Node v = mergeGraph_.v(e);
const size_t dU = mergeGraph_.degree(u);
const size_t dV = mergeGraph_.degree(u);
const BaseGraphEdge ee=EdgeHelper::itemToGraphItem(mergeGraph_,e);
const BaseGraphNode uu=NodeHelper::itemToGraphItem(mergeGraph_,u);
const BaseGraphNode vv=NodeHelper::itemToGraphItem(mergeGraph_,v);
const float sizeU = nodeSizeMap_[uu];
const float sizeV = nodeSizeMap_[vv];
const ValueType wardFac = 2.0 / ( 1.0/std::pow(sizeU,wardness_) + 1/std::pow(sizeV,wardness_) );
const ValueType fromEdgeIndicator = edgeIndicatorMap_[ee];
ValueType fromNodeDist = metric_(nodeFeatureMap_[uu],nodeFeatureMap_[vv]);
ValueType totalWeight = ((1.0-beta_)*fromEdgeIndicator + beta_*fromNodeDist)*wardFac;
const UInt32 labelA = nodeLabelMap_[uu];
const UInt32 labelB = nodeLabelMap_[vv];
if(labelA!=0 && labelB!=0){
if(labelA == labelB){
totalWeight*=sameLabelMultiplier_;
}
else{
totalWeight += gamma_;
}
}
return totalWeight;
}
MergeGraph & mergeGraph_;
EDGE_INDICATOR_MAP edgeIndicatorMap_;
EDGE_SIZE_MAP edgeSizeMap_;
NODE_FEATURE_MAP nodeFeatureMap_;
NODE_SIZE_MAP nodeSizeMap_;
MIN_WEIGHT_MAP minWeightEdgeMap_;
NODE_LABEL_MAP nodeLabelMap_;
vigra::ChangeablePriorityQueue< ValueType > pq_;
ValueType beta_;
ValueType wardness_;
ValueType gamma_;
ValueType sameLabelMultiplier_;
metrics::Metric<float> metric_;
};
} // end namespace cluster_operators
/// \brief do hierarchical clustering with a given cluster operator
template< class CLUSTER_OPERATOR>
class HierarchicalClustering{
public:
typedef CLUSTER_OPERATOR ClusterOperator;
typedef typename ClusterOperator::MergeGraph MergeGraph;
typedef typename MergeGraph::Graph Graph;
typedef typename Graph::Edge BaseGraphEdge;
typedef typename Graph::Node BaseGraphNode;
typedef typename MergeGraph::Edge Edge;
typedef typename MergeGraph::Node Node;
typedef typename CLUSTER_OPERATOR::WeightType ValueType;
typedef typename MergeGraph::index_type MergeGraphIndexType;
struct Parameter{
Parameter(
const size_t nodeNumStopCond = 1,
const bool buildMergeTree = true,
const bool verbose = false
)
: nodeNumStopCond_ (nodeNumStopCond),
buildMergeTreeEncoding_(buildMergeTree),
verbose_(verbose){
}
size_t nodeNumStopCond_;
bool buildMergeTreeEncoding_;
bool verbose_;
};
struct MergeItem{
MergeItem(
const MergeGraphIndexType a,
const MergeGraphIndexType b,
const MergeGraphIndexType r,
const ValueType w
):
a_(a),b_(b),r_(r),w_(w){
}
MergeGraphIndexType a_;
MergeGraphIndexType b_;
MergeGraphIndexType r_;
ValueType w_;
};
typedef std::vector<MergeItem> MergeTreeEncoding;
/// \brief construct HierarchicalClustering from clusterOperator and an optional parameter object
HierarchicalClustering(
ClusterOperator & clusterOperator,
const Parameter & parameter = Parameter()
)
:
clusterOperator_(clusterOperator),
param_(parameter),
mergeGraph_(clusterOperator_.mergeGraph()),
graph_(mergeGraph_.graph()),
timestamp_(graph_.maxNodeId()+1),
toTimeStamp_(),
timeStampIndexToMergeIndex_(),
mergeTreeEndcoding_()
{
if(param_.buildMergeTreeEncoding_){
// this can be be made smater since user can pass
// stoping condition based on nodeNum
mergeTreeEndcoding_.reserve(graph_.nodeNum()*2);
toTimeStamp_.resize(graph_.maxNodeId()+1);
timeStampIndexToMergeIndex_.resize(graph_.maxNodeId()+1);
for(MergeGraphIndexType nodeId=0;nodeId<=mergeGraph_.maxNodeId();++nodeId){
toTimeStamp_[nodeId]=nodeId;
}
}
}
/// \brief start the clustering
void cluster(){
if(param_.verbose_)
std::cout<<"\n";
while(mergeGraph_.nodeNum()>param_.nodeNumStopCond_ && mergeGraph_.edgeNum()>0 && !clusterOperator_.done()){
const Edge edgeToRemove = clusterOperator_.contractionEdge();
if(param_.buildMergeTreeEncoding_){
const MergeGraphIndexType uid = mergeGraph_.id(mergeGraph_.u(edgeToRemove));
const MergeGraphIndexType vid = mergeGraph_.id(mergeGraph_.v(edgeToRemove));
const ValueType w = clusterOperator_.contractionWeight();
// do the merge
mergeGraph_.contractEdge( edgeToRemove);
const MergeGraphIndexType aliveNodeId = mergeGraph_.hasNodeId(uid) ? uid : vid;
const MergeGraphIndexType deadNodeId = aliveNodeId==vid ? uid : vid;
timeStampIndexToMergeIndex_[timeStampToIndex(timestamp_)]=mergeTreeEndcoding_.size();
mergeTreeEndcoding_.push_back(MergeItem( toTimeStamp_[aliveNodeId],toTimeStamp_[deadNodeId],timestamp_,w));
toTimeStamp_[aliveNodeId]=timestamp_;
timestamp_+=1;
}
else{
//std::cout<<"constract\n";
// do the merge
mergeGraph_.contractEdge( edgeToRemove );
}
if(param_.verbose_ && mergeGraph_.nodeNum()%1==0){
std::cout<<"\rNodes: "<<std::setw(10)<<mergeGraph_.nodeNum()<<std::flush;
}
}
if(param_.verbose_)
std::cout<<"\n";
}
/// \brief get the encoding of the merge tree
const MergeTreeEncoding & mergeTreeEndcoding()const{
return mergeTreeEndcoding_;
}
template<class EDGE_MAP>
void ucmTransform(EDGE_MAP & edgeMap)const{
typedef typename Graph::EdgeIt BaseGraphEdgeIt;
for(BaseGraphEdgeIt iter(graph()); iter!=lemon::INVALID; ++iter ){
const BaseGraphEdge edge=*iter;
edgeMap[edge] = edgeMap[mergeGraph().reprGraphEdge(edge)];
}
}
/// \brief get the node id's which are the leafes of a treeNodeId
template<class OUT_ITER>
size_t leafNodeIds(const MergeGraphIndexType treeNodeId, OUT_ITER begin)const{
if(treeNodeId<=graph_.maxNodeId()){
*begin=treeNodeId;
++begin;
return 1;
}
else{
size_t leafNum=0;
std::queue<MergeGraphIndexType> queue;
queue.push(treeNodeId);
while(!queue.empty()){
const MergeGraphIndexType id = queue.front();
queue.pop();
const MergeGraphIndexType mergeIndex = timeStampToMergeIndex(id);
const MergeGraphIndexType ab[]= { mergeTreeEndcoding_[mergeIndex].a_, mergeTreeEndcoding_[mergeIndex].b_};
for(size_t i=0;i<2;++i){
if(ab[i]<=graph_.maxNodeId()){
*begin=ab[i];
++begin;
++leafNum;
}
else{
queue.push(ab[i]);
}
}
}
return leafNum;
}
}
/// \brief get the graph the merge graph is based on
const Graph & graph()const{
return graph_;
}
/// \brief get the merge graph
const MergeGraph & mergeGraph()const{
return mergeGraph_;
}
/// \brief get the representative node id
const MergeGraphIndexType reprNodeId(const MergeGraphIndexType id)const{
return mergeGraph_.reprNodeId(id);
}
private:
MergeGraphIndexType timeStampToIndex(const MergeGraphIndexType timestamp)const{
return timestamp- graph_.maxNodeId();
}
MergeGraphIndexType timeStampToMergeIndex(const MergeGraphIndexType timestamp)const{
return timeStampIndexToMergeIndex_[timeStampToIndex(timestamp)];
}
ClusterOperator & clusterOperator_;
Parameter param_;
MergeGraph & mergeGraph_;
const Graph & graph_;
// parameter object
// timestamp
MergeGraphIndexType timestamp_;
std::vector<MergeGraphIndexType> toTimeStamp_;
std::vector<MergeGraphIndexType> timeStampIndexToMergeIndex_;
// data which can reconstruct the merge tree
MergeTreeEncoding mergeTreeEndcoding_;
};
}
#endif // VIGRA_HIERARCHICAL_CLUSTERING_HXX
|