forked from ericaltendorf/plotman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_util.py
67 lines (55 loc) · 1.83 KB
/
plot_util.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
import math
import os
import re
GB = 1_000_000_000
def df_b(d):
'Return free space for directory (in bytes)'
stat = os.statvfs(d)
return stat.f_frsize * stat.f_bfree
def get_k32_plotsize():
return 108 * GB
def human_format(num, precision):
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num /= 1000.0
return (('%.' + str(precision) + 'f%s') %
(num, ['', 'K', 'M', 'G', 'T', 'P'][magnitude]))
def time_format(sec):
if sec < 60:
return '%ds' % sec
else:
return '%d:%02d' % (int(sec / 3600), int((sec % 3600) / 60))
def tmpdir_phases_str(tmpdir_phases_pair):
tmpdir = tmpdir_phases_pair[0]
phases = tmpdir_phases_pair[1]
phase_str = ', '.join(['%d:%d' % ph_subph for ph_subph in sorted(phases)])
return ('%s (%s)' % (tmpdir, phase_str))
def split_path_prefix(items):
if not items:
return ('', [])
prefix = os.path.commonpath(items)
if prefix == '/':
return ('', items)
else:
remainders = [ os.path.relpath(i, prefix) for i in items ]
return (prefix, remainders)
def list_k32_plots(d):
'List completed k32 plots in a directory (not recursive)'
plots = []
for plot in os.listdir(d):
if re.match(r'^plot-k32-.*plot$', plot):
plot = os.path.join(d, plot)
if os.stat(plot).st_size > (0.95 * get_k32_plotsize()):
plots.append(plot)
return plots
def column_wrap(items, n_cols, filler=None):
'''Take items, distribute among n_cols columns, and return a set
of rows containing the slices of those columns.'''
rows = []
n_rows = math.ceil(len(items) / n_cols)
for row in range(n_rows):
row_items = items[row : : n_rows]
# Pad and truncate
rows.append( (row_items + ([filler] * n_cols))[:n_cols] )
return rows