blib/lib/Text/NumericData/App/txdcontract.pm

filter: Statementstmt 82.9% 102 / 123 Branchbran 74.1% 46 / 62 Conditioncond 66.6% 10 / 15 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 6 totaltotal 76.6% 171 / 223 SCAR 39.4
CC
38
Cov
79%
process_data
33.6
preinit
8.9
process_first_data
7.1
CRAP 51.4
« blib/lib/Text/NumericData/Calc.pm ↑ Summary blib/lib/Text/NumericData/App/txdnorm.pm »
1 package Text::NumericData::App::txdcontract;
2  
3 1 ▶use Text::NumericData::App;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # This is just a placeholder because of a past build system bug.
8 # The one and only version for Text::NumericData is kept in
9 # the Text::NumericData module itself.
10 1 our $VERSION = '1';
11 1 $VERSION = eval $VERSION;
12  
13 #the infostring says it all
14 1 my $infostring = 'contract a data file by computing the mean over n input rows to produce one output row
15  
16 txdcontract n < 100rows.dat > 50rows.dat
17  
18 A partial bin at the end is dropped. When you choose value-based binning, there
19 will always be some data dropped at the end. That way, you only get mean values
20 that actually represent a full interval/bin. Furthermore, the input data is
21 assumed to be sorted according to the column chosen for value-based binning.';
22  
23 1 our @ISA = ('Text::NumericData::App');
24  
25 1 my $stats_stddev = 1;
26 1 my $stats_minmax = 2;
27  
28 sub new
29 {
30 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
31 1 my @pars = (
32 'bincol', '', 'c'
33 , 'bin by values in given column instead of contracting by row count'
34 , 'binsize', 1, 'b', 'size of one bin'
35 , 'stats', 0, 's', 'add columns with statistic values for the bins'
36 . " ($stats_stddev: standard deviation,"
37 . " $stats_minmax: min and max values, "
38 . ($stats_stddev|$stats_minmax).": stddev, min, max)"
39 );
40  
41 1 return $class->SUPER::new
42 ({
43 parconf =>
44 {
45 info=>$infostring # default version
46 # default author
47 # default copyright
48 }
49 ,pardef => \@pars
50 ,pipemode => 1
51 ,pipe_init => \&preinit
52 ,pipe_begin => \&init
53 ,pipe_data => \&process_data
54 ,pipe_header => \&process_header
55 ,pipe_first_data => \&process_first_data
56 });
57 }
58  
59 sub preinit
60 {
61 4 ▶ my $self = shift;
Subroutine preinit: called CC=5 SCAR=21.9
Pod undocumented
62 4 my $n = shift(@{$self->{argv}});
63 4 my $bincol = int($self->{param}{bincol});
64 4 ▶ if($bincol > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($bincol > 0) { } 0 4 4
65 {
66 0 $self->{bincol} = $bincol-1;
67 0 ▶ unless($self->{param}{binsize} > 0)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"param"}{"binsize"} > 0) 0 0 0
68 {
69 0 print STDERR "Need positive binsize!\n";
70 0 return -1;
71 }
72 0 $self->{binsize} = 0+$self->{param}{binsize};
73 }
74 else
75 {
76 4 ▶ unless(defined $n)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 0 4 4
77 {
78 0 print STDERR "Need n as argument!\n";
79 0 return -1;
80 }
81 4 ▶ unless($n>0)
Branch: 1 branch
BranchTrueFalseTotal
unless ($n > 0) 0 4 4
82 {
83 0 print STDERR "Need n>0!\n";
84 0 return -1;
85 }
86 4 $self->{n} = int($n);
87 }
88 4 return 0;
89 }
90  
91 sub init
92 {
93 4 ▶ my $self = shift;
Subroutine init: called CC=2 SCAR=7.0
Pod undocumented
94  
95 4 $self->new_txd();
96 4 $self->{binval} = undef;
97 4 $self->{mean} = [];
98 4 $self->{binbuffer} = [];
99 4 ▶ $self->{meancount} = defined $self->{n} ? $self->{n} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'n'} ? : 4 0 4
100 4 $self->{ln} = 0;
101 4 $self->{sline} = '';
102 }
103  
104 # Delay header printout for processing column headers.
105 sub process_header
106 {
107 8 ▶ my $self = shift;
Subroutine process_header: called CC=1 SCAR=0.0
Pod undocumented
108 8 my $sline = $_[0];
109 8 $_[0] = $self->{sline};
110 8 $self->{sline} = $sline;
111 }
112  
113 # Append stats titles. This is rather convoluted, I need to make this nicer.
114 sub process_first_data
115 {
116 4 ▶ my $self = shift;
Subroutine process_first_data: called CC=7 SCAR=19.5
Pod undocumented
117 4 my $txd = $self->{txd};
118 4 my $data = $txd->line_data($_[0]);
119 4 ▶ if(@{$self->{txd}{titles}})
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'txd'}{'titles'};}) { } 4 0 4
120 {
121 4 my $cols = @{$data};
122 4 my $devi = $cols;
123 4 ▶ my $mini = $self->{param}{stats} & $stats_stddev ? $devi+$cols : $cols;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'param'}{'stats'} & $stats_stddev ? : 2 2 4
124 4 ▶ for(my $i=0; $i<$cols; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $cols) 12 4 16
125 {
126 12 ▶ my $tit = defined $txd->{titles}[$i] ? $txd->{titles}[$i] : ($i+1);
Branch: 1 branch
BranchTrueFalseTotal
defined $txd->{'titles'}[$i] ? : 12 0 12
127 $txd->{titles}[$devi+$i] = 'dev:'.$tit
128 12 ▶ if($self->{param}{stats} & $stats_stddev);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"param"}{"stats"} & $stats_stddev 6 6 12
129 12 ▶ if($self->{param}{stats} & $stats_minmax)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"param"}{"stats"} & $stats_minmax) 6 6 12
130 {
131 6 $txd->{titles}[$mini+$i] = 'min:'.$tit;
132 6 $txd->{titles}[$mini+$cols+$i] = 'max:'.$tit;
133 }
134 }
135 4 return $self->{txd}->title_line();
136 }
137 0 else{ return \$self->{sline}; }
138 }
139  
140 sub process_data
141 {
142 16 ▶ my $self = shift;
Subroutine process_data: called CC=27 SCAR=35.2
Pod undocumented
143 16 my $data = $self->{txd}->line_data($_[0]);
144 16 my $bin_finished;
145 my $binval;
146 16 $_[0] = '';
147 return
148 16 ▶ unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 16 16
149  
150 # Line-based binning knows already where the boundary is, can
151 # compute before output.
152 16 ▶ if(defined $self->{n})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'n'}) { } 16 0 16
153 {
154 16 ▶ for(my $i = 0; $i <= $#{$data}; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$data;}) 48 16 64
155 {
156 48 $self->{mean}[$i] += $data->[$i];
157 }
158 12 push(@{$self->{binbuffer}}, [@{$data}])
159 16 ▶ if($self->{param}{stats});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"param"}{"stats"} 12 4 16
160 16 ++$self->{ln};
161 }
162 else # Value-based binning needs to figure out if we crossed the border.
163 {
164 $binval = sprintf('%.0f', $data->[$self->{bincol}]/$self->{binsize})
165 0 * $self->{binsize};
166 0 ▶ $bin_finished = (@{$self->{mean}} and $binval != $self->{binval});
Condition0%0/3
@{$self->{'mean'};} && $binval != $self->{'binval'}
!ll&&!rl&&r
000
MC/DC0%0/2
@{$self->{'mean'};} && $binval != $self->{'binval'}
@{$self->{'mean'};} $binval != $self->{'binval'}
Truth table0%0/3
A && B
A= @{$self->{'mean'};}B= $binval != $self->{'binval'}
ABresult
0X0
100
111
167 }
168  
169 16 ▶ if($bin_finished or (defined $self->{n} and $self->{ln} == $self->{n}))
Branch: 1 branch
BranchTrueFalseTotal
if ($bin_finished or defined $self->{"n"} and $self->{"ln"} == $self->{"n"}) 8 8 16
Condition66%4/6
$bin_finished or defined $self->{"n"} and $self->{"ln"} == $self->{"n"}
l!l&&r!l&&!r
088
defined $self->{"n"} and $self->{"ln"} == $self->{"n"}
!ll&&!rl&&r
088
MC/DC33%1/3
$bin_finished or defined $self->{"n"} and $self->{"ln"} == $self->{"n"}
$bin_finished defined $self->{"n"} $self->{"ln"} == $self->{"n"}
Truth table50%2/4
A or B and C
A= $bin_finishedB= defined $self->{"n"}C= $self->{"ln"} == $self->{"n"}
ABCresult
1XX1
0111
00X0
0100
170 {
171 8 my @outdata;
172 8 for(@{$self->{mean}})
173 {
174 24 push(@outdata, $_ /= $self->{ln});
175 }
176 8 ▶ if($self->{param}{stats} & $stats_stddev)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"param"}{"stats"} & $stats_stddev) 4 4 8
177 {
178 4 my @sum;
179 4 ▶ for(my $d=0; $d<$self->{ln}; ++$d)
Branch: 1 branch
BranchTrueFalseTotal
if ($d < $self->{"ln"}) 8 4 12
180 {
181 8 ▶ for(my $i=0; $i<@{$self->{mean}}; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"mean"};}) 24 8 32
182 {
183 24 $sum[$i] += ($self->{binbuffer}[$d][$i] - $self->{mean}[$i])**2;
184 }
185 }
186 4 ▶ for(my $i=0; $i<@{$self->{mean}}; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"mean"};}) 12 4 16
187 {
188 12 push(@outdata, sqrt($sum[$i]/$self->{ln}));
189 }
190 }
191 8 ▶ if($self->{param}{stats} & $stats_minmax)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"param"}{"stats"} & $stats_minmax) 4 4 8
192 {
193 4 my @min;
194 my @max;
195 4 ▶ for(my $d=0; $d<$self->{ln}; ++$d)
Branch: 1 branch
BranchTrueFalseTotal
if ($d < $self->{"ln"}) 8 4 12
196 {
197 8 ▶ for(my $i=0; $i<@{$self->{mean}}; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"mean"};}) 24 8 32
198 {
199 $max[$i] = $self->{binbuffer}[$d][$i]
200 if( not defined $max[$i] or
201 24 ▶ $self->{binbuffer}[$d][$i] > $max[$i] );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $max[$i] or $self->{"binbuffer"}[$d][$i] > $max[$i] 14 10 24
Condition100%3/3
not defined $max[$i] or $self->{"binbuffer"}[$d][$i] > $max[$i]
l!l&&r!l&&!r
12210
MC/DC100%2/2
not defined $max[$i] or $self->{"binbuffer"}[$d][$i] > $max[$i]
not defined $max[$i] $self->{"binbuffer"}[$d][$i] > $max[$i]
Truth table100%3/3
not(A) or B
A= not defined $max[$i]B= $self->{"binbuffer"}[$d][$i] > $max[$i]
ABresult
1X1
011
000
202 $min[$i] = $self->{binbuffer}[$d][$i]
203 if( not defined $min[$i] or
204 24 ▶ $self->{binbuffer}[$d][$i] < $min[$i] );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $min[$i] or $self->{"binbuffer"}[$d][$i] < $min[$i] 22 2 24
Condition100%3/3
not defined $min[$i] or $self->{"binbuffer"}[$d][$i] < $min[$i]
l!l&&r!l&&!r
12102
MC/DC100%2/2
not defined $min[$i] or $self->{"binbuffer"}[$d][$i] < $min[$i]
not defined $min[$i] $self->{"binbuffer"}[$d][$i] < $min[$i]
Truth table100%3/3
not(A) or B
A= not defined $min[$i]B= $self->{"binbuffer"}[$d][$i] < $min[$i]
ABresult
1X1
011
000
205 }
206 }
207 # Loop ensures that we really have the given amount of entries,
208 # even if buffer was empty.
209 4 ▶ for(my $i=0; $i<@{$self->{mean}}; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"mean"};}) 12 4 16
210 {
211 12 push(@outdata, $min[$i]);
212 }
213 4 ▶ for(my $i=0; $i<@{$self->{mean}}; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"mean"};}) 12 4 16
214 {
215 12 push(@outdata, $max[$i]);
216 }
217 }
218  
219 $outdata[$self->{bincol}] = $self->{binval}
220 8 ▶ if $bin_finished;
Branch: 1 branch
BranchTrueFalseTotal
if $bin_finished 0 8 8
221 8 $_[0] = ${$self->{txd}->data_line(\@outdata)};
222 8 @{$self->{mean}} = ();
223 8 @{$self->{binbuffer}} = ();
224 8 $self->{ln} = 0;
225 }
226  
227 # Need to add bin values after output. Slightly twisted logic compared to
228 # line-based binning.
229 16 ▶ unless(defined $self->{n})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"n"}) 0 16 16
230 {
231 0 $self->{binval} = $binval;
232 0 ▶ for(my $i = 0; $i <= $#{$data}; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$data;}) 0 0 0
233 {
234 0 $self->{mean}[$i] += $data->[$i];
235 }
236 0 push(@{$self->{binbuffer}}, [@{$data}])
237 0 ▶ if($self->{param}{stats});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"param"}{"stats"} 0 0 0
238 0 ++$self->{ln};
239 }
240 }
241  
242 1 1;
« blib/lib/Text/NumericData/Calc.pm ↑ Summary blib/lib/Text/NumericData/App/txdnorm.pm »