| 1 | package Chart::GGPlot::Stat::Count; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Statistic method that counts number of data in bin | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use Chart::GGPlot::Class qw(:pdl); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use namespace::autoclean -except => 'stat'; | ||||||||||||||||||||||||||
|
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 | |||||||||||||||||||||||||||||
| 13 | 7 | ▶ | use Chart::GGPlot::Aes::Functions qw(aes); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 7 | ▶ | use Chart::GGPlot::Layer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 7 | ▶ | use Chart::GGPlot::Util qw(resolution stat); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 7 | ▶ | use Chart::GGPlot::Util::Pod qw(layer_func_pod); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 7 | with qw( | |||||||||||||||||||||||||||
| 19 | Chart::GGPlot::Stat | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | has '+default_aes' => ( | ||||||||||||||||||||||||||||
| 23 | 2 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | aes( | |||||||||||||||||||||||||||
| 25 | y => q{stat($count)}, | ||||||||||||||||||||||||||||
| 26 | weight => 1, | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | 7 | ); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 4 | ▶ | classmethod required_aes() { ['x'] } | ||||||||||||||||||||||||||
|
Subroutine
required_aes: called CC=3 SCAR=11.2
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 7 | my $stat_count_pod = layer_func_pod(<<'EOT'); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | stat_count(:$mapping=undef, :$data=undef, | ||||||||||||||||||||||||||||
| 36 | :$geom='bar', :$position='stack', | ||||||||||||||||||||||||||||
| 37 | :$width=undef, | ||||||||||||||||||||||||||||
| 38 | :$na_rm=false, :$show_legend=undef, :$inherit_aes=true, | ||||||||||||||||||||||||||||
| 39 | %rest) | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | Arguments: | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =over 4 | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | %TMPL_COMMON_ARGS% | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =item * $width | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | Bar width. By default, set to 90% of the resolution of the data. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =back | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | EOT | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | my $stat_count_code = fun ( | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
Branch: 1 branch
Condition0%0/3
@_ > 0 and @_ % 2
MC/DC0%0/2
@_ > 0 and @_ % 2
@_ > 0
@_ % 2
Truth table0%0/3
A and B
A= @_ > 0B= @_ % 2
| |||||||||||||||||||||||||||||
| 56 | :$mapping = undef, :$data = undef, | ||||||||||||||||||||||||||||
| 57 | :$geom = 'bar', :$position = 'stack', | ||||||||||||||||||||||||||||
| 58 | :$width = undef, :$na_rm = false, | ||||||||||||||||||||||||||||
| 59 | :$show_legend = undef, :$inherit_aes = true, | ||||||||||||||||||||||||||||
| 60 | %rest ) | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 62 | 0 | my $params = { | |||||||||||||||||||||||||||
| 63 | na_rm => $na_rm, | ||||||||||||||||||||||||||||
| 64 | width => $width, | ||||||||||||||||||||||||||||
| 65 | %rest | ||||||||||||||||||||||||||||
| 66 | }; | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ( $data->exists('y') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | die "stat_count() must not be used with a y aesthetic."; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | return Chart::GGPlot::Layer->new( | |||||||||||||||||||||||||||
| 72 | data => $data, | ||||||||||||||||||||||||||||
| 73 | mapping => $mapping, | ||||||||||||||||||||||||||||
| 74 | stat => 'count', | ||||||||||||||||||||||||||||
| 75 | geom => $geom, | ||||||||||||||||||||||||||||
| 76 | position => $position, | ||||||||||||||||||||||||||||
| 77 | show_legend => $show_legend, | ||||||||||||||||||||||||||||
| 78 | inherit_aes => $inherit_aes, | ||||||||||||||||||||||||||||
| 79 | params => $params, | ||||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||
| 81 | 7 | }; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 7 | ▶ | classmethod ggplot_functions() { | ||||||||||||||||||||||||||
|
Subroutine
ggplot_functions: called CC=3 SCAR=11.3
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 84 | return [ | ||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||
| 86 | 7 | name => 'stat_count', | |||||||||||||||||||||||||||
| 87 | code => $stat_count_code, | ||||||||||||||||||||||||||||
| 88 | pod => $stat_count_pod, | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | ]; | ||||||||||||||||||||||||||||
| 91 | 7 | } | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | ▶ | method setup_params ($data, $params) { | ||||||||||||||||||||||||||
|
Subroutine
setup_params: called CC=4 SCAR=14.8
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 94 | 1 | ▶ | if ( $data->exists('y') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | die "stat_count() must not be used with a y aesthetic."; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 1 | return $params; | |||||||||||||||||||||||||||
| 98 | 7 | } | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 7 | ▶ | method compute_group ($data, $scales, $params) { | ||||||||||||||||||||||||||
|
Subroutine
compute_group: called CC=5 SCAR=16.6
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 101 | 7 | my $width = $params->at('width'); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 7 | my $x = $data->at('x'); | |||||||||||||||||||||||||||
| 104 | 7 | ▶ | my $weight = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | $data->exists('weight') | ||||||||||||||||||||||||||||
| 106 | ? $data->at('weight') | ||||||||||||||||||||||||||||
| 107 | : PDL::Core::ones($x->length); | ||||||||||||||||||||||||||||
| 108 | 7 | ▶ | $width = $width // pdl(resolution($x) * 0.9); | ||||||||||||||||||||||||||
|
Condition33%1/3
$width // pdl(resolution($x) * "0.9")
MC/DC0%0/2
$width // pdl(resolution($x) * "0.9")
$width
pdl(resolution($x) * "0.9")
Truth table33%1/3
A // B
A= $widthB= pdl(resolution($x) * "0.9")
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # TODO: R does tapply() here. It's somewhat like groupby and then | ||||||||||||||||||||||||||||
| 111 | # summarise on # each group. We can change this once we have | ||||||||||||||||||||||||||||
| 112 | # groupby + summarize in our data frame implementations. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 7 | my $uniq = $x->uniq->qsort; | |||||||||||||||||||||||||||
| 115 | my $count = pdl( | ||||||||||||||||||||||||||||
| 116 | 7 | ▶ | $uniq->unpdl->map( sub { int( $weight->where( $x == $_ )->sum ) } ) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 117 | 7 | ); | |||||||||||||||||||||||||||
| 118 | 7 | $count->setbadtoval(0); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 7 | return Data::Frame->new( | |||||||||||||||||||||||||||
| 121 | columns => [ | ||||||||||||||||||||||||||||
| 122 | count => $count, | ||||||||||||||||||||||||||||
| 123 | prop => $count / $count->abs->sum, | ||||||||||||||||||||||||||||
| 124 | x => $uniq, | ||||||||||||||||||||||||||||
| 125 | width => $width, | ||||||||||||||||||||||||||||
| 126 | ] | ||||||||||||||||||||||||||||
| 127 | ); | ||||||||||||||||||||||||||||
| 128 | 7 | } | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 7 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 7 | 1; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | __END__ | ||||||||||||||||||||||||||||