blib/lib/PDL/Graphics/Prima/Scaling.pm untested

filter: Statementstmt 0.0% 0 / 202 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 30 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 378 CC 48 SCAR 26.9
CC
48
Cov
0%
_adjusted_position
30.0
compute_ticks
30.0
compute_ticks
20.0
CRAP 14.7
1 use strict;
2 use warnings;
3  
4 package PDL::Graphics::Prima::Scaling;
5  
6 our $VERSION = 0.17; # update with update-version.pl
7  
8 package
9 sc;
10 use constant Linear => 'PDL::Graphics::Prima::Scaling::Linear';
11 use constant Log => 'PDL::Graphics::Prima::Scaling::Log';
12  
13  
14 # Defines the scaling classes.
15 # working here - add a broken axis scaling class
16  
17 package PDL::Graphics::Prima::Scaling::Linear;
18  
19 use PDL;
20 use Carp;
21  
22 # order of magnitude returns the power of 10 that is below the given value.
23 # Examples:
24 # 12 -> 10
25 # 0.04 -> 0.01
26 # 3.2e3 -> 1e3
27 sub order_of_magnitude {
28 my $number = shift;
29 confess("Internal error: non-positive numbers are not allowed in order_of_magnitude (I received $number)")
30 if $number <= 0;
31 my $exponent = log($number) / log(10);
32 # Correct for truncation of negative numbers:
33 $exponent-- if $exponent < 0;
34 return 10**(int $exponent);
35 }
36  
37 # A function that returns a usable min/max when the two are identical. For
38 # linear scaling, this amounts to returning the current value +- half itself,
39 # unless that value is zero (in which case I return +-1).
40 sub min_max_for_degenerate {
41 my ($class, $value) = @_;
42 if ($value == 0) {
43 return (-1, 1);
44 }
45 elsif ($value > 0) {
46 return (0.5*$value, 1.5 * $value);
47 }
48 else {
49 return (1.5*$value, 0.5 * $value);
50 }
51 }
52  
53 sub _adjusted_position {
54 my ($value, $interval, $offset, $direction) = @_;
55
56 {
57 no warnings 'numeric';
58 # check for nan and croak since I can't continue with nans:
59 confess("_adjusted_position got \$value of nan!") if ($value != $value);
60 confess("_adjusted_position got \$interval of nan!")
61 if ($interval != $interval);
62 }
63
64 # Find the first copy of $interval that's less than $value:
65 my $to_return = int($value / $interval) * $interval;
66 # include the offset:
67 $to_return += $offset;
68 while(1) {
69 return $to_return if $value == $to_return;
70 return $to_return if (($to_return <=> $value) == $direction);
71 $to_return += $direction * $interval;
72 }
73 # Return the final, adjusted position:
74 return $to_return;
75 }
76  
77  
78 sub compute_ticks {
79 my (undef, $min, $max) = @_;
80 # This revised algorithm is much simpler and faster, and designed to
81 # reduce tick "jitter" when panning or scrolling.
82 # min max ticks at
83 # 3 4 3, 3.25, 3.5, 3.75, 4
84 # 3.1 4.1 3.25, 3.5, 3.75, 4
85 # 2 3.7 2, 2.25, 2.5, 2.75, 3, 3.25, 3.5
86 # 2 3.8 2, 2.5, 3, 3.5
87 # 2 4 2, 2.5, 3, 3.5, 4
88 # 2.2 4.2 2.5, 3, 3.5, 4
89 # 2 5 2, 2.5, 3, 3.5, 4, 4.5, 5
90 # 2 5.5 2, 3, 4, 5
91 # 2 8 2, 3, 4, 5, 6, 7, 8
92 # 2 9 2, 4, 6, 8
93 # 0 20 0, 5, 10, 15, 20
94 # 30.5 37.5 32, 34, 36
95 # 0 112 0, 25, 50, 75, 100
96
97 # Determine the order of magnitude of the min/max range:
98 my $full_range = $max - $min;
99 my $order_of_magnitude = order_of_magnitude($full_range);
100 my $relative_range = $full_range / $order_of_magnitude;
101
102 # This is a table for determining the tick size as a *function* of the
103 # size of the range wrt the order of magnitude
104 # relative range less than... # use tick size of...
105 my $rel_interval = $relative_range < 1.75 ? 0.25
106 : $relative_range < 3 ? 0.5
107 : $relative_range < 7 ? 1
108 : 2
109 ;
110
111 # Compute the major tick interval and the layout
112 my $Tick_interval = $order_of_magnitude * $rel_interval;
113 # The ticks will start at either a multiple of the interval,
114 # or a multiple of the interval plus the offset. So,
115 # recompute the range in light of the tick size:
116 my $min_Tick = _adjusted_position($min, $Tick_interval, 0, 1);
117 my $max_Tick = _adjusted_position($max, $Tick_interval, 0, -1);
118 # Count the number of ticks between min_Tick and max_Tick
119 my $N_Ticks = ($max_Tick - $min_Tick) / $Tick_interval + 1;
120 # Add 0.1 to escape rounding trouble.
121 my $Ticks = zeroes($N_Ticks + 0.1)->xlinvals($min_Tick, $max_Tick);
122
123 # Construct the minor tick marks:
124 my $tick_interval = $Tick_interval / 5;
125 $tick_interval = $Tick_interval / 4 if $rel_interval >= 1;
126 my $min_tick = _adjusted_position($min, $tick_interval, 0, 1);
127 my $max_tick = _adjusted_position($max, $tick_interval, 0, -1);
128 my $N_ticks = ($max_tick - $min_tick) / $tick_interval + 1;
129 # Add 0.1 to escape rounding trouble.
130 my $ticks = zeroes($N_ticks + 0.1)->xlinvals($min_tick, $max_tick);
131  
132 # finally, set up zero values that make sense:
133 # working here - convoluted so the debugger wouldn't get angry at me:
134 # $best_Ticks->where(abs($best_Ticks) * 1e10 < ($max - $min)) .= 0;
135 # $ticks->where(abs($ticks) * 1e10 < ($max - $min)) .= 0;
136 my $foo = $Ticks->where(abs($Ticks) * 1e10 < ($max - $min));
137 $foo .= 0;
138 $foo = $ticks->where(abs($ticks) * 1e10 < ($max - $min));
139 $foo .= 0;
140 return ($Ticks, $ticks);
141 }
142  
143 # Rescales the data so that the min has a value of zero and the max has a value
144 # of 1.
145 use Carp 'croak';
146 sub transform {
147 my ($class, $min, $max, $data) = @_;
148 my $range = $max - $min;
149 croak("Scaling::Linear::transform needs min != max but got min = max = $min")
150 if $range == 0;
151 croak('Scaling::Linear::transform needs well-defined data')
152 if not defined $data;
153 return (($data - $min) / $range);
154 }
155  
156 # Rescales data so that a value of zero gets the minimum and a value of 1 gets
157 # the maximum:
158 sub inv_transform {
159 my ($class, $min, $max, $data) = @_;
160 my $range = $max - $min;
161 croak("Scaling::Linear::inv_transform needs min != max but got min = max = $min")
162 if $range == 0;
163 croak('Scaling::Linear::inv_transform needs well-defined data')
164 if not defined $data;
165 return $range * $data + $min;
166 }
167  
168 # Generates a collection of points that extrapolate evenly from the min to the
169 # max
170 sub sample_evenly {
171 my ($class, $min, $max, $N_values) = @_;
172 croak("Scaling::Linear::sample_evenly needs min != max but got min = max = $min")
173 if $min == $max;
174 croak("Scaling::Linear::sample_evenly expects N_values > 1")
175 if not defined $N_values or $N_values < 2;
176 return zeroes($N_values)->xlinvals($min, $max);
177 }
178  
179 # This should ONLY be called on real numbers, numbers that you actually intend
180 # to use in plotting. In other words, inf will return 0!
181 # Usage:
182 # On success, it returns 1. On failure, it returns zero and modifies $@ with
183 # an explanation (which is used in capturing error messages).
184 sub is_valid_extremum {
185 no warnings 'numeric';
186
187 # Allow anything except infinity or nan:
188 if ($_[1] != $_[1]) { # nan
189 $@ = 'nan is not allowed; value must be real';
190 return 0;
191 }
192 elsif ($_[1] * 0.0 != 0.0) { # inf
193 $@ = 'value must be finite';
194 return 0;
195 }
196 return 1;
197 }
198  
199 package PDL::Graphics::Prima::Scaling::Log;
200 #our @ISA = qw(PDL::Graphics::Prima::Scaling::Linear);
201  
202 use PDL;
203 use Carp;
204  
205 sub min_max_for_degenerate {
206 my ($class, $value) = @_;
207 croak("All values must be strictly positive with logarithmic scaling")
208 unless $value > 0;
209 return ($value / 2, $value * 2);
210 }
211  
212 # For data between 0.5 and 15, I would want tick marks at
213 # 0.5, 1, 1.5, 2.5, 5, 10, 15
214 # for data between 10 and 300, I would want tick marks at
215 # 10, 20, 50, 100, 200,
216  
217 sub compute_ticks {
218 # These are the actual data's min and max:
219 my (undef, $min, $max) = @_;
220
221 # I have three different possible behaviors depending on the dynamic range:
222 my $dynamic_range = log($max / $min) / log(10);
223
224 # First thing's first: if the data show a minimal dynamic range, just use
225 # the linear tick algorithm:
226 return PDL::Graphics::Prima::Scaling::Linear::compute_ticks(undef, $min, $max)
227 if ($dynamic_range < 1.5);
228
229 # If the data show low dynamic range, return the scaling for that:
230 return low_dynamic_ticks($min, $max, $dynamic_range) if ($dynamic_range < 7);
231
232 # If the data show an medium dynamic range, return the scaling for that:
233 return medium_dynamic_ticks($min, $max, $dynamic_range) if ($dynamic_range < 13);
234
235 # If the data show a high dynamic range (from, say 10**-6 to 10**15), use
236 # the high-dynamic range algorithm, which simply wraps the linear tick
237 # algorithm:
238 # working here - this doesn't always work!
239 my ($Ticks, $ticks) = PDL::Graphics::Prima::Scaling::Linear::compute_ticks(undef, log($min)/log(1000), log($max) / log(1000));
240 return (1000**$Ticks, 1000**$ticks);
241 }
242  
243 # This algorithm determines the logarithmic tick marks for low dynamic ranges,
244 # which are dynamic ranges between 1.5 and 3.
245 sub low_dynamic_ticks {
246 my ($min, $max, $dynamic_range) = @_;
247
248 # Determine the number of ticks per order (dynamic range cutoff of 2 was
249 # determined empirically):
250 # For dynamic ranges less than... use ... ticks per order
251 my $N_Ticks_per_order = $dynamic_range < 2 ? 3
252 : $dynamic_range < 3 ? 2
253 : 1;
254
255 # Accumulate the Tick marks:
256 my @Ticks = (_smallest_next_low_number($min, $N_Ticks_per_order));
257 my $max_Tick = _largest_previous_low_number($max, $N_Ticks_per_order);
258 for(my $current_value = _smallest_next_low_number($min*1.01, $N_Ticks_per_order)
259 ; $current_value < $max_Tick
260 ; $current_value = _smallest_next_low_number($current_value*1.01, $N_Ticks_per_order)) {
261 push @Ticks, $current_value;
262 }
263 push @Ticks, $max_Tick;
264
265 # Determine the minimum and maximum tick marks (notice the lower case):
266 my $min_order = PDL::Graphics::Prima::Scaling::Linear::order_of_magnitude($min);
267 my $max_order = PDL::Graphics::Prima::Scaling::Linear::order_of_magnitude($max);
268
269 # Add a tick for all 10 values in each decade:
270 my $ticks = (sequence(10) * $min_order);
271 $ticks = $ticks->where($min < $ticks);
272 for (my $current_order = $min_order * 10; $current_order <= $max_order
273 ; $current_order *= 10) {
274 my $to_add = (sequence(10) * $current_order);
275 $ticks = $ticks->append($to_add->where($to_add < $max));
276 }
277
278 # All done. PDLify the ticks and return them:
279 return (pdl(@Ticks), $ticks);
280 }
281  
282 # Determines the first number greater than the given value that satisfies the
283 # given number of ticks per order, as used for low dynamic ranges. For example:
284 # Value N_ticks_per_order return
285 # 34 1 100
286 # 34 2,3 50
287 # 19 2 50
288 # 19 3 20
289 # 99 1,2,3 100
290 # 100 1,2,3 100
291 sub _smallest_next_low_number {
292 my ($value, $N_ticks_per_order) = @_;
293
294 # First get the order of magnitude of this value:
295 my $order = PDL::Graphics::Prima::Scaling::Linear::order_of_magnitude($value);
296
297 # The order of magnitude is guaranteed to be less than the actual value.
298 # Now determine which value to return.
299 return $order if $value == $order;
300 return $order*2 if $value < $order*2 and $N_ticks_per_order > 2;
301 return $order*5 if $value < $order*5 and $N_ticks_per_order > 1;
302 return $order*10;
303 }
304  
305 # Determines the first number smaller than the given value that satisfies the
306 # given number of ticks per order, as used for low dynamic ranges. For example:
307 # Value N_ticks_per_order return
308 # 34 1,2 10
309 # 34 3 20
310 # 19 1,2,3 10
311 # 21 1,2 10
312 # 57 1,2,3 50
313 # 100 1,2,3 100
314 sub _largest_previous_low_number {
315 my ($value, $N_ticks_per_order) = @_;
316  
317 # First get the order of magnitude of this value:
318 my $order = PDL::Graphics::Prima::Scaling::Linear::order_of_magnitude($value);
319  
320 # The order of magnitude is guaranteed to be less than the actual value.
321 # Now determine which value to return.
322 return $order if $value == $order;
323 return $order*5 if $order*5 < $value and $N_ticks_per_order > 1;
324 return $order*2 if $order*2 < $value and $N_ticks_per_order > 2;
325 return $order;
326 }
327  
328 # This algorithm determines the logarithmic tick marks for medium dynamic ranges,
329 # which are dynamic ranges between 3 and 12. The number of decades per tick mark
330 # for these ranges should be either one or two.
331 sub medium_dynamic_ticks {
332 my ($min, $max, $dynamic_range) = @_;
333
334 # Set the decades per tick:
335 my $N_decades_per_tick = 2;
336
337 # Accumulate the Tick marks:
338 my @Ticks = (_smallest_next_number($min, $N_decades_per_tick));
339 my $max_Tick = _largest_previous_number($max, $N_decades_per_tick);
340 for(my $current_value = _smallest_next_number($min*1.01, $N_decades_per_tick)
341 ; $current_value < $max_Tick
342 ; $current_value = _smallest_next_number($current_value*1.01, $N_decades_per_tick)) {
343 push @Ticks, $current_value;
344 }
345 push @Ticks, $max_Tick;
346
347 # Determine the minimum and maximum tick marks:
348 my $min_order = PDL::Graphics::Prima::Scaling::Linear::order_of_magnitude($min);
349 my $max_order = PDL::Graphics::Prima::Scaling::Linear::order_of_magnitude($max);
350
351 my @ticks;
352 for (my $current_order = $min_order; $current_order < $max; $current_order *= 10) {
353 push @ticks, $current_order
354 if ($min < $current_order and $current_order < $max);
355 push @ticks, $current_order * sqrt(10)
356 if ($min < $current_order * sqrt(10) and $current_order * sqrt(10) < $max);
357 }
358
359 # All done. PDLify the ticks and return them:
360 return (pdl(@Ticks), pdl(@ticks));
361 }
362  
363 # Determines the next number larger than the given value for the given number
364 # of decades per tick (at least one) and decade offset. This probably only makes
365 # sense with some examples.
366 # Consider the smallest next number for a given number of decades. Suppose I
367 # wanted to work with triples of decades, i.e. 1, 1e3, 1e6, 1e9, etc. For any
368 # value less than 1000, the smallest next number is 1000. For any number greater
369 # than 1000 and less than a million, the smallest next number is a million.
370 sub _smallest_next_number {
371 my ($value, $N_decades_per_tick) = @_;
372 # Compute the exponent (see order_of_magnitude)
373 my $exponent = log($value) / log(10);
374 # Correct for truncation of negative numbers:
375 $exponent-- if $exponent < 0;
376 my $trunc_exponent = int $exponent;
377
378 # Return the desired next number:
379 return 10**($trunc_exponent + $N_decades_per_tick
380 - ($trunc_exponent % $N_decades_per_tick));
381 }
382  
383 # Determines the previous number smaller than the given value for the given
384 # number of decades per tick. See the previous function for details.
385 sub _largest_previous_number {
386 my ($value, $N_decades_per_tick) = @_;
387 # Compute the exponent (see order_of_magnitude)
388 my $exponent = log($value) / log(10);
389 # Correct for truncation of negative numbers:
390 $exponent-- if $exponent < 0;
391 my $trunc_exponent = int $exponent;
392
393 # Return the desired previous number:
394 return 10**($trunc_exponent - ($trunc_exponent % $N_decades_per_tick));
395 }
396  
397 # Rescales the data so that the min has a value of zero and the max has a value
398 # of 1.
399 sub transform {
400 my ($class, $min, $max, $data) = @_;
401 croak("Scaling::Log::transform needs min > 0 and max > 0 but got min = $min and max = $max")
402 unless $min > 0 and $max > 0;
403 my ($log_min, $log_max, $log_data) = (log($min), log($max), log($data));
404 my $range = $log_max - $log_min;
405 croak('Scaling::Log::transform needs min != max') if $range == 0;
406 croak('Scaling::Log::transform needs well-defined data')
407 if not defined $data;
408 return (($log_data - $log_min) / $range);
409 }
410  
411 # Rescales data so that a value of zero gets the minimum and a value of 1 gets
412 # the maximum:
413 sub inv_transform {
414 # working here - make sure this works
415 my ($class, $min, $max, $data) = @_;
416 croak("Scaling::Log::inv_transform needs min > 0 and max > 0 but got min = $min and max = $max")
417 unless $min > 0 and $max > 0;
418 my ($log_min, $log_max) = (log($min), log($max));
419 my $range = $log_max - $log_min;
420 croak('Scaling::Log::transform needs min != max') if $range == 0;
421 croak('Scaling::Log::transform needs well-defined data')
422 if not defined $data;
423 return exp($range * $data + $log_min);
424 }
425  
426 # Generates a collection of points that extrapolate evenly from the min to the
427 # max
428 sub sample_evenly {
429 my ($class, $min, $max, $N_values) = @_;
430 croak("Scaling::Log::sample_evenly needs min != max but got min = max = $min")
431 if $min == $max;
432 croak("Scaling::Log::sample_evenly expects N_values > 1")
433 if not defined $N_values or $N_values < 2;
434 return zeroes($N_values)->xlogvals($min, $max);
435 }
436  
437 sub is_valid_extremum {
438 my ($class, $extremum) = @_;
439 no warnings 'numeric';
440 if ($extremum <= 0) {
441 # no negative values:
442 $@ = 'must be positive for logarithmic scaling';
443 return 0;
444 }
445 elsif ($extremum != $extremum) {
446 # nans not allowed:
447 $@ = 'nan is not allowed';
448 return 0;
449 }
450 elsif ($extremum * 0.0 != 0.0) {
451 # inf is bad:
452 $@ = 'must be finite';
453 return 0;
454 }
455 # otherwise we're good:
456 return 1;
457 }
458  
459 1;
460  
461 =head1 AUTHOR
462  
463 David Mertens (dcmertens.perl@gmail.com)
464  
465 =head1 ADDITIONAL MODULES
466  
467 Here is the full list of modules in this distribution:
468  
469 =over
470  
471 =item L<PDL::Graphics::Prima|PDL::Graphics::Prima/>
472  
473 Defines the Plot widget for use in Prima applications
474  
475 =item L<PDL::Graphics::Prima::Axis|PDL::Graphics::Prima::Axis/>
476  
477 Specifies the behavior of axes (but not the scaling)
478  
479 =item L<PDL::Graphics::Prima::DataSet|PDL::Graphics::Prima::DataSet/>
480  
481 Specifies the behavior of DataSets
482  
483 =item L<PDL::Graphics::Prima::Limits|PDL::Graphics::Prima::Limits/>
484  
485 Defines the lm:: namespace
486  
487 =item L<PDL::Graphics::Prima::Palette|PDL::Graphics::Prima::Palette/>
488  
489 Specifies a collection of different color palettes
490  
491 =item L<PDL::Graphics::Prima::PlotType|PDL::Graphics::Prima::PlotType/>
492  
493 Defines the different ways to visualize your data
494  
495 =item L<PDL::Graphics::Prima::ReadLine|PDL::Graphics::Prima::ReadLine/>
496  
497 Encapsulates all interaction with the L<Term::ReadLine> family of
498 modules.
499  
500 =item L<PDL::Graphics::Prima::Scaling|PDL::Graphics::Prima::Scaling/>
501  
502 Specifies different kinds of scaling, including linear and logarithmic
503  
504 =item L<PDL::Graphics::Prima::Simple|PDL::Graphics::Prima::Simple/>
505  
506 Defines a number of useful functions for generating simple and not-so-simple
507 plots
508  
509 =back
510  
511 =head1 LICENSE AND COPYRIGHT
512  
513 Unless otherwise stated, all contributions in code and documentation are
514 copyright (c) their respective authors, all rights reserved.
515  
516 Portions of this module's code are copyright (c) 2011 The Board of
517 Trustees at the University of Illinois.
518  
519 Portions of this module's code are copyright (c) 2011-2013 Northwestern
520 University.
521  
522 Portions of this module's code are copyright (c) 2013-2014 Dickinson
523 College.
524  
525 This module's documentation is copyright (c) 2011-2014 David Mertens.
526  
527 This module is free software; you can redistribute it and/or
528 modify it under the same terms as Perl itself.
529  
530 =cut