blib/lib/Statistics/Hartigan.pm

filter: Statementstmt 16.1% 20 / 124 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.1% 4 / 7 Podpod 0.0% 0 / 3 totaltotal 14.2% 24 / 168 CC 20 SCAR 49.8
CC
20
Cov
13%
hartigan
240.0
WGSS
30.0
dist_euclidean_sqr
6.0
CRAP 144.8
↑ Summary
1 package Statistics::Hartigan;
2  
3 1 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 require Exporter;
8 1 ▶use AutoLoader qw(AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our @ISA = qw(Exporter);
11  
12 1 our @EXPORT = qw( hartigan );
13  
14 1 our $VERSION = '0.01';
15  
16 # global variable
17 1 my @H = ();
18 1 my @d = ();
19 1 my @W = ();
20 1 my $rcnt = 0;
21  
22 sub hartigan
23 0 ▶{
Subroutine hartigan: not called CC=15 SCAR=54.8
Pod undocumented
24 # Input params
25 0 my $matrixfile = shift;
26 0 my $clustmtd = shift;
27 0 my $K = shift;
28 0 my $threshold = shift;
29  
30 0 my $i = 0;
31 0 my $j = 0;
32  
33 # Read the matrix file into a 2 dimensional array.
34 0 my @inpmat = ();
35 0 ▶ open(INP,"<$matrixfile") || die "Error opening input matrix file!";
Branch: 1 branch
BranchTrueFalseTotal
open INP, "<$matrixfile" or die "Error opening input matrix file!" 0 0 0
36  
37 # Extract the number of rows from the first line in the file.
38 0 my $ccnt = 0;
39 0 my $line;
40  
41 0 $line = <INP>;
42 0 chomp($line);
43 0 $line=~s/\s+/ /;
44
45 0 ($rcnt,$ccnt) = split(/\s+/,$line);
46  
47 # Not a valid condition:
48 # If maximum number of clusters requested (k) is greater than the
49 # number of observations.
50 0 ▶ if($K > $rcnt)
Branch: 1 branch
BranchTrueFalseTotal
if ($K > $rcnt) 0 0 0
51 {
52 0 print STDERR "The K value ($K) cannot be greater than the number of observations present in the input data ($rcnt). \n";
53 0 exit 1;
54 }
55  
56 # Copy the complete matrix to a 2D array
57 0 ▶ while(<INP>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline INP)) 0 0 0
58 {
59 # remove the newline at the end of the input line
60 0 chomp;
61  
62 # skip empty lines
63 0 ▶ if(m/^\s*\s*\s*$/)
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*\s*\s*$/) 0 0 0
64 {
65 0 next;
66 }
67  
68 # remove leading white spaces
69 0 s/^\s+//;
70  
71 # seperate individual values in a line
72 0 my @tmp = ();
73 0 @tmp = split(/\s+/);
74
75 # populate them into the 2D matrix
76 0 push @inpmat, [ @tmp ];
77 }
78  
79 0 close INP;
80  
81 0 my @row1 = ();
82 0 my @row2 = ();
83  
84 0 my %hash = ();
85  
86 # Calculate all possible unique pairwise distances between the vectors
87 0 ▶ for($i = 0; $i < $rcnt; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rcnt) 0 0 0
88 {
89 # for all the rows in the cluster
90 0 ▶ for($j = $i+1; $j < $rcnt; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $rcnt) 0 0 0
91 {
92 0 @row1 = @{$inpmat[$i]};
93 0 @row2 = @{$inpmat[$j]};
94 0 $d[$i][$j] = &dist_euclidean_sqr(\@row1, \@row2);
95 }
96 0 $hash{0} .= "$i ";
97 }
98
99 0 $hash{0} = substr($hash{0},0,length($hash{0})-1);
100 0 $W[1] = &WGSS(\%hash);
101  
102 0 my $k = 0;
103 0 my $flag = 0;
104  
105 # For each K
106 0 ▶ for($k=1; $k<$K; $k++)
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $K) 0 0 0
107 {
108 0 my $lineNo = 0;
109 0 my %hash = ();
110  
111 # Cluster the input dataset into k+1 clusters
112 0 my $status = 0;
113 0 my $next_k = $k + 1;
114 0 $status = system("vcluster --clmethod $clustmtd $matrixfile $next_k >& tmpfile");
115 0 ▶ die "Error running vcluster \n" unless $status==0;
Branch: 1 branch
BranchTrueFalseTotal
unless $status == 0 0 0 0
116  
117 # read the clustering output file
118 0 ▶ open(CO,"<$matrixfile.clustering.$next_k") || die "Error opening clustering output file.";
Branch: 1 branch
BranchTrueFalseTotal
open CO, "<$matrixfile.clustering.$next_k" or die "Error opening clustering output file." 0 0 0
119  
120 0 my $clust = 0;
121 0 ▶ while($clust = <CO>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($clust = readline CO)) 0 0 0
122 {
123 # hash on the cluster# and append the observation#
124 0 chomp($clust);
125 0 ▶ if(exists $hash{$clust})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash{$clust}) { } 0 0 0
126 {
127 0 $hash{$clust} .= " $lineNo";
128 }
129 else
130 {
131 0 $hash{$clust} = $lineNo;
132 }
133  
134 # increment the line number
135 0 $lineNo++;
136 }
137  
138 0 close CO;
139  
140 # Calculate the "Within Cluster Sum of Squared W(k+1)
141 # for given matrix and k+1 value.
142 0 $W[$next_k] = &WGSS(\%hash);
143  
144 0 unlink "$matrixfile.clustering.$next_k", "tmpfile","$matrixfile.tree";
145  
146 0 ▶ if($W[$next_k] == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($W[$next_k] == 0) { } 0 0 0
147 {
148 0 print $next_k . "\n";
149 0 $flag = 1;
150 0 last;
151 }
152 else
153 {
154 0 $H[$k] = ( $W[$k]/$W[$next_k] - 1 ) * ( $rcnt - $k - 1 );
155 0 $H[$k] = sprintf("%.4f",$H[$k]);
156 }
157  
158 0 ▶ if($H[$k] <= $threshold)
Branch: 1 branch
BranchTrueFalseTotal
if ($H[$k] <= $threshold) 0 0 0
159 {
160 0 print $k . "\n";
161 0 $flag = 1;
162 0 last;
163 }
164 }
165  
166 0 ▶ if(!$flag)
Branch: 1 branch
BranchTrueFalseTotal
if (not $flag) 0 0 0
167 {
168 0 print "NAN\n";
169 }
170 }
171  
172 sub WGSS
173 0 ▶{
Subroutine WGSS: not called CC=5 SCAR=34.0
Pod undocumented
174 # Input arguments
175 0 my %clustout = %{(shift)};
176  
177 # Local variables
178 0 my $i;
179 my $j;
180 0 my @rownum;
181 0 my $key;
182 0 my $row1;
183 0 my $row2;
184 0 my @D = ();
185 0 my $W = 0;
186  
187 # For each cluster
188 0 foreach $key (sort keys %clustout)
189 {
190 0 $D[$key] = 0;
191  
192 0 @rownum = split(/\s+/,$clustout{$key});
193  
194 # for each instance in the cluster
195 0 ▶ for($i = 0; $i < $#rownum; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#rownum) 0 0 0
196 {
197 # for all the rows in the cluster
198 0 ▶ for($j = $i+1; $j <= $#rownum; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $#rownum) 0 0 0
199 {
200 # find the distance between the 2 rows of the matrix.
201 0 $row1 = $rownum[$i];
202 0 $row2 = $rownum[$j];
203  
204 # store the Dr value
205 0 ▶ if(exists $d[$row1][$row2])
Branch: 1 branch
BranchTrueFalseTotal
if (exists $d[$row1][$row2]) { } 0 0 0
206 {
207 0 $D[$key] += $d[$row1][$row2];
208 }
209 else
210 {
211 0 $D[$key] += $d[$row2][$row1];
212 }
213 }
214 }
215  
216 0 $W += ( $#rownum - 1 ) * $D[$key];
217 }
218  
219 0 $W = $W/2;
220  
221 0 return $W;
222 }
223  
224 sub dist_euclidean_sqr
225 0 ▶{
Subroutine dist_euclidean_sqr: not called CC=2 SCAR=17.9
Pod undocumented
226 # arguments
227 0 my @i = @{(shift)};
228 0 my @j = @{(shift)};
229  
230 # local variables
231 0 my $a;
232 0 my $dist = 0;
233 0 my $retvalue = 0;
234  
235 # Squared Euclidean measure
236 # summation on all j (xij - xi'j)^2 where i, i' are the rows indicies.
237 0 for $a (0 .. $#i)
238 {
239 0 $dist += (($i[$a] - $j[$a])**2);
240 }
241  
242 0 $retvalue = sprintf("%.4f",$dist);
243 0 return $retvalue;
244 }
245  
246  
247 1 1;
248 __END__
↑ Summary