| 1 | package Data::Decimate; | ||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Exporter qw/import/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | our @EXPORT_OK = qw(decimate); | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | Data::Decimate - A module that allows to decimate a data feed. | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | use Data::Decimate qw(decimate); | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | my @data = ( | ||||||||||||||||||||||||||||||||||||||||||
| 21 | {epoch => 1479203101, | ||||||||||||||||||||||||||||||||||||||||||
| 22 | #... | ||||||||||||||||||||||||||||||||||||||||||
| 23 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 24 | {epoch => 1479203102, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | #... | ||||||||||||||||||||||||||||||||||||||||||
| 26 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | {epoch => 1479203103, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | #... | ||||||||||||||||||||||||||||||||||||||||||
| 29 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | #... | ||||||||||||||||||||||||||||||||||||||||||
| 31 | {epoch => 1479203114, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | #... | ||||||||||||||||||||||||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | {epoch => 1479203117, | ||||||||||||||||||||||||||||||||||||||||||
| 35 | #... | ||||||||||||||||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | {epoch => 1479203118, | ||||||||||||||||||||||||||||||||||||||||||
| 38 | #... | ||||||||||||||||||||||||||||||||||||||||||
| 39 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 40 | #... | ||||||||||||||||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | my $output = Data::Decimate::decimate(15, \@data); | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | #epoch=1479203114 , decimate_epoch=1479203115 | ||||||||||||||||||||||||||||||||||||||||||
| 46 | print $output->[0]->{epoch}; | ||||||||||||||||||||||||||||||||||||||||||
| 47 | print $output->[0]->{decimate_epoch}; | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | A module that allows you to resample a data feed | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | =head2 decimate | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | Decimate a given data based on sampling frequency. | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | ▶ | sub decimate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decimate: called CC=9 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | my ($interval, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | if (not defined $interval or not defined $data or ref($data) ne "ARRAY") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not defined $interval && defined $data or ref $data ne "ARRAY"
defined $interval && defined $data
MC/DC0%0/3
not defined $interval && defined $data or ref $data ne "ARRAY"
defined $interval
defined $data
ref $data ne "ARRAY"
Truth table25%1/4
not(A && B) or C
A= defined $intervalB= defined $dataC= ref $data ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | die "interval and data are required parameters."; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | my @res; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | my $el = $data->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | my $decimate_epoch; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | ▶ | $decimate_epoch = do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | ▶ | use integer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | (($el->{epoch} + $interval - 1) / $interval) * $interval; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } if $data->[0]; | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | $el->{count} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | $el->{decimate_epoch} = $decimate_epoch; | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | ▶ | push @res, $el if $data->[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 2 | ▶ | for (my $i = 1; $i < @$data; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 268 | $el = $data->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 268 | $decimate_epoch = do { | |||||||||||||||||||||||||||||||||||||||||
| 88 | 2 | ▶ | use integer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 268 | (($el->{epoch} + $interval - 1) / $interval) * $interval; | |||||||||||||||||||||||||||||||||||||||||
| 90 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | # same decimate_epoch | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 268 | ▶ | if ($decimate_epoch == $res[-1]->{decimate_epoch}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 251 | $res[-1]->{count}++; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 251 | $el->{decimate_epoch} = $decimate_epoch; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 251 | $el->{count} = $res[-1]->{count}; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 251 | $res[-1] = $el; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 251 | next; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | # fill in the gaps if any | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 17 | ▶ | while ($res[-1]->{decimate_epoch} + $interval < $decimate_epoch) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | my %clone = %{$res[-1]}; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | $clone{count} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | $clone{decimate_epoch} += $interval; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | push @res, \%clone; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | # and finally add the current element | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 17 | $el->{count} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 17 | $el->{decimate_epoch} = $decimate_epoch; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 17 | push @res, $el; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 2 | return \@res; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | Binary.com, C<< <support at binary.com> >> | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | Please report any bugs or feature requests to C<bug-data-resample at rt.cpan.org>, or through | ||||||||||||||||||||||||||||||||||||||||||
| 125 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Decimate>. I will be notified, and then you'll | ||||||||||||||||||||||||||||||||||||||||||
| 126 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | perldoc Data::Decimate | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Decimate> | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | L<http://annocpan.org/dist/Data-Decimate> | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | =item * CPAN Ratings | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | L<http://cpanratings.perl.org/d/Data-Decimate> | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | =item * Search CPAN | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | L<http://search.cpan.org/dist/Data-Decimate/> | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||