lib/Statistics/Candidates.pm

filter: Statementstmt 98.6% 74 / 75 Branchbran 76.6% 23 / 30 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 90.9% 10 / 11 Podpod 80.0% 4 / 5 totaltotal 83.8% 114 / 136 CC 19 SCAR 16.5
CC
19
Cov
88%
write
9.1
check
5.3
read
5.0
CRAP 5.2
« lib/Statistics/MaxEntropy.pm ↑ Summary lib/Statistics/SparseVector.pm »
1 package Statistics::Candidates;
2  
3 ##---------------------------------------------------------------------------##
4 ## Author:
5 ## Hugo WL ter Doest terdoest@cs.utwente.nl
6 ## Description:
7 ## Object/methods for candidate features
8 ##
9 ##---------------------------------------------------------------------------##
10 ## Copyright (C) 1998, 1999 Hugo WL ter Doest terdoest@cs.utwente.nl
11 ##
12 ## This library is free software; you can redistribute it and/or modify
13 ## it under the terms of the GNU General Public License as published by
14 ## the Free Software Foundation; either version 2 of the License, or
15 ## (at your option) any later version.
16 ##
17 ## This library is distributed in the hope that it will be useful,
18 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ## GNU General Public License for more details.
21 ##
22 ## You should have received a copy of the GNU Library General Public
23 ## License along with this program; if not, write to the Free Software
24 ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 ##---------------------------------------------------------------------------##
26  
27  
28 ##---------------------------------------------------------------------------##
29 ## Globals
30 ##---------------------------------------------------------------------------##
31 2 ▶use vars qw($VERSION
Subroutine BEGIN: called CC=1 SCAR=0.0
32 @ISA
33 @EXPORT
34 2 $VECTOR_PACKAGE);
35  
36  
37 ##---------------------------------------------------------------------------##
38 ## Require libraries
39 ##---------------------------------------------------------------------------##
40 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use diagnostics -verbose;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use Statistics::SparseVector;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 $VECTOR_PACKAGE = "Statistics::SparseVector";
45 2 require Exporter;
46 2 require AutoLoader;
47  
48 2 @ISA = qw(Exporter AutoLoader);
49 # Items to export into callers namespace by default. Note: do not export
50 # names by default without a very good reason. Use EXPORT_OK instead.
51 # Do not simply export all your public functions/methods/constants.
52 2 @EXPORT = qw(
53 );
54  
55  
56 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
57 1 my($this, $arg) = @_;
58  
59 # for calling $self->new($someth):
60 1 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
61 1 my $self = {};
62 1 bless $self, $class;
63 1 ▶ if ($arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 1 0 1
64 1 $self->read($arg);
65 }
66 1 return($self);
67 }
68  
69  
70 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
71  
72 }
73  
74  
75 # reads a candidates file
76 # dies if insufficient events or inconsistent lines
77 # syntax first line: <name> <tab> <name> <tab> ... <cr>
78 # syntax other lines: <bitvector>
79 1 ▶sub read {
Subroutine read: called CC=5 SCAR=16.1
Pod undocumented
80 1 my($self, $file) = @_;
81  
82 1 my($features,
83 $sum,
84 $event,
85 $candidate_names);
86  
87 # prologue
88 1 ▶ open(CANDS, $file) ||
Branch: 1 branch
BranchTrueFalseTotal
open CANDS, $file or die "Could not open $file\n" 1 0 1
89 die "Could not open $file\n";
90 1 print "Opened $file\n";
91  
92 # read candidate names, skip comments
93 1 $candidate_names = "";
94 1 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
not $candidate_names =~ /\#.*/ or do { do { $candidate_names = readline CANDS } } 1 0 1
95 1 $candidate_names = <CANDS>;
96 } until ($candidate_names !~ /\#.*/);
97 1 chomp $candidate_names;
98 1 $self->{CANDIDATE_NAMES} = [split(/\t/,$candidate_names)];
99 1 $self->{NR_CANDIDATES} = $#{$self->{CANDIDATE_NAMES}} + 1;
100 # read the candidate bitvectors
101 1 $self->{NR_CLASSES} = 0;
102 1 ▶ while (<CANDS>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CANDS)) 100 1 101
103 100 ▶ if (!/\#.*/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\#.*/) 100 0 100
104 100 chomp;
105 100 $features = $_;
106 $self->{CANDIDATES}[$self->{NR_CLASSES}++] =
107 100 $VECTOR_PACKAGE->new_vec($self->{NR_CANDIDATES}, $features, "binary");
108 }
109 }
110  
111 # epilogue
112 1 close(CANDS);
113 # check the candidates for constant functions
114 1 $self->check();
115 1 print "Read $self->{NR_CANDIDATES} candidates for $self->{NR_CLASSES} events; ";
116 1 print "closed $file\n";
117 }
118  
119  
120 # check whether for all features f, \sum_x f(x) > 0, and
121 # \sum_x f(x) != nr_classes
122 1 ▶sub check {
Subroutine check: called CC=5 SCAR=16.7
Pod documented
123 1 my($self) = @_;
124  
125 1 my($x,
126 $f,
127 $sum);
128  
129 1 ▶ for ($f = 0; $f < $self->{NR_CANDIDATES}; $f++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f < $self->{"NR_CANDIDATES"}) 3 1 4
130 3 $sum = 0;
131 3 ▶ for ($x = 0; $x < $self->{NR_CLASSES}; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"NR_CLASSES"}) 300 3 303
132 300 $sum += $self->{CANDIDATES}[$x]->bit_test($f);
133 }
134 3 ▶ if (!$sum || ($sum == $self->{NR_CLASSES})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sum or $sum == $self->{"NR_CLASSES"}) 0 3 3
Condition33%1/3
not $sum or $sum == $self->{"NR_CLASSES"}
l!l&&r!l&&!r
003
MC/DC0%0/2
not $sum or $sum == $self->{"NR_CLASSES"}
not $sum $sum == $self->{"NR_CLASSES"}
Truth table33%1/3
not(A) or B
A= not $sumB= $sum == $self->{"NR_CLASSES"}
ABresult
1X1
011
000
135 0 croak "Candidate ",$f+1, " is constant, remove it\n";
136 }
137 }
138 }
139  
140  
141 # writes remaining candidates to a file
142 # syntax: same as input candidates file
143 1 ▶sub write {
Subroutine write: called CC=9 SCAR=22.1
Pod documented
144 1 my($self, $file) = @_;
145  
146 1 my($x,
147 $f);
148  
149 1 ▶ if (($self->{NR_CANDIDATES} > 0) && ($self->{NR_CLASSES})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"NR_CANDIDATES"} > 0 and $self->{"NR_CLASSES"}) 1 0 1
Condition33%1/3
$self->{"NR_CANDIDATES"} > 0 and $self->{"NR_CLASSES"}
!ll&&!rl&&r
001
MC/DC0%0/2
$self->{"NR_CANDIDATES"} > 0 and $self->{"NR_CLASSES"}
$self->{"NR_CANDIDATES"} > 0 $self->{"NR_CLASSES"}
Truth table33%1/3
A and B
A= $self->{"NR_CANDIDATES"} > 0B= $self->{"NR_CLASSES"}
ABresult
0X0
100
111
150 1 ▶ open(CANDIDATES,">$file") ||
Branch: 1 branch
BranchTrueFalseTotal
open CANDIDATES, ">$file" or die "Could not open $file\n" 1 0 1
151 die "Could not open $file\n";
152 1 print "Opened $file\n";
153  
154 # write the list of candidate names that were not added
155 1 ▶ for ($f = 0; $f < $self->{NR_CANDIDATES}; $f++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f < $self->{"NR_CANDIDATES"}) 3 1 4
156 3 ▶ if (!$self->{ADDED}{$f}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ADDED"}{$f}) 1 2 3
157 1 print CANDIDATES "$self->{CANDIDATE_NAMES}[$f]\t";
158 }
159 }
160 1 print CANDIDATES "\n";
161  
162 # write candidates that were not added
163 1 ▶ for ($x = 0; $x < $self->{NR_CLASSES}; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"NR_CLASSES"}) 100 1 101
164 100 ▶ for ($f = 0; $f < $self->{NR_CANDIDATES}; $f++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f < $self->{"NR_CANDIDATES"}) 300 100 400
165 300 ▶ if (!$self->{ADDED}{$f}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ADDED"}{$f}) 100 200 300
166 100 print CANDIDATES $self->{CANDIDATES}[$x]->bit_test($f);
167 }
168 }
169 100 print CANDIDATES "\n";
170 }
171  
172 1 close CANDIDATES;
173 1 print "Closed $file\n";
174 }
175 }
176  
177  
178 4 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
179 4 my($self) = @_;
180  
181 4 undef $self->{ADDED};
182 }
183  
184 2 1;
185  
186 __END__
« lib/Statistics/MaxEntropy.pm ↑ Summary lib/Statistics/SparseVector.pm »