| 1 | package Text::Histogram; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | our @EXPORT_OK = qw(histogram); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | my @scales = (1, 2, 5, 10, 25, 50, 100, 250, 500); | |||||||||||||||||||||||||||
| 13 | 2 | push @scales, map { ( 1 * $_, 2.5 * $_, 5 * $_) } ( | |||||||||||||||||||||||||||
| 14 | 1000, 10_000, 100_000 | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | my @binsizes = (1, 2, 5, 10, 25, 50, 100, 250, 500); | |||||||||||||||||||||||||||
| 18 | 2 | push @binsizes, map { ( 1 * $_, 2.5 * $_, 5 * $_ ) } ( | |||||||||||||||||||||||||||
| 19 | 1000, 10_000, 100_000 | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub histogram { | ||||||||||||||||||||||||||
|
Subroutine
histogram: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 0 | my ($data, $opts) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | unless (ref $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | $data = [@_]; | |||||||||||||||||||||||||||
| 27 | 0 | $opts = {}; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | 0 | my $pts = scalar @$data; | |||||||||||||||||||||||||||
| 30 | 0 | ▶ | $opts->{bins} ||= 8; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{"bins"} ||= 8
MC/DC0%0/1
$opts->{"bins"} ||= 8
$opts->{"bins"}
Truth table0%0/2
A
A= $opts->{"bins"}
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | $opts->{bins} = $pts if $pts < $opts->{bins}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | $opts->{histogram_size} ||= 50; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{"histogram_size"} ||= 50
MC/DC0%0/1
$opts->{"histogram_size"} ||= 50
$opts->{"histogram_size"}
Truth table0%0/2
A
A= $opts->{"histogram_size"}
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | my $vcnt = scalar @$data; | |||||||||||||||||||||||||||
| 35 | 0 | my @data = sort { $a <=> $b } @$data; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | my ($min, $max, $rmin, $rmax, $pmin, $pmax) | |||||||||||||||||||||||||||
| 38 | = _check_outliers($vcnt, $opts, @data); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | my ($scale, $binsize, %bins) | |||||||||||||||||||||||||||
| 41 | = _get_frequency($min,$max,$rmin,$rmax, $opts, \@data); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | my $hist = ""; | |||||||||||||||||||||||||||
| 44 | 0 | my $hsize = $opts->{histogram_size}; | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | if ($min != $rmin) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $freq = _ceil(($bins{'min'}||0)/$scale); | ||||||||||||||||||||||||||
|
Condition0%0/2
$bins{'min'} || 0
MC/DC0%0/1
$bins{'min'} || 0
$bins{'min'}
Truth table0%0/2
A
A= $bins{'min'}
| |||||||||||||||||||||||||||||
| 47 | $hist.= sprintf "%8d %-${hsize}s - %6d\n", | ||||||||||||||||||||||||||||
| 48 | $min, | ||||||||||||||||||||||||||||
| 49 | "#" x $freq, | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | ($bins{'min'}||0); | ||||||||||||||||||||||||||
|
Condition0%0/2
$bins{'min'} || 0
MC/DC0%0/1
$bins{'min'} || 0
$bins{'min'}
Truth table0%0/2
A
A= $bins{'min'}
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | for (my $i = _ceil(($rmin+1)/$binsize)-1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | $i <= _ceil(($rmax+1)/$binsize)-1; $i++) { | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | my $freq = _ceil(($bins{$i}||0)/$scale); | ||||||||||||||||||||||||||
|
Condition0%0/2
$bins{$i} || 0
MC/DC0%0/1
$bins{$i} || 0
$bins{$i}
Truth table0%0/2
A
A= $bins{$i}
| |||||||||||||||||||||||||||||
| 55 | 0 | my $val = $i*$binsize; | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | $val = $rmin if $val < $rmin; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | $freq = "#" x $freq; | |||||||||||||||||||||||||||
| 58 | $hist .= sprintf "%8d %-${hsize}s - %6d\n", | ||||||||||||||||||||||||||||
| 59 | $val, | ||||||||||||||||||||||||||||
| 60 | $freq, | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | ($bins{$i}||0) | ||||||||||||||||||||||||||
|
Condition0%0/2
$bins{$i} || 0
MC/DC0%0/1
$bins{$i} || 0
$bins{$i}
Truth table0%0/2
A
A= $bins{$i}
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ($max != $rmax) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my $freq = _ceil(($bins{'max'}||0)/$scale); | ||||||||||||||||||||||||||
|
Condition0%0/2
$bins{'max'} || 0
MC/DC0%0/1
$bins{'max'} || 0
$bins{'max'}
Truth table0%0/2
A
A= $bins{'max'}
| |||||||||||||||||||||||||||||
| 65 | $hist.= sprintf "%8d %-${hsize}s - %6d\n", | ||||||||||||||||||||||||||||
| 66 | $pmax, | ||||||||||||||||||||||||||||
| 67 | "#" x $freq, | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | ($bins{'max'}||0); | ||||||||||||||||||||||||||
|
Condition0%0/2
$bins{'max'} || 0
MC/DC0%0/1
$bins{'max'} || 0
$bins{'max'}
Truth table0%0/2
A
A= $bins{'max'}
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 0 | return $hist; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub _get_frequency { | ||||||||||||||||||||||||||
|
Subroutine
_get_frequency: not called CC=15 SCAR=54.8
| |||||||||||||||||||||||||||||
| 74 | 0 | my ($min, $max, $rmin, $rmax, $opts, $data) = @_; | |||||||||||||||||||||||||||
| 75 | 0 | my %bins = (); | |||||||||||||||||||||||||||
| 76 | 0 | my $bins = $opts->{bins}; | |||||||||||||||||||||||||||
| 77 | 0 | ▶ | $bins-- if $rmin != $min; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $bins-- if $rmax != $max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | my $hsize = $opts->{histogram_size}; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | my $binsize = _best_scale( ($rmax - $rmin) / $bins, @binsizes ); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | for my $v (@$data) { | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ( $v < $rmin ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 85 | 0 | $bins{'min'}++ ; | |||||||||||||||||||||||||||
| 86 | } elsif ( $v > $rmax ) { | ||||||||||||||||||||||||||||
| 87 | 0 | $bins{'max'}++ ; | |||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||
| 89 | 0 | $bins{_ceil(($v+1)/$binsize) - 1}++ ; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | my ($minf, $maxf, $scale, $maxval) = (undef, undef, 1, 0); | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | while ( my ($key, $value) = each (%bins) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | next if $key eq 'min' or $key eq 'max'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$key eq "min" or $key eq "max"
MC/DC0%0/2
$key eq "min" or $key eq "max"
$key eq "min"
$key eq "max"
Truth table0%0/3
A or B
A= $key eq "min"B= $key eq "max"
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | $minf = $key if !defined($minf) || $key < $minf; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $minf or $key < $minf
MC/DC0%0/2
not defined $minf or $key < $minf
not defined $minf
$key < $minf
Truth table0%0/3
not(A) or B
A= not defined $minfB= $key < $minf
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | $maxf = $key if !defined($maxf) || $key > $maxf; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $maxf or $key > $maxf
MC/DC0%0/2
not defined $maxf or $key > $maxf
not defined $maxf
$key > $maxf
Truth table0%0/3
not(A) or B
A= not defined $maxfB= $key > $maxf
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | $maxval = $value if $value > $maxval; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | $scale = _best_scale($maxval/$hsize, @scales) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | if $maxval>$hsize; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | return $scale, $binsize, %bins; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub _ceil { | ||||||||||||||||||||||||||
|
Subroutine
_ceil: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 109 | 0 | my ($number) = shift; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ($number != int($number)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | $number = int($number) + 1; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 0 | return $number; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub _check_outliers { | ||||||||||||||||||||||||||
|
Subroutine
_check_outliers: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 117 | 0 | my ($vcnt, $opts, @data) = @_; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | my ($min,$max) = my ($tmin, $tmax) = @data[0,-1]; | |||||||||||||||||||||||||||
| 120 | 0 | my $bins = $opts->{bins}; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | my $cnt = int($vcnt/50); #max 2+2% of outlier points | |||||||||||||||||||||||||||
| 123 | 0 | my $val = $data[0]; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | my $c = 0; | |||||||||||||||||||||||||||
| 126 | 0 | ▶ | my $bn = $bins > 2 ? $bins - 2 : 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | my $bs = ($tmax - $tmin) / $bn; | |||||||||||||||||||||||||||
| 128 | 0 | my $binsize = _best_scale($bs, @binsizes); | |||||||||||||||||||||||||||
| 129 | ; | ||||||||||||||||||||||||||||
| 130 | 0 | my ($rmin, $rmax) = (0, 0); | |||||||||||||||||||||||||||
| 131 | 0 | my ($pmin, $pmax) = (0, 0); | |||||||||||||||||||||||||||
| 132 | 0 | ▶ | while ( ($tmin != $rmin) or ($tmax != $rmax) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$tmin != $rmin or $tmax != $rmax
MC/DC0%0/2
$tmin != $rmin or $tmax != $rmax
$tmin != $rmin
$tmax != $rmax
Truth table0%0/3
A or B
A= $tmin != $rminB= $tmax != $rmax
| |||||||||||||||||||||||||||||
| 133 | 0 | $rmin = $tmin; | |||||||||||||||||||||||||||
| 134 | 0 | $rmax = $tmax; | |||||||||||||||||||||||||||
| 135 | 0 | $val = $data[0]; | |||||||||||||||||||||||||||
| 136 | 0 | for my $i (1..$cnt) { | |||||||||||||||||||||||||||
| 137 | # point with more than half the size of a bin are grouped | ||||||||||||||||||||||||||||
| 138 | # in a big bin, in the beginning. | ||||||||||||||||||||||||||||
| 139 | 0 | $c = $data[$i] - $val; | |||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ( $c > $binsize ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | $tmin = $data[$i]; | |||||||||||||||||||||||||||
| 142 | 0 | $val = $data[$i]; | |||||||||||||||||||||||||||
| 143 | 0 | $binsize = ($tmax - $tmin) / $bn; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | last if $i >= $cnt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | $val = $data[-1]; | |||||||||||||||||||||||||||
| 149 | 0 | for my $i (1..$cnt) { | |||||||||||||||||||||||||||
| 150 | 0 | my $v1 = $data[-1-$i]; | |||||||||||||||||||||||||||
| 151 | 0 | $c = $val - $v1; | |||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ($c > $binsize) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | $tmax = $v1; | |||||||||||||||||||||||||||
| 154 | 0 | $val = $v1; | |||||||||||||||||||||||||||
| 155 | 0 | $binsize = _best_scale(($tmax - $tmin) / $bn, @binsizes);; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 0 | $val = $v1; | |||||||||||||||||||||||||||
| 158 | 0 | ▶ | last if $i > $cnt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | return ($min, $max, $rmin, $rmax, $pmin, $pmax); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub _best_scale { | ||||||||||||||||||||||||||
|
Subroutine
_best_scale: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 166 | 0 | my ($val, @opts) = @_; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | for my $opt (@opts) { | |||||||||||||||||||||||||||
| 169 | 0 | ▶ | return $opt if $opt > $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | return 99_999_999_999; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 2 | 1; # End of Text::Histogram | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||||||||||||