blib/lib/Algorithm/AM/DataSet/Item.pm

filter: Statementstmt 100% 46 / 46 Branchbran 100% 6 / 6 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 7 / 7 totaltotal 100% 75 / 75 CC 6 SCAR 8.6
CC
6
Cov
100%
new
5.0
comment
2.0
BEGIN
1.0
CRAP 2.4
« blib/lib/Algorithm/AM/DataSet.pm ↑ Summary blib/lib/Algorithm/AM/BigInt.pm »
1 package Algorithm::AM::DataSet::Item;
2 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 our $VERSION = '3.13';
5 # ABSTRACT: A single item for classification training and testing
6 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 our @CARP_NOT = qw(Algorithm::AM::DataSet);
8 11 ▶use Exporter::Easy (
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 OK => ['new_item']
10 11 );
11  
12 # use to assign unique ids to new items; not meant to be secure
13 # or anything, just unique.
14 11 my $current_id = 'a';
15  
16  
17 #pod =head1 SYNOPSIS
18 #pod
19 #pod use Algorithm::AM::DataSet::Item 'new_item';
20 #pod
21 #pod my $item = new_item(
22 #pod features => ['a', 'b', 'c'],
23 #pod class => 'x',
24 #pod comment => 'a sample, meaningless item'
25 #pod );
26 #pod
27 #pod =head1 DESCRIPTION
28 #pod
29 #pod This class represents a single item contained in a data set. Each
30 #pod item has a feature vector and possibly a class label and comment
31 #pod string. Once created, the item is immutable.
32 #pod
33 #pod =head1 METHODS
34 #pod
35 #pod =head2 C<new>
36 #pod
37 #pod Creates a new Item object. The only required argument is
38 #pod 'features', which should be an array ref containing the feature
39 #pod vector. Each element of this array should be a string indicating the
40 #pod value of the feature at the given index. 'class' and 'comment'
41 #pod arguments are also accepted, where 'class' is the classification
42 #pod label and 'comment' can be any string to be associated with the item.
43 #pod A missing or undefined 'class' value is assumed to mean that the item
44 #pod classification is unknown. For the feature vector, empty strings are
45 #pod taken to indicate null values.
46 #pod
47 #pod =cut
48 266 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
49 266 my ($class, %args) = @_;
50 266 ▶ if(!exists $args{features} ||
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{"features"} or "ARRAY" ne ref $args{"features"}) 3 263 266
Condition100%3/3
not exists $args{"features"} or "ARRAY" ne ref $args{"features"}
l!l&&r!l&&!r
21263
MC/DC100%2/2
not exists $args{"features"} or "ARRAY" ne ref $args{"features"}
not exists $args{"features"} "ARRAY" ne ref $args{"features"}
Truth table100%3/3
not(A) or B
A= not exists $args{"features"}B= "ARRAY" ne ref $args{"features"}
ABresult
1X1
011
000
51 'ARRAY' ne ref $args{features}){
52 3 croak q[Must provide 'features' parameter of type array ref];
53 }
54 263 my $self = {};
55 263 for(qw(features class comment)){
56 789 $self->{$_} = $args{$_};
57 789 delete $args{$_};
58 }
59 263 ▶ if(my $extra_keys = join ',', sort keys %args){
Branch: 1 branch
BranchTrueFalseTotal
if (my $extra_keys = join(",", sort(keys %args))) 1 262 263
60 1 croak "Unknown parameters: $extra_keys";
61 }
62 262 $self->{id} = $current_id;
63 262 $current_id++;
64 262 bless $self, $class;
65 262 return $self;
66 }
67  
68 #pod =head2 C<new_item>
69 #pod
70 #pod This is an exportable shortcut for the new method. If exported, then
71 #pod instead of calling C<<Algorithm::AM::DataSet::Item->new>>, you may
72 #pod simply call C<new_item>.
73 #pod
74 #pod =cut
75 3 ▶sub new_item {
Subroutine new_item: called CC=1 SCAR=0.0
Pod documented
76 # unpack here so that warnings about odd numbers of elements are
77 # reported for this function, not for the new method
78 3 my %args = @_;
79 3 return __PACKAGE__->new(%args);
80 }
81  
82 #pod =head2 C<class>
83 #pod
84 #pod Returns the classification label for this item, or undef if the class
85 #pod is unknown.
86 #pod
87 #pod =cut
88 90917 ▶sub class {
Subroutine class: called CC=1 SCAR=0.0
Pod documented
89 90917 my ($self) = @_;
90 90917 return $self->{class};
91 }
92  
93 #pod =head2 C<features>
94 #pod
95 #pod Returns the feature vector for this item. This is an arrayref
96 #pod containing the string value for each feature. An empty string
97 #pod indicates that the feature value is null (meaning that it has
98 #pod no value).
99 #pod
100 #pod =cut
101 60810 ▶sub features {
Subroutine features: called CC=1 SCAR=0.0
Pod documented
102 60810 my ($self) = @_;
103 # make a safe copy
104 60810 return [@{ $self->{features} }];
105 }
106  
107 #pod =head2 C<comment>
108 #pod
109 #pod Returns the comment for this item. By default, the comment is
110 #pod just a comma-separated list of the feature values.
111 #pod
112 #pod =cut
113 76 ▶sub comment {
Subroutine comment: called CC=2 SCAR=6.9
Pod documented
114 76 my ($self) = @_;
115 76 ▶ if(!defined $self->{comment}){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"comment"}) 1 75 76
116 1 $self->{comment} = join ',', @{ $self->{features} };
117 }
118 76 return $self->{comment};
119 }
120  
121 #pod =head2 C<cardinality>
122 #pod
123 #pod Returns the length of the feature vector for this item.
124 #pod
125 #pod =cut
126 481 ▶sub cardinality {
Subroutine cardinality: called CC=1 SCAR=0.0
Pod documented
127 481 my ($self) = @_;
128 481 return scalar @{$self->features};
129 }
130  
131 #pod =head2 C<id>
132 #pod
133 #pod Returns a unique string id for this item, for use as a hash key or
134 #pod similar situations.
135 #pod
136 #pod =cut
137 13 ▶sub id {
Subroutine id: called CC=1 SCAR=0.0
Pod documented
138 13 my ($self) = @_;
139 13 return $self->{id};
140 }
141  
142 11 1;
143  
144 __END__
« blib/lib/Algorithm/AM/DataSet.pm ↑ Summary blib/lib/Algorithm/AM/BigInt.pm »