Chart/GGPlot/Backend/Plotly/Util.pm

filter: Statementstmt 75.7% 125 / 165 Branchbran 46.0% 35 / 76 Conditioncond 66.6% 6 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 83.3% 20 / 24 Podpod 20.0% 1 / 5 totaltotal 65.9% 188 / 285 CC 43 SCAR 33.6
CC
43
Cov
66%
pdl_to_plotly
132.0
group_to_NA
16.5
ribbon
12.0
CRAP 28.8
« Chart/GGPlot/Scale/Functions.pm ↑ Summary Chart/GGPlot/Geom/Tile.pm »
1 package Chart::GGPlot::Backend::Plotly::Util;
2  
3 # ABSTRACT: Utilities used by Chart::GGPlot::Backend::Plotly
4  
5 2 ▶use Chart::GGPlot::Setup qw(:base :pdl);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.002003'; # VERSION
8  
9 2 ▶use Data::Frame;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Data::Munge qw(elem);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use List::AllUtils qw(all min max pairmap pairwise reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use PDL::Primitive qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Types::PDL qw(Piddle);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Types::Standard qw(Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use parent qw(Exporter::Tiny);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use Chart::GGPlot::Util::Scales qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
19 csshex_to_rgb255 colorname_to_csshex
20 2 );
21 2 ▶use Chart::GGPlot::Util qw(mm_to_px);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use constant br => '<br />';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 our @EXPORT_OK = qw(
26 to_px
27 br
28 to_rgb
29 group_to_NA
30 pdl_to_plotly
31 ribbon
32 );
33 2 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
34  
35 2 my $dpi = 96;
36  
37 0 ▶sub to_px { mm_to_px($_[0], $dpi) }
Subroutine to_px: not called CC=1 SCAR=6.9
Pod undocumented
38  
39 4 ▶sub _rgb {
Subroutine _rgb: called CC=3 SCAR=11.7
40 4 my ($c) = @_;
41 4 ▶ return 'transparent' if $c eq 'BAD';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "BAD" 0 4 4
42 4 ▶ return $c =~ /^#/ ? $c : colorname_to_csshex($c);
Branch: 1 branch
BranchTrueFalseTotal
$c =~ /^#/u ? : 0 4 4
43 }
44  
45 4 ▶sub _rgba {
Subroutine _rgba: called CC=5 SCAR=17.0
46 4 my ($c, $a) = @_;
47  
48 4 ▶ return 'transparent' if $c eq 'BAD';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "BAD" 0 4 4
49 4 ▶ $c = $c =~ /^#/ ? $c : colorname_to_csshex($c);
Branch: 1 branch
BranchTrueFalseTotal
$c =~ /^#/u ? : 0 4 4
50 4 ▶ return $c if $a == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $a == 1 1 3 4
51  
52 3 ▶ if ($c =~ /^#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /^#/u) 3 0 3
53 3 return sprintf(
54 "rgba(%s,%s,%s,%s)",
55 csshex_to_rgb255($c),
56 0+sprintf("%.2f", $a) # 0+ for removing trailing zeros
57 );
58 }
59 0 return $c;
60 }
61  
62 # plotly does not understands some non-rgb colors like "grey35"
63 4 ▶fun to_rgb ($color, $alpha=pdl(1)) {
Subroutine to_rgb: called CC=9 SCAR=22.7
Pod undocumented
Branch: 3 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun to_rgb (expected 1, got ' . @_ . ')') 0 4 4
@_ > 2 and Function::Parameters::_croak('Too many arguments for fun to_rgb (expected 2, got ' . @_ . ')') 0 4 4
@_ < 2 and $alpha = pdl(1) 1 3 4
64 4 state $check = Type::Params::compile((Piddle | Str), Piddle);
65 4 ($color, $alpha) = $check->($color, $alpha);
66  
67 4 ▶ if ( !ref($color) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $color) { } 0 4 4
68 0 return _rgba($color, $alpha->at(0));
69 }
70 else {
71 4 ▶ if ($alpha->length != $color->length and $alpha->length != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alpha->length != $color->length and $alpha->length != 1) 0 4 4
Condition66%2/3
$alpha->length != $color->length and $alpha->length != 1
!ll&&!rl&&r
220
MC/DC0%0/2
$alpha->length != $color->length and $alpha->length != 1
$alpha->length != $color->length $alpha->length != 1
Truth table66%2/3
A and B
A= $alpha->length != $color->lengthB= $alpha->length != 1
ABresult
0X0
100
111
72 0 die "alpha must be of length 1 or the same length as x";
73 }
74 4 $alpha = $alpha->setbadtoval(1);
75 4 $alpha->where($alpha > 1) .= 1;
76 4 $alpha->where($alpha < 0) .= 0;
77
78 4 my @color = $color->flatten;
79 4 my @rgba;
80 4 ▶ if ($alpha->uniq->length == 1 and $alpha->at(0) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alpha->uniq->length == 1 and $alpha->at(0) == 1) { } 2 2 4
Condition66%2/3
$alpha->uniq->length == 1 and $alpha->at(0) == 1
!ll&&!rl&&r
202
MC/DC50%1/2
$alpha->uniq->length == 1 and $alpha->at(0) == 1
$alpha->uniq->length == 1 $alpha->at(0) == 1
Truth table66%2/3
A and B
A= $alpha->uniq->length == 1B= $alpha->at(0) == 1
ABresult
0X0
100
111
81 2 @rgba = map { _rgb($_) } @color;
82 } else {
83 2 my @alpha = $alpha->flatten;
84 2 ▶ @rgba = pairwise { _rgba($a, $b) } @color, @alpha;
Subroutine __ANON__: called CC=1 SCAR=0.0
85 }
86  
87 4 return PDL::SV->new(\@rgba);
88 }
89 2 }
90  
91  
92 7 ▶fun group_to_NA ($df, :$group_vars=['group'],
Subroutine group_to_NA: called CC=16 SCAR=28.0
Pod documented
93 7 ▶ :$nested=[], :$ordered=[], :$retrace_first=false) {
Branch: 7 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun group_to_NA (expected 1, got ' . @_ . ')') 0 7 7
@_ > 1 and (@_ - 1) % 2 and Function::Parameters::_croak("Odd number of paired arguments for fun group_to_NA") 0 7 7
exists ${__rest}{'group_vars'} ? : 0 7 7
exists ${__rest}{'nested'} ? : 2 5 7
exists ${__rest}{'ordered'} ? : 2 5 7
exists ${__rest}{'retrace_first'} ? : 3 4 7
keys %{__rest} and Function::Parameters::_croak('In fun group_to_NA: no such named parameter: ' . join(", ", sort(keys %{__rest}))) 0 7 7
Condition66%2/3
@_ > 1 and (@_ - 1) % 2
!ll&&!rl&&r
340
MC/DC0%0/2
@_ > 1 and (@_ - 1) % 2
@_ > 1 (@_ - 1) % 2
Truth table66%2/3
A and B
A= @_ > 1B= (@_ - 1) % 2
ABresult
0X0
100
111
94 7 ▶ return $df if ( $df->nrow == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $df->nrow == 0 0 7 7
95  
96 7 my $df_names = $df->names;
97 7 $group_vars = $group_vars->intersect($df_names);
98 7 $nested = $nested->intersect($df_names);
99 7 $ordered = $ordered->intersect($df_names);
100  
101 # if group does not exist, just order the rows and exit
102 7 ▶ unless ( $group_vars->length ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($group_vars->length) 0 7 7
103 0 my @key_vars = ( @$nested, @$ordered );
104 0 ▶ return ( @key_vars ? $df->sort( \@key_vars ) : $df );
Branch: 1 branch
BranchTrueFalseTotal
@key_vars ? : 0 0 0
105 }
106  
107 7 ▶ if ( $df->nrow == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($df->nrow == 1) 2 5 7
108 2 ▶ return ( $retrace_first ? $df->append( $df->select_rows(0) ) : $df );
Branch: 1 branch
BranchTrueFalseTotal
$retrace_first ? : 1 1 2
109 }
110  
111 # ordered the rows
112 5 $df = $df->sort( [ @$nested, @$group_vars, @$ordered ] );
113  
114 #inserting NAs to ensure each "group"
115 5 my $changes_group = ( $df->select_columns($group_vars)->id->diff != 0 );
116 5 my $to_insert = $changes_group;
117 5 ▶ if ( $nested->length > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nested->length > 0) 2 3 5
118 2 my $changes_nested = ( $df->select_columns($nested)->id->diff == 0 );
119 2 $to_insert = ( $to_insert & $changes_nested );
120 }
121 5 my $idx_to_insert = which($to_insert); # insert after the indices
122  
123 # prepare row indices, each item has start row, stop row,
124 # and places to retrace.
125  
126 10 ▶ state $split_range = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
127 10 my ( $upper, $after ) = @_;
128 return (
129 19 ▶ pairmap { [ $a .. $b ] }
Subroutine __ANON__: called CC=1 SCAR=0.0
130 (
131 0,
132 10 ( map { ( $_, $_ + 1 ) } grep { $_ < $upper } $after->flatten ),
133 $upper
134 )
135 );
136 5 };
137  
138 5 my @group_rows = $split_range->( $df->nrow - 1, $idx_to_insert );
139 5 my @splitted = map { $df->select_rows($_) } @group_rows;
140 5 ▶ if ($retrace_first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retrace_first) 2 3 5
141 2 my $to_retrace = $changes_group->glue( 0, pdl( [1] ) );
142 my @retrace_at = map {
143 2 my $rindices = pdl($_);
144 5 which( $to_retrace->slice($rindices) )->unpdl;
145 } @group_rows;
146  
147 @splitted = map {
148 2 my $d = $splitted[$_];
149 5 my @retrace_rows = $split_range->( $d->nrow - 1, $retrace_at[$_] );
150 my @splitted_for_retrace = map {
151 5 my $x = $d->select_rows($_);
152 7 $x->append( $x->select_rows( [0] ) )
153 } @retrace_rows;
154 5 ▶ reduce { $a->append($b); } ( shift @splitted_for_retrace ),
Subroutine __ANON__: called CC=1 SCAR=0.0
155 @splitted_for_retrace;
156 } ( 0 .. $#splitted );
157 }
158  
159 5 my @key_vars = ( @$nested, @$group_vars );
160 5 my @vars_to_na = grep { !elem( $_, \@key_vars ) } $df->names->flatten;
161 return (
162 7 ▶ reduce {
Subroutine __ANON__: called CC=2 SCAR=6.9
163  
164 # copy last row and make it a NA row
165 7 my $na = $a->select_rows( [ $a->nrow - 1 ] )->copy;
166 7 for my $var (@vars_to_na) {
167 7 $na->at($var)->setbadat(0);
168 }
169 7 $a->append($na)->append($b);
170 }
171 5 ( shift @splitted ),
172 @splitted
173 );
174 2 }
175  
176 # prepare from piddle to aref or value, to be send to Chart::Plotly
177 0 ▶fun pdl_to_plotly ($p, $allow_collapse=false) {
Subroutine pdl_to_plotly: not called CC=11 SCAR=48.8
Pod undocumented
Branch: 3 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun pdl_to_plotly (expected 1, got ' . @_ . ')') 0 0 0
@_ > 2 and Function::Parameters::_croak('Too many arguments for fun pdl_to_plotly (expected 2, got ' . @_ . ')') 0 0 0
@_ < 2 and $allow_collapse = &false() 0 0 0
178 0 ▶ return [] if $p->length == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p->length == 0 0 0 0
179  
180 0 ▶ if ( $p->badflag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->badflag) 0 0 0
181 0 return $p->unpdl;
182 }
183  
184 0 ▶ if ($allow_collapse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_collapse) 0 0 0
185 0 ▶ return $p->at(0) if $p->length == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $p->length == 1 0 0 0
186  
187 0 ▶ if ( $p->$_DOES('PDL::SV') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->$Chart::GGPlot::Backend::Plotly::Util::_DOES('PDL::SV')) { } 0 0 0
188 0 my @lst = $p->flatten;
189 0 my $elem = shift @lst;
190 0 ▶ if ( all { $_ eq $elem } @lst ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&all(sub { $_ eq $elem; } , @lst)) 0 0 0
191 0 return $elem;
192 }
193 }
194 else {
195 0 my $elem = $p->at(0);
196 0 ▶ if ( ( $p == $elem )->all ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($p == $elem)->all) 0 0 0
197 0 return $elem;
198 }
199 }
200 }
201  
202 0 return $p->unpdl;
203 2 }
204  
205 # Transform geom_smooth prediction confidence intervals into format plotly
206 # likes
207 0 ▶fun ribbon ($data) {
Subroutine ribbon: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun ribbon (expected 1, got ' . @_ . ')') 0 0 0
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun ribbon (expected 1, got ' . @_ . ')') 0 0 0
208 0 my $n = $data->nrow;
209 0 my $tmp = $data->sort( ['x'] );
210 0 my $tmp2 = $data->sort( ['x'], false );
211 0 my $not_used = $data->names->setdiff( [qw(x ymin ymax y)] );
212  
213 # top-half of ribbon
214 0 my @others = map { $_ => $tmp->at($_) } @$not_used;
215 0 my $data1 = Data::Frame->new(
216 columns => [
217 x => $tmp->at('x'),
218 y => $tmp->at('ymax'),
219 @others,
220 ]
221 );
222  
223 # bottom-half of ribbon
224 0 my @others2 = map { $_ => $tmp2->at($_) } @$not_used;
225 0 my $data2 = Data::Frame->new(
226 columns => [
227 x => $tmp2->at('x'),
228 y => $tmp2->at('ymin'),
229 @others2,
230 ]
231 );
232  
233 0 return $data1->rbind($data2);
234 2 }
235  
236 2 1;
237  
238 __END__
« Chart/GGPlot/Scale/Functions.pm ↑ Summary Chart/GGPlot/Geom/Tile.pm »