| 1 | package Chart::GGPlot::Scale::Discrete; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Discrete scale | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Chart::GGPlot::Class qw(:pdl); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '0.002003'; # VERSION | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use PDL::Primitive qw(which); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Types::Standard qw(Any ArrayRef Bool CodeRef ConsumerOf InstanceOf); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Types::PDL -types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Chart::GGPlot::Range::Discrete; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Chart::GGPlot::Range::Functions qw(discrete_range); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Chart::GGPlot::Util qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | has drop => ( is => 'ro', default => sub { true } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 19 | 2 | has na_translate => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 20 | 2 | has _n_breaks_cache => ( is => 'rw' ); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | has _palette_cache => ( | ||||||||||||||||||||||||||||
| 23 | is => 'rw', | ||||||||||||||||||||||||||||
| 24 | isa => ConsumerOf ['PDL::SV'], | ||||||||||||||||||||||||||||
| 25 | 6 | ▶ | default => sub { PDL::SV->new( [] ); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 2 | ); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | has range => ( | ||||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||
| 30 | isa => ConsumerOf ["Chart::GGPlot::Range::Discrete"], | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | default => sub { discrete_range() } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 32 | 2 | ); | |||||||||||||||||||||||||||
| 33 | 2 | has range_c => ( | |||||||||||||||||||||||||||
| 34 | is => 'rw', | ||||||||||||||||||||||||||||
| 35 | isa => ConsumerOf ['Chart::GGPlot::Range::Continuous'], | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | with qw( | |||||||||||||||||||||||||||
| 39 | Chart::GGPlot::Scale | ||||||||||||||||||||||||||||
| 40 | MooseX::Clone | ||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | method train ($p) { | ||||||||||||||||||||||||||
|
Subroutine
train: called CC=4 SCAR=14.3
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | return if ( $p->isempty ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 1 | $self->range->train( $p, $self->drop, !$self->na_translate ); | |||||||||||||||||||||||||||
| 46 | 2 | } | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 3 | ▶ | method transform ($p) { $p; } | ||||||||||||||||||||||||||
|
Subroutine
transform: called CC=3 SCAR=11.2
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | ▶ | method map_to_limits ( $p, $limits = $self->get_limits ) { | ||||||||||||||||||||||||||
|
Subroutine
map_to_limits: called CC=9 SCAR=25.3
Pod
undocumented
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 51 | 1 | my $n = $limits->ngood; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | my $pal; | |||||||||||||||||||||||||||
| 54 | 1 | ▶ | if ( defined $self->_n_breaks_cache and $self->_n_breaks_cache == $n ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->_n_breaks_cache and $self->_n_breaks_cache == $n
MC/DC0%0/2
defined $self->_n_breaks_cache and $self->_n_breaks_cache == $n
defined $self->_n_breaks_cache
$self->_n_breaks_cache == $n
Truth table33%1/3
A and B
A= defined $self->_n_breaks_cacheB= $self->_n_breaks_cache == $n
| |||||||||||||||||||||||||||||
| 55 | 0 | $pal = $self->_palette_cache; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||||||||||
| 58 | 1 | ▶ | if ( defined $self->_n_breaks_cache ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | warn "Cached palette does not match requested"; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 1 | $pal = $self->palette->( int($n) ); | |||||||||||||||||||||||||||
| 62 | 1 | $self->_palette_cache($pal); | |||||||||||||||||||||||||||
| 63 | 1 | $self->_n_breaks_cache($n); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | my $pal_match = | ||||||||||||||||||||||||||
|
Condition33%1/3
$pal->$Chart::GGPlot::Scale::Discrete::_call_if_can("names") // $limits
MC/DC0%0/2
$pal->$Chart::GGPlot::Scale::Discrete::_call_if_can("names") // $limits
$pal->$Chart::GGPlot::Scale::Discrete::_call_if_can("names")
$limits
Truth table33%1/3
A // B
A= $pal->$Chart::GGPlot::Scale::Discrete::_call_if_can("names")B= $limits
| |||||||||||||||||||||||||||||
| 67 | $pal->slice( match( $p, ( $pal->$_call_if_can('names') // $limits ) ) ); | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | return ( | ||||||||||||||||||||||||||||
| 70 | 1 | ▶ | $self->na_translate | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | ? ifelse( | ||||||||||||||||||||||||||||
| 72 | ( $p->isbad | $pal_match->isbad ), | ||||||||||||||||||||||||||||
| 73 | PDL::SV->new( [ $self->na_value ] ), | ||||||||||||||||||||||||||||
| 74 | $pal_match | ||||||||||||||||||||||||||||
| 75 | ) | ||||||||||||||||||||||||||||
| 76 | : $pal_match | ||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | 2 | } | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | method dimension ( $expand = pdl([0, 0, 0, 0]) ) { | ||||||||||||||||||||||||||
|
Subroutine
dimension: not called CC=4 SCAR=30.0
Pod
undocumented
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 81 | 0 | return expand_range4( [ 0, scalar( @{ $self->get_limits } ) ], $expand ); | |||||||||||||||||||||||||||
| 82 | 2 | } | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 12 | ▶ | method get_breaks ( $limits = $self->get_limits() ) { | ||||||||||||||||||||||||||
|
Subroutine
get_breaks: called CC=8 SCAR=22.6
Pod
undocumented
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 85 | 12 | ▶ | if ( $limits->isempty ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | $limits = $self->get_limits(); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 12 | ▶ | return pdl( [] ) if $self->isempty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 10 | ▶ | return null if ( $self->breaks->$_call_if_object('isempty') ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 10 | my $breaks; | |||||||||||||||||||||||||||
| 93 | 10 | ▶ | if ( not defined $self->breaks ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 10 | $breaks = $limits; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||
| 97 | 0 | $breaks = call_if_coderef( $self->breaks, $limits ); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # Breaks can only occur only on values in domain | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # TODO: See if it's better to change the behavior of PDL::Factor or | ||||||||||||||||||||||||||||
| 103 | # make some level of abstraction there. | ||||||||||||||||||||||||||||
| 104 | 20 | ▶ | state $unpdl = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 105 | 20 | my ($x) = @_; | |||||||||||||||||||||||||||
| 106 | 20 | ▶ | if ($x->$_DOES('PDL::Factor')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | my $levels = $x->levels; | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | return $x->unpdl->map(sub { $levels->[$_] }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 109 | } else { | ||||||||||||||||||||||||||||
| 110 | 20 | return $x->unpdl; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 10 | }; | |||||||||||||||||||||||||||
| 113 | 10 | my $in_domain = $unpdl->($breaks)->intersect( $unpdl->($limits) ); | |||||||||||||||||||||||||||
| 114 | 10 | return PDL::SV->new($in_domain); | |||||||||||||||||||||||||||
| 115 | 2 | } | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | ▶ | sub get_breaks_minor { return []; } | ||||||||||||||||||||||||||
|
Subroutine
get_breaks_minor: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 5 | ▶ | method get_labels ( $breaks = $self->get_breaks ) { | ||||||||||||||||||||||||||
|
Subroutine
get_labels: called CC=10 SCAR=30.2
Pod
undocumented
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 120 | 5 | ▶ | return pdl( [] ) if $self->isempty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 3 | ▶ | return null if $breaks->$_call_if_object('isempty'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 3 | ▶ | return null if $self->labels->$_call_if_object('isempty'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 3 | ▶ | if ( not defined $self->labels ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 126 | 3 | return $self->get_breaks(); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | elsif ( Ref::Util::is_coderef( $self->labels ) ) { | ||||||||||||||||||||||||||||
| 129 | 0 | return $self->labels->($breaks); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | else { | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ( not $self->labels->names->isempty ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # If labels have names, use them to match with breaks | ||||||||||||||||||||||||||||
| 135 | 0 | my $labels = $breaks; | |||||||||||||||||||||||||||
| 136 | 0 | my $match_id = which( match( $self->labels->names, $labels ) > 0 ); | |||||||||||||||||||||||||||
| 137 | 0 | $labels->slice($match_id) .= $self->labels->slice($match_id); | |||||||||||||||||||||||||||
| 138 | 0 | return $labels; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | else { | ||||||||||||||||||||||||||||
| 141 | 0 | my $labels = $self->labels; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # TODO | ||||||||||||||||||||||||||||
| 144 | # Need to ensure that if breaks were dropped, corresponding labels are too | ||||||||||||||||||||||||||||
| 145 | # pos <- attr(breaks, "pos") | ||||||||||||||||||||||||||||
| 146 | # if (!is.null(pos)) { | ||||||||||||||||||||||||||||
| 147 | # <- labels[pos] | ||||||||||||||||||||||||||||
| 148 | # } | ||||||||||||||||||||||||||||
| 149 | 0 | return $labels; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 2 | } | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 2 | ▶ | method break_info ( $range = undef ) { | ||||||||||||||||||||||||||
|
Subroutine
break_info: called CC=4 SCAR=14.0
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # for discrete, limits != range | ||||||||||||||||||||||||||||
| 157 | 2 | my $limits = $self->get_limits; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 2 | my $major = $self->get_breaks($limits); | |||||||||||||||||||||||||||
| 160 | 2 | my $major_n; | |||||||||||||||||||||||||||
| 161 | my $labels; | ||||||||||||||||||||||||||||
| 162 | 2 | ▶ | if ( defined $major ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 2 | $labels = $self->get_labels($major); | |||||||||||||||||||||||||||
| 164 | 2 | $major = $self->map_to_limits($major); | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 2 | $major_n = rescale( $major, [ 0, 1 ], $range ); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | # Discreate scale has no minor breaks | ||||||||||||||||||||||||||||
| 170 | return { | ||||||||||||||||||||||||||||
| 171 | 2 | range => $range, | |||||||||||||||||||||||||||
| 172 | labels => $labels, | ||||||||||||||||||||||||||||
| 173 | major => $major_n, | ||||||||||||||||||||||||||||
| 174 | major_source => $major, | ||||||||||||||||||||||||||||
| 175 | }; | ||||||||||||||||||||||||||||
| 176 | 2 | } | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 2 | 1; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | __END__ | ||||||||||||||||||||||||||||