blib/lib/Text/Quantize.pm

filter: Statementstmt 100% 78 / 78 Branchbran 92.1% 35 / 38 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 94.9% 132 / 139 CC 27 SCAR 23.5
CC
27
Cov
96%
bucketize
16.0
_endpoints_for
7.0
quantize
6.0
CRAP 10.5
↑ Summary
1 package Text::Quantize;
2 # ABSTRACT: render a list of numbers as a textual chart
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use List::Util 'sum';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
7 exports => ['quantize', 'bucketize'],
8 groups => {
9 default => ['quantize'],
10 },
11 6 };
12  
13 25 ▶sub bucketize {
Subroutine bucketize: called CC=16 SCAR=27.8
Pod documented
14 25 my $elements = shift;
15 my $options = {
16 add_endpoints => 1,
17 25 ▶ %{ shift(@_) || {} },
Condition50%1/2
shift @_ || {}
l!l
250
MC/DC0%0/1
shift @_ || {}
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
18 };
19  
20 25 my %buckets;
21 25 for my $element (@$elements) {
22 359 my $bucket;
23  
24 359 ▶ if ($element == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($element == 0) { } 8 351 359
elsif ($element < 0) { } 123 228 351
25 8 $bucket = 0;
26 }
27 elsif ($element < 0) {
28 # log(negative) is an error, so take the log of the absolute value then negate it
29 123 $bucket = -1 * (2 ** int(log(-$element) / log(2)));
30 }
31 else {
32 # which power of 2 is this greater-than-or-equal to?
33 228 $bucket = 2 ** int(log($element) / log(2));
34 }
35  
36 359 $buckets{$bucket}++;
37 }
38  
39 # allow user to specify only one of the two endpoints if desired, and figure the other out
40 25 ▶ if ($options->{add_endpoints}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"add_endpoints"}) 6 19 25
41 6 ▶ unless (defined($options->{minimum}) && defined($options->{maximum})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options->{"minimum"} and defined $options->{"maximum"}) 5 1 6
Condition66%2/3
defined $options->{"minimum"} and defined $options->{"maximum"}
!ll&&!rl&&r
501
MC/DC50%1/2
defined $options->{"minimum"} and defined $options->{"maximum"}
defined $options->{"minimum"} defined $options->{"maximum"}
Truth table66%2/3
A and B
A= defined $options->{"minimum"}B= defined $options->{"maximum"}
ABresult
0X0
100
111
42 5 my ($min, $max) = _endpoints_for(\%buckets);
43 5 ▶ $options->{minimum} = $min if !defined($options->{minimum});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"minimum"} 5 0 5
44 5 ▶ $options->{maximum} = $max if !defined($options->{maximum});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"maximum"} 4 1 5
45 }
46 }
47  
48 # if add_endpoints, then use the expanded range that we calculated or the user specified
49 # otherwise, start at the first bucket with data and go until the last bucket with data
50 my ($start, $end) = $options->{add_endpoints}
51 ? ($options->{minimum}, $options->{maximum})
52 25 ▶ : (sort { $a <=> $b } keys %buckets)[0,-1];
Branch: 1 branch
BranchTrueFalseTotal
$options->{'add_endpoints'} ? : 6 19 25
53  
54 # force every bucket in the range to exist
55 25 my $i = $start;
56 25 ▶ while ($i <= $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $end) 141 25 166
57 141 ▶ $buckets{$i} ||= 0;
Condition100%2/2
$buckets{$i} ||= 0
l!l
11130
MC/DC100%1/1
$buckets{$i} ||= 0
$buckets{$i}
Truth table100%2/2
A
A= $buckets{$i}
Aresult
11
00
58 141 ▶ if ($i == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($i == 0) { } 9 132 141
elsif ($i == -1) { } 5 127 132
elsif ($i < 0) { } 29 98 127
59 9 $i = 1;
60 }
61 elsif ($i == -1) {
62 5 $i = 0;
63 }
64 elsif ($i < 0) {
65 29 $i /= 2; # since we're negative, increasing means smaller numbers
66 }
67 else {
68 98 $i *= 2;
69 }
70 }
71 25 return \%buckets;
72 }
73  
74 6 ▶sub quantize {
Subroutine quantize: called CC=6 SCAR=17.9
Pod documented
75 6 my $elements = shift;
76 my %options = (
77 distribution_width => 40,
78 distribution_character => '@',
79 left_label => 'value',
80 middle_label => 'Distribution',
81 right_label => 'count',
82 6 ▶ %{ shift(@_) || {} },
Condition100%2/2
shift @_ || {}
l!l
42
MC/DC100%1/1
shift @_ || {}
shift @_
Truth table100%2/2
A
A= shift @_
Aresult
11
00
83 );
84  
85 6 my $buckets = bucketize($elements, \%options);
86  
87 # pull these out because we consult them a lot, and in loops
88 6 my $distribution_width = $options{distribution_width};
89 6 my $distribution_character = $options{distribution_character};
90  
91 # the divisor deciding how wide each bucket's bar will be
92 6 my $sum = sum values %$buckets;
93  
94 # how wide must the first column (with the left_label and values) be?
95 6 my $left_width = length($options{left_label});
96 6 for my $bucket (keys %$buckets) {
97 69 ▶ $left_width = length($bucket)
Branch: 1 branch
BranchTrueFalseTotal
if length $bucket > $left_width 0 69 69
98 if length($bucket) > $left_width;
99 }
100 # add that extra space before every row
101 6 $left_width++;
102  
103 # how many - characters do we need?
104 6 my $middle_spacer = $distribution_width - length($options{middle_label}) - 2;
105  
106 # these will be different when $middle_spacer is odd, but we
107 # always want them to sum to $middle_spacer.
108 6 my $middle_left = int($middle_spacer / 2);
109 6 my $middle_right = $middle_spacer - $middle_left;
110  
111 my @output = sprintf '%*s %s %s %s %s',
112 $left_width,
113 $options{left_label},
114 ('-' x $middle_left),
115 $options{middle_label},
116 ('-' x $middle_right),
117 6 $options{right_label};
118  
119 6 for my $bucket (sort { $a <=> $b } keys %$buckets) {
120 69 my $count = $buckets->{$bucket};
121 69 my $ratio = ($count / $sum);
122 69 my $width = $distribution_width * $ratio;
123  
124 69 push @output, sprintf '%*d |%-*s %d',
125 $left_width,
126 $bucket,
127 $distribution_width,
128 ($distribution_character x $width),
129 $count;
130 }
131  
132 6 ▶ return wantarray ? @output : (join "\n", @output)."\n";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 6 6
133 }
134  
135 # given a set of buckets, find the power of two smaller than the
136 # smallest element, and the power of two greater than or equal to the
137 # largest element. used for add_endpoints
138 21 ▶sub _endpoints_for {
Subroutine _endpoints_for: called CC=7 SCAR=19.5
139 21 my $buckets = shift;
140 21 my ($min_endpoint, $max_endpoint);
141  
142 21 my @sorted_buckets = (sort { $a <=> $b } keys %$buckets);
143 21 my ($min, $max) = @sorted_buckets[0, -1];
144  
145 21 ▶ if ($min == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($min == 0) { } 4 17 21
elsif ($min == 1) { } 1 16 17
elsif ($min < 0) { } 6 10 16
146 4 $min_endpoint = -1;
147 }
148 elsif ($min == 1) {
149 1 $min_endpoint = 0;
150 }
151 elsif ($min < 0) {
152 6 $min_endpoint = -1 * (2 ** (int(log(-$min) / log(2)) + 1));
153 }
154 else {
155 10 $min_endpoint = 2 ** (int(log($min) / log(2)) - 1);
156 }
157  
158 21 ▶ if ($max == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($max == 0) { } 1 20 21
elsif ($max == -1) { } 2 18 20
elsif ($max < 0) { } 2 16 18
159 1 $max_endpoint = 1;
160 }
161 elsif ($max == -1) {
162 2 $max_endpoint = 0;
163 }
164 elsif ($max < 0) {
165 2 $max_endpoint = -1 * (2 ** (int(log(-$max) / log(2)) - 1));
166 }
167 else {
168 16 $max_endpoint = 2 ** (int(log($max) / log(2)) + 1);
169 }
170  
171 21 return ($min_endpoint, $max_endpoint);
172 }
173  
174 6 1;
175  
176  
177  
178 =pod
179  
180 =head1 NAME
181  
182 Text::Quantize - render a list of numbers as a textual chart
183  
184 =head1 VERSION
185  
186 version 0.05
187  
188 =head1 SYNOPSIS
189  
190 use Text::Quantize;
191
192 print quantize([26, 24, 51, 77, 21]);
193
194 __END__
195
196 value ------------- Distribution ------------- count
197 8 | 0
198 16 |@@@@@@@@@@@@@@@@@@@@@@@@ 3
199 32 |@@@@@@@@ 1
200 64 |@@@@@@@@ 1
201 128 | 0
202  
203 =head1 FUSSY SYNOPSIS
204  
205 use Text::Quantize ();
206  
207 print Text::Quantize::quantize([map { chomp; $_ } <DATA>], {
208 left_label => 'microseconds',
209 middle_label => 'Calls per time bucket',
210 right_label => 'syscalls',
211 distribution_width => 80,
212 distribution_character => '=',
213 });
214
215 __END__
216
217 microseconds ---------------------------- Calls per time bucket ----------------------------- syscalls
218 256 | 0
219 512 |==== 5
220 1024 |===== 7
221 2048 |================== 23
222 4096 |============================ 36
223 8192 |======= 9
224 16384 |= 2
225 32768 | 1
226 262144 | 1
227 524288 | 1
228 1048576 | 1
229 2097152 |======= 9
230 4194304 |=== 4
231 8388608 | 1
232 16777216 | 0
233  
234 =head1 FUNCTIONS
235  
236 =head2 C<quantize([integers], {options})>
237  
238 C<quantize> takes an array reference of integers and an optional
239 hash reference of options, and produces a textual histogram of the
240 integers bucketed into powers-of-2 sets.
241  
242 Options include:
243  
244 =over 4
245  
246 =item C<left_label> (default: C<value>)
247  
248 Controls the text of the left-most label which represents the
249 bucket's contents.
250  
251 =item C<middle_label> (default: C<Distribution>)
252  
253 Controls the text of the middle label which can be used to title
254 the histogram.
255  
256 =item C<right_label> (default: C<count>)
257  
258 Controls the text of the right-most label which represents how many
259 items are in that bucket.
260  
261 =item C<distribution_width> (default: C<40>)
262  
263 Controls how many characters wide the textual histogram is. This
264 does not include the legends.
265  
266 =item C<distribution_character> (default: C<@>)
267  
268 Controls the character used to represent the data in the histogram.
269  
270 =item C<add_endpoints> (default: C<1>)
271  
272 Controls whether the top and bottom lines (which are going to have
273 values of 0) are added. They're included by default because it hints
274 that the data set is complete.
275  
276 =back
277  
278 =head2 C<bucketize([integers], {options})>
279  
280 C<bucketize> takes an array reference of integers and an optional
281 hash reference of options, and produces a hash reference of those
282 integers bucketed into powers-of-2 sets.
283  
284 Options include:
285  
286 =over 4
287  
288 =item add_endpoints (default: C<1>)
289  
290 Controls whether extra buckets, smaller than the minimum value and
291 larger than the maximum value, (which are going to have values of
292 0) are added. They're included by default because it hints that the
293 data set is complete.
294  
295 =back
296  
297 =head1 SEE ALSO
298  
299 C<DTrace>, which is where I first saw this kind of C<quantize()>
300 histogram.
301  
302 L<dip>, which ported C<quantize()> to Perl first, and from which I
303 took a few insights.
304  
305 =head1 AUTHOR
306  
307 Shawn M Moore <code@sartak.org>
308  
309 =head1 COPYRIGHT AND LICENSE
310  
311 This software is copyright (c) 2012 by Infinity Interactive.
312  
313 This is free software; you can redistribute it and/or modify it under
314 the same terms as the Perl 5 programming language system itself.
315  
316 =cut
317  
318  
319 __END__
↑ Summary