Chart/GGPlot/Trans/Functions.pm

filter: Statementstmt 57.8% 132 / 228 Branchbran 25.3% 33 / 130 Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 59.5% 28 / 47 Podpod 5.8% 1 / 17 totaltotal 46.2% 199 / 430 CC 68 SCAR 19.8
CC
68
Cov
47%
boxcox_trans
20.0
trans_range
12.0
__ANON__
12.0
CRAP 7.3
« Chart/GGPlot/Geom.pm ↑ Summary Chart/GGPlot/Theme/Defaults.pm »
1 package Chart::GGPlot::Trans::Functions;
2  
3 # ABSTRACT: Function interface for Chart::GGPlot::Trans
4  
5 8 ▶use Chart::GGPlot::Setup qw(:base :pdl);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 our $VERSION = '0.002003'; # VERSION
8  
9 8 ▶use PDL::Math;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use PDL::Primitive qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Role::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 ▶use Chart::GGPlot::Trans;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Chart::GGPlot::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use Chart::GGPlot::Util qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 8 ▶use parent qw(Exporter::Tiny);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 8 our @EXPORT_OK = qw(
20 is_trans as_trans trans_range
21 asn_trans atanh_trans
22 identity_trans log_trans
23 log10_trans log2_trans
24 log1p_trans reciprocal_trans
25 reverse_trans sqrt_trans
26 time_trans
27 );
28 8 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
29  
30 25 ▶sub is_trans { $_[0]->$_isa('Chart::GGPlot::Trans'); }
Subroutine is_trans: called CC=1 SCAR=0.0
Pod undocumented
31  
32  
33 8 my %trans_registry = ();
34  
35 88 ▶fun _register_trans($trans) {
Subroutine _register_trans: called CC=3 SCAR=11.3
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun _register_trans (expected 1, got ' . @_ . ')') 0 88 88
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun _register_trans (expected 1, got ' . @_ . ')') 0 88 88
36 88 $trans_registry{ $trans->name } = $trans;
37 8 }
38  
39 22 ▶fun as_trans($x) {
Subroutine as_trans: called CC=5 SCAR=16.2
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun as_trans (expected 1, got ' . @_ . ')') 0 22 22
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun as_trans (expected 1, got ' . @_ . ')') 0 22 22
40 22 ▶ return $x if ( is_trans($x) );
Branch: 1 branch
BranchTrueFalseTotal
if is_trans($x) 1 21 22
41  
42 21 my $trans = $trans_registry{$x};
43 21 ▶ unless ($trans) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($trans) 1 20 21
44 1 die "'$x' is not a transformation";
45 }
46 20 return $trans;
47 8 }
48  
49 # Compute range of transformed values.
50 0 ▶fun trans_range( $trans, $p ) {
Subroutine trans_range: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for fun trans_range (expected 2, got ' . @_ . ')') 0 0 0
@_ > 2 and Function::Parameters::_croak('Too many arguments for fun trans_range (expected 2, got ' . @_ . ')') 0 0 0
51 0 $trans = as_trans($trans);
52 0 return range(
53 $trans->transform->(
54 range_( squish( $p, $trans->domain ), true )
55 )
56 );
57 8 }
58  
59 89 ▶fun _trans_new( $name, $trans, $inv, %rest ) {
Subroutine _trans_new: called CC=4 SCAR=14.5
Branch: 2 branches
BranchTrueFalseTotal
@_ < 3 and Function::Parameters::_croak('Too few arguments for fun _trans_new (expected 3, got ' . @_ . ')') 0 89 89
@_ > 3 and (@_ - 3) % 2 and Function::Parameters::_croak("Odd number of paired arguments for fun _trans_new") 0 89 89
Condition66%2/3
@_ > 3 and (@_ - 3) % 2
!ll&&!rl&&r
41480
MC/DC0%0/2
@_ > 3 and (@_ - 3) % 2
@_ > 3 (@_ - 3) % 2
Truth table66%2/3
A and B
A= @_ > 3B= (@_ - 3) % 2
ABresult
0X0
100
111
60 89 return Chart::GGPlot::Trans->new(
61 name => $name,
62 transform => $trans,
63 inverse => $inv,
64 %rest
65 );
66 8 }
67  
68 # Identity transformation (do nothing).
69 9 ▶fun identity_trans() {
Subroutine identity_trans: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 and Function::Parameters::_croak('Too many arguments for fun identity_trans (expected 0, got ' . @_ . ')') 0 9 9
70 9 ▶ my $id = fun($p) { $p->copy };
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 80 80
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 80 80
71 9 _trans_new( 'identity', $id, $id);
72 8 }
73  
74 # Arc-sin square root transformation.
75 8 ▶fun asn_trans() {
Subroutine asn_trans: called CC=2 SCAR=14.5
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 and Function::Parameters::_croak('Too many arguments for fun asn_trans (expected 0, got ' . @_ . ')') 0 8 8
76 0 ▶ _trans_new(
Branch: 1 branch
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
77 'asn',
78 0 ▶ fun($p) { $p->sqrt->asin * 2; },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
79 0 ▶ fun($p) { ( $p/2 )->sin ** 2; },
Subroutine __ANON__: not called CC=3 SCAR=21.6
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
80 );
81 8 }
82  
83 # Arc-tangent transformation.
84 8 ▶fun atanh_trans() {
Subroutine atanh_trans: called CC=2 SCAR=11.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 and Function::Parameters::_croak('Too many arguments for fun atanh_trans (expected 0, got ' . @_ . ')') 0 8 8
85 0 ▶ my $atanh = fun($p) { $p->atanh };
Subroutine __ANON__: not called CC=3 SCAR=22.6
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
86 8 _trans_new( 'atanh', $atanh, $atanh );
87 8 }
88  
89 # Box-Cox power transformation.
90 0 ▶fun boxcox_trans($x) {
Subroutine boxcox_trans: not called CC=4 SCAR=30.0
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun boxcox_trans (expected 1, got ' . @_ . ')') 0 0 0
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun boxcox_trans (expected 1, got ' . @_ . ')') 0 0 0
91 0 ▶ if ( abs($x) < 1e-07 ) { return ( log_trans() ); }
Branch: 1 branch
BranchTrueFalseTotal
if (abs $x < "1e-07") 0 0 0
92 _trans_new(
93 0 ▶ 'pow-' . $x,
Branch: 1 branch
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
94 0 ▶ fun($p) { ($p ** $x - 1) / $x * sign( $p -1) },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
95 0 ▶ fun($p) { ($p->abs * $x + 1 * sign($p)) ** (1/$x) },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
96 );
97 8 }
98  
99 # Exponential transformation (inverse of log transformation).
100 0 ▶fun exp_trans( $base = exp(1) ) {
Subroutine exp_trans: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun exp_trans (expected 1, got ' . @_ . ')') 0 0 0
@_ < 1 and $base = "2.71828182845905" 0 0 0
101 0 ▶ _trans_new(
Branch: 1 branch
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
102 'power-' . $base,
103 0 ▶ fun($p) { pdl($base) ** $p },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
104 0 ▶ fun($p) { $p->log / log($base) },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
105 );
106 8 }
107  
108 # Log transformation.
109 24 ▶fun log_trans( $base = undef ) {
Subroutine log_trans: called CC=4 SCAR=20.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun log_trans (expected 1, got ' . @_ . ')') 0 24 24
110 24 ▶ my $name = defined $base ? "log$base" : 'log';
Branch: 1 branch
BranchTrueFalseTotal
defined $base ? : 16 8 24
111 24 ▶ $base //= exp(1);
Condition100%2/2
$base //= "2.71828182845905"
l!l
168
MC/DC100%1/1
$base //= "2.71828182845905"
$base
Truth table100%2/2
A
A= $base
Aresult
11
00
112 0 ▶ _trans_new( $name,
Branch: 1 branch
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
113 0 ▶ fun($p) { $p->log / log($base) },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
114 0 ▶ fun($p) { pdl($base) ** $p },
Subroutine __ANON__: not called CC=3 SCAR=21.6
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
115 breaks => log_breaks($base),
116 );
117 8 }
118  
119 8 ▶sub log10_trans { log_trans(10) }
Subroutine log10_trans: called CC=1 SCAR=0.0
Pod undocumented
120 8 ▶sub log2_trans { log_trans(2) }
Subroutine log2_trans: called CC=1 SCAR=0.0
Pod undocumented
121  
122 8 ▶fun log1p_trans() {
Subroutine log1p_trans: called CC=2 SCAR=14.5
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 and Function::Parameters::_croak('Too many arguments for fun log1p_trans (expected 0, got ' . @_ . ')') 0 8 8
123 0 ▶ _trans_new( 'log1p',
Branch: 1 branch
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
124 0 ▶ fun($p) { ($p + 1)->log() },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
125 0 ▶ fun($p) { $p->exp() - 1 },
Subroutine __ANON__: not called CC=3 SCAR=21.6
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
126 );
127 8 }
128  
129 # Probability transformation.
130 0 ▶fun probability_trans( $distribution, @rest ) {
Subroutine probability_trans: not called CC=2 SCAR=15.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun probability_trans (expected 1, got ' . @_ . ')') 0 0 0
131  
132 # TODO: we probably can use Math::CDF or Math::GSL::CDF
133 ...
134 0 }
135  
136 # TODO: revisit this once probability_trans is fixed
137 #sub logit_trans { probability_trans("logis") }
138 #sub probit_trans { probability_trans("norm") }
139  
140 # Reciprocal transformation.
141 8 ▶fun reciprocal_trans() {
Subroutine reciprocal_trans: called CC=2 SCAR=11.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 and Function::Parameters::_croak('Too many arguments for fun reciprocal_trans (expected 0, got ' . @_ . ')') 0 8 8
142 0 ▶ my $reci = fun($p) { 1 / $p };
Subroutine __ANON__: not called CC=3 SCAR=22.6
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
143 8 _trans_new( 'reciprocal', $reci, $reci);
144 8 }
145  
146 # Reverse transformation.
147 8 ▶fun reverse_trans() {
Subroutine reverse_trans: called CC=2 SCAR=11.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 and Function::Parameters::_croak('Too many arguments for fun reverse_trans (expected 0, got ' . @_ . ')') 0 8 8
148 0 ▶ my $rev = fun($p) { -$p };
Subroutine __ANON__: not called CC=3 SCAR=22.6
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
149 8 _trans_new(
150 'reverse', $rev, $rev,
151 minor_breaks => regular_minor_breaks(true),
152 );
153 8 }
154  
155 # Square-root transformation.
156 8 ▶fun sqrt_trans() {
Subroutine sqrt_trans: called CC=2 SCAR=14.5
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 and Function::Parameters::_croak('Too many arguments for fun sqrt_trans (expected 0, got ' . @_ . ')') 0 8 8
157 0 ▶ _trans_new(
Branch: 1 branch
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
158 'sqrt',
159 0 ▶ fun($p) { $p->sqrt },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
160 0 ▶ fun($p) { $p ** 2 },
Subroutine __ANON__: not called CC=3 SCAR=21.6
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 0 0
161 domain => pdl([ 0, 'inf' ])
162 );
163 8 }
164  
165 8 ▶fun time_trans ($tz=undef) {
Subroutine time_trans: called CC=2 SCAR=7.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun time_trans (expected 1, got ' . @_ . ')') 0 8 8
166 13 ▶ state $fix = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
167 13 my ($p, $r) = @_;
168 13 ▶ if ( $p->$_DOES('PDL::Role::HasNames') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->$Chart::GGPlot::Trans::Functions::_DOES("PDL::Role::HasNames")) 5 8 13
169 5 Role::Tiny->apply_roles_to_object( $r, 'PDL::Role::HasNames' );
170 5 $r->names( $p->names );
171 }
172 13 ▶ if ($p->badflag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->badflag) 3 10 13
173 3 $r->setbadif($p->isbad);
174 }
175 13 return $r;
176 8 };
177  
178 # TODO: we don't yet support timezone
179 8 ▶ my $from_time = fun($p) {
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 8 8
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 8 8
180 8 my $rslt = pdl( $p->unpdl );
181 8 return $fix->($p, $rslt);
182 8 };
183 5 ▶ my $to_time = fun($p) {
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 5 5
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 5 5
184 #TODO: See if there is a better way to do it.
185 my $rslt =
186 5 ▶ PDL::DateTime->new( $p->unpdl->map( sub { $_ eq 'BAD' ? 0 : $_ } ) );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'BAD' ? : 4 16 20
187 5 return $fix->($p, $rslt);
188 8 };
189 8 _trans_new( 'time', $from_time, $to_time, breaks => pretty_breaks() );
190 8 }
191  
192 8 for my $trans (
193 asn_trans(), atanh_trans(),
194 identity_trans(), log_trans(),
195 log10_trans(), log2_trans(),
196 log1p_trans(), reciprocal_trans(),
197 reverse_trans(), sqrt_trans(),
198 time_trans(),
199 )
200 {
201 88 _register_trans($trans);
202 }
203  
204 8 1;
205  
206 __END__
« Chart/GGPlot/Geom.pm ↑ Summary Chart/GGPlot/Theme/Defaults.pm »