blib/lib/Lingua/Ogmios/LearningDataSet.pm

filter: Statementstmt 73.7% 171 / 232 Branchbran 52.9% 36 / 68 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 75.8% 22 / 29 Podpod 0.0% 0 / 27 totaltotal 63.0% 234 / 371 CC 46 SCAR 20.1
CC
46
Cov
68%
printAttributes
30.0
loadAttributesFromFile
20.0
printDataset
12.0
CRAP 7.5
« blib/lib/Lingua/Ogmios/Config.pm ↑ Summary blib/lib/Lingua/Ogmios/LearningDataSet/Data.pm »
1 package Lingua::Ogmios::LearningDataSet;
2  
3  
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 our $VERSION='0.1';
8  
9 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
10  
11 2 my $class = shift;
12  
13 2 my $learningDataSet = {
14 'relation' => undef,
15 'attributes' => [],
16 'attribute_index' => {},
17 'dataset' => [],
18 'comments' => [],
19 'classes' => {},
20
21 };
22  
23 2 bless $learningDataSet, $class;
24  
25 2 return($learningDataSet);
26 }
27  
28 4 ▶sub relation {
Subroutine relation: called CC=2 SCAR=6.9
Pod undocumented
29 4 my $self = shift;
30  
31 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 2 4
32 2 $self->{relation} = shift;
33 }
34 4 return($self->{relation});
35 }
36  
37 161 ▶sub attributes {
Subroutine attributes: called CC=2 SCAR=7.6
Pod undocumented
38 161 my $self = shift;
39  
40 161 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 161 161
41 0 $self->{attributes} = shift;
42 }
43 161 return($self->{attributes});
44 }
45  
46 28 ▶sub countValAttr {
Subroutine countValAttr: called CC=1 SCAR=0.0
Pod undocumented
47 28 my $self = shift;
48  
49 28 return(scalar(@{$self->{attributes}}));
50 }
51  
52 106 ▶sub attribute_index {
Subroutine attribute_index: called CC=2 SCAR=7.6
Pod undocumented
53 106 my $self = shift;
54  
55 106 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 106 106
56 0 $self->{attribute_index} = shift;
57 }
58 106 return($self->{attribute_index});
59 }
60  
61 150 ▶sub classes {
Subroutine classes: called CC=6 SCAR=19.7
Pod undocumented
62 150 my $self = shift;
63  
64 150 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 148 150
65 2 my $classes = shift;
66 2 ▶ if (ref($classes) eq "HASH") {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $classes eq 'HASH') { } 0 2 2
elsif (ref $classes eq 'ARRAY') { } 2 0 2
elsif (ref $classes eq '') { } 0 0 0
67 0 $self->{classes} = {};
68 0 %{$self->{classes}} = %$classes;
69 } elsif (ref($classes) eq "ARRAY"){
70
71 2 my $i = 0;
72 2 $self->{classes} = {};
73 2 ▶ for($i=0; $i < scalar(@$classes); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$classes) 6 2 8
74 6 $self->{classes}->{$classes->[$i]} = $i + 1;
75 }
76 } elsif (ref($classes) eq "") {
77 # TODO
78 }
79 }
80 150 return($self->{classes});
81 }
82  
83 8 ▶sub firstClass {
Subroutine firstClass: called CC=1 SCAR=0.0
Pod undocumented
84 8 my $self = shift;
85  
86 # if (@_) {
87 # $self->{'fistClass'} = shift;
88 # }
89  
90 8 my @tmp = sort { $self->classes->{$a} <=> $self->classes->{$b} } keys %{$self->classes};
91  
92 8 return($tmp[0]);
93 }
94  
95 27 ▶sub existsInClasses {
Subroutine existsInClasses: called CC=1 SCAR=0.0
Pod undocumented
96 27 my ($self, $class) = @_;
97  
98 # warn $self->classes . "\n";
99 27 return(exists($self->classes->{$class}));
100 }
101  
102 35 ▶sub dataset {
Subroutine dataset: called CC=2 SCAR=7.6
Pod undocumented
103 35 my $self = shift;
104  
105 35 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 35 35
106 0 $self->{dataset} = shift;
107 }
108 35 return($self->{dataset});
109 }
110  
111 4 ▶sub comments {
Subroutine comments: called CC=2 SCAR=7.6
Pod undocumented
112 4 my $self = shift;
113  
114 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 4 4
115 0 $self->{comments} = shift;
116 }
117 4 return($self->{comments});
118 }
119  
120 53 ▶sub addAttribute {
Subroutine addAttribute: called CC=1 SCAR=0.0
Pod undocumented
121 53 my ($self, $attr) = @_;
122  
123 53 push @{$self->attributes}, $attr;
124 53 $self->attribute_index->{$attr->name} = scalar(@{$self->attributes}) - 1;
125 53 $attr->index(scalar(@{$self->attributes}) - 1);
126 53 return($self->attribute_index->{$attr->name});
127 }
128  
129 0 ▶sub delAttribute {
Subroutine delAttribute: not called CC=1 SCAR=6.9
Pod undocumented
130 0 my ($self, $attr_name) = @_;
131
132 0 splice @{$self->attributes}, $self->attribute_index->{$attr_name}, 1;
133 0 delete $self->attribute_index->{$attr_name};
134
135 }
136  
137 0 ▶sub getAttributeIndex {
Subroutine getAttributeIndex: not called CC=1 SCAR=6.9
Pod undocumented
138 0 my ($self, $attr_name) = @_;
139  
140 0 return($self->attribute_index->{$attr_name});
141
142 }
143  
144  
145 0 ▶sub existsAttribute {
Subroutine existsAttribute: not called CC=1 SCAR=6.9
Pod undocumented
146 0 my ($self, $attr_name) = @_;
147  
148 0 return(exists $self->attribute_index->{$attr_name});
149  
150 }
151  
152 30 ▶sub addData {
Subroutine addData: called CC=1 SCAR=0.0
Pod undocumented
153 30 my ($self, $data) = @_;
154  
155 30 push @{$self->dataset}, $data;
156  
157 }
158  
159 1 ▶sub addComment {
Subroutine addComment: called CC=1 SCAR=0.0
Pod undocumented
160 1 my ($self, $comment) = @_;
161  
162 1 push @{$self->comments}, $comment;
163 }
164  
165 1 ▶sub getARFF {
Subroutine getARFF: called CC=3 SCAR=11.3
Pod undocumented
166 1 my $self = shift;
167 1 my $printheader = shift;
168  
169 1 my $arffString;
170  
171 1 ▶ if ((!defined $printheader) || ($printheader == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $printheader or $printheader == 1) 1 0 1
Condition33%1/3
not defined $printheader or $printheader == 1
l!l&&r!l&&!r
100
MC/DC0%0/2
not defined $printheader or $printheader == 1
not defined $printheader $printheader == 1
Truth table33%1/3
not(A) or B
A= not defined $printheaderB= $printheader == 1
ABresult
1X1
011
000
172 1 $arffString .= $self->getARFFHeader;
173 }
174  
175 1 $arffString .= '@DATA' . "\n";
176 1 $arffString .= $self->getARFFData;
177 1 return($arffString);
178 }
179  
180 1 ▶sub getARFFData {
Subroutine getARFFData: called CC=2 SCAR=6.9
Pod undocumented
181 1 my $self = shift;
182  
183 1 my $arffString;
184 my $data;
185  
186 1 foreach $data (@{$self->dataset}) {
187 3 $arffString .= $data->getARFF . "\n";
188 }
189 1 return($arffString);
190 }
191 1 ▶sub getARFFHeader {
Subroutine getARFFHeader: called CC=3 SCAR=11.0
Pod undocumented
192 1 my $self = shift;
193  
194 1 my $arffString;
195 my $comment;
196 1 my $attribute;
197 1 my $data;
198  
199 1 foreach $comment (@{$self->comments}) {
200 1 $arffString .= "% $comment\n";
201 }
202 1 $arffString .= '@RELATION ' . $self->relation . "\n\n";
203
204 1 foreach $attribute (@{$self->attributes}) {
205 2 $arffString .= $attribute->getARFF . "\n";
206 }
207 1 $arffString .= '@ATTRIBUTE class {' . join (",", sort({$self->classes->{$a} <=> $self->classes->{$b}} keys(%{$self->classes}))) . "}\n";
208 1 $arffString .= "\n";
209  
210 1 return($arffString);
211 }
212  
213 2 ▶sub getSVM {
Subroutine getSVM: called CC=5 SCAR=16.4
Pod undocumented
214 2 my $self = shift;
215 2 my $printComments = shift;
216 2 my $SVMString;
217 my $comment;
218 2 my $data;
219  
220 2 ▶ if ((!defined $printComments) || ($printComments == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $printComments or $printComments == 1) 2 0 2
Condition33%1/3
not defined $printComments or $printComments == 1
l!l&&r!l&&!r
200
MC/DC0%0/2
not defined $printComments or $printComments == 1
not defined $printComments $printComments == 1
Truth table33%1/3
not(A) or B
A= not defined $printCommentsB= $printComments == 1
ABresult
1X1
011
000
221 2 foreach $comment (@{$self->comments}) {
222 1 $SVMString .= "# $comment\n";
223 }
224 }
225  
226 2 foreach $data (@{$self->dataset}) {
227 30 $SVMString .= $data->getSVM($self->classes, $printComments) . "\n";
228 }
229 2 return($SVMString);
230
231 }
232  
233 0 ▶sub loadAttributesFromFile {
Subroutine loadAttributesFromFile: not called CC=4 SCAR=30.0
Pod undocumented
234 0 my ($self, $filename, $type, $prefix) = @_;
235 0 my $line;
236 my $attr;
237  
238 0 warn "openning $filename\n";
239 0 ▶ open FILE, $filename or die "No such file $filename\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $filename or die "No such file $filename\n" 0 0 0
240 0 binmode(FILE, ":utf8");
241  
242 0 ▶ while($line = <FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline FILE)) 0 0 0
243 0 chomp $line;
244 0 ▶ if ($line !~ /^\s*#/o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\s*#/o) 0 0 0
245 0 $attr = Lingua::Ogmios::LearningDataSet::Attribute->new(
246 {"name" => "$prefix". "_" . $line,
247 "type" => uc($type),
248 "prefix" => $prefix,
249 "value" => $line,
250 }
251 );
252 0 $self->addAttribute($attr);
253 }
254 }
255  
256 0 close FILE;
257 }
258  
259 1 ▶sub parseARFF {
Subroutine parseARFF: called CC=7 SCAR=19.5
Pod undocumented
260 1 my ($self, $filename) = @_;
261  
262 1 my $line;
263 my $relation;
264 1 my $attribute;
265 1 my $name;
266 1 my $type;
267 1 my $data;
268 1 my $info_attr;
269 1 my $prefix;
270 1 my @classes;
271 1 my @tmp ;
272  
273 1 ▶ open FILE, $filename or die "No such file $filename\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $filename or die "No such file $filename\n" 1 0 1
274 1 binmode(FILE, ":utf8");
275 1 ▶ while($line = <FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline FILE)) 56 1 57
276 56 chomp $line;
277  
278 # Relation
279 56 ▶ if ($line =~ /\@relation\s+/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\@relation\s+/io) 1 55 56
280 1 $relation = $'; # '
281 1 $relation =~ s/^['"]//o;
282 1 $relation =~ s/['"]$//o;
283 1 $self->relation($relation);
284 }
285  
286 # Attribute
287 56 ▶ if ($line =~ /\@attribute\s+/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\@attribute\s+/io) 52 4 56
288 52 $info_attr = $'; # '
289 52 ($name, $type) = split / /, $info_attr;
290 52 ▶ if ($name eq "class") {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq 'class') { } 1 51 52
291 1 $type =~ s/^{//o;
292 1 $type =~ s/}$//o;
293 1 @classes = split /\s*,\s*/, $type;
294 1 $self->classes(\@classes);
295 } else {
296 51 @tmp = split /_/, $name;
297 51 $prefix = shift @tmp;
298
299 51 $attribute = Lingua::Ogmios::LearningDataSet::Attribute->new(
300 {"name" => $name,
301 "type" => $type,
302 "prefix" => $prefix,
303 "value" => join('_', @tmp),
304 }
305 );
306 51 $self->addAttribute($attribute);
307 }
308 }
309
310 # Data
311 56 ▶ if ($line =~ /\@data\s*/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\@data\s*/io) 1 55 56
312 # warn "$line\n";
313 # while($line = <FILE>) {
314 # chomp $line;
315 1 $self->parseDataset(\*FILE);
316 # }
317 }
318  
319 }
320  
321 1 close FILE;
322 1 return(1);
323 }
324  
325 27 ▶sub parseData {
Subroutine parseData: called CC=9 SCAR=23.1
Pod undocumented
326 27 my ($self, $fh, $line) = @_;
327  
328 27 my $addr_val;
329 my $addr;
330 27 my $val;
331 27 my $data;
332 27 my @values;
333 27 my $index;
334 # warn "line: $line\n";
335 27 ▶ if ($line =~ /{/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /{/o) { } 27 0 27
336 27 $data = Lingua::Ogmios::LearningDataSet::Data->new({type => "sparse", 'countVal' => $self->countValAttr});
337 27 $line =~ s/^{\s*//o;
338 27 $line =~ s/\s*}$//o;
339 } else {
340 0 $data = Lingua::Ogmios::LearningDataSet::Data->new({type => "normal", 'countVal' => $self->countValAttr});
341 }
342 27 @values = split /\s*,\s*/, $line;
343 27 $addr_val = pop @values;
344 27 ▶ if (defined $addr_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addr_val) { } 27 0 27
345 27 ▶ if ($data->isSparse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->isSparse) { } 27 0 27
346 27 ($addr, $val) = split /\s+/, $addr_val;
347 } else {
348 0 $val = $addr_val;
349 }
350 } else {
351 0 $val = undef;
352 }
353 # warn "line: $line\n$addr\n$val\n";
354
355 27 ▶ if ((defined $val) && ($self->existsInClasses($val))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $self->existsInClasses($val)) { } 19 8 27
Condition66%2/3
defined $val and $self->existsInClasses($val)
!ll&&!rl&&r
0819
MC/DC50%1/2
defined $val and $self->existsInClasses($val)
defined $val $self->existsInClasses($val)
Truth table66%2/3
A and B
A= defined $valB= $self->existsInClasses($val)
ABresult
0X0
100
111
356 # warn "\n\n==>$val\n";exit;
357 } else {
358 8 $val = $self->firstClass;
359 8 ▶ if (defined $addr_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addr_val) 8 0 8
360 8 push @values, $addr_val;
361 }
362 }
363 # warn "val: $val\n";
364 27 $data->class($val);
365  
366 # warn "\n\n====>" . $data->class . "\n";exit;
367  
368 # $data->countVal($val);
369 27 $index = 0;
370 27 foreach $addr_val (@values) {
371 425 ▶ if ($data->isSparse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->isSparse) { } 425 0 425
372 425 ($addr, $val) = split /\s+/, $addr_val;
373 } else {
374 0 $val = $addr_val;
375 0 $addr = $index;
376 }
377 425 $data->value($addr, $val);
378 425 $index++;
379 }
380  
381 27 $self->addData($data);
382 }
383  
384 1 ▶sub parseDataset {
Subroutine parseDataset: called CC=3 SCAR=11.0
Pod undocumented
385 1 my ($self, $fh) = @_;
386  
387 1 my $line;
388 1 ▶ while ($line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 28 1 29
389 28 chomp $line;
390 28 ▶ if ($line !~ /^\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\s*$/o) 27 1 28
391 27 $self->parseData($fh, $line);
392 }
393 }
394 }
395 0 ▶sub printAttributes {
Subroutine printAttributes: not called CC=5 SCAR=34.0
Pod undocumented
396 0 my ($self, $corresp, $fh) = @_;
397 0 my $name;
398 my $attribute;
399  
400 0 ▶ if (!defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 0 0
401 0 $fh = \*STDOUT;
402 }
403 0 binmode($fh, ":utf8");
404  
405 0 ▶ if (!defined $corresp) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $corresp) 0 0 0
406 0 $corresp = 0;
407 }
408 0 foreach $attribute (@{$self->attributes}) {
409 0 $name = $attribute->name;
410 0 $name =~ s/^[^_]+_//;
411 0 print $fh "$name";
412 0 ▶ if ($corresp == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($corresp == 1) 0 0 0
413 0 print $fh "\t" . $attribute->name;
414 0 print $fh "\t" . $attribute->type;
415 }
416 0 print $fh "\n";
417 }
418  
419 }
420  
421  
422 0 ▶sub printDataset {
Subroutine printDataset: not called CC=3 SCAR=24.8
Pod undocumented
423 0 my ($self, $fh) = @_;
424  
425 0 my $data;
426 0 ▶ if (!defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 0 0
427 0 $fh = \*STDOUT;
428 }
429 0 binmode($fh, ":utf8");
430  
431 0 foreach $data (@{$self->dataset}) {
432 0 print $fh $data->getARFF . "\n";
433 0 print $fh $data->getSVM($self->classes) . "\n";
434 }
435 }
436 0 ▶sub printRelation {
Subroutine printRelation: not called CC=2 SCAR=17.9
Pod undocumented
437 0 my ($self, $fh) = @_;
438  
439 0 ▶ if (!defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 0 0
440 0 $fh = \*STDOUT;
441 }
442 0 binmode($fh, ":utf8");
443  
444 0 print $fh $self->relation . "\n";
445
446 }
447  
448 8 1;
449  
450 __END__
« blib/lib/Lingua/Ogmios/Config.pm ↑ Summary blib/lib/Lingua/Ogmios/LearningDataSet/Data.pm »