| 1 | ######################### -*- Mode: Perl -*- ######################### | ||||||||||||||||||||
| 2 | ## | ||||||||||||||||||||
| 3 | ## File : Average.pm | ||||||||||||||||||||
| 4 | ## | ||||||||||||||||||||
| 5 | ## Author : Norbert Goevert | ||||||||||||||||||||
| 6 | ## Created On : Wed Feb 5 17:22:44 1997 | ||||||||||||||||||||
| 7 | ## Last Modified : Time-stamp: <2000-11-23 17:45:59 goevert> | ||||||||||||||||||||
| 8 | ## | ||||||||||||||||||||
| 9 | ## Description : calculate average over precision curves | ||||||||||||||||||||
| 10 | ## | ||||||||||||||||||||
| 11 | ## $Id: Average.pm,v 1.28 2003/06/13 12:29:30 goevert Exp $ | ||||||||||||||||||||
| 12 | ## | ||||||||||||||||||||
| 13 | ###################################################################### | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =pod #---------------------------------------------------------------# | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | =head1 NAME | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | RePrec - compute average of recall-precision curves | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | require RePrec::Average; | ||||||||||||||||||||
| 28 | $av = RePrec::Average->new(@reprecs); | ||||||||||||||||||||
| 29 | $av->calculate; | ||||||||||||||||||||
| 30 | $av->gnuplot; | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | Given some recall-precision RePrec(3) oobjects the average precision | ||||||||||||||||||||
| 35 | over same recall points is calculated (macro measure). | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | =head1 METHODS | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | =over | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =cut #---------------------------------------------------------------# | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | package RePrec::Average; | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 48 | 1 | ▶ | use IO::File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 1 | require RePrec::Tools; | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 1 | our $VERSION; | |||||||||||||||||||
| 54 | 1 | '$Name: release_0_32 $ 0_0' =~ /(\d+)[-_](\d+)/; $VERSION = sprintf '%d.%03d', $1, $2; | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | ## public ############################################################ | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =pod #---------------------------------------------------------------# | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =item $av = RePrec::Average->new(@reprecs) | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | constructor. @reprecs is an array of RePrec(3) objects. | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =cut #---------------------------------------------------------------# | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | my $proto = shift; | |||||||||||||||||||
| 70 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 71 | 0 | my $self = {}; | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | $self->{rp} = [ @_ ]; | |||||||||||||||||||
| 74 | 0 | $self->{divisor} = scalar @_; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | bless $self => $class; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =cut #---------------------------------------------------------------# | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =item ($graph, $average) = $rp->calculate([$points]) | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | calculates precision values for $points (see respective method in | ||||||||||||||||||||
| 84 | RePrec(3)). As a result you get a list of (recall, average precision) | ||||||||||||||||||||
| 85 | pairs (array of array references with two elements each) and the | ||||||||||||||||||||
| 86 | averaged average precision (over all recall points computed). | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =cut #---------------------------------------------------------------# | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | ▶ | sub calculate { | ||||||||||||||||||
|
Subroutine
calculate: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | my $self = shift; | |||||||||||||||||||
| 93 | 0 | my $points = shift; | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | my(%sum, $sum); | |||||||||||||||||||
| 96 | 0 | foreach (@{$self->{rp}}) { | |||||||||||||||||||
| 97 | 0 | my($result, $average) = @{$_->calculate($points)}; | |||||||||||||||||||
| 98 | 0 | ▶ | unless ($result) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 0 | $self->{divisor}--; | |||||||||||||||||||
| 100 | 0 | next; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | 0 | foreach my $point (@{$result}) { | |||||||||||||||||||
| 103 | 0 | $sum{$point->[0]} += $point->[1]; | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | 0 | $sum += $average; | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 0 | my @average; | |||||||||||||||||||
| 109 | 0 | foreach (sort keys %sum) { | |||||||||||||||||||
| 110 | 0 | push @average, [$_, $sum{$_} / $self->{divisor}]; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | 0 | my $average = $sum / $self->{divisor}; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 0 | $self->{rpgraph} = [ \@average, $average]; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =cut #---------------------------------------------------------------# | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =item $rp->gnuplot([$gnuplot]) | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | plot curve with gnuplot(1). $gnuplot is a hash reference where | ||||||||||||||||||||
| 123 | parameters for gnuplot can be set. | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =cut #---------------------------------------------------------------# | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 0 | ▶ | sub gnuplot { | ||||||||||||||||||
|
Subroutine
gnuplot: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 0 | my $self = shift; | |||||||||||||||||||
| 130 | 0 | my %gnuplot = @_; | |||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 0 | ▶ | return undef unless $self->{rpgraph}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 0 | ▶ | $gnuplot{output} ||= '/tmp/RPave'; | ||||||||||||||||||
|
Condition0%0/2
$gnuplot{"output"} ||= "/tmp/RPave"
MC/DC0%0/1
$gnuplot{"output"} ||= "/tmp/RPave"
$gnuplot{"output"}
Truth table0%0/2
A
A= $gnuplot{"output"}
| |||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 0 | RePrec::Tools::gnuplot(@{$self->{rpgraph}}, \%gnuplot); | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =pod #---------------------------------------------------------------# | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =item $rp->write_rpdata($file, [$average]); | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | Write the recall-precision data to file(s). Writes data for average | ||||||||||||||||||||
| 145 | precision if $average is true. | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | =cut #---------------------------------------------------------------# | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 0 | ▶ | sub write_rpdata { | ||||||||||||||||||
|
Subroutine
write_rpdata: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 0 | my $self = shift; | |||||||||||||||||||
| 152 | 0 | my $file = shift; | |||||||||||||||||||
| 153 | 0 | my $average = shift; | |||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 0 | ▶ | return undef unless $self->{rpgraph}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 0 | ▶ | if ($average) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | 0 | RePrec::Tools::write_rpdata($file, @{$self->{rpgraph}}); | |||||||||||||||||||
| 159 | } else { | ||||||||||||||||||||
| 160 | 0 | RePrec::Tools::write_rpdata($file, $self->{rpgraph}->[0]); | |||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | ## private ########################################################### | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =pod #---------------------------------------------------------------# | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =back | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =head1 BUGS | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | Yes. Please let me know! | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | =head1 SEE ALSO | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | gnuplot(1), | ||||||||||||||||||||
| 178 | perl(1). | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =head1 AUTHOR | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | Norbert GE<ouml>vert E<lt>F<goevert@ls6.cs.uni-dortmund.de>E<gt> | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | =cut #---------------------------------------------------------------# | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 1 | 1; | |||||||||||||||||||
| 188 | __END__ | ||||||||||||||||||||