blib/lib/Statistics/Contingency.pm

filter: Statementstmt 89.5% 86 / 96 Branchbran 78.9% 30 / 38 Conditioncond 95.0% 19 / 20 MC/DCmcdc 91.6% 11 / 12 Subroutinesub 84.0% 21 / 25 Podpod 100% 15 / 15 totaltotal 88.3% 182 / 206 CC 33 SCAR 18.4
CC
33
Cov
88%
add_result
16.1
_macro_stats
6.0
stats_table
4.0
CRAP 6.3
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Statistics::Contingency;
4 {
5 1 $Statistics::Contingency::VERSION = '0.09';
6 }
7  
8 # Correct=Y Correct=N
9 # +-----------+-----------+
10 # Assigned=Y | a | b |
11 # +-----------+-----------+
12 # Assigned=N | c | d |
13 # +-----------+-----------+
14  
15 # accuracy = (a+d)/(a+b+c+d)
16 # precision = a/(a+b)
17 # recall = a/(a+c)
18 # F1 = 2a/(2a + b + c)
19  
20 # Edge cases:
21 # precision(0,0,+,d) = 0
22 # precision(a,0,c,d) = 1
23 # precision(0,+,c,d) = 0
24 # recall(a,b,0,d) = 1
25 # recall(0,b,+,d) = 0
26 # F1(a,0,0,d) = 1
27 # F1(0,+++,d) = 0
28  
29 1 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 8 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
32 8 my $package = shift;
33 8 my $self = bless { validate @_,
34 {
35 verbose => { type => SCALAR, default => 0 },
36 categories => { type => ARRAYREF|HASHREF },
37 }
38 }, $package;
39
40 8 $self->{$_} = 0 foreach qw(a b c d);
41 8 my $c = delete $self->{categories};
42 8 ▶ $self->{categories} = { map {($_ => {a=>0, b=>0, c=>0, d=>0})}
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($c, 'HASH') ? : 0 8 8
43 UNIVERSAL::isa($c, 'HASH') ? keys(%$c) : @$c
44 };
45 8 return $self;
46 }
47  
48 1 ▶sub set_entries {
Subroutine set_entries: called CC=1 SCAR=0.0
Pod documented
49 1 my $self = shift;
50 1 @{ $self }{'a', 'b', 'c', 'd'} = @_;
51 }
52  
53 8 ▶sub add_result {
Subroutine add_result: called CC=15 SCAR=27.8
Pod documented
54 8 my ($self, $assigned, $correct, $name) = @_;
55 8 my $cats_table = $self->{categories};
56  
57 # Hashify
58 8 foreach ($assigned, $correct) {
59 16 ▶ $_ = {$_ => 1}, next unless ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 0 16 16
60 16 ▶ next if UNIVERSAL::isa($_, 'HASH'); # Leave alone
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_, "HASH") 0 16 16
61 16 ▶ $_ = { map {($_ => 1)} @$_ }, next if UNIVERSAL::isa($_, 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_, "ARRAY") 16 0 16
62 0 die "Unknown type '$_' for category list";
63 }
64  
65 # Add to the micro/macro tables
66 8 foreach my $cat (keys %$cats_table) {
67 32 ▶ $cats_table->{$cat}{a}++, $self->{a}++ if $assigned->{$cat} and $correct->{$cat};
Branch: 1 branch
BranchTrueFalseTotal
if $assigned->{$cat} and $correct->{$cat} 3 29 32
Condition100%3/3
$assigned->{$cat} and $correct->{$cat}
!ll&&!rl&&r
2093
MC/DC100%2/2
$assigned->{$cat} and $correct->{$cat}
$assigned->{$cat} $correct->{$cat}
Truth table100%3/3
A and B
A= $assigned->{$cat}B= $correct->{$cat}
ABresult
0X0
100
111
68 32 ▶ $cats_table->{$cat}{b}++, $self->{b}++ if $assigned->{$cat} and !$correct->{$cat};
Branch: 1 branch
BranchTrueFalseTotal
if $assigned->{$cat} and not $correct->{$cat} 9 23 32
Condition100%3/3
$assigned->{$cat} and not $correct->{$cat}
!ll&&!rl&&r
2039
MC/DC100%2/2
$assigned->{$cat} and not $correct->{$cat}
$assigned->{$cat} not $correct->{$cat}
Truth table100%3/3
A and not(B)
A= $assigned->{$cat}B= not $correct->{$cat}
ABresult
0X0
100
111
69 32 ▶ $cats_table->{$cat}{c}++, $self->{c}++ if !$assigned->{$cat} and $correct->{$cat};
Branch: 1 branch
BranchTrueFalseTotal
if not $assigned->{$cat} and $correct->{$cat} 4 28 32
Condition100%3/3
not $assigned->{$cat} and $correct->{$cat}
!ll&&!rl&&r
12164
MC/DC100%2/2
not $assigned->{$cat} and $correct->{$cat}
not $assigned->{$cat} $correct->{$cat}
Truth table100%3/3
not(A) and B
A= not $assigned->{$cat}B= $correct->{$cat}
ABresult
0X0
100
111
70 32 ▶ $cats_table->{$cat}{d}++, $self->{d}++ if !$assigned->{$cat} and !$correct->{$cat};
Branch: 1 branch
BranchTrueFalseTotal
unless $assigned->{$cat} or $correct->{$cat} 16 16 32
Condition100%3/3
$assigned->{$cat} or $correct->{$cat}
l!l&&r!l&&!r
12416
MC/DC100%2/2
$assigned->{$cat} or $correct->{$cat}
$assigned->{$cat} $correct->{$cat}
Truth table100%3/3
A or B
A= $assigned->{$cat}B= $correct->{$cat}
ABresult
1X1
011
000
71 }
72  
73 8 ▶ if ($self->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 8 8
74 0 print "$name: assigned=(@{[ keys %$assigned ]}) correct=(@{[ keys %$correct ]})\n";
75 }
76  
77 # Clear any cached results
78 8 delete $self->{macro};
79  
80 8 $self->{hypotheses}++;
81 }
82  
83 69 ▶sub _invert {
Subroutine _invert: called CC=3 SCAR=11.0
84 69 my ($self, $x, $y) = @_;
85 69 ▶ return 1 unless $y;
Branch: 1 branch
BranchTrueFalseTotal
unless $y 36 33 69
86 33 ▶ return 0 unless $x;
Branch: 1 branch
BranchTrueFalseTotal
unless $x 22 11 33
87 11 return 1 / (1 + $y/$x);
88 }
89  
90 16 ▶sub _accuracy {
Subroutine _accuracy: called CC=2 SCAR=7.1
91 16 my $h = $_[1];
92 16 ▶ return 1 unless grep $h->{$_}, qw(a b c d);
Branch: 1 branch
BranchTrueFalseTotal
unless grep $h->{$_}, ("a", "b", "c", "d") 0 16 16
93 16 return +($h->{a} + $h->{d}) / ($h->{a} + $h->{b} + $h->{c} + $h->{d});
94 }
95  
96 18 ▶sub _error {
Subroutine _error: called CC=2 SCAR=7.1
97 18 my $h = $_[1];
98 18 ▶ return 0 unless grep $h->{$_}, qw(a b c d);
Branch: 1 branch
BranchTrueFalseTotal
unless grep $h->{$_}, ("a", "b", "c", "d") 0 18 18
99 18 return +($h->{b} + $h->{c}) / ($h->{a} + $h->{b} + $h->{c} + $h->{d});
100 }
101  
102 24 ▶sub _precision {
Subroutine _precision: called CC=4 SCAR=13.9
103 24 my ($self, $h) = @_;
104 24 ▶ return 0 if $h->{c} and !$h->{a} and !$h->{b};
Branch: 1 branch
BranchTrueFalseTotal
if $h->{"c"} and not $h->{"a"} and not $h->{"b"} 3 21 24
Condition100%6/6
$h->{"c"} and not $h->{"a"} and not $h->{"b"}
!ll&&!rl&&r
1923
$h->{"c"} and not $h->{"a"}
!ll&&!rl&&r
1725
MC/DC100%3/3
$h->{"c"} and not $h->{"a"} and not $h->{"b"}
$h->{"c"} not $h->{"a"} not $h->{"b"}
Truth table100%4/4
A and not(B) and not(C)
A= $h->{"c"}B= not $h->{"a"}C= not $h->{"b"}
ABCresult
0XX0
10X0
1100
1111
105 21 return $self->_invert($h->{a}, $h->{b});
106 }
107
108 25 ▶sub _recall {
Subroutine _recall: called CC=1 SCAR=0.0
109 25 my ($self, $h) = @_;
110 25 return $self->_invert($h->{a}, $h->{c});
111 }
112
113 23 ▶sub _F1 {
Subroutine _F1: called CC=1 SCAR=0.0
114 23 my ($self, $h) = @_;
115 23 return $self->_invert(2 * $h->{a}, $h->{b} + $h->{c});
116 }
117  
118 # Fills in precision, recall, etc. for each category, and computes their averages
119 10 ▶sub _macro_stats {
Subroutine _macro_stats: called CC=6 SCAR=17.9
120 10 my $self = shift;
121 10 ▶ return $self->{macro} if $self->{macro};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"macro"} 6 4 10
122
123 4 my @metrics = qw(precision recall F1 accuracy error);
124  
125 4 my $cats = $self->{categories};
126 4 ▶ die "No category information has been recorded"
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$cats 0 4 4
127 unless keys %$cats;
128  
129 4 my %results;
130 4 ▶ while (my ($cat, $scores) = each %$cats) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$cats) 16 4 20
131 16 foreach my $metric (@metrics) {
132 80 my $method = "_$metric";
133 80 $results{$metric} += ($scores->{$metric} = $self->$method($scores));
134 }
135 }
136 4 foreach (@metrics) {
137 20 $results{$_} /= keys %$cats;
138 }
139 4 $self->{macro} = \%results;
140 }
141  
142 0 ▶sub micro_accuracy { $_[0]->_accuracy( $_[0]) }
Subroutine micro_accuracy: not called CC=1 SCAR=6.9
Pod documented
143 2 ▶sub micro_error { $_[0]->_error( $_[0]) }
Subroutine micro_error: called CC=1 SCAR=0.0
Pod documented
144 8 ▶sub micro_precision { $_[0]->_precision($_[0]) }
Subroutine micro_precision: called CC=1 SCAR=0.0
Pod documented
145 9 ▶sub micro_recall { $_[0]->_recall( $_[0]) }
Subroutine micro_recall: called CC=1 SCAR=0.0
Pod documented
146 7 ▶sub micro_F1 { $_[0]->_F1( $_[0]) }
Subroutine micro_F1: called CC=1 SCAR=0.0
Pod documented
147  
148 0 ▶sub macro_accuracy { shift()->_macro_stats->{accuracy} }
Subroutine macro_accuracy: not called CC=1 SCAR=6.9
Pod documented
149 0 ▶sub macro_error { shift()->_macro_stats->{error} }
Subroutine macro_error: not called CC=1 SCAR=6.9
Pod documented
150 3 ▶sub macro_precision { shift()->_macro_stats->{precision} }
Subroutine macro_precision: called CC=1 SCAR=0.0
Pod documented
151 4 ▶sub macro_recall { shift()->_macro_stats->{recall} }
Subroutine macro_recall: called CC=1 SCAR=0.0
Pod documented
152 3 ▶sub macro_F1 { shift()->_macro_stats->{F1} }
Subroutine macro_F1: called CC=1 SCAR=0.0
Pod documented
153  
154 0 ▶sub category_stats {
Subroutine category_stats: not called CC=1 SCAR=6.9
Pod documented
155 0 my $self = shift;
156 0 $self->_macro_stats;
157  
158 0 return $self->{categories};
159 }
160  
161 2 ▶sub stats_table {
Subroutine stats_table: called CC=4 SCAR=13.9
Pod documented
162 2 my $self = shift;
163 2 ▶ my $figs = shift || 3;
Condition50%1/2
shift() || 3
l!l
02
MC/DC0%0/1
shift() || 3
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
164  
165 2 my @data = map $self->_sig_figs($_, $figs),
166 (
167 $self->macro_recall,
168 $self->macro_precision,
169 $self->macro_F1,
170 $self->micro_recall,
171 $self->micro_precision,
172 $self->micro_F1,
173 $self->micro_error,
174 );
175
176 2 my $m = 0; # Max length of @data items
177 2 for (@data) {
178 14 ▶ $m = length() if length() > $m;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ > $m 2 12 14
179 }
180 2 my $s = ' ' x ($m - 4);
181
182 2 my $out = "+" . ("-" x (10 + 7*$m)) . "+\n";
183 2 $out .= "| $s maR $s maP$s maF1 $s miR $s miP$s miF1 $s Err |\n";
184 2 $out .= "| %${m}s %${m}s %${m}s %${m}s %${m}s %${m}s %${m}s |\n";
185 2 $out .= "+" . ("-" x (10 + 7*$m)) . "+\n";
186  
187 2 return sprintf($out, @data);
188 }
189  
190 14 ▶sub _sig_figs {
Subroutine _sig_figs: called CC=2 SCAR=6.9
191 14 my ($self, $number, $figs) = @_;
192 14 ▶ my $after_point = $figs - int ($number != 0 ? log($number)/log(10) : 0);
Branch: 1 branch
BranchTrueFalseTotal
$number != 0 ? : 11 3 14
193 14 return sprintf "%.${after_point}f", $number;
194 }
195  
196 1 1;
197  
198 __END__
↑ Summary