RePrec/Tools.pm

filter: Statementstmt 25.7% 18 / 70 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 44.4% 4 / 9 Podpod 100% 5 / 5 totaltotal 20.0% 27 / 135 CC 23 SCAR 42.7
CC
23
Cov
16%
gnuplot
156.0
write_rpdata
30.0
system
30.0
CRAP 71.7
« RePrec/Collection.pm ↑ Summary RePrec.pm »
1 ###*###################### -*- Mode: Perl -*- #########################
2 ##
3 ## File : $RCSfile: Tools.pm,v $
4 ##
5 ## Author : Norbert Gövert
6 ## Created On : Fri Nov 10 13:21:58 2000
7 ## Last Modified : Time-stamp: <2002-04-25 17:18:31 goevert>
8 ##
9 ## Description :
10 ##
11 ## $Id: Tools.pm,v 1.6 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::Tools - Collection of tools for RePrec(3) libraries
24  
25 =head1 SYNOPSIS
26  
27 use RePrec::Tools qw(gnuplot system choose fac);
28  
29 =head1 DESCRIPTION
30  
31 Functions shared between the various RePrec(3) libraries.
32  
33 =head1 FUNCTIONS
34  
35 =over
36  
37 =cut #---------------------------------------------------------------#
38  
39 package RePrec::Tools;
40  
41  
42 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44  
45 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48  
49 1 our $VERSION;
50 1 '$Name: release_0_32 $ 0_0' =~ /(\d+)[-_](\d+)/; $VERSION = sprintf '%d.%03d', $1, $2;
51  
52 1 our @EXPORT_OK = qw( gnuplot
53 system
54 choose
55 fac
56 write_rpdata
57 );
58  
59  
60 ## public ############################################################
61  
62 =cut #---------------------------------------------------------------#
63  
64 =item gnuplot($rpdata, $average, $gnuplot)
65  
66 plot curve with gnuplot(1). $rpdata and $average are the data for the
67 curves to be displayed. $gnuplot is a hash reference where
68 configuration options for gnuplot can be set. The default settings
69 are:
70  
71 style => 'lines'
72 title => 'Recall-Precision'
73 ylabel => 'Precision'
74 xlabel => 'Recall'
75 output => '/tmp/RP'
76 binary => 'gnuplot'
77  
78 The I<output> parameter gives a prefix name used for files created
79 during the plotting. By default the following files are created:
80 F</tmp/RP.dat> (holds the data for the curves), F</tmp/RP.average.dat>
81 (holds the average precision), and F</tmp/RP.gp> (holds the gnuplot
82 config file).
83  
84 The I<binary> parameter gives the name of the gnuplot binary. The
85 I<terminal> parameter selects the gnuplot terminal to use (for
86 example: C<postscript eps enhanced 22>).
87  
88 =cut #---------------------------------------------------------------#
89  
90 0 ▶sub gnuplot {
Subroutine gnuplot: not called CC=12 SCAR=50.5
Pod documented
91  
92 0 my($rpdata, $average, $gnuplot) = @_;
93  
94 0 ▶ my $file = $gnuplot->{output} || "/tmp/RP";
Condition0%0/2
$gnuplot->{'output'} || '/tmp/RP'
l!l
00
MC/DC0%0/1
$gnuplot->{'output'} || '/tmp/RP'
$gnuplot->{'output'}
Truth table0%0/2
A
A= $gnuplot->{'output'}
Aresult
11
00
95 0 ▶ $file = "/tmp/RP" unless $file =~ s/^\s*([A-z\/.\d_-]+).*$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ s[^\s*([A-z/.\d_-]+).*$][$1] 0 0 0
96  
97 0 ▶ my $style = $gnuplot->{style} || 'lines';
Condition0%0/2
$gnuplot->{'style'} || 'lines'
l!l
00
MC/DC0%0/1
$gnuplot->{'style'} || 'lines'
$gnuplot->{'style'}
Truth table0%0/2
A
A= $gnuplot->{'style'}
Aresult
11
00
98 0 ▶ my $title = $gnuplot->{title} || 'Recall-Precision';
Condition0%0/2
$gnuplot->{'title'} || 'Recall-Precision'
l!l
00
MC/DC0%0/1
$gnuplot->{'title'} || 'Recall-Precision'
$gnuplot->{'title'}
Truth table0%0/2
A
A= $gnuplot->{'title'}
Aresult
11
00
99 0 ▶ my $ylabel = $gnuplot->{ylabel} || 'Precision';
Condition0%0/2
$gnuplot->{'ylabel'} || 'Precision'
l!l
00
MC/DC0%0/1
$gnuplot->{'ylabel'} || 'Precision'
$gnuplot->{'ylabel'}
Truth table0%0/2
A
A= $gnuplot->{'ylabel'}
Aresult
11
00
100 0 ▶ my $xlabel = $gnuplot->{xlabel} || 'Recall';
Condition0%0/2
$gnuplot->{'xlabel'} || 'Recall'
l!l
00
MC/DC0%0/1
$gnuplot->{'xlabel'} || 'Recall'
$gnuplot->{'xlabel'}
Truth table0%0/2
A
A= $gnuplot->{'xlabel'}
Aresult
11
00
101  
102 0 my $head = qq{
103 set title "$title"
104 set ylabel "$ylabel"
105 set xlabel "$xlabel"
106 set xrange [0:1]
107 set yrange [0:1]
108 set xtics 0,.5,1
109 set ytics 0,.2,1
110 #set xtics 0,.1,1
111 #set ytics 0,.1,1
112 set data style $style
113 set size square 0.757, 1.0
114 set grid
115 };
116  
117 0 my $plot = "plot '$file.average.dat' title 'Average', '$file.dat' title 'Recall-Precision'\n";
118  
119 0 ▶ if (defined $gnuplot->{terminal} and $gnuplot->{terminal} =~ /postscript/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gnuplot->{'terminal'} and $gnuplot->{'terminal'} =~ /postscript/i) { } 0 0 0
Condition0%0/3
defined $gnuplot->{"terminal"} and $gnuplot->{"terminal"} =~ /postscript/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $gnuplot->{"terminal"} and $gnuplot->{"terminal"} =~ /postscript/i
defined $gnuplot->{"terminal"} $gnuplot->{"terminal"} =~ /postscript/i
Truth table0%0/3
A and B
A= defined $gnuplot->{"terminal"}B= $gnuplot->{"terminal"} =~ /postscript/i
ABresult
0X0
100
111
120 0 my $ext = 'ps';
121 0 ▶ $ext = 'eps' if $gnuplot->{terminal} =~ /eps/i;
Branch: 1 branch
BranchTrueFalseTotal
if $gnuplot->{"terminal"} =~ /eps/i 0 0 0
122 0 $head .= qq{
123 set terminal $gnuplot->{terminal}
124 set output "$file.$ext"
125 $plot
126 };
127 } else {
128 0 $head .= $plot . "pause -1 'Hit return to continue... '\n";
129 }
130  
131 # write gnuplot config file
132 0 ▶ my $GP = IO::File->new("$file.gp", 'w')
Branch: 1 branch
BranchTrueFalseTotal
my $GP = "IO::File"->new("$file.gp", "w") or croak("Couldn't write open file `$file.gp': $!\n") 0 0 0
133 or croak "Couldn't write open file `$file.gp': $!\n";
134 0 $GP->print($head);
135 0 $GP->close;
136  
137 0 write_rpdata($file, $rpdata, $average);
138  
139 # call gnuplot?!
140 0 ▶ my $GPbin = $gnuplot->{binary} || 'gnuplot';
Condition0%0/2
$gnuplot->{'binary'} || 'gnuplot'
l!l
00
MC/DC0%0/1
$gnuplot->{'binary'} || 'gnuplot'
$gnuplot->{'binary'}
Truth table0%0/2
A
A= $gnuplot->{'binary'}
Aresult
11
00
141 0 &system($GPbin, "$file.gp");
142 }
143  
144  
145 =pod #---------------------------------------------------------------#
146  
147 =item write_rpdata($file, $rpdata, [$average]);
148  
149 Write the recall precision data to file(s).
150  
151 =cut #---------------------------------------------------------------#
152  
153 0 ▶sub write_rpdata {
Subroutine write_rpdata: not called CC=5 SCAR=34.0
Pod documented
154  
155 0 my($file, $rpdata, $average) = @_;
156  
157 # write gnuplot data file for curve
158 0 ▶ my $fh = IO::File->new("$file.dat", 'w')
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new("$file.dat", "w") or croak("Couldn't write open file `$file.dat': $!\n") 0 0 0
159 or croak "Couldn't write open file `$file.dat': $!\n";
160 0 foreach (@{$rpdata}) {
161 0 $fh->print("$_->[0] $_->[1]\n");
162 }
163 0 $fh->close;
164  
165 0 ▶ return unless defined $average;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $average 0 0 0
166  
167 # write gnuplot data file for average
168 0 ▶ $fh = IO::File->new("$file.average.dat", 'w')
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new("$file.average.dat", "w") or croak("Couldn't write open file `$file.average.dat': $!\n") 0 0 0
169 or croak"Couldn't write open file `$file.average.dat': $!\n";
170 0 $fh->print("0 $average\n1 $average\n");
171 0 $fh->close;
172 }
173  
174  
175 =pod #---------------------------------------------------------------#
176  
177 =item $rp->system(@args)
178  
179 forks of a process and executes therein the command given by @args
180 (list of executable's name and arguments). Displays some proper return
181 status interpretations.
182  
183 =cut #---------------------------------------------------------------#
184  
185 0 ▶sub system {
Subroutine system: not called CC=5 SCAR=34.0
Pod documented
186  
187 0 my @args = @_;
188  
189 0 my $rc = 0xffff & system @args;
190  
191 0 printf STDERR "system(%s) returned %#04x: ", "@args", $rc;
192  
193 0 ▶ if ($rc == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rc == 0) { } 0 0 0
elsif ($rc == 65280) { } 0 0 0
elsif ($rc > 128) { } 0 0 0
194 0 print STDERR "ran with normal exit\n";
195 } elsif ($rc == 0xff00) {
196 0 print STDERR "command failed: $!\n";
197 } elsif ($rc > 0x80) {
198 0 $rc >>= 8;
199 0 print STDERR "ran with non-zero exit status $rc\n";
200 } else {
201 0 print STDERR "ran with ";
202 0 ▶ if ($rc & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc & 128) 0 0 0
203 0 $rc &= ~0x80;
204 0 print STDERR "core dump from ";
205 }
206 0 print STDERR "signal $rc\n"
207 }
208 0 my $ok = ($rc != 0);
209 0 print STDERR "ok: $ok\n";
210 }
211  
212  
213 =pod #---------------------------------------------------------------#
214  
215 =item $bc = choose($n, $k)
216  
217 computes the binomial coefficient for $n over $k.
218  
219 =cut #---------------------------------------------------------------#
220  
221 0 ▶sub choose {
Subroutine choose: not called CC=2 SCAR=17.9
Pod documented
222  
223 0 my($n, $k) = @_;
224  
225 0 ▶ die "choose($n, $k) not defined" if $n < $k;
Branch: 1 branch
BranchTrueFalseTotal
if $n < $k 0 0 0
226  
227 0 fac($n) / ( fac($k) * fac($n - $k));
228 }
229  
230  
231 =pod #---------------------------------------------------------------#
232  
233 =item $fac = fac($n)
234  
235 computes faculty of $n.
236  
237 =cut #---------------------------------------------------------------#
238  
239 1 our @_fac = (1, 1);
240 0 ▶sub fac {
Subroutine fac: not called CC=3 SCAR=24.8
Pod documented
241  
242 0 my $n = shift;
243  
244 0 ▶ die "fac($n) not defined" if $n < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 0 0 0 0
245  
246 0 ▶ return $_fac[$n] if exists $_fac[$n];
Branch: 1 branch
BranchTrueFalseTotal
if exists $RePrec::Tools::_fac[$n] 0 0 0
247 0 $_fac[$n] = $n * fac($n - 1);
248 }
249  
250  
251 =pod #---------------------------------------------------------------#
252  
253 =back
254  
255 =head1 BUGS
256  
257 Yes. Please let me know!
258  
259 =head1 SEE ALSO
260  
261 RePrec::Average(3),
262 RePrec(3),
263 perl(1).
264  
265 =head1 AUTHOR
266  
267 Norbert Gövert E<lt>F<goevert@ls6.cs.uni-dortmund.de>E<gt>
268  
269 =cut #---------------------------------------------------------------#
270  
271  
272 1 1;
273 __END__
« RePrec/Collection.pm ↑ Summary RePrec.pm »