Data/Frame/PDL.pm

filter: Statementstmt 83.3% 80 / 96 Branchbran 65.6% 21 / 32 Conditioncond 62.5% 5 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 85.0% 17 / 20 Podpod 88.8% 8 / 9 totaltotal 78.8% 134 / 170 CC 22 SCAR 21.9
CC
22
Cov
78%
as_pdlsv
42.0
repeat
7.0
__ANON__
6.0
CRAP 9.0
« Data/Frame/IO/CSV.pm ↑ Summary Data/Frame.pm »
1 package Data::Frame::PDL;
2 20 $Data::Frame::PDL::VERSION = '0.006005';
3 # ABSTRACT: A mixin to add some methods to PDL
4  
5 20 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 20 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 20 ▶use List::AllUtils qw(pairmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 20 ▶use PDL::Core qw(pdl);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 20 ▶use PDL::Primitive qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 20 ▶use POSIX qw(ceil);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 20 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 6365 ▶sub length { $_[0]->dim(0); }
Subroutine length: called CC=1 SCAR=0.0
Pod documented
18  
19 1 ▶sub number_of_rows {
Subroutine number_of_rows: called CC=2 SCAR=7.1
Pod documented
20 1 ▶ if ( warnings::enabled("deprecated") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (warnings::enabled("deprecated")) 1 0 1
21 1 warnings::warn( "deprecated",
22 "PDL::number_of_rows (from Data::Frame::PDL) is deprecated. "
23 . "Use PDL::length instead." );
24 }
25 1 $_[0]->dim(0);
26 }
27  
28  
29 2 ▶sub diff {
Subroutine diff: called CC=2 SCAR=6.9
Pod documented
30 2 my ( $self, $lag ) = @_;
31 2 ▶ $lag //= 1;
Condition100%2/2
$lag //= 1
l!l
11
MC/DC100%1/1
$lag //= 1
$lag
Truth table100%2/2
A
A= $lag
Aresult
11
00
32  
33 2 my $idx = PDL->sequence( $self->length - $lag );
34 2 return $self->slice( $idx + $lag ) - $self->slice($idx);
35 }
36  
37  
38 3 ▶sub flatten { @{ $_[0]->unpdl }; }
Subroutine flatten: called CC=1 SCAR=0.0
Pod documented
39  
40 0 ▶sub flatten_deep { $_[0]->list; }
Subroutine flatten_deep: not called CC=1 SCAR=6.9
Pod documented
41  
42  
43 378 ▶sub repeat {
Subroutine repeat: called CC=7 SCAR=19.5
Pod documented
44 378 my ( $self, $n ) = @_;
45 378 ▶ if ( $self->length == 0 or $n <= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->length == 0 or $n <= 1) 365 13 378
Condition100%3/3
$self->length == 0 or $n <= 1
l!l&&r!l&&!r
136413
MC/DC100%2/2
$self->length == 0 or $n <= 1
$self->length == 0 $n <= 1
Truth table100%3/3
A or B
A= $self->length == 0B= $n <= 1
ABresult
1X1
011
000
46 365 my $p = $self->copy;
47  
48 # Make sure we return a piddle of at least 1D.
49 365 ▶ $p->reshape(1) if ($self->ndims == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->ndims == 0 2 363 365
50 365 return $p;
51 }
52  
53 13 my $class = ref($self);
54  
55 10 ▶ state $repeat = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
56 10 my ($p, $n) = @_;
57
58 my $data = [
59 (
60 $p->badflag
61 14 ▶ ? ( map { $_ eq 'BAD' ? 0 : $_ } @{ $p->unpdl } )
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'BAD' ? : 2 12 14
62 10 ▶ : ( @{ $p->unpdl } )
Branch: 1 branch
BranchTrueFalseTotal
$p->badflag ? : 4 6 10
63 ) x $n
64 ];
65 10 return $class->new($data);
66 13 };
67  
68 13 my $p;
69 13 ▶ if ( $self->$_DOES('PDL::SV') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->$Data::Frame::PDL::_DOES('PDL::SV')) { } 3 10 13
elsif ($self->$Data::Frame::PDL::_DOES('PDL::Factor')) { } 3 7 10
70 3 $p = $class->new( [ ( @{ $self->unpdl } ) x $n ] );
71 }
72 elsif ( $self->$_DOES('PDL::Factor') ) {
73 3 $p = $class->new( $self, levels => $self->levels );
74 3 $p->{PDL} = $repeat->( $p->{PDL}, $n );
75 }
76 else {
77 7 $p = $repeat->($self, $n);
78 }
79  
80 13 ▶ if ( $self->badflag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->badflag) 5 8 13
81 5 $p = $p->setbadif( PDL::Core::pdl( [ ( $self->isbad->list ) x $n ] ) );
82 }
83 13 return $p;
84 }
85  
86 373 ▶sub repeat_to_length {
Subroutine repeat_to_length: called CC=3 SCAR=11.0
Pod documented
87 373 my ( $self, $length ) = @_;
88 373 ▶ return $self->copy if ( $self->length == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->length == 0 5 368 373
89  
90 368 my $x = $self->repeat( ceil( $length / $self->length ) );
91 368 ▶ return ( $x->length == $length ? $x : $x->slice( "0:" . ( $length - 1 ) ) );
Branch: 1 branch
BranchTrueFalseTotal
$x->length == $length ? : 366 2 368
92 }
93  
94 0 ▶sub as_pdlsv {
Subroutine as_pdlsv: not called CC=6 SCAR=37.4
Pod undocumented
95 0 my ($self) = @_;
96  
97 0 ▶ my $new_pdlsv = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
98 0 my ($x) = @_;
99 0 my $new = PDL::SV->new($x);
100 0 ▶ if ($self->badflag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->badflag) 0 0 0
101 0 $new = $new->setbadif($self->isbad);
102 }
103 0 return $new;
104 0 };
105  
106 0 ▶ if ($self->$_DOES('PDL::Factor')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->$Data::Frame::PDL::_DOES('PDL::Factor')) { } 0 0 0
elsif ($self->$Data::Frame::PDL::_DOES('PDL::DateTime')) { } 0 0 0
107 0 my $levels = $self->levels;
108 0 ▶ my $is_bad = $self->badflag ? $self->isbad : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->badflag ? : 0 0 0
109 my @x = map {
110 0 ▶ ( defined $is_bad and $is_bad->at($_) )
Branch: 1 branch
BranchTrueFalseTotal
defined $is_bad && $is_bad->at($_) ? : 0 0 0
Condition0%0/3
defined $is_bad and $is_bad->at($_)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $is_bad and $is_bad->at($_)
defined $is_bad $is_bad->at($_)
Truth table0%0/3
A and B
A= defined $is_badB= $is_bad->at($_)
ABresult
0X0
100
111
111 ? 'BAD'
112 : $levels->[ $self->at($_) ];
113 } ( 0 .. $self->length - 1 );
114 0 return $new_pdlsv->(\@x);
115 }
116 elsif ($self->$_DOES('PDL::DateTime')) {
117 0 return $new_pdlsv->($self->dt_unpdl);
118 }
119 else {
120 0 return $self->copy;
121 }
122 }
123  
124  
125 1 ▶sub id {
Subroutine id: called CC=4 SCAR=13.9
Pod documented
126 1 my ($self) = @_;
127  
128 1 my %uniq_values; # value to row indices
129 my @uniq_indices; # first index for each set of uniq values
130 1 for my $ridx ( which($self->isgood)->list ) {
131 3 my $value = $self->at($ridx);
132 3 ▶ if ( not exists $uniq_values{$value} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $uniq_values{$value}) 2 1 3
133 2 $uniq_values{$value} = [];
134 2 push @uniq_indices, $ridx;
135 }
136 3 push @{ $uniq_values{$value} }, $ridx;
137 }
138  
139 1 ▶ my %index_to_value = pairmap { $b->[0] => $a } %uniq_values;
Subroutine __ANON__: called CC=1 SCAR=0.0
140  
141 1 my $rslt = PDL::Core::zeros( $self->length );
142 1 $rslt .= -1; # for BAD values
143 1 for my $i ( 0 .. $#uniq_indices ) {
144 my $value =
145 2 $index_to_value{ $uniq_indices[ $i ] };
146 2 my $indices = $uniq_values{$value};
147 2 $rslt->slice( pdl($indices) ) .= $i;
148 }
149 1 return $rslt;
150 }
151  
152 20 1;
153  
154 __END__
« Data/Frame/IO/CSV.pm ↑ Summary Data/Frame.pm »