line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::NumericData::App::txdcontract; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
72293
|
use Text::NumericData::App; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
36
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1357
|
|
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
|
|
|
|
|
|
|
our $VERSION = '1'; |
11
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
#the infostring says it all |
14
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
our @ISA = ('Text::NumericData::App'); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $stats_stddev = 1; |
26
|
|
|
|
|
|
|
my $stats_minmax = 2; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub new |
29
|
|
|
|
|
|
|
{ |
30
|
1
|
|
|
1
|
0
|
88
|
my $class = shift; |
31
|
1
|
|
|
|
|
11
|
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
|
|
|
|
|
16
|
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
|
|
|
4
|
0
|
7
|
my $self = shift; |
62
|
4
|
|
|
|
|
8
|
my $n = shift(@{$self->{argv}}); |
|
4
|
|
|
|
|
9
|
|
63
|
4
|
|
|
|
|
18
|
my $bincol = int($self->{param}{bincol}); |
64
|
4
|
50
|
|
|
|
9
|
if($bincol > 0) |
65
|
|
|
|
|
|
|
{ |
66
|
0
|
|
|
|
|
0
|
$self->{bincol} = $bincol-1; |
67
|
0
|
0
|
|
|
|
0
|
unless($self->{param}{binsize} > 0) |
68
|
|
|
|
|
|
|
{ |
69
|
0
|
|
|
|
|
0
|
print STDERR "Need positive binsize!\n"; |
70
|
0
|
|
|
|
|
0
|
return -1; |
71
|
|
|
|
|
|
|
} |
72
|
0
|
|
|
|
|
0
|
$self->{binsize} = 0+$self->{param}{binsize}; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
else |
75
|
|
|
|
|
|
|
{ |
76
|
4
|
50
|
|
|
|
11
|
unless(defined $n) |
77
|
|
|
|
|
|
|
{ |
78
|
0
|
|
|
|
|
0
|
print STDERR "Need n as argument!\n"; |
79
|
0
|
|
|
|
|
0
|
return -1; |
80
|
|
|
|
|
|
|
} |
81
|
4
|
50
|
|
|
|
14
|
unless($n>0) |
82
|
|
|
|
|
|
|
{ |
83
|
0
|
|
|
|
|
0
|
print STDERR "Need n>0!\n"; |
84
|
0
|
|
|
|
|
0
|
return -1; |
85
|
|
|
|
|
|
|
} |
86
|
4
|
|
|
|
|
9
|
$self->{n} = int($n); |
87
|
|
|
|
|
|
|
} |
88
|
4
|
|
|
|
|
10
|
return 0; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub init |
92
|
|
|
|
|
|
|
{ |
93
|
4
|
|
|
4
|
0
|
356
|
my $self = shift; |
94
|
|
|
|
|
|
|
|
95
|
4
|
|
|
|
|
21
|
$self->new_txd(); |
96
|
4
|
|
|
|
|
40
|
$self->{binval} = undef; |
97
|
4
|
|
|
|
|
9
|
$self->{mean} = []; |
98
|
4
|
|
|
|
|
8
|
$self->{binbuffer} = []; |
99
|
4
|
50
|
|
|
|
12
|
$self->{meancount} = defined $self->{n} ? $self->{n} : 0; |
100
|
4
|
|
|
|
|
9
|
$self->{ln} = 0; |
101
|
4
|
|
|
|
|
11
|
$self->{sline} = ''; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# Delay header printout for processing column headers. |
105
|
|
|
|
|
|
|
sub process_header |
106
|
|
|
|
|
|
|
{ |
107
|
8
|
|
|
8
|
0
|
14
|
my $self = shift; |
108
|
8
|
|
|
|
|
13
|
my $sline = $_[0]; |
109
|
8
|
|
|
|
|
14
|
$_[0] = $self->{sline}; |
110
|
8
|
|
|
|
|
16
|
$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
|
|
|
4
|
0
|
7
|
my $self = shift; |
117
|
4
|
|
|
|
|
8
|
my $txd = $self->{txd}; |
118
|
4
|
|
|
|
|
13
|
my $data = $txd->line_data($_[0]); |
119
|
4
|
50
|
|
|
|
8
|
if(@{$self->{txd}{titles}}) |
|
4
|
|
|
|
|
11
|
|
120
|
|
|
|
|
|
|
{ |
121
|
4
|
|
|
|
|
7
|
my $cols = @{$data}; |
|
4
|
|
|
|
|
7
|
|
122
|
4
|
|
|
|
|
7
|
my $devi = $cols; |
123
|
4
|
100
|
|
|
|
30
|
my $mini = $self->{param}{stats} & $stats_stddev ? $devi+$cols : $cols; |
124
|
4
|
|
|
|
|
13
|
for(my $i=0; $i<$cols; ++$i) |
125
|
|
|
|
|
|
|
{ |
126
|
12
|
50
|
|
|
|
29
|
my $tit = defined $txd->{titles}[$i] ? $txd->{titles}[$i] : ($i+1); |
127
|
|
|
|
|
|
|
$txd->{titles}[$devi+$i] = 'dev:'.$tit |
128
|
12
|
100
|
|
|
|
38
|
if($self->{param}{stats} & $stats_stddev); |
129
|
12
|
100
|
|
|
|
29
|
if($self->{param}{stats} & $stats_minmax) |
130
|
|
|
|
|
|
|
{ |
131
|
6
|
|
|
|
|
14
|
$txd->{titles}[$mini+$i] = 'min:'.$tit; |
132
|
6
|
|
|
|
|
18
|
$txd->{titles}[$mini+$cols+$i] = 'max:'.$tit; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
} |
135
|
4
|
|
|
|
|
20
|
return $self->{txd}->title_line(); |
136
|
|
|
|
|
|
|
} |
137
|
0
|
|
|
|
|
0
|
else{ return \$self->{sline}; } |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub process_data |
141
|
|
|
|
|
|
|
{ |
142
|
16
|
|
|
16
|
0
|
33
|
my $self = shift; |
143
|
16
|
|
|
|
|
44
|
my $data = $self->{txd}->line_data($_[0]); |
144
|
16
|
|
|
|
|
28
|
my $bin_finished; |
145
|
|
|
|
|
|
|
my $binval; |
146
|
16
|
|
|
|
|
27
|
$_[0] = ''; |
147
|
|
|
|
|
|
|
return |
148
|
16
|
50
|
|
|
|
37
|
unless defined $data; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# Line-based binning knows already where the boundary is, can |
151
|
|
|
|
|
|
|
# compute before output. |
152
|
16
|
50
|
|
|
|
48
|
if(defined $self->{n}) |
153
|
|
|
|
|
|
|
{ |
154
|
16
|
|
|
|
|
24
|
for(my $i = 0; $i <= $#{$data}; ++$i) |
|
64
|
|
|
|
|
131
|
|
155
|
|
|
|
|
|
|
{ |
156
|
48
|
|
|
|
|
96
|
$self->{mean}[$i] += $data->[$i]; |
157
|
|
|
|
|
|
|
} |
158
|
12
|
|
|
|
|
20
|
push(@{$self->{binbuffer}}, [@{$data}]) |
|
12
|
|
|
|
|
33
|
|
159
|
16
|
100
|
|
|
|
45
|
if($self->{param}{stats}); |
160
|
16
|
|
|
|
|
33
|
++$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
|
|
|
|
|
0
|
* $self->{binsize}; |
166
|
0
|
|
0
|
|
|
0
|
$bin_finished = (@{$self->{mean}} and $binval != $self->{binval}); |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
16
|
100
|
66
|
|
|
118
|
if($bin_finished or (defined $self->{n} and $self->{ln} == $self->{n})) |
|
|
|
66
|
|
|
|
|
170
|
|
|
|
|
|
|
{ |
171
|
8
|
|
|
|
|
15
|
my @outdata; |
172
|
8
|
|
|
|
|
21
|
for(@{$self->{mean}}) |
|
8
|
|
|
|
|
20
|
|
173
|
|
|
|
|
|
|
{ |
174
|
24
|
|
|
|
|
50
|
push(@outdata, $_ /= $self->{ln}); |
175
|
|
|
|
|
|
|
} |
176
|
8
|
100
|
|
|
|
23
|
if($self->{param}{stats} & $stats_stddev) |
177
|
|
|
|
|
|
|
{ |
178
|
4
|
|
|
|
|
7
|
my @sum; |
179
|
4
|
|
|
|
|
23
|
for(my $d=0; $d<$self->{ln}; ++$d) |
180
|
|
|
|
|
|
|
{ |
181
|
8
|
|
|
|
|
14
|
for(my $i=0; $i<@{$self->{mean}}; ++$i) |
|
32
|
|
|
|
|
72
|
|
182
|
|
|
|
|
|
|
{ |
183
|
24
|
|
|
|
|
52
|
$sum[$i] += ($self->{binbuffer}[$d][$i] - $self->{mean}[$i])**2; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
} |
186
|
4
|
|
|
|
|
8
|
for(my $i=0; $i<@{$self->{mean}}; ++$i) |
|
16
|
|
|
|
|
36
|
|
187
|
|
|
|
|
|
|
{ |
188
|
12
|
|
|
|
|
24
|
push(@outdata, sqrt($sum[$i]/$self->{ln})); |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
} |
191
|
8
|
100
|
|
|
|
24
|
if($self->{param}{stats} & $stats_minmax) |
192
|
|
|
|
|
|
|
{ |
193
|
4
|
|
|
|
|
10
|
my @min; |
194
|
|
|
|
|
|
|
my @max; |
195
|
4
|
|
|
|
|
13
|
for(my $d=0; $d<$self->{ln}; ++$d) |
196
|
|
|
|
|
|
|
{ |
197
|
8
|
|
|
|
|
11
|
for(my $i=0; $i<@{$self->{mean}}; ++$i) |
|
32
|
|
|
|
|
70
|
|
198
|
|
|
|
|
|
|
{ |
199
|
|
|
|
|
|
|
$max[$i] = $self->{binbuffer}[$d][$i] |
200
|
|
|
|
|
|
|
if( not defined $max[$i] or |
201
|
24
|
100
|
100
|
|
|
76
|
$self->{binbuffer}[$d][$i] > $max[$i] ); |
202
|
|
|
|
|
|
|
$min[$i] = $self->{binbuffer}[$d][$i] |
203
|
|
|
|
|
|
|
if( not defined $min[$i] or |
204
|
24
|
100
|
100
|
|
|
73
|
$self->{binbuffer}[$d][$i] < $min[$i] ); |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
# Loop ensures that we really have the given amount of entries, |
208
|
|
|
|
|
|
|
# even if buffer was empty. |
209
|
4
|
|
|
|
|
15
|
for(my $i=0; $i<@{$self->{mean}}; ++$i) |
|
16
|
|
|
|
|
31
|
|
210
|
|
|
|
|
|
|
{ |
211
|
12
|
|
|
|
|
23
|
push(@outdata, $min[$i]); |
212
|
|
|
|
|
|
|
} |
213
|
4
|
|
|
|
|
10
|
for(my $i=0; $i<@{$self->{mean}}; ++$i) |
|
16
|
|
|
|
|
34
|
|
214
|
|
|
|
|
|
|
{ |
215
|
12
|
|
|
|
|
21
|
push(@outdata, $max[$i]); |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
$outdata[$self->{bincol}] = $self->{binval} |
220
|
8
|
50
|
|
|
|
16
|
if $bin_finished; |
221
|
8
|
|
|
|
|
11
|
$_[0] = ${$self->{txd}->data_line(\@outdata)}; |
|
8
|
|
|
|
|
28
|
|
222
|
8
|
|
|
|
|
15
|
@{$self->{mean}} = (); |
|
8
|
|
|
|
|
18
|
|
223
|
8
|
|
|
|
|
11
|
@{$self->{binbuffer}} = (); |
|
8
|
|
|
|
|
18
|
|
224
|
8
|
|
|
|
|
20
|
$self->{ln} = 0; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
# Need to add bin values after output. Slightly twisted logic compared to |
228
|
|
|
|
|
|
|
# line-based binning. |
229
|
16
|
50
|
|
|
|
57
|
unless(defined $self->{n}) |
230
|
|
|
|
|
|
|
{ |
231
|
0
|
|
|
|
|
|
$self->{binval} = $binval; |
232
|
0
|
|
|
|
|
|
for(my $i = 0; $i <= $#{$data}; ++$i) |
|
0
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
{ |
234
|
0
|
|
|
|
|
|
$self->{mean}[$i] += $data->[$i]; |
235
|
|
|
|
|
|
|
} |
236
|
0
|
|
|
|
|
|
push(@{$self->{binbuffer}}, [@{$data}]) |
|
0
|
|
|
|
|
|
|
237
|
0
|
0
|
|
|
|
|
if($self->{param}{stats}); |
238
|
0
|
|
|
|
|
|
++$self->{ln}; |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
1; |