-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbucketize
More file actions
executable file
·212 lines (161 loc) · 4.88 KB
/
bucketize
File metadata and controls
executable file
·212 lines (161 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/perl
use strict;
use Getopt::Std;
use Statistics::Descriptive;
use POSIX qw(ceil floor);
my $usage = "Usage: cat file | bucketize [options] > output \n";
$usage .= " -s size of bucket (e.g. 10000)\n";
$usage .= " -l low end of bucket range\n";
$usage .= " -h high end of bucket range\n";
$usage .= " -z print zero when bucket is empty\n";
$usage .= " -r print range\n";
$usage .= " -S print statistics for each bucket\n";
$usage .= " -k column to use as key\n";
$usage .= " -v column to use as value (useful for option S)\n";
$usage .= " -c custom bucket sizes, comma separated, given on cmd line\n";
$usage .= " -n normalize outputs so that total of buckets is one\n";
$usage .= " Using c precludes using s and z\n";
$usage .= " l and h are useful for combining with z to produce a steady range\n";
my %buckets = ();
my %bucket_values = ();
my $size = 0;
my %para = ();
my $key_column = 0;
my $value_column = 1;
my $use_stats = 0;
getopts('s:zrcnl:h:k:v:S', \%para);
if ((!defined ($para{'s'})) && (!defined($para{'c'}))) {
die ($usage);
}
if (defined ($para{'z'}) && defined($para{'c'})) {
die ($usage);
}
if (defined ($para{'k'})) {
$key_column = $para{'k'};
}
if (defined ($para{'v'})) {
$value_column = $para{'v'};
}
if (defined ($para{'S'})) {
$use_stats = 1;
}
$size = $para{'s'};
my @bucketMax = ();
if (defined ($para{'c'})) {
@bucketMax = @ARGV;
}
my $sum = 0;
my $min_bucket = undef;
my $max_bucket = undef;
# grab the data
while (my $line = <STDIN>) {
next if ($line =~ /^\#/);
my (@data) = split (/\s/,$line);
my $key = $data[$key_column];
my $value = 1;
if ($use_stats) {
$value = $data[$value_column];
if (($value =~ /(-?\d+\.\d+)/) || ($value =~ /(-?\d+)/)) {
} else {
die ("Bad value $value line $line");
}
}
if (($key =~ /(-?\d+\.\d+)/) || ($key =~ /(-?\d+)/)) {
my $bucket = 0;
if (defined($para{'c'})) {
my $i = 0;
# this puts values below the lowest bucket into the lowest bucket
# and values gt the highest into the highest.
for (; $i <= $#bucketMax && $key > $bucketMax[$i]; $i++) {}
if (!defined ($bucketMax[$i])) {
$i--;
}
$bucket = $bucketMax[$i];
die if (!defined ($bucketMax[$i]));
} else {
$bucket = (floor ($key / $size)) * $size;
}
if ($use_stats) {
if (!defined($bucket_values{$bucket})) {
my @items = ();
$bucket_values{$bucket} = \@items;
}
my $items = $bucket_values{$bucket};
push (@$items, $value);
}
$buckets{$bucket}++;
$sum++;
if (!defined($min_bucket) || $min_bucket > $bucket) {
$min_bucket = $bucket;
}
if (!defined($max_bucket) || $max_bucket < $bucket) {
$max_bucket = $bucket;
}
#print "line $line val $key bucket $bucket bucket-count $buckets{$bucket}\n";
} else {
die ("Bad input line: $line\n");
}
}
if (defined($para{'l'})) {
$min_bucket = $para{'l'};
}
if (defined($para{'h'})) {
$max_bucket = $para{'h'};
}
#print "max $max_bucket\n";
# fill in missing buckets
if (defined($para{'z'})) {
for (my $i = $min_bucket; $i < $max_bucket+$size; $i += $size) {
if (!defined ($buckets{$i})) {
my $key = sprintf ("%.3f", $i);
$buckets{$key} = 0;
#print "adding bucket ".$key."\n";
}
}
}
# make the range the title if that is asked for
my %titles = ();
my (@names) = sort {$a <=> $b} keys %buckets;
for (my $i = 0; $i < scalar @names; $i++) {
my $name = $names[$i];
if (defined ($para{'r'})) {
if ($i != scalar @names - 1) {
$name .= "-$names[$i+1]";
} else {
$name = ">=$name";
}
}
$titles{$names[$i]} = $name;
}
# normalize
if (defined($para{'n'})) {
foreach my $bucket (@names) {
my $key = $buckets{$bucket};
my $pct = $key / $sum;
my $pctOut = sprintf ("%1.3f", $pct);
$buckets{$bucket} = $pctOut;
}
}
# print output
foreach my $bucket (@names) {
my $title = $titles{$bucket};
my $key = $buckets{$bucket};
if ($use_stats) {
my $items = $bucket_values{$bucket};
my $stat = Statistics::Descriptive::Full->new();
$stat->add_data(@$items);
my $out2 = sprintf
("%d %.3f %.3f %.3f %.3f %.3f %.3f %.3f",
$stat->count(), $stat->mean(), $stat->standard_deviation(),
$stat->min(),
$stat->percentile(25),
$stat->percentile(50),
$stat->percentile(75),
$stat->max());
my $out3 = $stat->count().' '.$stat->mean().' '.$stat->standard_deviation().' '.$stat->min().' '.$stat->percentile(25).' '.$stat->percentile(50).' '.$stat->percentile(75).' '.$stat->max();
my $out = $stat->count().' '.$stat->mean().' '.$stat->standard_deviation().' '.$stat->percentile(5).' '.$stat->percentile(25).' '.$stat->percentile(50).' '.$stat->percentile(75).' '.$stat->percentile(95);
print "$title $out\n";
} else {
print "$title $key\n";
}
}