Chart/GGPlot/ScalesList.pm

filter: Statementstmt 95.4% 166 / 174 Branchbran 50.0% 37 / 74 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 96.9% 32 / 33 Podpod 46.1% 6 / 13 totaltotal 78.9% 244 / 309 CC 43 SCAR 15.1
CC
43
Cov
80%
add_defaults
12.0
train_df
7.1
add
6.1
CRAP 4.5
« Chart/GGPlot/Global.pm ↑ Summary Chart/GGPlot/Scale/ContinuousPosition.pm »
1 package Chart::GGPlot::ScalesList;
2  
3 # ABSTRACT: Encapsulation multiple scale objects
4  
5 3 ▶use Chart::GGPlot::Class qw(:pdl);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.002003'; # VERSION
9  
10 3 ▶use List::AllUtils qw(pairmap pairkeys);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Types::Standard qw(Any ArrayRef Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Type::Params;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use PDL::Primitive qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use Chart::GGPlot::Aes::Functions qw(aes_to_scale);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Chart::GGPlot::Scale::Functions qw(find_scale);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Chart::GGPlot::Types qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Chart::GGPlot::Util qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 9 ▶has scales => ( is => 'ro', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
22  
23  
24 69 ▶sub find {
Subroutine find: called CC=1 SCAR=0.0
Pod documented
25 69 state $check = Type::Params::compile( Object,
26 ArrayRef->plus_coercions(ArrayRefFromAny) );
27 69 my ( $self, $aesthetic ) = $check->(@_);
28  
29 return pdl(
30 [
31 121 map { $_->aesthetics->intersect($aesthetic)->length > 0 }
32 69 @{ $self->scales }
33 ]
34 );
35 }
36  
37  
38 6 ▶method has_scale ($aesthetic) {
Subroutine has_scale: called CC=3 SCAR=11.2
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method has_scale (expected 2, got ' . @_ . ')') 0 6 6
@_ > 2 and Function::Parameters::_croak('Too many arguments for method has_scale (expected 2, got ' . @_ . ')') 0 6 6
39 6 return !!( $self->find($aesthetic)->any );
40 3 }
41  
42 20 ▶method add ($scale) {
Subroutine add: called CC=5 SCAR=18.0
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method add (expected 2, got ' . @_ . ')') 0 20 20
@_ > 2 and Function::Parameters::_croak('Too many arguments for method add (expected 2, got ' . @_ . ')') 0 20 20
43 20 ▶ return unless $scale;
Branch: 1 branch
BranchTrueFalseTotal
unless $scale 0 20 20
44  
45 20 my $prev_aes = $self->find( $scale->aesthetics );
46 20 ▶ if ( $prev_aes->any ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_aes->any) 0 20 20
47 0 my $aes_name =
48 $self->scales->slice( [ $prev_aes->at(0) ] )->aesthetics->[0];
49 0 my $message =
50 sprintf( "Scale for '%s' is already present. "
51 . "Adding another scale for '%s', which will replace the existing scale.",
52 $aes_name, $aes_name );
53 0 warn($message);
54  
55 # Remove old scale for this aesthetic (if it exists)
56 0 $self->scales = $self->scales->slice( !$prev_aes );
57 }
58 20 push @{$self->scales}, $scale;
59 20 return;
60 3 }
61  
62  
63 20 ▶method length () { $self->scales->length; }
Subroutine length: called CC=3 SCAR=11.2
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method length (expected 1, got ' . @_ . ')') 0 20 20
@_ > 1 and Function::Parameters::_croak('Too many arguments for method length (expected 1, got ' . @_ . ')') 0 20 20
64  
65 17 ▶method input () {
Subroutine input: called CC=3 SCAR=11.2
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method input (expected 1, got ' . @_ . ')') 0 17 17
@_ > 1 and Function::Parameters::_croak('Too many arguments for method input (expected 1, got ' . @_ . ')') 0 17 17
66 17 ▶ return $self->scales->map( sub { @{ $_->aesthetics } } );
Subroutine __ANON__: called CC=1 SCAR=0.0
67 3 }
68  
69 7 ▶method non_position_scales () {
Subroutine non_position_scales: called CC=3 SCAR=11.2
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method non_position_scales (expected 1, got ' . @_ . ')') 0 7 7
@_ > 1 and Function::Parameters::_croak('Too many arguments for method non_position_scales (expected 1, got ' . @_ . ')') 0 7 7
70 7 my $class = ref($self);
71 7 my @indices = which( !$self->find('x') & !$self->find('y') )->flatten;
72 7 return $class->new( scales => $self->scales->slice( \@indices ) );
73 3 }
74  
75  
76 26 ▶method get_scales ($aesthetic) {
Subroutine get_scales: called CC=4 SCAR=14.0
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method get_scales (expected 2, got ' . @_ . ')') 0 26 26
@_ > 2 and Function::Parameters::_croak('Too many arguments for method get_scales (expected 2, got ' . @_ . ')') 0 26 26
77 26 my $indexes = which( $self->find($aesthetic) );
78 26 ▶ return undef if ( $indexes->isempty );
Branch: 1 branch
BranchTrueFalseTotal
if $indexes->isempty 1 25 26
79 25 return $self->scales->at( $indexes->at(0) );
80 3 }
81  
82 10 ▶method isempty () { $self->length == 0 }
Subroutine isempty: called CC=3 SCAR=11.2
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method isempty (expected 1, got ' . @_ . ')') 0 10 10
@_ > 1 and Function::Parameters::_croak('Too many arguments for method isempty (expected 1, got ' . @_ . ')') 0 10 10
83  
84  
85 2 ▶method train_df ($df, $drop=false) {
Subroutine train_df: called CC=6 SCAR=19.6
Pod documented
Branch: 3 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method train_df (expected 2, got ' . @_ . ')') 0 2 2
@_ > 3 and Function::Parameters::_croak('Too many arguments for method train_df (expected 3, got ' . @_ . ')') 0 2 2
@_ < 2 and $drop = &false() 2 0 2
86 2 ▶ return [] if ( $df->isempty or $self->isempty );
Branch: 1 branch
BranchTrueFalseTotal
if $df->isempty or $self->isempty 0 2 2
Condition33%1/3
$df->isempty or $self->isempty
l!l&&r!l&&!r
002
MC/DC0%0/2
$df->isempty or $self->isempty
$df->isempty $self->isempty
Truth table33%1/3
A or B
A= $df->isemptyB= $self->isempty
ABresult
1X1
011
000
87  
88 2 ▶ return $self->scales->map( sub { $_->train_df($df) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
89 3 }
90  
91  
92 2 ▶method map_df ($df) {
Subroutine map_df: called CC=5 SCAR=17.1
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method map_df (expected 2, got ' . @_ . ')') 0 2 2
@_ > 2 and Function::Parameters::_croak('Too many arguments for method map_df (expected 2, got ' . @_ . ')') 0 2 2
93 2 ▶ return $df if ( $df->isempty or $self->isempty );
Branch: 1 branch
BranchTrueFalseTotal
if $df->isempty or $self->isempty 0 2 2
Condition33%1/3
$df->isempty or $self->isempty
l!l&&r!l&&!r
002
MC/DC0%0/2
$df->isempty or $self->isempty
$df->isempty $self->isempty
Truth table33%1/3
A or B
A= $df->isemptyB= $self->isempty
ABresult
1X1
011
000
94  
95 my $mapped = $self->scales->map(
96 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
97 2 my $x = $_->map_df($df);
98 2 ▶ return defined $x ? $x->flatten : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $x ? : 2 0 2
99 }
100 2 );
101 return Data::Frame->new(
102 columns => [
103 @$mapped,
104 $df->names->setdiff( [ pairkeys @$mapped ] )
105 2 ▶ ->map( sub { $_ => $df->at($_) } )->flatten
Subroutine __ANON__: called CC=1 SCAR=0.0
106 ]
107 );
108 3 }
109  
110 # Transform values to cardinal representation
111 6 ▶method transform_df ($df) {
Subroutine transform_df: called CC=5 SCAR=16.7
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method transform_df (expected 2, got ' . @_ . ')') 0 6 6
@_ > 2 and Function::Parameters::_croak('Too many arguments for method transform_df (expected 2, got ' . @_ . ')') 0 6 6
112 6 ▶ return $df if ( $df->isempty or $self->isempty );
Branch: 1 branch
BranchTrueFalseTotal
if $df->isempty or $self->isempty 0 6 6
Condition33%1/3
$df->isempty or $self->isempty
l!l&&r!l&&!r
006
MC/DC0%0/2
$df->isempty or $self->isempty
$df->isempty $self->isempty
Truth table33%1/3
A or B
A= $df->isemptyB= $self->isempty
ABresult
1X1
011
000
113  
114 my $transformed =
115 6 ▶ $self->scales->map( sub { $_->transform_df($df)->flatten } );
Subroutine __ANON__: called CC=1 SCAR=0.0
116 6 my @transformed_vars = pairkeys @$transformed;
117 my $new = Data::Frame->new(
118 columns => [
119 @$transformed,
120 $df->names->setdiff( \@transformed_vars )
121 6 ▶ ->map( sub { $_ => $df->at($_) } )->flatten
Subroutine __ANON__: called CC=1 SCAR=0.0
122 ]
123 );
124 6 return $new;
125 3 }
126  
127 # aesthetics: a list of aesthetic-variable mappings. The name of each
128 # item is the aesthetic, and the value of each item is the variable in data.
129 8 ▶method add_defaults ($data, $aesthetics) {
Subroutine add_defaults: called CC=9 SCAR=24.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 3 and Function::Parameters::_croak('Too few arguments for method add_defaults (expected 3, got ' . @_ . ')') 0 8 8
@_ > 3 and Function::Parameters::_croak('Too many arguments for method add_defaults (expected 3, got ' . @_ . ')') 0 8 8
130 8 ▶ return if ( $aesthetics->isempty );
Branch: 1 branch
BranchTrueFalseTotal
if $aesthetics->isempty 0 8 8
131  
132 8 $aesthetics = $aesthetics->rename(\&aes_to_scale);
133 8 my $new_aesthetics = $aesthetics->names->setdiff( $self->input );
134  
135 # No new aesthetics, so no new scales to add
136 8 ▶ return if ( $new_aesthetics->isempty );
Branch: 1 branch
BranchTrueFalseTotal
if $new_aesthetics->isempty 0 8 8
137  
138 8 state $skip_aes = {
139 group => 1,
140 };
141  
142 17 ▶ my %datacols = pairmap { $a => $data->eval_tidy($b) }
Subroutine __ANON__: called CC=1 SCAR=0.0
143 8 ( $aesthetics->hslice($new_aesthetics)->flatten );
144 8 for my $aes ( sort grep { not $skip_aes->{$_} } keys %datacols ) {
145 16 my ( $scale_f, $func_name ) = find_scale( $aes, $datacols{$aes} );
146 16 ▶ unless ( defined $scale_f ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $scale_f) 0 16 16
147 # some aesthetics do not have scale functions
148 0 ▶ if ( List::AllUtils::any { $aes eq $_ } (qw(weight width label)) ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&List::AllUtils::any(sub { $aes eq $_; } , 'weight', 'width', 'label')) { } 0 0 0
149 0 next;
150 }
151 else {
152 0 die sprintf(
153 "Cannot find scale for aes %s. Missing a function of name %s",
154 $aes, $func_name );
155 }
156 }
157 $log->debugf(
158 16 ▶ "ScalesList::add_defaults : Got scale function %s for aes %s",
Branch: 1 branch
BranchTrueFalseTotal
if $Chart::GGPlot::ScalesList::log->is_debug 0 16 16
159 $func_name, $aes )
160 if $log->is_debug;
161 16 $self->add( $scale_f->() );
162 }
163 3 }
164  
165 # Add missing but required scales. $aes_names is typically [qw(x y)].
166 7 ▶method add_missing ($aes_names) {
Subroutine add_missing: called CC=4 SCAR=13.9
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method add_missing (expected 2, got ' . @_ . ')') 0 7 7
@_ > 2 and Function::Parameters::_croak('Too many arguments for method add_missing (expected 2, got ' . @_ . ')') 0 7 7
167 7 state $check =
168 Type::Params::compile( ArrayRef->plus_coercions(ArrayRefFromAny) );
169 7 ($aes_names) = $check->($aes_names);
170  
171 7 for my $aes ( @{ $aes_names->setdiff( $self->input ) } ) {
172 2 my $scale_name = "scale_${aes}_continuous";
173 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
174 2 my $scale_f = \&{"Chart::GGPlot::Scale::Functions::$scale_name"};
175 2 $self->add( $scale_f->() );
176 }
177 3 }
178  
179 3 __PACKAGE__->meta->make_immutable;
180  
181 3 1;
182  
183 __END__
« Chart/GGPlot/Global.pm ↑ Summary Chart/GGPlot/Scale/ContinuousPosition.pm »