KernelKMeans/PP.pm

filter: Statementstmt 100% 144 / 144 Branchbran 86.3% 19 / 22 Conditioncond 69.2% 9 / 13 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 26 / 26 Podpod 0.0% 0 / 9 totaltotal 90.5% 201 / 222 CC 19 SCAR 12.2
CC
19
Cov
96%
run
9.1
init_clusters
4.0
BUILD
4.0
CRAP 3.4
↑ Summary KernelKMeans/Util.pm »
1 package Algorithm::KernelKMeans::PP;
2  
3 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use List::Util qw/shuffle sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use List::MoreUtils qw/natatime pairwise/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use MooseX::Types::Common::Numeric qw/PositiveNum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use MooseX::Types::Moose qw/ArrayRef HashRef CodeRef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use POSIX qw/floor/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Algorithm::KernelKMeans::Util qw/generate_polynominal_kernel/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 our $DEBUG = 0;
17 3 our $VERSION = '0.02';
18  
19 3 has 'vertices' => (
20 is => 'ro',
21 isa => ArrayRef[ HashRef[PositiveNum] ],
22 required => 1,
23 traits => [qw/Array/],
24 handles => +{
25 num_vertices => 'count',
26 vertex => 'get'
27 }
28 );
29  
30 3 has 'weights' => (
31 is => 'ro',
32 isa => ArrayRef[PositiveNum],
33 lazy => 1,
34 builder => '_build_weights',
35 traits => [qw/Array/],
36 handles => +{
37 num_weights => 'count',
38 weight => 'get'
39 }
40 );
41  
42 3 has 'kernel' => (
43 is => 'ro',
44 isa => CodeRef,
45 lazy => 1,
46 builder => '_build_kernel'
47 );
48  
49 # vertex index -> vertex index -> kernel
50 # Note that the clusterer only uses lower triangle part of the matrix.
51 # i.e. It is assumed that K(x1, x2) = K(x2, x1)
52 3 has 'kernel_matrix' => (
53 is => 'ro',
54 isa => ArrayRef[ ArrayRef[PositiveNum] ],
55 lazy => 1,
56 builder => '_build_kernel_matrix'
57 );
58  
59 6 ▶sub _build_weights { [ (1) x shift->num_vertices ] }
Subroutine _build_weights: called CC=1 SCAR=0.0
60  
61 # Polynorminal kernel K(x1, x2) = (1 + x1x2)^2
62 1 ▶sub _build_kernel { generate_polynominal_kernel(1, 2) }
Subroutine _build_kernel: called CC=1 SCAR=0.0
63  
64 4 ▶sub _build_kernel_matrix {
Subroutine _build_kernel_matrix: called CC=1 SCAR=0.0
65 4 my $self = shift;
66 my @matrix = map {
67 4 my $i = $_;
68 [ map {
69 1024 my $j = $_;
70 131584 $self->kernel->($self->vertex($i), $self->vertex($j));
71 } 0 .. $i ];
72 } 0 .. $self->num_vertices - 1;
73 4 return \@matrix;
74 }
75  
76 7 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
77 7 my $self = shift;
78 7 ▶ if ($self->num_vertices != $self->num_weights) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->num_vertices != $self->num_weights) 1 6 7
79 1 croak 'Array "vertices" and "weights" must be same size';
80 }
81 6 ▶ if (@{ $self->kernel_matrix } < $self->num_vertices
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->kernel_matrix;} < $self->num_vertices or @{$self->kernel_matrix->[-1];} < $self->num_vertices) 1 5 6
Condition66%2/3
@{$self->kernel_matrix;} < $self->num_vertices or @{$self->kernel_matrix->[-1];} < $self->num_vertices
l!l&&r!l&&!r
105
MC/DC50%1/2
@{$self->kernel_matrix;} < $self->num_vertices or @{$self->kernel_matrix->[-1];} < $self->num_vertices
@{$self->kernel_matrix;} < $self->num_vertices @{$self->kernel_matrix->[-1];} < $self->num_vertices
Truth table66%2/3
A or B
A= @{$self->kernel_matrix;} < $self->num_verticesB= @{$self->kernel_matrix->[-1];} < $self->num_vertices
ABresult
1X1
011
000
82 5 or @{ $self->kernel_matrix->[-1] } < $self->num_vertices ) {
83 1 croak 'Kernel matrix seems too small';
84 }
85 };
86  
87 3 ▶sub init_clusters {
Subroutine init_clusters: called CC=4 SCAR=13.9
Pod undocumented
88 3 my ($self, $k, $shuffle) = @_;
89 3 my $cluster_size = floor($self->num_vertices / $k);
90 3 my @indices = (0 .. $self->num_vertices - 1);
91 3 ▶ @indices = shuffle @indices if $shuffle;
Branch: 1 branch
BranchTrueFalseTotal
if $shuffle 0 3 3
92 3 my $iter = natatime $cluster_size, @indices;
93 3 my @clusters;
94 3 ▶ while (my @cluster = $iter->()) { push @clusters, \@cluster }
Branch: 1 branch
BranchTrueFalseTotal
if (my(@cluster) = &$iter()) 21 3 24
95 3 ▶ if (@{ $clusters[-1] } < $cluster_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$clusters[-1];} < $cluster_size) 3 0 3
96 3 my $last_cluster = pop @clusters;
97 3 push @{ $clusters[-1] }, @$last_cluster;
98 }
99 3 return \@clusters;
100 }
101  
102 18 ▶sub vertices_of {
Subroutine vertices_of: called CC=1 SCAR=0.0
Pod undocumented
103 18 my ($self, $cluster) = @_;
104 18 [ map { $self->vertex($_) } @$cluster ];
105 }
106  
107 63222 ▶sub weights_of {
Subroutine weights_of: called CC=1 SCAR=0.0
Pod undocumented
108 63222 my ($self, $cluster) = @_;
109 63222 [ map { $self->weight($_) } @$cluster ];
110 }
111  
112 246 ▶sub total_weight_of {
Subroutine total_weight_of: called CC=1 SCAR=0.0
Pod undocumented
113 246 my ($self, $cluster) = @_;
114 246 sum @{ $self->weights_of($cluster) };
115 }
116  
117 38 ▶sub step {
Subroutine step: called CC=2 SCAR=6.9
Pod undocumented
118 38 my ($self, $clusters, $norms) = @_;
119 38 my @new_clusters = map { [] } 0 .. $#$clusters;
120 38 for my $i (0 .. $self->num_vertices - 1) {
121 96581 my ($nearest) = sort { $a->[1] <=> $b->[1] } map {
122 9728 [ $_ => $norms->[$i][$_] ]
123 } 0 .. $#$clusters;
124 9728 push @{ $new_clusters[$nearest->[0]] }, $i;
125 }
126 38 return [ grep { @$_ != 0 } @new_clusters ];
127 }
128  
129 41 ▶sub compute_score {
Subroutine compute_score: called CC=2 SCAR=6.9
Pod undocumented
130 41 my ($self, $clusters, $norms) = @_;
131 41 my $score = 0;
132 41 for my $cluster_idx (0 .. $#$clusters) {
133 246 my $cluster = $clusters->[$cluster_idx];
134 $score += sum map {
135 246 $self->weight($_) * $norms->[$_][$cluster_idx]
136 } @$cluster;
137 }
138 41 return $score;
139 }
140  
141 41 ▶sub compute_norms {
Subroutine compute_norms: called CC=1 SCAR=0.0
Pod undocumented
142 41 my ($self, $clusters) = @_;
143 41 my @total_weights = map { $self->total_weight_of($_) } @$clusters;
144  
145 my @term3_denoms = map {
146 41 $self->_norm_term3_denom_of($_)
147 } @$clusters;
148 41 ▶ my @term3s = pairwise { $a / ($b ** 2) } @term3_denoms, @total_weights;
Subroutine __ANON__: called CC=1 SCAR=0.0
149  
150 my @norms = map {
151 41 my $i = $_;
152 10496 my $term1 = $self->kernel_matrix->[$i][$i];
153 [ map {
154 10496 my $cluster_idx = $_;
155 62976 my $cluster = $clusters->[$cluster_idx];
156 62976 my $total_weight = $total_weights[$cluster_idx];
157  
158 62976 my $weights = $self->weights_of($cluster);
159 2686976 ▶ my $term2 = -2 * sum(pairwise {
Subroutine __ANON__: called CC=2 SCAR=6.9
160 2686976 ▶ my ($s, $t) = $i >= $a ? ($i, $a) : ($a, $i);
Branch: 1 branch
BranchTrueFalseTotal
$i >= $a ? : 1348736 1338240 2686976
161 2686976 $self->kernel_matrix->[$s][$t] * $b
162 62976 } @$cluster, @$weights) / $total_weight;
163 62976 my $term3 = $term3s[$cluster_idx];
164  
165 62976 $term1 + $term2 + $term3;
166 } 0 .. $#$clusters ]
167 } 0 .. $self->num_vertices - 1;
168 41 return \@norms;
169 }
170  
171 246 ▶sub _norm_term3_denom_of {
Subroutine _norm_term3_denom_of: called CC=2 SCAR=6.9
172 246 my ($self, $cluster) = @_;
173 sum map {
174 246 my $i = $_;
175 map {
176 10496 my $j = $_;
177 511004 ▶ my ($s, $t) = $i >= $_ ? ($i, $_) : ($_, $i);
Branch: 1 branch
BranchTrueFalseTotal
$i >= $_ ? : 260750 250254 511004
178 511004 $self->weight($s) * $self->weight($t) * $self->kernel_matrix->[$s][$t];
179 } @$cluster;
180 } @$cluster;
181 }
182  
183 6 ▶sub run {
Subroutine run: called CC=9 SCAR=22.1
Pod undocumented
184 6 my ($self, %opts) = @_;
185 6 ▶ my $k = delete $opts{k} // croak 'Required argument "k" missing';
Condition100%2/2
delete $opts{"k"} // croak("Required argument \"k\" missing")
l!l
51
MC/DC100%1/1
delete $opts{"k"} // croak("Required argument \"k\" missing")
delete $opts{"k"}
Truth table100%2/2
A
A= delete $opts{"k"}
Aresult
11
00
186 5 ▶ my $k_min = delete $opts{k_min} // $k;
Condition66%2/3
delete $opts{"k_min"} // $k
l!l&&r!l&&!r
140
MC/DC0%0/2
delete $opts{"k_min"} // $k
delete $opts{"k_min"} $k
Truth table66%2/3
A // B
A= delete $opts{"k_min"}B= $k
ABresult
1X1
011
000
187 5 ▶ croak '"k_min" must be less than or equal to "k"' if $k_min > $k;
Branch: 1 branch
BranchTrueFalseTotal
if $k_min > $k 1 4 5
188 38 ▶ my $converged = delete $opts{converged} // sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
189 38 my ($score, $new_score) = @_;
190 38 $score == $new_score;
191 4 ▶ };
Condition33%1/3
delete $opts{"converged"} // sub { my($score, $new_score) = @_; $score == $new_score; }
l!l&&r!l&&!r
040
MC/DC0%0/2
delete $opts{"converged"} // sub { my($score, $new_score) = @_; $score == $new_score; }
delete $opts{"converged"} sub { my($score, $new_score) = @_; $score == $new_score; }
Truth table33%1/3
A // B
A= delete $opts{"converged"}B= sub { my($score, $new_score) = @_; $score == $new_score; }
ABresult
1X1
011
000
192 4 ▶ my $shuffle = delete $opts{shuffle} // 1;
Condition100%2/2
delete $opts{"shuffle"} // 1
l!l
31
MC/DC100%1/1
delete $opts{"shuffle"} // 1
delete $opts{"shuffle"}
Truth table100%2/2
A
A= delete $opts{"shuffle"}
Aresult
11
00
193 4 ▶ if (keys %opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %opts) 1 3 4
194 1 croak 'Unknown argument(s): ', join ', ', map { qq/"$_"/ } sort keys %opts;
195 }
196  
197 # cluster index -> [vertex index]
198 3 my $clusters = $self->init_clusters($k, $shuffle);
199 # vertex index -> cluster index -> norm
200 3 my $norms = $self->compute_norms($clusters);
201 3 my $score;
202 3 my $new_score = $self->compute_score($clusters, $norms);
203 3 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
&$converged($score, $new_score) or do { do { $clusters = $self->step($clusters, $norms); croak("Number of clusters became less than k_min=$k_min") if @$clusters < $k_min; $norms = $self->compute_norms($clusters); $score = $new_score; $new_score = $self->compute_score($clusters, $norms) } } 3 35 38
204 38 $clusters = $self->step($clusters, $norms);
205 38 ▶ croak "Number of clusters became less than k_min=$k_min"
Branch: 1 branch
BranchTrueFalseTotal
if @$clusters < $k_min 0 38 38
206 if @$clusters < $k_min;
207 38 $norms = $self->compute_norms($clusters);
208 38 $score = $new_score;
209 38 $new_score = $self->compute_score($clusters, $norms);
210 } until $converged->($score, $new_score);
211  
212 3 [ map { $self->vertices_of($_) } @$clusters ];
213 }
214  
215 3 __PACKAGE__->meta->make_immutable;
216  
217 __END__
↑ Summary KernelKMeans/Util.pm »