-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplotty-histogram.py
executable file
·296 lines (277 loc) · 8.2 KB
/
plotty-histogram.py
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python3
"""plotty-histogram.py module description.
Produces a histogram from a column in a given CSV input file.
"""
import argparse
import numpy as np
import pandas as pd
import sys
__version__ = "0.1"
HISTOGRAM_TYPES = ("raw", "pdf", "cdf")
default_values = {
"debug": 0,
"dry_run": False,
"col": None,
"nbins": 100,
"range-min": None,
"range-max": None,
"header": True,
"zeroes": True,
"zeroes-strip": False,
"zeroes-coalesce": False,
"sigma": None,
"type": "raw",
"infile": None,
"outfile": None,
}
def calculate_histogram(options):
# read infile
indf = pd.read_csv(options.infile, header=0 if options.header else None)
# get the column
if isinstance(options.col, int):
column = indf.iloc[:, int(options.col)]
else:
column = indf.loc[:, options.col]
# remove NaNs
column = column.dropna()
# process the data
# get the histogram
density = True if (options.type in ("pdf", "cdf")) else False
range_min = options.range_min if options.range_min is not None else column.min()
range_max = options.range_max if options.range_max is not None else column.max()
hist, bin_edges = np.histogram(
column, bins=options.nbins, range=(range_min, range_max), density=density
)
# center the histogram (bin_edges points to the left side)
bin_edges = (bin_edges[1:] + bin_edges[:-1]) / 2
# add up the values for a CDF
if options.type == "cdf":
raise AssertionError("error: CDF mode unimplemented")
# create the output dataframe
outdf = pd.DataFrame({"value": bin_edges, "total": hist})
if not options.zeroes:
# remove zeroes
outdf = outdf[outdf["total"] != 0]
elif options.zeroes_strip:
first_element = outdf[outdf.total != 0].index[0]
last_element = outdf[outdf.total != 0].index[-1]
outdf = outdf.iloc[first_element:last_element]
elif options.zeroes_coalesce:
# remove all rows such that (a) its value for the column total is zero, and
# (b) the value for the column total is zero for both the top and bottom row
outdf = outdf[
(outdf.total != 0)
| (
(outdf.total == 0)
& ((outdf.total.shift() != 0) | (outdf.total.shift(-1) != 0))
)
]
# add ratios
total_occurrences = int(outdf.total.sum())
outdf["ratio"] = outdf.apply(lambda row: row["total"] / total_occurrences, axis=1)
# write outfile
outdf.to_csv(options.outfile, index=False)
def get_options(argv):
"""Generic option parser.
Args:
argv: list containing arguments
Returns:
Namespace - An argparse.ArgumentParser-generated option object
"""
# init parser
# usage = 'usage: %prog [options] arg1 arg2'
# parser = argparse.OptionParser(usage=usage)
# parser.print_help() to get argparse.usage (large help)
# parser.print_usage() to get argparse.usage (just usage line)
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"-v",
"--version",
action="store_true",
dest="version",
default=False,
help="Print version",
)
parser.add_argument(
"-d",
"--debug",
action="count",
dest="debug",
default=default_values["debug"],
help="Increase verbosity (use multiple times for more)",
)
parser.add_argument(
"--quiet",
action="store_const",
dest="debug",
const=-1,
help="Zero verbosity",
)
parser.add_argument(
"--dry-run",
action="store_true",
dest="dry_run",
default=default_values["dry_run"],
help="Dry run",
)
parser.add_argument(
"--col",
action="store",
dest="col",
default=default_values["col"],
metavar="COL",
help="use COL column",
)
parser.add_argument(
"--nbins",
action="store",
type=int,
dest="nbins",
default=default_values["nbins"],
metavar="NBINS",
help="use NBINS bins",
)
parser.add_argument(
"--range-min",
action="store",
type=float,
dest="range_min",
default=default_values["range-min"],
metavar="RANGE-MIN",
help="use RANGE-MIN as range min",
)
parser.add_argument(
"--range-max",
action="store",
type=float,
dest="range_max",
default=default_values["range-max"],
metavar="RANGE-MAX",
help="use RANGE-MAX as range max",
)
parser.add_argument(
"--header",
dest="header",
action="store_true",
default=default_values["header"],
help="Input file has CSV header%s"
% (" [default]" if default_values["header"] else ""),
)
parser.add_argument(
"--no-header",
dest="header",
action="store_false",
help="Input file does not have CSV header%s"
% (" [default]" if not default_values["header"] else ""),
)
parser.add_argument(
"--zeroes",
dest="zeroes",
action="store_true",
default=default_values["zeroes"],
help="Do not ignore zero elements%s"
% (" [default]" if default_values["zeroes"] else ""),
)
parser.add_argument(
"--no-zeroes",
dest="zeroes",
action="store_false",
help="Ignore zero elements%s"
% (" [default]" if not default_values["zeroes"] else ""),
)
parser.add_argument(
"--zeroes-strip",
dest="zeroes_strip",
action="store_true",
default=default_values["zeroes-strip"],
help="Strip zero elements at the left and right sides%s"
% (" [default]" if default_values["zeroes-strip"] else ""),
)
parser.add_argument(
"--no-zeroes-strip",
dest="zeroes_strip",
action="store_false",
help="Do not strip zero elements at the left and right sides%s"
% (" [default]" if not default_values["zeroes-strip"] else ""),
)
parser.add_argument(
"--zeroes-coalesce",
dest="zeroes_coalesce",
action="store_true",
default=default_values["zeroes-coalesce"],
help="Coalesce adjacent zero elements%s"
% (" [default]" if default_values["zeroes-coalesce"] else ""),
)
parser.add_argument(
"--no-zeroes-coalesce",
dest="zeroes_coalesce",
action="store_false",
help="Do not coalesce adjacent zero elements%s"
% (" [default]" if not default_values["zeroes-coalesce"] else ""),
)
parser.add_argument(
"--sigma",
action="store",
type=int,
dest="sigma",
default=default_values["sigma"],
metavar="SIGMA",
help="use SIGMA sigma",
)
parser.add_argument(
"--type",
action="store",
type=str,
dest="type",
default=default_values["type"],
choices=HISTOGRAM_TYPES,
metavar="[%s]"
% (
" | ".join(
HISTOGRAM_TYPES,
)
),
help="enum arg",
)
parser.add_argument(
"-i",
"--infile",
dest="infile",
type=str,
default=default_values["infile"],
metavar="input-file",
help="input file",
)
parser.add_argument(
"-o",
"--outfile",
dest="outfile",
type=str,
default=default_values["outfile"],
metavar="output-file",
help="output file",
)
# do the parsing
options = parser.parse_args(argv[1:])
if options.version:
return options
return options
def main(argv):
# parse options
options = get_options(argv)
if options.version:
print("version: %s" % __version__)
sys.exit(0)
# get infile/outfile
if options.infile is None or options.infile == "-":
options.infile = "/dev/fd/0"
if options.outfile is None or options.outfile == "-":
options.outfile = "/dev/fd/1"
# print results
if options.debug > 0:
print(options)
# do something
calculate_histogram(options)
if __name__ == "__main__":
# at least the CLI program name: (CLI) execution
main(sys.argv)