| 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
| |||||||||||||||||||||||||||||
| 39 | 29 | ▶ | return if ( $p->isempty ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 44 | 10 | my $new_p = $self->trans->transform->($p); | |||||||||||||||||||||||||||
| 45 | 10 | ▶ | if ( ( $p->isfinite != $new_p->isfinite )->any ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $type = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | $self->scale_name eq "position_c" ? "continuous" : "discrete"; | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my $axis = $self->aesthetics->exists('x') ? "x" : "y"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 74 | 17 | ▶ | if ( $limits->isempty ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | $limits = $self->get_limits(); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 17 | ▶ | return pdl( [] ) if $self->isempty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 17 | ▶ | return null if ( $self->breaks->$_call_if_object('isempty') ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 108 | 12 | ▶ | if ( $limits->isempty ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | $limits = $self->get_limits(); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 12 | ▶ | return pdl( [] ) if ( zero_range($limits) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 12 | ▶ | return null if ( $self->minor_breaks->$_call_if_object('isempty') ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 12 | my $breaks; | |||||||||||||||||||||||||||
| 116 | 12 | ▶ | if ( not defined $self->minor_breaks ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 117 | 12 | ▶ | $breaks = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 137 | 13 | ▶ | return null if ( $breaks->isempty ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 13 | $breaks = $self->trans->inverse->($breaks); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 13 | my $labels; | |||||||||||||||||||||||||||
| 142 | 13 | ▶ | if ( not defined $self->labels ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 158 | 11 | ▶ | if ( $range->$_DOES('Chart::GGPlot::Range') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | $range = $range->range; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 11 | ▶ | if ( $range->isempty ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
not $labels->isempty and $major->badflag
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
| |||||||||||||||||||||||||||||
| 171 | 11 | $labels = $labels->where( $major->isgood ); | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 11 | ▶ | if ( not $major->isempty and $major->badflag ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $major->isempty and $major->badflag
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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$minor->isempty and $minor->badflag
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||