File Coverage

blib/lib/Algorithm/Cluster/Thresh.pm
Criterion Covered Total %
statement 31 31 100.0
branch 7 8 87.5
condition n/a
subroutine 7 7 100.0
pod n/a
total 45 46 97.8


line stmt bran cond sub pod time code
1             package Algorithm::Cluster::Thresh;
2             BEGIN {
3 1     1   66271 $Algorithm::Cluster::Thresh::VERSION = '0.05';
4             }
5             # ABSTRACT: Adds thresholding to hierarchical clustering of Algorithm::Cluster
6 1     1   1092 use Algorithm::Cluster;
  1         10417  
  1         163  
7              
8              
9              
10             # Add new method to standard package:
11             package Algorithm::Cluster::Tree;
12             BEGIN {
13 1     1   21 $Algorithm::Cluster::Tree::VERSION = '0.05';
14             }
15 1     1   12 use strict;
  1         1  
  1         34  
16 1     1   5 use warnings;
  1         2  
  1         30  
17 1     1   22 use 5.008;
  1         68  
  1         787  
18              
19             sub cutthresh {
20 3     3   41166 my ($tree, $thresh) = @_;
21 3         8 my @nodecluster;
22             my @leafcluster;
23             # Binary tree: number of internal nodes is 1 less than # of leafs
24             # Last node is the root, walking down the tree
25 3         8 my $icluster = 0;
26             # Elements in tree
27 3         15 my $length = $tree->length;
28             # Root node belongs to cluster 0
29 3         8 $nodecluster[$length-1] = $icluster++;
30 3         18 for (my $i = $length-1; $i >= 0; $i--) {
31 33         121 my $node = $tree->get($i);
32             # print sprintf "%3d %3d %.3f\n", $i,$nodecluster[$i], $node->distance;
33 33         78 my $left = $node->left;
34             # Nodes are numbered -1,-2,... Leafs are numbered 0,1,2,...
35 33 100       92 my $leftref = $left < 0 ? \$nodecluster[-$left-1] : \$leafcluster[$left];
36 33         40 my $assigncluster = $nodecluster[$i];
37             # Left is always the same as the parent node's cluster
38 33         44 $$leftref = $assigncluster;
39             # print sprintf "\tleft %3d %3d\n", $left, $$leftref;
40 33         107 my $right = $node->right;
41             # Put right into a new cluster, when thresh not satisfied
42 33 100       91 if ($node->distance > $thresh) { $assigncluster = $icluster++ }
  19         31  
43 33 100       570 my $rightref = $right < 0 ? \$nodecluster[-$right-1] : \$leafcluster[$right];
44 33         149 $$rightref = $assigncluster;
45             # print sprintf "\tright %3d %3d\n", $right, $$rightref;
46             }
47 3 50       18 return wantarray ? @leafcluster : \@leafcluster;
48             }
49              
50             1;
51              
52             __END__