File Coverage

blib/lib/RePrec/Average.pm
Criterion Covered Total %
statement 9 48 18.7
branch 0 8 0.0
condition 0 5 0.0
subroutine 3 7 42.8
pod 4 4 100.0
total 16 72 22.2


line stmt bran cond sub pod time code
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     1   449 use strict;
  1         2  
  1         41  
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     1   5 use Carp;
  1         1  
  1         53  
48 1     1   5 use IO::File;
  1         2  
  1         605  
49              
50             require RePrec::Tools;
51              
52              
53             our $VERSION;
54             '$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             sub new {
68              
69 0     0 1   my $proto = shift;
70 0   0       my $class = ref($proto) || $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             sub calculate {
91              
92 0     0 1   my $self = shift;
93 0           my $points = shift;
94              
95 0           my(%sum, $sum);
96 0           foreach (@{$self->{rp}}) {
  0            
97 0           my($result, $average) = @{$_->calculate($points)};
  0            
98 0 0         unless ($result) {
99 0           $self->{divisor}--;
100 0           next;
101             }
102 0           foreach my $point (@{$result}) {
  0            
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             sub gnuplot {
128              
129 0     0 1   my $self = shift;
130 0           my %gnuplot = @_;
131              
132 0 0         return undef unless $self->{rpgraph};
133              
134 0   0       $gnuplot{output} ||= '/tmp/RPave';
135              
136 0           RePrec::Tools::gnuplot(@{$self->{rpgraph}}, \%gnuplot);
  0            
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             sub write_rpdata {
150              
151 0     0 1   my $self = shift;
152 0           my $file = shift;
153 0           my $average = shift;
154              
155 0 0         return undef unless $self->{rpgraph};
156              
157 0 0         if ($average) {
158 0           RePrec::Tools::write_rpdata($file, @{$self->{rpgraph}});
  0            
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 GEvert EFE
183              
184             =cut #---------------------------------------------------------------#
185              
186              
187             1;
188             __END__