csvtool/Smudge.pm

filter: Statementstmt 89.0% 65 / 73 Branchbran 81.2% 13 / 16 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 95.0% 19 / 20 Podpod n/a totaltotal 83.6% 102 / 122 CC 17 SCAR 16.7
CC
17
Cov
85%
run
13.1
commandable_more_help
6.0
mk_filter
3.0
CRAP 5.3
↑ Summary csvtool.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2024 -- leonerd@leonerd.org.uk
5  
6 package App::csvtool::Smudge 0.05;
7  
8 16 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 C<App::csvtool::Smudge> - implements the F<csvtool> F<smudge> command
15  
16 =head1 COMMANDS
17  
18 =for highlighter
19  
20 =cut
21  
22 package App::csvtool::smudge
23 16 {
24  
25 =head2 smudge
26  
27 $ csvtool smudge -F IDX:FILTER INPUT...
28  
29 Applies smudging filters to columns of the input, generating new data that is
30 output in the same shape. A "smudge" filter is one that operates on numerical
31 data, attempting to reduce the imact of any individual values and smooth out
32 small variations, emitting values that follow general trends. This assumes
33 that successive rows of data represent successive moments in time, containing
34 measurements or readings taken at each instant.
35  
36 Different filters can be applied to individual columns, as specified by the
37 C<--filter> (or C<-F>) argument. Any columns that are not filtered are simply
38 copied as they stand, and thus do not even have to be numeric in nature.
39  
40 =head3 --filter, -F
41  
42 A filter specification to apply to a column of data. Specified as a string
43 giving the column index (starting from 1), and the name of the filter. May be
44 specified multiple times to apply multiple different filters for different
45 columns. C<IDX> may specify multiple field indexes, separated by commas.
46  
47 =cut
48  
49 16 ▶ use constant COMMAND_DESC => "Apply smudge filtering to columns of data";
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 16 ▶ use constant COMMAND_OPTS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
52 { name => "filter|F=", description => "filters to apply to each column",
53 multi => 1, },
54 16 );
55  
56 16 ▶ use constant WANT_READER => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 16 ▶ use constant WANT_OUTPUT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 16 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 16 ▶ use List::Util qw( sum );
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 =head2 FILTERS
64  
65 The following name templates may be used. Names are parametric,
66 and encode information about how the filter acts.
67  
68 =head3 avgI<NNN>
69  
70 Applies a moving window average over the previous I<NNN> values.
71  
72 =head3 midI<NNN>
73  
74 Sorts the previous I<NNN> values and returns the middle one. To be
75 well-behaved, N should be an odd number.
76  
77 =head3 ravgI<NNN>
78  
79 Recursive average with weighting of C<2 ** -NNN>.
80  
81 =head3 total
82  
83 Running total of every value seen so far.
84  
85 =cut
86  
87 my @FILTERS = (
88 {
89 name => "avgN",
90 desc => "N-point moving window average",
91 3 ▶ make => sub ( $count ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
92 3 my @hist;
93 15 ▶ return sub ( $new ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
94 15 push @hist, $new;
95 15 ▶ shift @hist while @hist > $count;
Branch: 1 branch
BranchTrueFalseTotal
@hist > $count and do { shift @hist } 7 15 22
96 15 return sum(@hist) / @hist;
97 3 };
98 },
99 },
100 {
101 name => "midN",
102 desc => "Median of N values",
103 1 ▶ make => sub ( $count ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
104 1 my @hist;
105 12 ▶ return sub ( $new ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
106 12 push @hist, $new;
107 12 ▶ shift @hist while @hist > $count;
Branch: 1 branch
BranchTrueFalseTotal
@hist > $count and do { shift @hist } 9 12 21
108 12 my @sorted = sort { $a <=> $b } @hist;
109 12 return $sorted[$#sorted/2];
110 1 };
111 }
112 },
113 {
114 name => "ravgN",
115 desc => "Recusive average with weighting 2 ** -N",
116 1 ▶ make => sub ( $param ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
117 1 my $alpha = 2 ** -$1;
118 1 my $prev;
119 11 ▶ return sub ( $new ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
120 11 ▶ return $prev = $new if !defined $prev;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prev 1 10 11
121 # $result = ( $prev * ( 1 - $alpha ) ) + ( $new * $alpha )
122 # = $prev * 1 - $prev * $alpha + $new * $alpha
123 10 return $prev = $prev + $alpha * ( $new - $prev );
124 1 };
125 }
126 },
127 {
128 name => "total",
129 desc => "Running total",
130 1 ▶ make => sub ( $ ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
131 1 my $total = 0;
132 6 ▶ return sub ( $new ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
133 6 $total += $new;
134 6 return $total;
135 }
136 1 }
137 },
138 16 );
139  
140 6 ▶ sub mk_filter ( $name )
Subroutine mk_filter: called CC=3 SCAR=11.1
141 {
142 6 foreach ( @FILTERS ) {
143 12 my $re = $_->{name} =~ s/N$/(\\d+)/r;
144 12 ▶ next unless $name =~ qr/^$re$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ qr/^$re$/u 6 6 12
145 6 return $_->{make}( $1 );
146 }
147  
148 0 croak "Unrecognised filter name $name";
149 }
150  
151 # For Commandable's builtin 'help' support
152 sub commandable_more_help
153 0 ▶ {
Subroutine commandable_more_help: not called CC=2 SCAR=17.9
154 0 Commandable::Output->printf( "\n" );
155 0 Commandable::Output->print_heading( "FILTERS:" );
156  
157 0 Commandable::Output->printf( " Each filter should be specified as IDX(,IDX...):FILTER\n" );
158 0 Commandable::Output->printf( "\n" );
159  
160 0 foreach ( @FILTERS ) {
161 Commandable::Output->printf( " %s\n",
162 0 Commandable::Output->format_note( $_->{name}, 1 ) );
163 Commandable::Output->printf( " %s\n",
164 0 $_->{desc} );
165 }
166 }
167  
168 6 ▶ sub run ( $pkg, $opts, $reader, $output )
Subroutine run: called CC=11 SCAR=25.7
169 {
170 6 my @filters;
171 6 ▶ foreach my $spec ( ( $opts->{filter} // [] )->@* ) {
Condition100%2/2
$opts->{"filter"} // []
l!l
51
MC/DC100%1/1
$opts->{"filter"} // []
$opts->{"filter"}
Truth table100%2/2
A
A= $opts->{"filter"}
Aresult
11
00
172 # TODO: Accept DD-DD,DD-DD,etc... as indexes
173 5 ▶ my ( $fields, $filter ) = $spec =~ m/^(\d+(?:,\d+)*):(.*)$/ or
Branch: 1 branch
BranchTrueFalseTotal
() = $spec =~ /^(\d+(?:,\d+)*):(.*)$/u or warn("Unrecognised --filter spec; expected IDX:FILTER\n"), next 5 0 5
174 warn( "Unrecognised --filter spec; expected IDX:FILTER\n" ), next;
175  
176 5 foreach my $idx ( split m/,/, $fields ) {
177 6 $filters[$idx - 1] = mk_filter( $filter );
178 }
179 }
180  
181 6 ▶ while( my $row = $reader->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = &$reader()) 42 6 48
182 42 my @data = @$row;
183  
184 # Skip header lines
185 42 ▶ unless( @data and $data[0] =~ m/^#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@data and $data[0] =~ /^#/u) 42 0 42
Condition33%1/3
@data and $data[0] =~ /^#/u
!ll&&!rl&&r
0420
MC/DC0%0/2
@data and $data[0] =~ /^#/u
@data $data[0] =~ /^#/u
Truth table33%1/3
A and B
A= @dataB= $data[0] =~ /^#/u
ABresult
0X0
100
111
186 42 foreach my $idx ( keys @filters ) {
187 44 ▶ length $data[$idx] and defined $filters[$idx] and
Branch: 1 branch
BranchTrueFalseTotal
length $data[$idx] and defined $filters[$idx] and $data[$idx] = $filters[$idx]->($data[$idx]) 44 0 44
Condition33%1/3
length $data[$idx] and defined $filters[$idx]
!ll&&!rl&&r
0044
MC/DC0%0/2
length $data[$idx] and defined $filters[$idx]
length $data[$idx] defined $filters[$idx]
Truth table33%1/3
A and B
A= length $data[$idx]B= defined $filters[$idx]
ABresult
0X0
100
111
188 $data[$idx] = $filters[$idx]->( $data[$idx] );
189 }
190 }
191  
192 42 $output->( \@data );
193 }
194 }
195 }
196  
197 =head1 AUTHOR
198  
199 Paul Evans <leonerd@leonerd.org.uk>
200  
201 =cut
202  
203 16 0x55AA;
↑ Summary csvtool.pm »