blib/lib/Data/DownSample/LargestTriangleThreeBuckets.pm

filter: Statementstmt 12.5% 13 / 104 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 57.1% 4 / 7 Podpod 66.6% 2 / 3 totaltotal 12.5% 19 / 151 CC 15 SCAR 36.7
CC
15
Cov
10%
lttb
56.0
lttb_ref
56.0
new
12.0
CRAP 39.2
↑ Summary
1 package Data::DownSample::LargestTriangleThreeBuckets;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '1.00';
10
11 sub new
12 0 ▶{
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
13 0 my $caller = shift;
14  
15 # In case someone wants to sub-class
16 0 my $caller_is_obj = ref($caller);
17 0 ▶ my $class = $caller_is_obj || $caller;
Condition0%0/3
$caller_is_obj || $caller
l!l&&r!l&&!r
000
MC/DC0%0/2
$caller_is_obj || $caller
$caller_is_obj $caller
Truth table0%0/3
A || B
A= $caller_is_objB= $caller
ABresult
1X1
011
000
18  
19 # Passing reference or hash
20 0 my $arg_ref;
21 0 ▶ if ( ref($_[0]) eq "HASH" ) { $arg_ref = shift @_ }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
22 0 else { $arg_ref = { shift @_ } }
23  
24 # The object data structure
25 my $self = bless {
26 'threshold' => $arg_ref->{threshold},
27 0 }, $class;
28
29 0 return $self;
30 }
31  
32 sub lttb
33 0 ▶{
Subroutine lttb: not called CC=7 SCAR=40.3
Pod documented
34 0 my $self = shift @_;
35 0 my $data_ref = shift @_;
36 0 ▶ my $threshold = shift @_ || $self->{threshold};
Condition0%0/3
shift @_ || $self->{'threshold'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @_ || $self->{'threshold'}
shift @_ $self->{'threshold'}
Truth table0%0/3
A || B
A= shift @_B= $self->{'threshold'}
ABresult
1X1
011
000
37
38 # Validate input...
39 # ...tbd...
40
41 # Bucket size. Leave room for start and end data points
42 0 my $data_len = scalar @$data_ref;
43 0 my $every = ( $data_len - 2) / ( $threshold - 2 );
44  
45 0 my $a_pnt = 0; # Initially a is the first point in the triangle
46 0 my $next_a_pnt = 0;
47 0 my $max_area_point = (0,0);
48  
49 0 my $sampled_ref = [ $data_ref->[0] ]; # Always add the first point
50  
51 # ie c bucket
52 0 my $avg_range_start = int( floor($every) + 1);
53 0 my $avg_range_end = int( floor(2 * $every) + 1);
54  
55 # Get the range for this bucket - ie b bucket
56 0 my $range_offs = 1;
57 0 my $range_to = int( floor( $every + 1));
58  
59 0 ▶ for ( my $inx = 0; $inx < $threshold - 2; $inx++)
Branch: 1 branch
BranchTrueFalseTotal
if ($inx < $threshold - 2) 0 0 0
60 {
61 # Calculate point average for next bucket (containing c)
62 0 my $avg_x = 0;
63 0 my $avg_y = 0;
64
65 0 ▶ if ( $avg_range_end >= $data_len ) { $avg_range_end = $data_len }
Branch: 1 branch
BranchTrueFalseTotal
if ($avg_range_end >= $data_len) 0 0 0
66  
67 0 my $avg_range_length = $avg_range_end - $avg_range_start;
68  
69 0 ▶ for ( ; $avg_range_start < $avg_range_end; $avg_range_start++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($avg_range_start < $avg_range_end) 0 0 0
70 {
71 0 $avg_x += $data_ref->[ $avg_range_start ]->[ 0 ];
72 0 $avg_y += $data_ref->[ $avg_range_start ]->[ 1 ];
73 }
74  
75 0 $avg_x /= $avg_range_length;
76 0 $avg_y /= $avg_range_length;
77  
78 # Point a
79 0 my $point_ax = $data_ref->[$a_pnt]->[0];
80 0 my $point_ay = $data_ref->[$a_pnt]->[1];
81  
82 0 my $max_area = -1;
83  
84 0 my $diff_ax__avg_x = $point_ax - $avg_x;
85 0 my $diff_avg_y__ay = $avg_y - $point_ay;
86 0 ▶ while ( $range_offs < $range_to )
Branch: 1 branch
BranchTrueFalseTotal
if ($range_offs < $range_to) 0 0 0
87 {
88 # Calculate triangle area over three buckets
89 0 my $area = abs( ( $diff_ax__avg_x ) * ( $data_ref->[$range_offs][1] - $point_ay ) -
90 ( $point_ax - $data_ref->[$range_offs][0]) * ( $diff_avg_y__ay ) );
91
92 0 ▶ if ( $area > $max_area )
Branch: 1 branch
BranchTrueFalseTotal
if ($area > $max_area) 0 0 0
93 {
94 0 $max_area = $area;
95 0 $max_area_point = $data_ref->[$range_offs];
96 0 $next_a_pnt = $range_offs; # Next a is this b
97 }
98 0 $range_offs += 1;
99 }
100
101 0 push @$sampled_ref, $max_area_point; # Pick this point from the bucket
102 0 $a_pnt = $next_a_pnt; # This a is the next a (chosen b)
103
104 0 $range_offs = $range_to;
105 0 $avg_range_start = $avg_range_end;
106 0 $range_to = $avg_range_end;
107 0 $avg_range_end = int( floor(($inx+3) * $every) + 1);
108
109 }
110
111 0 push @$sampled_ref, $data_ref->[$data_len - 1]; # Always add last
112  
113 0 return $sampled_ref;
114 }
115  
116 # ------------------------------------
117 sub lttb_ref
118 0 ▶{
Subroutine lttb_ref: not called CC=7 SCAR=40.3
Pod documented
119 0 my $self = shift @_;
120 0 my $data_ref = shift @_;
121 0 ▶ my $threshold = shift @_ || $self->{threshold};
Condition0%0/3
shift @_ || $self->{'threshold'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @_ || $self->{'threshold'}
shift @_ $self->{'threshold'}
Truth table0%0/3
A || B
A= shift @_B= $self->{'threshold'}
ABresult
1X1
011
000
122
123 # Validate input...
124 # ...tbd...
125
126 # Bucket size. Leave room for start and end data points
127 0 my $data_len = scalar @$data_ref;
128 0 my $every = ( $data_len - 2) / ( $threshold - 2 );
129  
130 0 my $a_pnt = 0; # Initially a is the first point in the triangle
131 0 my $next_a_pnt = 0;
132 0 my $max_area_point = (0,0);
133  
134 0 my $sampled_ref = [ $data_ref->[0] ]; # Always add the first point
135  
136 0 ▶ for ( my $inx = 0; $inx < $threshold - 2; $inx++)
Branch: 1 branch
BranchTrueFalseTotal
if ($inx < $threshold - 2) 0 0 0
137 {
138 # Calculate point average for next bucket (containing c)
139 0 my $avg_x = 0;
140 0 my $avg_y = 0;
141 0 my $avg_range_start = int( floor(($inx+1) * $every) + 1);
142 0 my $avg_range_end = int( floor(($inx+2) * $every) + 1);
143
144 0 ▶ if ( $avg_range_end >= $data_len ) { $avg_range_end = $data_len }
Branch: 1 branch
BranchTrueFalseTotal
if ($avg_range_end >= $data_len) 0 0 0
145  
146 0 my $avg_range_length = $avg_range_end - $avg_range_start;
147
148 0 ▶ for ( ; $avg_range_start < $avg_range_end; $avg_range_start++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($avg_range_start < $avg_range_end) 0 0 0
149 {
150 0 $avg_x += $data_ref->[ $avg_range_start ]->[ 0 ];
151 0 $avg_y += $data_ref->[ $avg_range_start ]->[ 1 ];
152 }
153  
154 0 $avg_x /= $avg_range_length;
155 0 $avg_y /= $avg_range_length;
156  
157 # Get the range for this bucket
158 0 my $range_offs = int( floor(( $inx+0 ) * $every) + 1);
159 0 my $range_to = int( floor(( $inx+1 ) * $every) + 1);
160  
161 # Point a
162 0 my $point_ax = $data_ref->[$a_pnt]->[0];
163 0 my $point_ay = $data_ref->[$a_pnt]->[1];
164  
165 0 my $max_area = -1;
166  
167 0 ▶ while ( $range_offs < $range_to )
Branch: 1 branch
BranchTrueFalseTotal
if ($range_offs < $range_to) 0 0 0
168 {
169 # Calculate triangle area over three buckets
170 0 my $area = abs( ( $point_ax - $avg_x) * ( $data_ref->[$range_offs][1] - $point_ay ) -
171 ( $point_ax - $data_ref->[$range_offs][0]) * ( $avg_y - $point_ay))*0.5;
172
173 0 ▶ if ( $area > $max_area )
Branch: 1 branch
BranchTrueFalseTotal
if ($area > $max_area) 0 0 0
174 {
175 0 $max_area = $area;
176 0 $max_area_point = $data_ref->[$range_offs];
177 0 $next_a_pnt = $range_offs; # Next a is this b
178 }
179 0 $range_offs += 1;
180 }
181
182 0 push @$sampled_ref, $max_area_point; # Pick this point from the bucket
183 0 $a_pnt = $next_a_pnt; # This a is the next a (chosen b)
184 }
185
186 0 push @$sampled_ref, $data_ref->[$data_len - 1]; # Always add last
187  
188 0 return $sampled_ref;
189 }
190
191 #__data_ref__
192  
193 =head1 NAME
194  
195 Data::DownSample::LargestTriangleThreeBuckets
196  
197 =head1 VERSION
198  
199 Version 1.00
200  
201 =cut
202  
203 =head1 DESCRIPTION
204  
205 Implements a downsample technique known as Largest Triangle Three Buckets as defined
206 in Sveinn Steinarsson MS thesis.
207  
208 http://skemman.is/stream/get/1946/15343/37285/3/SS_MSthesis.pdf
209  
210 The idea is to downsample a data set without losing the visual
211 character of the plotted data. The technique draws on ideas in cartographic
212 generalization or polyline simplification. This technique is often useful in
213 client-server applications such as webserver-browser where the length of the data far exceeds the pixels available to
214 plot. Reducing the data size significantly speeds up data transfer and rendering time.
215  
216 =head1 SYNOPSIS
217
218 use Data::DownSample::LargestTriangleThreeBuckets;
219  
220 my $lttb = new Data::DownSample::LargestTriangleThreeBuckets( {threshold=>1000} );
221  
222 my $data_src = [ [1,2], [2,3], [3,4], [4,5], ... ]; # <-- load your data source here as reference to a list of a list
223  
224 my $data_sampled = $lttb->lttb($data_src);
225
226
227 =head1 SUBROUTINES/METHODS
228  
229 =head2 lttb()
230  
231 my $data_sampled = $lttb->lttb($data_src);
232  
233 Also can overwrite the class threashold variable such as
234  
235 my $data_sampled = $lttb->lttb($data_src,20000);
236  
237 This function is an optimized version of the algorithm given in Sveinn Steinarsson paper.
238 Several of the math operation are factored out of the loops and it executes better than
239 33% faster.
240  
241 =head2 lttb_ref()
242  
243 my $data_sampled = $lttb->lttb_ref($data_src);
244  
245 The reference function implimentation of the algorithm given in
246 Sveinn Steinarsson paper and used to verify that the optimized version
247 is giving correct results.
248  
249 =head1 AUTHOR
250  
251 Steve Troxel, C<< <troxel at perlworks.com> >>
252  
253 =head1 BUGS
254  
255 Please report any bugs or feature requests to C<bug-data-downsample-largesttrianglethreebuckets at rt.cpan.org>, or through
256 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-DownSample-LargestTriangleThreeBuckets>. I will be notified, and then you'll
257 automatically be notified of progress on your bug as I make changes.
258  
259 =head1 SUPPORT
260  
261 You can find documentation for this module with the perldoc command.
262  
263 perldoc Data::DownSample::LargestTriangleThreeBuckets
264  
265  
266 You can also look for information at:
267  
268 =over 4
269  
270 =item * RT: CPAN's request tracker (report bugs here)
271  
272 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-DownSample-LargestTriangleThreeBuckets>
273  
274 =item * AnnoCPAN: Annotated CPAN documentation
275  
276 L<http://annocpan.org/dist/Data-DownSample-LargestTriangleThreeBuckets>
277  
278 =item * CPAN Ratings
279  
280 L<http://cpanratings.perl.org/d/Data-DownSample-LargestTriangleThreeBuckets>
281  
282 =item * Search CPAN
283  
284 L<http://search.cpan.org/dist/Data-DownSample-LargestTriangleThreeBuckets/>
285  
286 =back
287  
288  
289 =head1 LICENSE AND COPYRIGHT
290  
291 Copyright 2016 Steve Troxel.
292  
293 This program is free software; you can redistribute it and/or modify it
294 under the terms of the the Artistic License (2.0). You may obtain a
295 copy of the full license at:
296  
297 L<http://www.perlfoundation.org/artistic_license_2_0>
298  
299 Any use, modification, and distribution of the Standard or Modified
300 Versions is governed by this Artistic License. By using, modifying or
301 distributing the Package, you accept this license. Do not use, modify,
302 or distribute the Package, if you do not accept this license.
303  
304 If your Modified Version has been derived from a Modified Version made
305 by someone other than you, you are nevertheless required to ensure that
306 your Modified Version complies with the requirements of this license.
307  
308 This license does not grant you the right to use any trademark, service
309 mark, tradename, or logo of the Copyright Holder.
310  
311 This license includes the non-exclusive, worldwide, free-of-charge
312 patent license to make, have made, use, offer to sell, sell, import and
313 otherwise transfer the Package with respect to any patent claims
314 licensable by the Copyright Holder that are necessarily infringed by the
315 Package. If you institute patent litigation (including a cross-claim or
316 counterclaim) against any party alleging that the Package constitutes
317 direct or contributory patent infringement, then this Artistic License
318 to you shall terminate on the date that such litigation is filed.
319  
320 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
321 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
322 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
323 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
324 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
325 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
326 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
327 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
328  
329  
330 =cut
331  
332 1 1; # End of Data::DownSample::LargestTriangleThreeBuckets
↑ Summary