Chart/GGPlot/Scale/Continuous.pm

filter: Statementstmt 83.8% 130 / 155 Branchbran 49.0% 49 / 100 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 76.1% 16 / 21 Podpod 0.0% 0 / 9 totaltotal 66.0% 198 / 300 CC 54 SCAR 23.7
CC
54
Cov
69%
get_breaks_minor
20.9
break_info
16.1
string
12.0
CRAP 10.7
« Chart/GGPlot/Util/_Labeling.pm ↑ Summary Chart/GGPlot/Layout.pm »
1 package Chart::GGPlot::Scale::Continuous;
2  
3 # ABSTRACT: Continuous scale
4  
5 4 ▶use Chart::GGPlot::Class qw(:pdl);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 our $VERSION = '0.002003'; # VERSION
9  
10 4 ▶use PDL::Primitive qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Types::PDL qw(Piddle PiddleFromAny);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Types::Standard qw(Bool CodeRef ConsumerOf Maybe InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use Chart::GGPlot::Range::Continuous;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Chart::GGPlot::Types qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Chart::GGPlot::Util qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 0 ▶has rescaler => ( is => 'ro', isa => CodeRef, default => sub { \&rescale } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
20  
21  
22 0 ▶has oob => ( is => 'ro', isa => CodeRef, default => sub { \&censor } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
23  
24  
25 4 has minor_breaks => ( is => 'ro', isa => Maybe [ ( Piddle | CodeRef ) ] );
26 has range => (
27 is => 'ro',
28 isa => ConsumerOf ["Chart::GGPlot::Range::Continuous"],
29 0 ▶ default => sub { Chart::GGPlot::Range::Continuous->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 4 );
31  
32 4 with qw(
33 Chart::GGPlot::Scale
34 MooseX::Clone
35 );
36  
37 # merge $p into range of the scale object
38 29 ▶method train ($p) {
Subroutine train: called CC=4 SCAR=14.3
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method train (expected 2, got ' . @_ . ')') 0 29 29
@_ > 2 and Function::Parameters::_croak('Too many arguments for method train (expected 2, got ' . @_ . ')') 0 29 29
39 29 ▶ return if ( $p->isempty );
Branch: 1 branch
BranchTrueFalseTotal
if $p->isempty 0 29 29
40 29 $self->range->train($p);
41 4 }
42  
43 10 ▶method transform ($p) {
Subroutine transform: called CC=6 SCAR=22.9
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method transform (expected 2, got ' . @_ . ')') 0 10 10
@_ > 2 and Function::Parameters::_croak('Too many arguments for method transform (expected 2, got ' . @_ . ')') 0 10 10
44 10 my $new_p = $self->trans->transform->($p);
45 10 ▶ if ( ( $p->isfinite != $new_p->isfinite )->any ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($p->isfinite != $new_p->isfinite)->any) 0 10 10
46 0 ▶ my $type =
Branch: 1 branch
BranchTrueFalseTotal
$self->scale_name eq 'position_c' ? : 0 0 0
47 $self->scale_name eq "position_c" ? "continuous" : "discrete";
48 0 ▶ my $axis = $self->aesthetics->exists('x') ? "x" : "y";
Branch: 1 branch
BranchTrueFalseTotal
$self->aesthetics->exists('x') ? : 0 0 0
49 0 warn("Transformation introduced infinite values in $type ${axis}-axis");
50 }
51 10 return $new_p;
52 4 }
53  
54 1 ▶method map_to_limits ( $p, $limits = $self->get_limits() ) {
Subroutine map_to_limits: called CC=4 SCAR=14.1
Pod undocumented
Branch: 3 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method map_to_limits (expected 2, got ' . @_ . ')') 0 1 1
@_ > 3 and Function::Parameters::_croak('Too many arguments for method map_to_limits (expected 3, got ' . @_ . ')') 0 1 1
@_ < 2 and $limits = $self->get_limits 1 0 1
55  
56 # rescale from $limits to [0,1]
57 1 my $new_p =
58 $self->rescaler->( $self->oob->( $p, $limits ), pdl( [ 0, 1 ] ),
59 $limits );
60  
61 1 my $uniq = $new_p->uniqvec; # $uniq is sorted pdl
62 1 my $pal = $self->palette->($uniq);
63 1 my $scaled = $pal->slice( match( $new_p, $uniq ) );
64  
65 1 return $scaled->setbadtoval( $self->na_value );
66 4 }
67  
68 12 ▶method dimension ( $expand = pdl([0, 0, 0, 0]) ) {
Subroutine dimension: called CC=4 SCAR=14.0
Pod undocumented
Branch: 3 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method dimension (expected 1, got ' . @_ . ')') 0 12 12
@_ > 2 and Function::Parameters::_croak('Too many arguments for method dimension (expected 2, got ' . @_ . ')') 0 12 12
@_ < 1 and $expand = pdl([0, 0, 0, 0]) 2 10 12
69 12 return expand_range4( $self->get_limits(), $expand );
70 4 }
71  
72 # return arrayref of breaks
73 17 ▶method get_breaks ( $limits=$self->get_limits ) {
Subroutine get_breaks: called CC=9 SCAR=24.2
Pod undocumented
Branch: 3 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method get_breaks (expected 1, got ' . @_ . ')') 0 17 17
@_ > 2 and Function::Parameters::_croak('Too many arguments for method get_breaks (expected 2, got ' . @_ . ')') 0 17 17
@_ < 1 and $limits = $self->get_limits 4 13 17
74 17 ▶ if ( $limits->isempty ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limits->isempty) 0 17 17
75 0 $limits = $self->get_limits();
76 }
77  
78 17 ▶ return pdl( [] ) if $self->isempty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->isempty 0 17 17
79 17 ▶ return null if ( $self->breaks->$_call_if_object('isempty') );
Branch: 1 branch
BranchTrueFalseTotal
if $self->breaks->$Chart::GGPlot::Scale::Continuous::_call_if_object("isempty") 0 17 17
80  
81 # Limits in transformed space need to be converted back to data space
82 17 $limits = $self->trans->inverse->($limits);
83  
84 17 my $breaks;
85 17 ▶ if ( zero_range($limits) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (zero_range($limits)) { } 0 17 17
elsif (not defined $self->breaks) { } 17 0 17
86 0 $breaks = pdl( [ $limits->at(0) ] );
87 }
88 elsif ( not defined $self->breaks ) {
89 17 $breaks = $self->trans->breaks->($limits);
90 }
91 else {
92 0 $breaks = call_if_coderef( $self->breaks, $limits );
93 }
94  
95 # Breaks in data space need to be converted back to transformed space
96 # And any breaks outside the dimensions need to be flagged as missing
97  
98 17 $breaks = censor( $self->trans->transform->($breaks),
99 $self->trans->transform->($limits), 0 );
100 17 return $breaks;
101 4 }
102  
103 12 ▶method get_breaks_minor (
Subroutine get_breaks_minor: called CC=12 SCAR=30.4
Pod undocumented
104 $n = 2,
105 $b = $self->break_positions,
106 $limits = $self->get_limits()
107 12 ▶ ) {
Branch: 5 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method get_breaks_minor (expected 1, got ' . @_ . ')') 0 12 12
@_ > 4 and Function::Parameters::_croak('Too many arguments for method get_breaks_minor (expected 4, got ' . @_ . ')') 0 12 12
@_ < 3 and do { @_ < 2 and do { @_ < 1 and $n = 2; $b = $self->break_positions }; $limits = $self->get_limits } 1 11 12
@_ < 2 and do { @_ < 1 and $n = 2; $b = $self->break_positions } 1 0 1
@_ < 1 and $n = 2 1 0 1
108 12 ▶ if ( $limits->isempty ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limits->isempty) 0 12 12
109 0 $limits = $self->get_limits();
110 }
111  
112 12 ▶ return pdl( [] ) if ( zero_range($limits) );
Branch: 1 branch
BranchTrueFalseTotal
if zero_range($limits) 0 12 12
113 12 ▶ return null if ( $self->minor_breaks->$_call_if_object('isempty') );
Branch: 1 branch
BranchTrueFalseTotal
if $self->minor_breaks->$Chart::GGPlot::Scale::Continuous::_call_if_object("isempty") 0 12 12
114  
115 12 my $breaks;
116 12 ▶ if ( not defined $self->minor_breaks ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->minor_breaks) { } 12 0 12
elsif (Ref::Util::XS::is_coderef($self->minor_breaks)) { } 0 0 0
117 12 ▶ $breaks =
Branch: 1 branch
BranchTrueFalseTotal
$b->isempty ? : 0 12 12
118 $b->isempty
119 ? null
120 : $self->trans->minor_breaks->( $b, $limits, $n );
121 }
122 elsif ( Ref::Util::is_coderef( $self->minor_breaks ) ) {
123  
124 # Find breaks in data space, and convert to numeric
125 0 $breaks = $self->minor_breaks->( $self->trans->inverse->($limits) );
126 0 $breaks = $self->trans->transform->($breaks);
127 }
128 else {
129 0 $breaks = $self->trans->transform->( $self->minor_breaks );
130 }
131  
132 # Any minor breaks outside the dimensions need to be thrown away
133 12 return discard( $breaks, $limits );
134 4 }
135  
136 13 ▶method get_labels ( $breaks = $self->get_breaks ) {
Subroutine get_labels: called CC=8 SCAR=23.6
Pod undocumented
Branch: 3 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method get_labels (expected 1, got ' . @_ . ')') 0 13 13
@_ > 2 and Function::Parameters::_croak('Too many arguments for method get_labels (expected 2, got ' . @_ . ')') 0 13 13
@_ < 1 and $breaks = $self->get_breaks 2 11 13
137 13 ▶ return null if ( $breaks->isempty );
Branch: 1 branch
BranchTrueFalseTotal
if $breaks->isempty 0 13 13
138  
139 13 $breaks = $self->trans->inverse->($breaks);
140  
141 13 my $labels;
142 13 ▶ if ( not defined $self->labels ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->labels) { } 13 0 13
elsif ($self->labels->$Chart::GGPlot::Scale::Continuous::_call_if_object('isempty')) { } 0 0 0
143 13 $labels = $self->trans->format->($breaks);
144 }
145 elsif ( $self->labels->$_call_if_object('isempty') ) {
146 0 return PDL::SV->new( [] );
147 }
148 else {
149 0 $labels = call_if_coderef( $self->labels, $breaks );
150 }
151 13 ▶ if ( $labels->length != $breaks->length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($labels->length != $breaks->length) 0 13 13
152 0 die("Breaks and labels are different lengths");
153 }
154 13 return $labels;
155 4 }
156  
157 11 ▶method break_info ($range=$self->dimension) {
Subroutine break_info: called CC=12 SCAR=27.8
Pod undocumented
Branch: 3 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method break_info (expected 1, got ' . @_ . ')') 0 11 11
@_ > 2 and Function::Parameters::_croak('Too many arguments for method break_info (expected 2, got ' . @_ . ')') 0 11 11
@_ < 1 and $range = $self->dimension 0 11 11
158 11 ▶ if ( $range->$_DOES('Chart::GGPlot::Range') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range->$Chart::GGPlot::Scale::Continuous::_DOES("Chart::GGPlot::Range")) 0 11 11
159 0 $range = $range->range;
160 }
161 11 ▶ if ( $range->isempty ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range->isempty) 1 10 11
162 1 $range = $self->dimension;
163 }
164  
165 # major breaks and labels
166 11 my $major = $self->get_breaks($range);
167 11 my $labels = $self->get_labels($major);
168  
169 # drop oob breaks/labels by testing major == NA
170 11 ▶ if ( not $labels->isempty and $major->badflag ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $labels->isempty and $major->badflag) 11 0 11
Condition33%1/3
not $labels->isempty and $major->badflag
!ll&&!rl&&r
0011
MC/DC0%0/2
not $labels->isempty and $major->badflag
not $labels->isempty $major->badflag
Truth table33%1/3
not(A) and B
A= not $labels->isemptyB= $major->badflag
ABresult
0X0
100
111
171 11 $labels = $labels->where( $major->isgood );
172 }
173 11 ▶ if ( not $major->isempty and $major->badflag ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $major->isempty and $major->badflag) 11 0 11
Condition33%1/3
not $major->isempty and $major->badflag
!ll&&!rl&&r
0011
MC/DC0%0/2
not $major->isempty and $major->badflag
not $major->isempty $major->badflag
Truth table33%1/3
not(A) and B
A= not $major->isemptyB= $major->badflag
ABresult
0X0
100
111
174 11 $major = $major->where( $major->isgood );
175 }
176  
177 11 my $minor = $self->get_breaks_minor( 2, $major, $range );
178 11 ▶ unless ( $minor->isempty and $minor->badflag ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($minor->isempty and $minor->badflag) 11 0 11
Condition33%1/3
$minor->isempty and $minor->badflag
!ll&&!rl&&r
1100
MC/DC0%0/2
$minor->isempty and $minor->badflag
$minor->isempty $minor->badflag
Truth table33%1/3
A and B
A= $minor->isemptyB= $minor->badflag
ABresult
0X0
100
111
179 11 $minor = $minor->where( $minor->isgood );
180 }
181  
182 # rescale to [0, 1]
183 11 my $major_n = rescale( $major, pdl( [ 0, 1 ] ), $range );
184 11 my $minor_n = rescale( $minor, pdl( [ 0, 1 ] ), $range );
185  
186 return {
187 11 range => $range,
188 labels => $labels,
189 major => $major_n,
190 minor => $minor_n,
191 major_source => $major,
192 minor_source => $minor,
193 };
194 4 }
195  
196 0 ▶method string () {
Subroutine string: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method string (expected 1, got ' . @_ . ')') 0 0 0
@_ > 1 and Function::Parameters::_croak('Too many arguments for method string (expected 1, got ' . @_ . ')') 0 0 0
197 0 ▶ my $show_range = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
198 0 my $p = shift;
199 0 return $p . '';
200 0 };
201  
202 0 return join( "\n",
203 "<" . ref($self) . ">",
204 " Range: " . &$show_range( $self->range->range ),
205 " Limits: " . &$show_range( $self->dimension() ),
206 ) . "\n";
207 4 }
208  
209 4 __PACKAGE__->meta->make_immutable;
210  
211 4 1;
212  
213 __END__
« Chart/GGPlot/Util/_Labeling.pm ↑ Summary Chart/GGPlot/Layout.pm »