Units/Rank.pm

filter: Statementstmt 91.9% 149 / 162 Branchbran 67.2% 39 / 58 Conditioncond 66.6% 6 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 8 totaltotal 82.4% 212 / 257 CC 41 SCAR 22.2
CC
41
Cov
85%
render
16.0
choose_juicy_ones
11.0
rank_power_variants
10.2
CRAP 9.2
« Units/Convert/Date.pm ↑ Summary Units/Convert/Base.pm »
1 package Math::Calc::Units::Rank;
2 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use vars qw(@EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶BEGIN { @EXPORT_OK = qw(choose_juicy_ones render render_unit); }
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Math::Calc::Units::Convert qw(convert canonical);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Math::Calc::Units::Convert::Multi qw(variants major_variants major_pref pref_score range_score get_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # choose_juicy_ones : value -> ( value )
11 #
12 # Pick the best-sounding units for the given value, and compute the
13 # resulting magnitude and score. The total number returned is based on
14 # a magical formula that examines the rates of decay of the scores.
15 #
16 12 ▶sub choose_juicy_ones {
Subroutine choose_juicy_ones: called CC=11 SCAR=24.0
Pod undocumented
17 12 my ($v, $options) = @_;
18  
19 # Collect the variants of the value, together with their scores.
20 12 my @variants = rank_variants($v, $options); # ( < {old=>new}, score > )
21  
22 # Remove duplicates
23 12 my %variants; # To remove duplicates: { id => [ {old=>new}, score ] }
24 12 for my $variant (@variants) {
25 126 my $id = join(";;", values %{ $variant->[0] });
26 126 $variants{$id} = $variant;
27 }
28  
29 12 my @options;
30 12 for my $variant (values %variants) {
31 126 my ($map, $score) = @$variant;
32 126 my %copy;
33 126 my ($magnitude, $units) = @$v;
34 126 ▶ while (my ($unit, $count) = each %$units) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$units) 140 126 266
35 140 $copy{$map->{$unit}} = $count;
36 }
37 126 push @options, [ $score, convert($v, \%copy) ];
38 }
39  
40 # Pick up to five of the highest scores. If any score is less than
41 # 1/10 of the previous score, or 1/25 of the highest score, then
42 # don't bother returning it (or anything worse than it.)
43 12 my @juicy;
44 my $first;
45 12 my $prev;
46 12 foreach (sort { $b->[0] <=> $a->[0] } @options) {
47 48 my ($score, $val) = @$_;
48 48 ▶ last if (defined $prev && ($prev / $score) > 8);
Branch: 1 branch
BranchTrueFalseTotal
if defined $prev and $prev / $score > 8 3 45 48
Condition100%3/3
defined $prev and $prev / $score > 8
!ll&&!rl&&r
12333
MC/DC100%2/2
defined $prev and $prev / $score > 8
defined $prev $prev / $score > 8
Truth table100%3/3
A and B
A= defined $prevB= $prev / $score > 8
ABresult
0X0
100
111
49 45 ▶ last if (defined $first && ($first / $score) > 25);
Branch: 1 branch
BranchTrueFalseTotal
if defined $first and $first / $score > 25 3 42 45
Condition100%3/3
defined $first and $first / $score > 25
!ll&&!rl&&r
12303
MC/DC100%2/2
defined $first and $first / $score > 25
defined $first $first / $score > 25
Truth table100%3/3
A and B
A= defined $firstB= $first / $score > 25
ABresult
0X0
100
111
50 42 push @juicy, $val;
51 42 ▶ $first = $score unless defined $first;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first 12 30 42
52 42 $prev = $score;
53 42 ▶ last if @juicy == 5;
Branch: 1 branch
BranchTrueFalseTotal
if @juicy == 5 4 38 42
54 }
55  
56 12 return @juicy;
57 }
58  
59 # rank_variants : <amount,unit> -> ( < map, score > )
60 # where map : {original unit => new unit}
61 #
62 12 ▶sub rank_variants {
Subroutine rank_variants: called CC=2 SCAR=7.1
Pod undocumented
63 12 my ($v, $options) = @_;
64  
65 12 $v = canonical($v);
66  
67 12 my ($mag, $count) = @$v;
68  
69 12 my @rangeable = grep { $count->{$_} > 0 } keys %$count;
70 12 ▶ if (@rangeable == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rangeable == 0) 0 12 12
71 0 @rangeable = keys %$count;
72 }
73  
74 12 return rank_power_variants($mag, \@rangeable, $count, $options);
75 }
76  
77 4 ▶sub choose_major {
Subroutine choose_major: called CC=1 SCAR=0.0
Pod undocumented
78 4 my (@possibilities) = @_;
79 4 my @majors = map { [ major_pref($_), $_ ] } @possibilities;
80 4 return (sort { $a->[0] <=> $b->[0] } @majors)[-1]->[1];
81 }
82  
83 # rank_power_variants : value x [unit] x {unit=>power} x options ->
84 # ( <map,score> )
85 #
86 # $top is the set of units that should be range checked.
87 #
88 26 ▶sub rank_power_variants {
Subroutine rank_power_variants: called CC=10 SCAR=23.2
Pod undocumented
89 26 my ($mag, $top, $power, $options) = @_;
90  
91 # Recursive case: we have multiple units left, so pick one to be
92 # the "major" unit and select the best combination of the other
93 # units for each major variant on the major unit.
94  
95 26 ▶ if (keys %$power > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$power > 1) 4 22 26
96 # Choose the major unit class (this will return the best
97 # result for each of the major variants)
98 4 my $major = choose_major(keys %$power);
99 4 my $majorClass = get_class($major);
100  
101 4 my %powerless = %$power;
102 4 delete $powerless{$major};
103  
104 4 my @ranked; # ( <map,score> )
105  
106 # Try every combination of each major variant and the other units
107 4 foreach my $variant (major_variants($major, $options)) {
108 14 my $mult = $majorClass->simple_convert($variant, $major);
109 14 my $cval = $mag / $mult ** $power->{$major};
110  
111 14 ▶ print "\n --- for $variant ---\n" if $options->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"verbose"} 0 14 14
112 14 my @r = rank_power_variants($cval, $top, \%powerless, $options);
113 14 ▶ next if @r == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @r == 0 0 14 14
114  
115 14 my $best = $r[0];
116 14 $best->[0]->{$major} = $variant; # Augment map
117 # Replace score with major pref
118 14 $best->[1] = pref_score($variant);
119 14 push @ranked, $best;
120 }
121  
122 4 return @ranked;
123 }
124  
125 # Base case: have a single unit left. Go through all possible
126 # variants of that unit.
127  
128 22 ▶ if (keys %$power == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$power == 0) 0 22 22
129 # Special case: we don't have any units at all
130 0 return [ {}, 1 ];
131 }
132  
133 22 my $unit = (keys %$power)[0];
134 22 $power = $power->{$unit}; # Now it's just the power of this unit
135 22 my $class = get_class($unit);
136 22 my (undef, $canon) = $class->to_canonical($unit);
137 22 my $mult = $class->simple_convert($unit, $canon);
138 22 $mag *= $mult ** $power;
139  
140 22 my @choices;
141 22 my @subtop = grep { $_ ne $canon } @$top;
142 22 my $add_variant = (@subtop == @$top); # Flag: add $variant to @$top?
143  
144 22 foreach my $variant (variants($canon)) {
145 # Convert from $canon to $variant
146 # Input: 4000 / sec ; (canon=sec)
147 # 1 ms -> .001 sec ; (variant=ms)
148 # 4000 / (.001 ** -1) = 4 / ms
149 282 my $mult = $class->simple_convert($variant, $canon);
150 282 my $minimag = $mag / $mult ** $power;
151  
152 282 my @vtop = @subtop;
153 282 ▶ push @vtop, $variant if $add_variant;
Branch: 1 branch
BranchTrueFalseTotal
if $add_variant 0 282 282
154  
155 282 my $score = score($minimag, $variant, \@vtop);
156 printf "($mag $unit) score %.6f:\t $minimag $variant\n", $score
157 282 ▶ if $options->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"verbose"} 0 282 282
158 282 push @choices, [ $score, $variant ];
159 }
160  
161 22 @choices = sort { $b->[0] <=> $a->[0] } @choices;
162 22 ▶ return () if @choices == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @choices == 0 0 22 22
163  
164 22 return map { [ {$unit => $_->[1]}, $_->[0] ] } @choices;
165 }
166  
167 # Return a string representing a given set of units. The input is a
168 # map from unit names to their powers (eg lightyears/sec/sec would be
169 # represented as { lightyears => 1, sec => -2 }); the output is a
170 # corresponding string such as "lightyears / sec**2".
171 4 ▶sub render_unit {
Subroutine render_unit: called CC=7 SCAR=21.4
Pod undocumented
172 4 my ($units, $options) = @_;
173  
174 # Positive powers just get appended together with spaces between
175 # them.
176 4 my $str = '';
177 4 ▶ while (my ($name, $power) = each %$units) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$units) 2 4 6
178 2 ▶ if ($power > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($power > 0) 2 0 2
179 2 $str .= get_class($name)->render_unit($name, $power, $options);
180 2 $str .= " ";
181 }
182 }
183 4 chop($str);
184  
185 # Negative powers will be placed after a "/" character, because
186 # they're in the denominator.
187 4 my $botstr = '';
188 4 ▶ while (my ($name, $power) = each %$units) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$units) 4 4 8
189 4 ▶ if ($power < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($power < 0) 0 4 4
190 0 $botstr .= get_class($name)->render_unit($name, -$power, $options);
191 0 $botstr .= " ";
192 }
193 }
194 4 chop($botstr);
195  
196 # Combine the numerator and denominator appropriately.
197 4 ▶ if ($botstr eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($botstr eq '') { } 4 0 4
elsif ($botstr =~ /\s/) { } 0 0 0
198 4 return $str;
199 } elsif ($botstr =~ /\s/) {
200 0 return "$str / ($botstr)";
201 } else {
202 0 return "$str / $botstr";
203 }
204 }
205  
206 # render : <value,unit> -> string
207 50 ▶sub render {
Subroutine render: called CC=12 SCAR=27.7
Pod undocumented
208 50 my ($v, $options) = @_;
209 50 my ($mag, $units) = @$v;
210  
211 # No units
212 50 ▶ if (keys %$units == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$units == 0) 0 50 50
213 # Special-case percentages
214 0 my $str = sprintf("%.4g", $mag);
215 0 ▶ if (($mag < 1) && ($mag >= 0.01)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mag < 1 and $mag >= "0.01") 0 0 0
Condition0%0/3
$mag < 1 and $mag >= "0.01"
!ll&&!rl&&r
000
MC/DC0%0/2
$mag < 1 and $mag >= "0.01"
$mag < 1 $mag >= "0.01"
Truth table0%0/3
A and B
A= $mag < 1B= $mag >= "0.01"
ABresult
0X0
100
111
216 0 ▶ if ($options->{abbreviate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'abbreviate'}) { } 0 0 0
217 0 $str .= sprintf(" = %.4g percent", 100 * $mag);
218 } else {
219 0 $str .= sprintf(" = %.4g%%", 100 * $mag);
220 }
221 }
222 0 return $str;
223 }
224  
225 50 my @top;
226 my @bottom;
227 50 ▶ while (my ($name, $power) = each %$units) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$units) 64 50 114
228 64 ▶ if ($power > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($power > 0) { } 51 13 64
229 51 push @top, $name;
230 } else {
231 13 push @bottom, $name;
232 }
233 }
234  
235 50 my $str;
236 50 ▶ if (@top == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@top == 1) { } 49 1 50
237 49 my ($name) = @top;
238 49 $str = get_class($name)->render($mag, $name, $units->{$name}, $options);
239 49 $str .= " ";
240 } else {
241 1 $str = sprintf("%.4g ", $mag);
242 1 foreach my $name (@top) {
243 2 $str .= get_class($name)->render_unit($name, $units->{$name}, $options);
244 2 $str .= " ";
245 }
246 }
247  
248 50 ▶ if (@bottom > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bottom > 0) 13 37 50
249 13 my $botstr;
250 13 foreach my $name (@bottom) {
251 13 $botstr .= get_class($name)->render_unit($name, -$units->{$name}, $options);
252 13 $botstr .= " ";
253 }
254 13 chop($botstr);
255  
256 13 ▶ if (@bottom > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bottom > 1) { } 0 13 13
257 0 $str .= "/ ($botstr) ";
258 } else {
259 13 $str .= "/ $botstr ";
260 }
261 }
262  
263 50 chop($str);
264 50 return $str;
265 }
266  
267 # max_range_score : amount x [ unit ] -> score
268 #
269 # Takes max score for listed units.
270 #
271 282 ▶sub max_range_score {
Subroutine max_range_score: called CC=3 SCAR=11.0
Pod undocumented
272 282 my ($mag, $units) = @_;
273 282 my $score = 0;
274  
275 282 foreach my $name (@$units) {
276 282 my $uscore = range_score($mag, $name);
277 282 ▶ $score = $uscore if $score < $uscore;
Branch: 1 branch
BranchTrueFalseTotal
if $score < $uscore 282 0 282
278 }
279  
280 282 return $score;
281 }
282  
283 # Arguments:
284 # $mag - The magnitude of the value (in the given unit)
285 # $unit - The unit to use to figure out what sounds best
286 # $top - ...I'll get back to you...
287 282 ▶sub score {
Subroutine score: called CC=2 SCAR=6.9
Pod undocumented
288 282 my ($mag, $unit, $top) = @_;
289 282 ▶ my @rangeable = @$top ? @$top : ($unit);
Branch: 1 branch
BranchTrueFalseTotal
@$top ? : 1 281 282
290 282 my $pref = pref_score($unit);
291 282 my $range_score = max_range_score($mag, \@rangeable);
292 282 return $pref * $range_score;
293 }
294  
295 1 1;
« Units/Convert/Date.pm ↑ Summary Units/Convert/Base.pm »