lib/Graph/Algorithm/HITS.pm

filter: Statementstmt 94.6% 71 / 75 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.7% 15 / 16 Podpod 0.0% 0 / 4 totaltotal 90.1% 91 / 101 CC 10 SCAR 8.2
CC
10
Cov
94%
_adj_matrix_builder
5.0
BUILD
2.3
__ANON__
2.0
CRAP 2.3
↑ Summary
1 package Graph::Algorithm::HITS;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '0.02';
6  
7 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use PDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 has graph => (is => 'ro', required => 1);
13 1 has adj_matrix => (is => 'ro', lazy => 1, builder => '_adj_matrix_builder');
14 1 has trans_adj => (is => 'ro', lazy => 1, builder => '_trans_adj_builder');
15 has trans_x_adj => (
16 is => 'ro',
17 lazy => 1,
18 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
19 1 my $self = shift;
20 1 $self->trans_adj x $self->adj_matrix;
21 }
22 1 );
23  
24 has hub_matrix => (
25 is => 'rw',
26 lazy => 1,
27 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 0 my $self = shift;
29 0 my $size = $self->graph->vertices;
30 0 ones 1,$size;
31 }
32 1 );
33  
34 has auth_matrix => (
35 is => 'rw',
36 lazy => 1,
37 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 1 my $self = shift;
39 1 my $size = $self->graph->vertices;
40 1 ones 1,$size;
41 },
42 1 );
43  
44 1 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=8.1
Pod undocumented
45 1 my $self = shift;
46 # make sure it's directed graph
47 1 ▶ unless ($self->graph->is_directed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->graph->is_directed) 0 1 1
48 0 croak 'Graph needs to be directed';
49 }
50 }
51  
52 #Create adjacency matrix from graph
53 1 ▶sub _adj_matrix_builder {
Subroutine _adj_matrix_builder: called CC=5 SCAR=16.1
54 1 my $self = shift;
55 1 my $matrix = [];
56 1 for my $v1 (sort $self->graph->vertices ) {
57 6 my @row = ();
58 6 for my $v2 (sort $self->graph->vertices ){
59 36 ▶ if ($v1 eq $v2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v1 eq $v2) { } 6 30 36
60 6 push @row, 0;
61 }else {
62 30 ▶ if ($self->graph->has_edge($v1, $v2)){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->graph->has_edge($v1, $v2)) { } 7 23 30
63 7 push @row, 1;
64 }else {
65 23 push @row, 0;
66 }
67 }
68 }
69 6 push @$matrix, \@row;
70 }
71 1 return pdl $matrix;
72 }
73  
74 #Create transpose adjacency matrix
75 1 ▶sub _trans_adj_builder {
Subroutine _trans_adj_builder: called CC=1 SCAR=0.0
76 1 my $self = shift;
77 1 return transpose $self->adj_matrix;
78 }
79  
80 1 ▶sub iterate {
Subroutine iterate: called CC=2 SCAR=6.9
Pod undocumented
81 1 my ($self, $itr) = @_;
82 1 for (1..$itr) {
83 20 my $m = $self->trans_x_adj x $self->auth_matrix;
84 20 $self->auth_matrix($m/$self->_get_sum($m));
85 }
86 1 my $m = $self->adj_matrix x $self->auth_matrix;
87 1 $self->hub_matrix($m/$self->_get_sum($m));
88 }
89  
90 21 ▶sub _get_sum {
Subroutine _get_sum: called CC=2 SCAR=6.9
91 21 my ($self, $m) = @_;
92 21 my $result = unpdl $m;
93 21 my $sum=0;
94 21 $sum += shift @$_ for (@$result);
95 21 return $sum;
96 }
97  
98 1 ▶sub get_authority {
Subroutine get_authority: called CC=2 SCAR=6.9
Pod undocumented
99 1 my $self = shift;
100 1 my $ref = unpdl $self->auth_matrix;
101 1 my %result;
102 1 for my $v (sort $self->graph->vertices) {
103 6 $result{$v} = shift @{ shift @$ref };
104 }
105 1 return \%result;
106 }
107  
108 1 ▶sub get_hub {
Subroutine get_hub: called CC=2 SCAR=6.9
Pod undocumented
109 1 my $self = shift;
110 1 my $ref = unpdl $self->hub_matrix;
111 1 my %result;
112 1 for my $v (sort $self->graph->vertices) {
113 6 $result{$v} = shift @{ shift @$ref };
114 }
115 1 return \%result;
116 }
117  
118 1 1;
119 __END__
↑ Summary