blib/lib/Algorithm/AM/Batch.pm

filter: Statementstmt 84.3% 113 / 134 Branchbran 78.0% 39 / 50 Conditioncond 66.6% 14 / 21 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 94.4% 17 / 18 Podpod 66.6% 2 / 3 totaltotal 79.5% 190 / 239 CC 36 SCAR 23.5
CC
36
Cov
81%
classify_all
18.2
_make_training_set
12.0
_log_result
12.0
CRAP 10.5
« blib/script/analogize.pl ↑ Summary blib/lib/Algorithm/AM.pm »
1 package Algorithm::AM::Batch;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 our $VERSION = '3.13';
5 # ABSTRACT: Classify items in batch mode
6 4 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 our @CARP_NOT = qw(Algorithm::AM::Batch);
10  
11 # Place this accessor here so that Class::Tiny doesn't generate
12 # a getter/setter pair.
13 38 ▶sub test_set {
Subroutine test_set: called CC=1 SCAR=0.0
Pod documented
14 38 my ($self) = @_;
15 38 return $self->{test_set};
16 }
17  
18 4 ▶use Class::Tiny qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
19 training_set
20  
21 exclude_nulls
22 exclude_given
23 linear
24 probability
25 repeat
26 max_training_items
27  
28 begin_hook
29 begin_test_hook
30 begin_repeat_hook
31 training_item_hook
32 end_repeat_hook
33 end_test_hook
34 end_hook
35  
36 test_set
37 ), {
38 exclude_nulls => 1,
39 exclude_given => 1,
40 linear => 0,
41 probability => 1,
42 repeat => 1,
43 4 };
44  
45 4 ▶use Algorithm::AM;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 4 ▶use Algorithm::AM::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 4 ▶use Algorithm::AM::BigInt 'bigcmp';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 4 ▶use Algorithm::AM::DataSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 4 ▶use Import::Into;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 # Use Import::Into to export classes into caller
51 4 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
52 4 my $target = caller;
53 4 Algorithm::AM::BigInt->import::into($target, 'bigcmp');
54 4 Algorithm::AM::DataSet->import::into($target, 'dataset_from_file');
55 4 Algorithm::AM::DataSet::Item->import::into($target, 'new_item');
56 4 return;
57 }
58  
59 14 ▶sub BUILD {
Subroutine BUILD: called CC=8 SCAR=21.0
Pod undocumented
60 14 my ($self, $args) = @_;
61  
62 # check for invalid arguments
63 14 my $class = ref $self;
64 14 my %valid_attrs = map {$_ => 1}
65 Class::Tiny->get_all_attributes_for($class);
66 14 my @invalids = grep {!$valid_attrs{$_}} sort keys %$args;
67 14 ▶ if(@invalids){
Branch: 1 branch
BranchTrueFalseTotal
if (@invalids) 1 13 14
68 1 croak "Invalid attributes for $class: " . join ' ',
69 sort @invalids;
70 }
71  
72 13 ▶ if(!exists $args->{training_set}){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"training_set"}) 1 12 13
73 1 croak "Missing required parameter 'training_set'";
74 }
75 12 ▶ if(!(ref $args) || !$args->{training_set}->isa(
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args and $args->{"training_set"}->isa("Algorithm::AM::DataSet")) 1 11 12
Condition66%2/3
ref $args and $args->{"training_set"}->isa("Algorithm::AM::DataSet")
!ll&&!rl&&r
0111
MC/DC50%1/2
ref $args and $args->{"training_set"}->isa("Algorithm::AM::DataSet")
ref $args $args->{"training_set"}->isa("Algorithm::AM::DataSet")
Truth table66%2/3
A and B
A= ref $argsB= $args->{"training_set"}->isa("Algorithm::AM::DataSet")
ABresult
0X0
100
111
76 'Algorithm::AM::DataSet')){
77 1 croak 'Parameter training_set should be an ' .
78 'Algorithm::AM::DataSet';
79 }
80 11 for(qw(
81 begin_hook
82 begin_test_hook
83 begin_repeat_hook
84 training_item_hook
85 end_repeat_hook
86 end_test_hook
87 end_hook
88 )){
89 77 ▶ if(exists $args->{$_} and 'CODE' ne ref $args->{$_}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{$_} and "CODE" ne ref $args->{$_}) 0 77 77
Condition66%2/3
exists $args->{$_} and "CODE" ne ref $args->{$_}
!ll&&!rl&&r
67100
MC/DC0%0/2
exists $args->{$_} and "CODE" ne ref $args->{$_}
exists $args->{$_} "CODE" ne ref $args->{$_}
Truth table66%2/3
A and B
A= exists $args->{$_}B= "CODE" ne ref $args->{$_}
ABresult
0X0
100
111
90 0 croak "Input $_ should be a subroutine";
91 }
92 }
93  
94 11 return;
95 }
96  
97 7 ▶sub classify_all {
Subroutine classify_all: called CC=17 SCAR=29.0
Pod documented
98 7 my ($self, $test_set) = @_;
99  
100 7 ▶ if(!$test_set || 'Algorithm::AM::DataSet' ne ref $test_set){
Branch: 1 branch
BranchTrueFalseTotal
if (not $test_set or "Algorithm::AM::DataSet" ne ref $test_set) 2 5 7
Condition100%3/3
not $test_set or "Algorithm::AM::DataSet" ne ref $test_set
l!l&&r!l&&!r
115
MC/DC100%2/2
not $test_set or "Algorithm::AM::DataSet" ne ref $test_set
not $test_set "Algorithm::AM::DataSet" ne ref $test_set
Truth table100%3/3
not(A) or B
A= not $test_setB= "Algorithm::AM::DataSet" ne ref $test_set
ABresult
1X1
011
000
101 2 croak q[Must provide a DataSet to classify_all];
102 }
103 5 ▶ if($self->training_set->cardinality != $test_set->cardinality){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->training_set->cardinality != $test_set->cardinality) 1 4 5
104 1 croak 'Training and test sets do not have the same ' .
105 'cardinality (' . $self->training_set->cardinality .
106 ' and ' . $test_set->cardinality . ')';
107 }
108 4 $self->_set_test_set($test_set);
109  
110 4 ▶ if($self->begin_hook){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->begin_hook) 1 3 4
111 1 $self->begin_hook->($self);
112 }
113  
114 # save the result objects from all items, all iterations, here
115 4 my @all_results;
116  
117 4 foreach my $item_number (0 .. $test_set->size - 1) {
118 178 ▶ if($log->is_debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($Algorithm::AM::Batch::log->is_debug) 0 178 178
119 0 $log->debug('Test items left: ' .
120 $test_set->size + 1 - $item_number);
121 }
122 178 my $test_item = $test_set->get_item($item_number);
123 # store the results just for this item
124 178 my @item_results;
125  
126 178 ▶ if($self->begin_test_hook){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->begin_test_hook) 2 176 178
127 2 $self->begin_test_hook->($self, $test_item);
128 }
129  
130 178 ▶ if($log->is_debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($Algorithm::AM::Batch::log->is_debug) 0 178 178
131 0 my ( $sec, $min, $hour ) = localtime();
132 0 $log->info(
133 sprintf( "Time: %2s:%02s:%02s\n", $hour, $min, $sec) .
134 $test_item->comment . "\n" .
135 sprintf( "0/$self->{repeat} %2s:%02s:%02s",
136 $hour, $min, $sec ) );
137 }
138  
139 178 my $iteration = 1;
140 178 ▶ while ( $iteration <= $self->repeat ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iteration <= $self->repeat) 182 178 360
141 182 ▶ if($self->begin_repeat_hook){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->begin_repeat_hook) 4 178 182
142 4 $self->begin_repeat_hook->(
143 $self, $test_item, $iteration);
144 }
145  
146 # this sets excluded_items
147 182 my ($training_set, $excluded_items) = $self->_make_training_set(
148 $test_item, $iteration);
149  
150 # classify the item with the given training set and
151 # configuration
152 182 my $am = Algorithm::AM->new(
153 training_set => $training_set,
154 exclude_nulls => $self->exclude_nulls,
155 exclude_given => $self->exclude_given,
156 linear => $self->linear,
157 );
158 182 my $result = $am->classify($test_item);
159  
160 182 ▶ _log_result($result)
Branch: 1 branch
BranchTrueFalseTotal
if $Algorithm::AM::Batch::log->is_info 0 182 182
161 if($log->is_info);
162  
163 182 ▶ if($log->is_info){
Branch: 1 branch
BranchTrueFalseTotal
if ($Algorithm::AM::Batch::log->is_info) 0 182 182
164 0 my ( $sec, $min, $hour ) = localtime();
165 0 $log->info(
166 sprintf(
167 $iteration . '/' . $self->repeat .
168 ' %2s:%02s:%02s',
169 $hour, $min, $sec
170 )
171 );
172 }
173  
174 182 ▶ if($self->end_repeat_hook){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->end_repeat_hook) 5 177 182
175 # pass in self, test item, data, and result
176 5 $self->end_repeat_hook->($self, $test_item,
177 $iteration, $excluded_items, $result);
178 }
179 182 push @item_results, $result;
180 182 $iteration++;
181 }
182  
183 178 ▶ if($self->end_test_hook){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->end_test_hook) 175 3 178
184 175 $self->end_test_hook->($self, $test_item, @item_results);
185 }
186  
187 178 push @all_results, @item_results;
188 }
189  
190 4 ▶ if($log->is_info){
Branch: 1 branch
BranchTrueFalseTotal
if ($Algorithm::AM::Batch::log->is_info) 0 4 4
191 0 my ( $sec, $min, $hour ) = localtime();
192 0 $log->info(
193 sprintf( "Time: %2s:%02s:%02s", $hour, $min, $sec ) );
194 }
195  
196 4 ▶ if($self->end_hook){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->end_hook) 1 3 4
197 1 $self->end_hook->($self, @all_results);
198 }
199 4 $self->_set_test_set(undef);
200 4 return @all_results;
201 }
202  
203 # log the summary printouts from the input result object
204 0 ▶sub _log_result {
Subroutine _log_result: not called CC=3 SCAR=24.8
205 0 my ($result) = @_;
206  
207 0 $log->info(${$result->statistical_summary});
208  
209 0 $log->info(${$result->analogical_set_summary()});
210  
211 0 ▶ if($log->is_debug){
Branch: 2 branches
BranchTrueFalseTotal
if ($Algorithm::AM::Batch::log->is_debug) { } 0 0 0
elsif ($Algorithm::AM::Batch::log->is_info) { } 0 0 0
212 0 $log->debug(${ $result->gang_summary(1) });
213 }elsif($log->is_info){
214 0 $log->info(${ $result->gang_summary(0) })
215 }
216 0 return;
217 }
218  
219 # create the training set for this iteration, calling training_item_hook and
220 # updating excluded_items along the way
221 182 ▶sub _make_training_set {
Subroutine _make_training_set: called CC=11 SCAR=24.9
222 182 my ($self, $test_item, $iteration) = @_;
223 182 my $training_set;
224  
225 # $self->_set_excluded_items([]);
226 my @excluded_items;
227 # Cap the amount of considered data if specified
228 182 ▶ my $max = defined $self->max_training_items ?
Branch: 1 branch
BranchTrueFalseTotal
defined $self->max_training_items ? : 4 178 182
229 int($self->max_training_items) :
230 $self->training_set->size;
231  
232 # use the original DataSet object if there are no settings
233 # that would trim items from it
234 182 ▶ if(!$self->training_item_hook &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->training_item_hook and $self->probability == 1 and $max >= $self->training_set->size) { } 177 5 182
Condition66%4/6
not $self->training_item_hook and $self->probability == 1 and $max >= $self->training_set->size
!ll&&!rl&&r
50177
not $self->training_item_hook and $self->probability == 1
!ll&&!rl&&r
50177
MC/DC33%1/3
not $self->training_item_hook and $self->probability == 1 and $max >= $self->training_set->size
not $self->training_item_hook $self->probability == 1 $max >= $self->training_set->size
Truth table50%2/4
not(A) and B and C
A= not $self->training_item_hookB= $self->probability == 1C= $max >= $self->training_set->size
ABCresult
0XX0
10X0
1100
1111
235 ($self->probability == 1) &&
236 $max >= $self->training_set->size){
237 177 $training_set = $self->training_set;
238 }else{
239 # otherwise, make a new set with just the selected
240 # items
241 5 $training_set = Algorithm::AM::DataSet->new(
242 cardinality => $self->training_set->cardinality);
243  
244 # don't try to add more items than we have!
245 5 ▶ my $num_items = ($max > $self->training_set->size) ?
Branch: 1 branch
BranchTrueFalseTotal
$max > $self->training_set->size ? : 4 1 5
246 $self->training_set->size :
247 $max;
248 5 for my $data_index ( 0 .. $num_items - 1 ) {
249 25 my $training_item =
250 $self->training_set->get_item($data_index);
251 # skip this data item if the training_item_hook returns false
252 25 ▶ if($self->training_item_hook &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->training_item_hook and not $self->training_item_hook->($self, $test_item, $iteration, $training_item)) 5 20 25
Condition66%2/3
$self->training_item_hook and not $self->training_item_hook->($self, $test_item, $iteration, $training_item)
!ll&&!rl&&r
0205
MC/DC50%1/2
$self->training_item_hook and not $self->training_item_hook->($self, $test_item, $iteration, $training_item)
$self->training_item_hook not $self->training_item_hook->($self, $test_item, $iteration, $training_item)
Truth table66%2/3
A and not(B)
A= $self->training_item_hookB= not $self->training_item_hook->($self, $test_item, $iteration, $training_item)
ABresult
0X0
100
111
253 !$self->training_item_hook->($self,
254 $test_item, $iteration, $training_item)
255 ){
256 5 push @excluded_items, $training_item;
257 5 next;
258 }
259 # skip this data item with probability $self->{probability}
260 20 ▶ if($self->probability != 1 &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->probability != 1 and rand() > $self->probability) 0 20 20
Condition33%1/3
$self->probability != 1 and rand() > $self->probability
!ll&&!rl&&r
2000
MC/DC0%0/2
$self->probability != 1 and rand() > $self->probability
$self->probability != 1 rand() > $self->probability
Truth table33%1/3
A and B
A= $self->probability != 1B= rand() > $self->probability
ABresult
0X0
100
111
261 rand() > $self->probability){
262 0 push @excluded_items, $training_item;
263 0 next;
264 }
265 20 $training_set->add_item($training_item);
266 }
267 }
268 # $self->_set_excluded_items(\@excluded_items);
269 182 return ($training_set, \@excluded_items);
270 }
271  
272 8 ▶sub _set_test_set {
Subroutine _set_test_set: called CC=1 SCAR=0.0
273 8 my ($self, $test_set) = @_;
274 8 $self->{test_set} = $test_set;
275 8 return;
276 }
277  
278 4 1;
279  
280 __END__
« blib/script/analogize.pl ↑ Summary blib/lib/Algorithm/AM.pm »