Chart/GGPlot/Stat/Boxplot.pm

filter: Statementstmt 86.3% 114 / 132 Branchbran 39.6% 23 / 58 Conditioncond 28.5% 4 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 5 totaltotal 67.3% 159 / 236 CC 35 SCAR 30.3
CC
35
Cov
69%
__ANON__
90.0
setup_params
13.0
compute_group
11.1
CRAP 20.7
« Chart/GGPlot/Geom/Rect.pm ↑ Summary Chart/GGPlot/Geom/Path.pm »
1 package Chart::GGPlot::Stat::Boxplot;
2  
3 # ABSTRACT: Statistic method that gets the statistics data for boxplot
4  
5 7 ▶use Chart::GGPlot::Class qw(:pdl);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use MooseX::Singleton;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 our $VERSION = '0.002003'; # VERSION
10  
11 7 ▶use Data::Frame;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use List::AllUtils qw(pairwise);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use PDL::Primitive qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use POSIX qw(floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 7 ▶use Chart::GGPlot::Aes::Functions qw(aes);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Chart::GGPlot::Layer;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use Chart::GGPlot::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
19 is_discrete range_ has_groups resolution remove_missing
20 7 );
21 7 ▶use Chart::GGPlot::Util::Pod qw(layer_func_pod);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 7 with qw(
24 Chart::GGPlot::Stat
25 );
26  
27 3 ▶classmethod required_aes () { ['y'] }
Subroutine required_aes: called CC=3 SCAR=11.2
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for classmethod required_aes (expected 1, got ' . @_ . ')') 0 3 3
@_ > 1 and Function::Parameters::_croak('Too many arguments for classmethod required_aes (expected 1, got ' . @_ . ')') 0 3 3
28 7 classmethod non_missing_aes() { ['weight'] }
29  
30 2 ▶classmethod _parameters () {
Subroutine _parameters: called CC=3 SCAR=11.3
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for classmethod _parameters (expected 1, got ' . @_ . ')') 0 2 2
@_ > 1 and Function::Parameters::_croak('Too many arguments for classmethod _parameters (expected 1, got ' . @_ . ')') 0 2 2
31 [
32 2 qw(
33 na_rm
34 bins binwidth boundary breaks center pad
35 )
36 ]
37 7 }
38  
39 7 my $stat_boxplot_pod = layer_func_pod(<<'EOT');
40  
41 stat_boxplot(:$mapping=undef, :$data=undef,
42 :$geom='boxplot', :$position='dodge2',
43 :$coef=1.5,
44 :$na_rm=false, :$show_legend='auto', :$inherit_aes=true,
45 %rest)
46  
47 Arguments:
48  
49 =over 4
50  
51 %TMPL_COMMON_ARGS%
52  
53 =item * $coef
54  
55 Length of the whiskers as multiple of IQR. Defaults to 1.5.
56  
57 =back
58  
59 EOT
60  
61 0 ▶my $stat_boxplot_code = fun (
Subroutine __ANON__: not called CC=9 SCAR=45.0
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 and @_ % 2 and Function::Parameters::_croak("Odd number of paired arguments for fun (anon)") 0 0 0
Condition0%0/3
@_ > 0 and @_ % 2
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 0 and @_ % 2
@_ > 0 @_ % 2
Truth table0%0/3
A and B
A= @_ > 0B= @_ % 2
ABresult
0X0
100
111
62 :$mapping=undef, :$data=undef,
63 :$geom='boxplot', :$position='dodge2',
64 :$coef=1.5, :$na_rm=false,
65 :$show_legend='auto', :$inherit_aes=true,
66 %rest )
67 0 ▶{
Branch: 6 branches
BranchTrueFalseTotal
exists $rest{'geom'} ? : 0 0 0
exists $rest{'position'} ? : 0 0 0
exists $rest{'coef'} ? : 0 0 0
exists $rest{'na_rm'} ? : 0 0 0
exists $rest{'show_legend'} ? : 0 0 0
exists $rest{'inherit_aes'} ? : 0 0 0
68 0 return Chart::GGPlot::Layer->new(
69 data => $data,
70 mapping => $mapping,
71 stat => 'boxplot',
72 geom => $geom,
73 position => $position,
74 show_legend => $show_legend,
75 inherit_aes => $inherit_aes,
76 params => {
77 na_rm => $na_rm,
78 coef => $coef,
79 %rest,
80 }
81 );
82 7 };
83  
84 7 ▶classmethod ggplot_functions () {
Subroutine ggplot_functions: called CC=3 SCAR=11.3
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for classmethod ggplot_functions (expected 1, got ' . @_ . ')') 0 7 7
@_ > 1 and Function::Parameters::_croak('Too many arguments for classmethod ggplot_functions (expected 1, got ' . @_ . ')') 0 7 7
85 return [
86 {
87 7 name => 'stat_boxplot',
88 code => $stat_boxplot_code,
89 pod => $stat_boxplot_pod,
90 }
91 ];
92 7 }
93  
94 1 ▶method setup_data($data, $params) {
Subroutine setup_data: called CC=4 SCAR=14.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 3 and Function::Parameters::_croak('Too few arguments for method setup_data (expected 3, got ' . @_ . ')') 0 1 1
@_ > 3 and Function::Parameters::_croak('Too many arguments for method setup_data (expected 3, got ' . @_ . ')') 0 1 1
95 1 ▶ unless ($data->exists('x')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->exists("x")) 0 1 1
96 0 $data->set('x', pdl(0));
97 }
98 1 return remove_missing(
99 $data,
100 na_rm => false,
101 vars => ['x'],
102 name => 'stat_boxplot'
103 );
104 7 }
105  
106 1 ▶method setup_params ($data, $params) {
Subroutine setup_params: called CC=9 SCAR=25.6
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 3 and Function::Parameters::_croak('Too few arguments for method setup_params (expected 3, got ' . @_ . ')') 0 1 1
@_ > 3 and Function::Parameters::_croak('Too many arguments for method setup_params (expected 3, got ' . @_ . ')') 0 1 1
107 1 ▶ unless ( $params->exists('width') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->exists("width")) 1 0 1
108 1 ▶ $params->set( 'width',
Branch: 1 branch
BranchTrueFalseTotal
$data->exists('x') ? : 1 0 1
109 resolution( $data->exists('x') ? $data->at('x') : 0 ) * 0.75 );
110 }
111  
112 1 ▶ if ( $data->exists('x') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->exists("x")) 1 0 1
113 1 my $x = $data->at('x');
114 1 ▶ if ( !is_discrete($x) and !has_groups($data) and $x->uniq->length > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (not is_discrete($x) || has_groups($data) and $x->uniq->length > 1) 0 1 1
Condition33%2/6
not is_discrete($x) || has_groups($data) and $x->uniq->length > 1
!ll&&!rl&&r
100
is_discrete($x) || has_groups($data)
l!l&&r!l&&!r
010
MC/DC0%0/3
not is_discrete($x) || has_groups($data) and $x->uniq->length > 1
is_discrete($x) has_groups($data) $x->uniq->length > 1
Truth table25%1/4
not(A || B) and C
A= is_discrete($x)B= has_groups($data)C= $x->uniq->length > 1
ABCresult
1XX0
01X0
0000
0011
115 {
116 0 warn("Continuous x aesthetic -- did you forget aes(group=>...)?");
117 }
118 }
119 1 return $params;
120 7 }
121  
122 7 ▶method compute_group ($data, $scales, $params) {
Subroutine compute_group: called CC=10 SCAR=24.1
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 4 and Function::Parameters::_croak('Too few arguments for method compute_group (expected 4, got ' . @_ . ')') 0 7 7
@_ > 4 and Function::Parameters::_croak('Too many arguments for method compute_group (expected 4, got ' . @_ . ')') 0 7 7
123 7 my $width = $params->at('width');
124 7 ▶ my $na_rm = $params->at('na_rm') // false;
Condition33%1/3
$params->at("na_rm") // &false()
l!l&&r!l&&!r
700
MC/DC0%0/2
$params->at("na_rm") // &false()
$params->at("na_rm") &false()
Truth table33%1/3
A // B
A= $params->at("na_rm")B= &false()
ABresult
1X1
011
000
125 7 ▶ my $coef = $params->at('coef') // 1.5;
Condition50%1/2
$params->at("coef") // "1.5"
l!l
07
MC/DC0%0/1
$params->at("coef") // "1.5"
$params->at("coef")
Truth table50%1/2
A
A= $params->at("coef")
Aresult
11
00
126  
127 7 my @qs = ( 0, 0.25, 0.5, 0.75, 1 );
128  
129 7 my @stats;
130 7 ▶ if ( $data->exists('weight') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->exists('weight')) { } 0 7 7
131 0 ...;
132 }
133 else {
134 7 @stats = map { $data->at('y')->pct($_) } @qs;
135 }
136 7 my $iqr = $stats[3] - $stats[1];
137  
138 7 my $y = $data->at('y');
139 7 my $outliers =
140 ( ( $y < $stats[1] - $coef * $iqr ) | ( $y > $stats[3] + $coef * $iqr ) );
141 7 ▶ if ( $outliers->any ) { # ajust min and max for outliers
Branch: 1 branch
BranchTrueFalseTotal
if ($outliers->any) 5 2 7
142 5 ( $stats[0], $stats[4] ) = $y->where( !$outliers )->minmax;
143 }
144  
145 7 ▶ if ( $data->at('x')->uniq->length > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->at("x")->uniq->length > 1) 0 7 7
146 0 $width = range_( $data->at('x') )->diff->at(0) * 0.9;
147 }
148  
149 7 my @names = qw(ymin lower middle upper ymax);
150 my $df = Data::Frame->new(
151 7 ▶ columns => [ pairwise { $a => pdl( [$b] ) } @names, @stats ] );
Subroutine __ANON__: called CC=1 SCAR=0.0
152 7 $df->set( 'outliers', PDL::SV->new( [ $y->where($outliers) ] ) );
153  
154 7 my $n;
155 7 ▶ if ( $data->exists('weight') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->exists('weight')) { } 0 7 7
156  
157 # Sum up weights for non-NA positions of y and weight
158 0 my $weight = $data->at('weight');
159 0 $n = int( $weight->where( $y->isgood & $weight->isgood )->sum );
160 }
161 else {
162 7 $n = int( $y->isgood->sum );
163 }
164  
165 7 my $middle = $df->at('middle');
166 7 my $sqrt_n = sqrt($n);
167 7 $df->set( 'notchupper', $middle + 1.58 * $iqr / $sqrt_n );
168 7 $df->set( 'notchlower', $middle - 1.58 * $iqr / $sqrt_n );
169  
170 7 my $x = $data->at('x');
171 7 ▶ $df->set( 'x',
Branch: 1 branch
BranchTrueFalseTotal
$x->$Chart::GGPlot::Stat::Boxplot::_DOES('PDL::Factor') ? : 0 7 7
172 pdl( [ $x->$_DOES('PDL::Factor') ? $x->at(0) : range_($x)->average ] )
173 );
174 7 $df->set( 'width', pdl( [$width] ) );
175 7 $df->set( 'relvarwidth', pdl( [$sqrt_n] ) );
176  
177 7 return $df;
178 7 }
179  
180 7 __PACKAGE__->meta->make_immutable;
181  
182 7 1;
183  
184 __END__
« Chart/GGPlot/Geom/Rect.pm ↑ Summary Chart/GGPlot/Geom/Path.pm »