PDL/Factor.pm

filter: Statementstmt 94.5% 189 / 200 Branchbran 82.0% 41 / 50 Conditioncond 70.5% 12 / 17 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 97.3% 37 / 38 Podpod 28.5% 2 / 7 totaltotal 88.8% 287 / 323 CC 34 SCAR 13.7
CC
34
Cov
91%
new
11.1
levels
6.0
glue
4.0
CRAP 3.9
« Data/Frame/Role/Rlike.pm ↑ Summary PDL/SV.pm »
1 package PDL::Factor;
2 17 $PDL::Factor::VERSION = '0.006005';
3 # ABSTRACT: PDL subclass for keeping categorical data
4  
5 17 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 17 ▶use failures qw/levels::mismatch levels::number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 17 ▶use PDL::Lite (); # PDL::Lite is the minimal to get PDL work
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use PDL::Core qw(pdl);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use PDL::Primitive qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 17 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use Ref::Util qw(is_plain_arrayref);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 17 ▶use Type::Params;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 17 ▶use Types::Standard qw(slurpy ArrayRef ConsumerOf Int);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 17 ▶use List::AllUtils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 17 ▶use parent 'PDL';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 17 ▶use Class::Method::Modifiers;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 17 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 17 with qw(PDL::Role::Enumerable);
27  
28 use overload
29 17 ▶ '==' => \&_eq,
Subroutine BEGIN: called CC=1 SCAR=0.0
30 'eq' => \&_eq,
31 '!=' => \&_ne,
32 'ne' => \&_ne,
33 17 fallback => 1;
34  
35 # after stringifiable role is added, the string method will exist
36 17 ▶eval q{
Subroutine BEGIN: called CC=1 SCAR=0.0
37 use overload (
38 '""' => \&PDL::Factor::string,
39 );
40 };
41  
42  
43 # check if given levels have duplicates
44 39 ▶sub _check_levels {
Subroutine _check_levels: called CC=3 SCAR=11.7
45 39 my ( $class, $levels ) = @_;
46  
47 39 my %levels;
48 39 for my $i ( 0 .. $#$levels ) {
49 128 ▶ if ( ( $levels{ $levels->[$i] }++ ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($levels{$levels->[$i]}++ > 0) 0 128 128
50 0 die "levels element [$i] is duplicated";
51 }
52 }
53 }
54  
55 # extract levels from piddle or arrayref
56 13 ▶sub _extract_levels {
Subroutine _extract_levels: called CC=4 SCAR=13.9
57 13 my ( $class, $x ) = @_;
58  
59 13 ▶ state $levels_from_arrayref = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
60 13 my ($aref) = @_;
61  
62 # Sort levels if levels is not given on construction.
63 13 my @uniq = sort { $a cmp $b } List::AllUtils::uniq(@$aref);
64 13 return \@uniq;
65 13 };
66  
67 13 ▶ if ( $x->$_DOES('PDL') ) { # PDL
Branch: 1 branch
BranchTrueFalseTotal
if ($x->$PDL::Factor::_DOES('PDL')) { } 4 9 13
68 4 ▶ $x = $x->slice( which( $x->isgood ) ) if $x->badflag;
Branch: 1 branch
BranchTrueFalseTotal
if $x->badflag 2 2 4
69 4 ▶ if ( $x->$_DOES('PDL::SV') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->$PDL::Factor::_DOES('PDL::SV')) { } 1 3 4
70 1 return $levels_from_arrayref->( [ $x->list ] );
71 }
72 else {
73 3 return $levels_from_arrayref->( [ $x->uniq->qsort->list ] );
74 }
75 }
76 else { # arrayref
77 9 return $levels_from_arrayref->($x);
78 }
79 }
80  
81 57 ▶sub new {
Subroutine new: called CC=11 SCAR=24.1
Pod documented
82 57 my ( $class, @args ) = @_;
83  
84 57 my $data;
85 57 ▶ if( @args % 2 != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args % 2 != 0) 57 0 57
86 57 $data = shift @args; # first arg
87 }
88 57 my %opt = @args;
89
90 57 ▶ if ($data->$_DOES('PDL::Factor')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->$PDL::Factor::_DOES("PDL::Factor")) 5 52 57
91 5 ▶ unless (exists $opt{levels}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt{"levels"}) 0 5 5
92 0 return $data->copy;
93 }
94  
95 #TODO: this does not support ND piddle yet.
96  
97 # reorder levels
98 5 my @levels = @{ delete $opt{levels} };
99 5 my @integer_old = $data->{PDL}->list;
100 5 my @levels_old = @{ $data->levels };
101 5 my %levels_new = do {
102 5 my $i = 0;
103 5 map { $_ => $i++ } @levels;
104 };
105 5 my @integer_new = do {
106 17 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
107 map {
108 5 my $level = $levels_old[$_];
109 33 ▶ defined $level ? $levels_new{$level} : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $level ? : 33 0 33
110 } @integer_old;
111 };
112  
113 5 my $new = $class->new( [ $levels[0] ], levels => \@levels, %opt );
114 5 my $p = PDL::Core::indx( \@integer_new );
115 5 ▶ $p = $p->setbadif( $data->isbad ) if $data->badflag;
Branch: 1 branch
BranchTrueFalseTotal
if $data->badflag 5 0 5
116 5 $new->{PDL} = $p;
117 5 return $new;
118 }
119  
120 52 my $enum = $data;
121 52 ▶ if ( !ref($enum) ) { # make sure $enum is arrayref
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $enum) 0 52 52
122 0 $enum = [$enum];
123 }
124  
125 52 my $levels;
126 52 ▶ if( my $levels_opt = $opt{levels} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $levels_opt = $opt{'levels'}) { } 39 13 52
127 # add the levels first if given levels option
128 39 $class->_check_levels($levels_opt);
129 39 $levels = $levels_opt;
130 }
131 else {
132 13 $levels = $class->_extract_levels($enum);
133 }
134  
135 # this is faster than Data::Rmap::rmap().
136 162387 ▶ state $rmap = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
137 162387 my ( $x, $levels ) = @_;
138 is_plain_arrayref($x)
139 162335 ▶ ? [ map { __SUB__->( $_, $levels ) } @$x ]
Condition100%2/2
$levels->{$x} // -1
l!l
1623296
MC/DC100%1/1
$levels->{$x} // -1
$levels->{$x}
Truth table100%2/2
A
A= $levels->{$x}
Aresult
11
00
140 162387 ▶ : ( $levels->{$x} // -1 );
Branch: 1 branch
BranchTrueFalseTotal
is_plain_arrayref($x) ? : 52 162335 162387
141 52 };
142  
143 52 my %levels = do {
144 52 my $i = 0;
145 52 map { $_ => $i++; } @$levels;
146 };
147 52 ▶ $enum =
Branch: 1 branch
BranchTrueFalseTotal
$enum->$PDL::Factor::_DOES('PDL') ? : 9 43 52
148 $rmap->( ( $enum->$_DOES('PDL') ? $enum->unpdl : $enum ), \%levels );
149  
150 52 my $self = $class->initialize();
151  
152 # BAD for integer enum data outside the range of level indices
153 # For indx type, setnantobad() does not work, have to setvaltobad($neg).
154 52 my $integer = PDL::Core::indx($enum)->setvaltobad(-1);
155 52 $integer = $integer->setbadif($integer >= @$levels);
156  
157 52 $self->{PDL} .= $integer;
158 52 $self->levels($levels);
159  
160 # rebless to PDL::Factor::Ordered if necessary
161 52 my $class_ordered = 'PDL::Factor::Ordered';
162 52 ▶ if ($opt{ordered} and not $class->DOES($class_ordered)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"ordered"} and not $class->DOES($class_ordered)) 3 49 52
Condition66%2/3
$opt{"ordered"} and not $class->DOES($class_ordered)
!ll&&!rl&&r
4903
MC/DC50%1/2
$opt{"ordered"} and not $class->DOES($class_ordered)
$opt{"ordered"} not $class->DOES($class_ordered)
Truth table66%2/3
A and not(B)
A= $opt{"ordered"}B= not $class->DOES($class_ordered)
ABresult
0X0
100
111
163 3 load $class_ordered;
164 3 bless $self, $class_ordered;
165 }
166  
167 52 return $self;
168 }
169  
170 371 ▶sub levels {
Subroutine levels: called CC=6 SCAR=17.9
Pod undocumented
171 371 my $self = shift;
172  
173 371 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 142 229 371
174 142 ▶ my $val =
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && is_plain_arrayref($_[0]) ? : 139 3 142
Condition66%2/3
@_ == 1 and is_plain_arrayref($_[0])
!ll&&!rl&&r
30139
MC/DC50%1/2
@_ == 1 and is_plain_arrayref($_[0])
@_ == 1 is_plain_arrayref($_[0])
Truth table66%2/3
A and B
A= @_ == 1B= is_plain_arrayref($_[0])
ABresult
0X0
100
111
175 ( @_ == 1 and is_plain_arrayref( $_[0] ) ) ? $_[0] : \@_;
176 142 ▶ if ( defined $self->{_levels} and @$val != $self->number_of_levels ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_levels"} and @$val != $self->number_of_levels) 2 140 142
Condition100%3/3
defined $self->{"_levels"} and @$val != $self->number_of_levels
!ll&&!rl&&r
13912
MC/DC100%2/2
defined $self->{"_levels"} and @$val != $self->number_of_levels
defined $self->{"_levels"} @$val != $self->number_of_levels
Truth table100%3/3
A and B
A= defined $self->{"_levels"}B= @$val != $self->number_of_levels
ABresult
0X0
100
111
177 2 failure::levels::number->throw(
178 {
179 msg => "incorrect number of levels",
180 trace => failure->croak_trace,
181 }
182 );
183 }
184 140 $self->{_levels} = $val;
185 }
186 369 return $self->{_levels};
187 }
188  
189 52 ▶sub initialize {
Subroutine initialize: called CC=2 SCAR=8.1
Pod undocumented
190 52 my ($class) = @_;
191 52 ▶ return bless( { PDL => PDL::Core::null }, ref $class || $class );
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0520
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
192 }
193  
194  
195 3 ▶sub glue {
Subroutine glue: called CC=4 SCAR=13.9
Pod documented
196 3 my $self = shift;
197  
198 3 state $check =
199 Type::Params::compile( Int,
200 slurpy ArrayRef [ ConsumerOf ['PDL::Factor'] ] );
201 3 my ( $dim, $others ) = $check->(@_);
202
203 2 my $class = ref($self);
204  
205 2 for my $x (@$others) {
206 2 ▶ unless ( $self->_compare_levels( $self->levels, $x->levels ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_compare_levels($self->levels, $x->levels)) 1 1 2
207 1 die "All piddles must have same levels to glue";
208 }
209 }
210  
211 1 ▶ if ( $dim != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dim != 0) 0 1 1
212 0 die('PDL::Factor::glue does not yet support $dim != 0');
213 }
214  
215 1 my $new = $class->new( $self->levels, levels => $self->levels );
216 1 $new->{PDL} = $self->{PDL}->glue( 0, map { $_->{PDL} } @$others );
217 1 return $new;
218 }
219  
220 8 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod undocumented
221 8 my ($self) = @_;
222 8 my ($class) = ref($self);
223  
224 8 my $new = $class->new( $self->levels, levels => $self->levels );
225 8 $new->{PDL} = $self->{PDL}->copy;
226 8 return $new;
227 }
228  
229 0 ▶sub inplace {
Subroutine inplace: not called CC=1 SCAR=6.9
Pod undocumented
230 0 my $self = shift;
231 0 $self->{PDL}->inplace(@_);
232 0 return $self;
233 }
234  
235 68 ▶sub _call_on_pdl {
Subroutine _call_on_pdl: called CC=1 SCAR=0.0
236 68 my ($method) = @_;
237  
238 90 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
239 90 my $self = shift;
240 90 return $self->{PDL}->$method(@_);
241 68 };
242 }
243  
244 17 for my $method (qw(isbad isgood ngood nbad)) {
245 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
246 68 *{$method} = _call_on_pdl($method);
247 }
248  
249 7 ▶sub setbadif {
Subroutine setbadif: called CC=1 SCAR=0.0
Pod undocumented
250 7 my $self = shift;
251  
252 7 my $new = $self->copy;
253 7 $new->{PDL} = $new->{PDL}->setbadif(@_);
254 7 return $new;
255 }
256  
257 40 ▶around string => sub {
Subroutine __ANON__: called CC=2 SCAR=8.7
258 40 my $orig = shift;
259 40 my ($self, %opt) = @_;
260 40 my $ret = $orig->(@_);
261 40 ▶ if( exists $opt{with_levels} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{"with_levels"}) 0 40 40
262 0 my @level_string = grep { defined } $self->levels->flatten;
263 0 $ret .= "\n";
264 0 $ret .= "Levels: @level_string";
265 }
266 40 $ret;
267 17 };
268  
269 15 ▶sub _compare_levels {
Subroutine _compare_levels: called CC=4 SCAR=13.9
270 15 my ($class, $a, $b) = @_;
271  
272 15 ▶ return unless @$a == @$b;
Branch: 1 branch
BranchTrueFalseTotal
unless @$a == @$b 0 15 15
273  
274 15 my $ea = List::AllUtils::each_arrayref($a, $b);
275 15 ▶ while ( my ($x, $y) = $ea->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$ea()) 39 12 51
276 39 ▶ return 0 unless $x eq $y;
Branch: 1 branch
BranchTrueFalseTotal
unless $x eq $y 3 36 39
277 }
278 12 return 1;
279 }
280  
281 # TODO overload, compare factor level sets
282 #
283 #R
284 # > g <- iris
285 # > levels(g$Species) <- c( levels(g$Species), "test")
286 # > iris$Species == g$Species
287 # : Error in Ops.factor(iris$Species, g$Species) :
288 # : level sets of factors are different
289 #
290 # > g <- iris
291 # > levels(g$Species) <- levels(g$Species)[c(3, 2, 1)]
292 # > iris$Species == g$Species
293 # : # outputs a logical vector where only 'versicolor' indices are TRUE
294 18 ▶sub _eq {
Subroutine _eq: called CC=4 SCAR=13.9
295 18 my ($self, $other, $d) = @_;
296 # TODO need to look at $d to determine direction
297 18 ▶ if( blessed($other) && $other->isa('PDL::Factor') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($other) and $other->isa('PDL::Factor')) { } 13 5 18
Condition66%2/3
builtin::blessed($other) and $other->isa("PDL::Factor")
!ll&&!rl&&r
5013
MC/DC50%1/2
builtin::blessed($other) and $other->isa("PDL::Factor")
builtin::blessed($other) $other->isa("PDL::Factor")
Truth table66%2/3
A and B
A= builtin::blessed($other)B= $other->isa("PDL::Factor")
ABresult
0X0
100
111
298 13 ▶ if( $self->_compare_levels($self->levels, $other->levels) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_compare_levels($self->levels, $other->levels)) { } 11 2 13
299 11 return $self->{PDL} == $other->{PDL};
300 # TODO return a PDL::Logical
301 } else {
302 2 failure::levels::mismatch->throw({
303 msg => "level sets of factors are different",
304 trace => failure->croak_trace,
305 payload => {
306 self_levels => $self->levels,
307 other_levels => $other->levels,
308 }
309 }
310 );
311 }
312 } else {
313 # TODO hacky. need to test this more
314 15 ▶ my $key_idx = List::AllUtils::first_index { $_ eq $other }
Subroutine __ANON__: called CC=1 SCAR=0.0
315 5 @{$self->levels};
316 5 return $self->{PDL} == $key_idx;
317 }
318 }
319  
320 9 ▶sub _ne { !_eq(@_); }
Subroutine _ne: called CC=1 SCAR=0.0
321  
322  
323 17 1;
324  
325 __END__
« Data/Frame/Role/Rlike.pm ↑ Summary PDL/SV.pm »