Chart/GGPlot/Position/Util.pm

filter: Statementstmt 80.7% 147 / 182 Branchbran 41.6% 35 / 84 Conditioncond 22.2% 4 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 92.0% 23 / 25 Podpod 0.0% 0 / 7 totaltotal 63.7% 209 / 328 CC 52 SCAR 25.7
CC
52
Cov
65%
pos_dodge
56.0
collide_setup
18.2
collide
13.5
CRAP 13.1
« Chart/GGPlot/Position/Dodge.pm ↑ Summary Chart/GGPlot/Layer.pm »
1 package Chart::GGPlot::Position::Util;
2  
3 # ABSTRACT: Utilities internally used by Chart::GGPlot::Position
4  
5 6 ▶use Chart::GGPlot::Setup qw(:base :pdl);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 our $VERSION = '0.002003'; # VERSION
8  
9 6 ▶use Data::Frame;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use List::AllUtils qw(reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use PDL::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use PDL::Primitive qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use Chart::GGPlot::Util qw(ifelse match pmax pmin);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 ▶use parent qw(Exporter::Tiny);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 6 our @EXPORT_OK = qw(collide collide2 pos_dodge pos_dodge2 pos_stack);
19 6 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
20  
21 2 ▶fun collide_setup ($data, $width, $name, $strategy,
Subroutine collide_setup: called CC=9 SCAR=29.0
Pod undocumented
22 2 ▶ $check_width=true, $reverse=false) {
Branch: 4 branches
BranchTrueFalseTotal
@_ < 4 and Function::Parameters::_croak('Too few arguments for fun collide_setup (expected 4, got ' . @_ . ')') 0 2 2
@_ > 6 and Function::Parameters::_croak('Too many arguments for fun collide_setup (expected 6, got ' . @_ . ')') 0 2 2
@_ < 6 and do { @_ < 5 and $check_width = &true(); $reverse = &false() } 0 2 2
@_ < 5 and $check_width = &true() 0 0 0
23 # determine width
24 2 ▶ if ( defined $width ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $width) { } 0 2 2
25 0 ▶ unless ( List::AllUtils::all { $data->exists($_) } qw(xmin xmax) ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless (&List::AllUtils::all(sub { $data->exists($_); } , "xmin", "xmax")) 0 0 0
26 0 $data->set( 'xmin', $data->at('x') - $width / 2 );
27 0 $data->set( 'xmax', $data->at('x') + $width / 2 );
28 }
29 }
30 else {
31 2 ▶ unless ( List::AllUtils::all { $data->exists($_) } qw(xmin xmax) ) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless (&List::AllUtils::all(sub { $data->exists($_); } , "xmin", "xmax")) 0 2 2
32 0 $data->set( 'xmin', $data->at('x') );
33 0 $data->set( 'xmax', $data->at('x') );
34 }
35  
36 2 my $widths = ( $data->at('xmax') - $data->at('xmin') )->uniq;
37 2 ▶ $widths = $widths->where( $widths->isgood ) if $widths->badflag;
Branch: 1 branch
BranchTrueFalseTotal
if $widths->badflag 0 2 2
38  
39 2 $width = $widths->at(0);
40 }
41  
42 2 return { data => $data, width => $width };
43 6 }
44  
45 1 ▶fun collide ($data, $width, $name, $strategy,
Subroutine collide: called CC=10 SCAR=26.0
Pod undocumented
46 1 ▶ :$check_width=true, :$reverse=false, %rest) {
Branch: 4 branches
BranchTrueFalseTotal
@_ < 4 and Function::Parameters::_croak('Too few arguments for fun collide (expected 4, got ' . @_ . ')') 0 1 1
@_ > 4 and (@_ - 4) % 2 and Function::Parameters::_croak("Odd number of paired arguments for fun collide") 0 1 1
exists $rest{'check_width'} ? : 0 1 1
exists $rest{'reverse'} ? : 1 0 1
Condition33%1/3
@_ > 4 and (@_ - 4) % 2
!ll&&!rl&&r
010
MC/DC0%0/2
@_ > 4 and (@_ - 4) % 2
@_ > 4 (@_ - 4) % 2
Truth table33%1/3
A and B
A= @_ > 4B= (@_ - 4) % 2
ABresult
0X0
100
111
47 1 my $dlist =
48 collide_setup( $data, $width, $name, $strategy, $check_width, $reverse );
49 1 $data = $dlist->{data};
50 1 $width = $dlist->{width};
51  
52 # Reorder by x position, then on group. The default stacking order
53 # reverses the group in order to match the legend order.
54 1 ▶ $data = $data->sort( [qw(xmin group)], $reverse ? true : [ true, false ] );
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 1 1
55  
56 # TODO: ddply to preserve the order.
57 # So firstly DF::split() shall preserve the order.
58 1 ▶ state $ddply = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
59 1 my ( $df, $vars, $func ) = @_;
60  
61 1 my $ids = $df->select_columns($vars)->id;
62 1 my $splitted = $df->split($ids);
63 1 my @transformed = map { $func->($_) } values %$splitted;
64 1 ▶ return ( reduce { $a->append($b) } @transformed );
Subroutine __ANON__: called CC=1 SCAR=0.0
65 1 };
66  
67 1 ▶ my $strategy_wrapped = sub { $strategy->( $_[0], $width, %rest ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
68 1 ▶ if ( $data->exists('ymax') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data->exists('ymax')) { } 1 0 1
elsif ($data->exists('y')) { } 0 0 0
69 1 $data = $ddply->( $data, ['xmin'], $strategy_wrapped );
70 }
71 elsif ( $data->exists('y') ) {
72 0 $data->set( 'ymax', $data->at('y') );
73 0 $data = $ddply->( $data, ['xmin'], $strategy_wrapped );
74 0 $data->set( 'y', $data->at('ymax') );
75 }
76 else {
77 0 die "Neither y nor ymax defined";
78 }
79  
80 # TODO: This is only to maintain some order to get some tests pass.
81 # This should be not needed once we fix ddply.
82 1 ▶ return $data->sort( [qw(xmin group)], $reverse ? true : [ true, false ] );
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 1 1
83 6 }
84  
85 # Alternate version of collide() used by position_dodge2()
86 1 ▶fun collide2 ($data, $width, $name, $strategy,
Subroutine collide2: called CC=7 SCAR=21.1
Pod undocumented
87 1 ▶ :$check_width=true, :$reverse=false, %rest) {
Branch: 4 branches
BranchTrueFalseTotal
@_ < 4 and Function::Parameters::_croak('Too few arguments for fun collide2 (expected 4, got ' . @_ . ')') 0 1 1
@_ > 4 and (@_ - 4) % 2 and Function::Parameters::_croak("Odd number of paired arguments for fun collide2") 0 1 1
exists $rest{'check_width'} ? : 1 0 1
exists $rest{'reverse'} ? : 1 0 1
Condition33%1/3
@_ > 4 and (@_ - 4) % 2
!ll&&!rl&&r
010
MC/DC0%0/2
@_ > 4 and (@_ - 4) % 2
@_ > 4 (@_ - 4) % 2
Truth table33%1/3
A and B
A= @_ > 4B= (@_ - 4) % 2
ABresult
0X0
100
111
88 1 my $dlist =
89 collide_setup( $data, $width, $name, $strategy, $check_width, $reverse );
90 1 $data = $dlist->{data};
91 1 $width = $dlist->{width};
92  
93 # Reorder by x position, then on group. The default stacking order is
94 # different than for collide() because of the order in which pos_dodge2
95 # places elements
96 1 ▶ $data = $data->sort( [qw(xmin group)], $reverse ? [ true, false ] : true );
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 1 1
97  
98 1 return $strategy->($data, $width, %rest);
99 6 }
100  
101 0 ▶fun pos_dodge ($df, $width, :$n=undef) {
Subroutine pos_dodge: not called CC=7 SCAR=40.3
Pod undocumented
Branch: 3 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for fun pos_dodge (expected 2, got ' . @_ . ')') 0 0 0
@_ > 2 and (@_ - 2) % 2 and Function::Parameters::_croak("Odd number of paired arguments for fun pos_dodge") 0 0 0
keys %{__rest} and Function::Parameters::_croak('In fun pos_dodge: no such named parameter: ' . join(", ", sort(keys %{__rest}))) 0 0 0
Condition0%0/3
@_ > 2 and (@_ - 2) % 2
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 2 and (@_ - 2) % 2
@_ > 2 (@_ - 2) % 2
Truth table0%0/3
A and B
A= @_ > 2B= (@_ - 2) % 2
ABresult
0X0
100
111
102 0 ▶ $n //= $df->at('group')->uniq->length;
Condition0%0/3
$n //= $df->at("group")->uniq->length
l!l&&r!l&&!r
000
MC/DC0%0/2
$n //= $df->at("group")->uniq->length
$n $df->at("group")->uniq->length
Truth table0%0/3
A //= B
A= $nB= $df->at("group")->uniq->length
ABresult
1X1
011
000
103 0 ▶ if ( $n == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 1) 0 0 0
104 0 return $df;
105 }
106  
107 0 my $d_width = ( $df->at('xmax') - $df->at('xmin') )->max;
108  
109 0 my $group = $df->at('group');
110 0 my $groupidx = match( $group, $group->uniq->qsort );
111 0 $df->set( 'x', $df->at('x') + $width * ( ( $groupidx + 0.5 ) / $n - 0.5 ) );
112  
113 0 my $half_width = $d_width / $n / 2;
114 0 $df->set( 'xmin', $df->at('x') - $half_width );
115 0 $df->set( 'xmax', $df->at('x') + $half_width );
116 0 return $df;
117 6 }
118  
119 1 ▶fun pos_dodge2 ($df, $width, :$n=undef, :$padding=0.1) {
Subroutine pos_dodge2: called CC=10 SCAR=24.6
Pod undocumented
Branch: 4 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for fun pos_dodge2 (expected 2, got ' . @_ . ')') 0 1 1
@_ > 2 and (@_ - 2) % 2 and Function::Parameters::_croak("Odd number of paired arguments for fun pos_dodge2") 0 1 1
exists ${__rest}{'padding'} ? : 1 0 1
keys %{__rest} and Function::Parameters::_croak('In fun pos_dodge2: no such named parameter: ' . join(", ", sort(keys %{__rest}))) 0 1 1
Condition33%1/3
@_ > 2 and (@_ - 2) % 2
!ll&&!rl&&r
010
MC/DC0%0/2
@_ > 2 and (@_ - 2) % 2
@_ > 2 (@_ - 2) % 2
Truth table33%1/3
A and B
A= @_ > 2B= (@_ - 2) % 2
ABresult
0X0
100
111
120 1 ▶ unless ( List::AllUtils::all { $df->exists($_) } qw(xmin xmax) ) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless (&List::AllUtils::all(sub { $df->exists($_); } , "xmin", "xmax")) 0 1 1
121 0 $df->set( 'xmin', $df->at('x') );
122 0 $df->set( 'xmax', $df->at('x') );
123 }
124  
125 # xid represents groups of boxes that share the same position
126 1 my $xid = find_x_overlaps($df);
127  
128 # based on xid find newx, i.e. the center of each group of overlapping
129 # elements. for boxes, bars, etc. this should be the same as original
130 # x, but for arbitrary rects it may not be
131 3 ▶ state $tapply = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
132 3 my ( $p, $factor, $func ) = @_;
133  
134 3 my $rslt = PDL::Core::zeros( $p->length );
135 3 for my $factor_enum ( $factor->uniq->flatten ) {
136 21 my $indices = which( $factor == $factor_enum );
137 21 my $group = $p->slice($indices);
138 21 $rslt->slice($indices) .= $func->($group);
139 }
140 3 ▶ $rslt->setbadif( $p->isbad ) if $p->badflag;
Branch: 1 branch
BranchTrueFalseTotal
if $p->badflag 0 3 3
141 3 return $rslt;
142 1 };
143  
144 7 ▶ my $newx = ( $tapply->( $df->at('xmin'), $xid, sub { $_[0]->min } ) +
Subroutine __ANON__: called CC=1 SCAR=0.0
145 1 ▶ $tapply->( $df->at('xmax'), $xid, sub { $_[0]->max } ) ) / 2;
Subroutine __ANON__: called CC=1 SCAR=0.0
146  
147 1 my $new_width;
148 1 ▶ if ( defined $n ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) { } 0 1 1
149 0 $new_width = ( $df->at('xmax') - $df->at('xmin') ) / $n;
150 }
151 else {
152  
153 # If n is null, preserve total widths of each group
154 1 my @xid = $xid->flatten;
155 1 ▶ my %n = List::AllUtils::count_by { $_ } @xid;
Subroutine __ANON__: called CC=1 SCAR=0.0
156 $new_width = ( $df->at('xmax') - $df->at('xmin') ) /
157 1 pdl( [ map { $n{$_} } @xid ] );
158 }
159  
160 # Find the total width of each group of elements
161 my $group_sizes = Data::Frame->new(
162 columns => [
163 1 ▶ size => $tapply->( $new_width, $xid, sub { $_[0]->sum } ),
Subroutine __ANON__: called CC=1 SCAR=0.0
164 newx => $newx
165 ]
166 )->uniq;
167  
168 # starting xmin for each group of elements
169 1 my $starts = $group_sizes->at('newx') - $group_sizes->at('size') / 2;
170  
171 # set the elements in place
172 1 for my $i ( 0 .. $starts->length - 1 ) {
173 7 my $indices = which( $xid == $i );
174 7 my $divisions =
175 $starts->slice( pdl($i) )->glue( 0, $new_width->slice($indices) )
176 ->cumusumover();
177 7 $df->slice( $indices, ['xmin'] ) .=
178 $divisions->slice( pdl( [ 0 .. $divisions->length - 2 ] ) );
179 7 $df->slice( $indices, ['xmax'] ) .=
180 $divisions->slice( pdl( [ 1 .. $divisions->length - 1 ] ) );
181 }
182  
183 # x values get moved to between xmin and xmax
184 1 $df->set( 'x', ( $df->at('xmin') + $df->at('xmax') ) / 2 );
185  
186 # If no elements occupy the same position, there is no need to add padding
187 1 ▶ if ( $xid->uniq->length == $xid->length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xid->uniq->length == $xid->length) 1 0 1
188 1 return $df;
189 }
190  
191 # shrink elements to add space between them
192 0 my $half_pad_width = $new_width * ( 1 - $padding ) / 2;
193 0 $df->set( 'xmin', $df->at('x') - $half_pad_width );
194 0 $df->set( 'xmax', $df->at('x') + $half_pad_width );
195  
196 0 return $df;
197 6 }
198  
199 7 ▶fun pos_stack ($df, $width, :$vjust=1, :$fill=false) {
Subroutine pos_stack: called CC=8 SCAR=22.3
Pod undocumented
Branch: 5 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for fun pos_stack (expected 2, got ' . @_ . ')') 0 7 7
@_ > 2 and (@_ - 2) % 2 and Function::Parameters::_croak("Odd number of paired arguments for fun pos_stack") 0 7 7
exists ${__rest}{'vjust'} ? : 7 0 7
exists ${__rest}{'fill'} ? : 7 0 7
keys %{__rest} and Function::Parameters::_croak('In fun pos_stack: no such named parameter: ' . join(", ", sort(keys %{__rest}))) 0 7 7
Condition33%1/3
@_ > 2 and (@_ - 2) % 2
!ll&&!rl&&r
070
MC/DC0%0/2
@_ > 2 and (@_ - 2) % 2
@_ > 2 (@_ - 2) % 2
Truth table33%1/3
A and B
A= @_ > 2B= (@_ - 2) % 2
ABresult
0X0
100
111
200 7 my $n = $df->nrow + 1;
201 7 my $y = ifelse( $df->at('y')->isbad, 0, $df->at('y') );
202 7 my $heights = pdl( [0] )->glue( 0, $y->cumusumover() );
203  
204 7 ▶ if ($fill) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fill) 0 7 7
205 0 $heights = $heights / abs($heights->at(-1));
206 }
207  
208 7 my $heights1 = $heights->slice( pdl( [ 0 .. $heights->length - 2 ] ) );
209 7 my $heights2 = $heights->slice( pdl( [ 1 .. $heights->length - 1 ] ) );
210 7 $df->set( 'ymin', pmin( $heights1, $heights2 ) );
211 7 $df->set( 'ymax', pmax( $heights1, $heights2 ) );
212 7 $df->set( 'y',
213 ( 1 - $vjust ) * $df->at('ymin') + $vjust * $df->at('ymax') );
214 7 return $df;
215 6 }
216  
217 # find groups of overlapping elements that need to be dodged from one another
218 1 ▶fun find_x_overlaps ($df) {
Subroutine find_x_overlaps: called CC=5 SCAR=16.3
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun find_x_overlaps (expected 1, got ' . @_ . ')') 0 1 1
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun find_x_overlaps (expected 1, got ' . @_ . ')') 0 1 1
219 1 my @overlaps = (0);
220 1 my $counter = 0;
221 1 my ($xmin, $xmax) = map { $df->at($_) } qw(xmin xmax);
222 1 for my $i (1 .. $df->nrow - 1) {
223 6 ▶ if ($xmin->at($i) >= $xmax->at($i-1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmin->at($i) >= $xmax->at($i - 1)) 6 0 6
224 6 $counter += 1;
225 }
226 6 push @overlaps, $counter;
227 }
228 1 return pdl(\@overlaps);
229 6 }
230  
231 6 1;
232  
233 __END__
« Chart/GGPlot/Position/Dodge.pm ↑ Summary Chart/GGPlot/Layer.pm »