| 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
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||||||||||
| 61 | 1 | my $self = {}; | |||||||||||||||||||||||||||
| 62 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 63 | 1 | ▶ | if ($arg) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 103 | 100 | ▶ | if (!/\#.*/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 130 | 3 | $sum = 0; | |||||||||||||||||||||||||||
| 131 | 3 | ▶ | for ($x = 0; $x < $self->{NR_CLASSES}; $x++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 300 | $sum += $self->{CANDIDATES}[$x]->bit_test($f); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 3 | ▶ | if (!$sum || ($sum == $self->{NR_CLASSES})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $sum or $sum == $self->{"NR_CLASSES"}
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"}
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$self->{"NR_CANDIDATES"} > 0 and $self->{"NR_CLASSES"}
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"}
| |||||||||||||||||||||||||||||
| 150 | 1 | ▶ | open(CANDIDATES,">$file") || | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 156 | 3 | ▶ | if (!$self->{ADDED}{$f}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 164 | 100 | ▶ | for ($f = 0; $f < $self->{NR_CANDIDATES}; $f++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 300 | ▶ | if (!$self->{ADDED}{$f}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||