-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgressBar.py
78 lines (56 loc) · 2.35 KB
/
ProgressBar.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
import time
import numpy as np
class ProgressBar:
def __init__(self, total, verb='', start_at=0, bar_length=50, begin_on_newline=True):
self.total = total
self.verb = verb
self.start_at = start_at
self.bar_length = bar_length
self.start_time = None
self.mean_time = 0
self.n_iters = 0
self.iter_per_sec = 0
self.init_time = time.time()
self.last_print_time = 0
if begin_on_newline:
print('')
def start(self):
self.n_iters += 1
self.start_time = time.time()
def stop(self):
alpha = (1 / (self.n_iters + 1))
elapsed = time.time() - self.start_time
self.mean_time = alpha * elapsed + (1 - alpha) * self.mean_time
if time.time() - self.last_print_time > 0.25 or self.n_iters == self.total:
self.print_progress()
@staticmethod
def _time_to_string(timestamp):
minutes, seconds = np.divmod(timestamp, 60)
minutes = int(minutes)
minutes = '0' * (2 - len(str(minutes))) + str(minutes)
seconds = int(seconds)
seconds = '0' * (2 - len(str(seconds))) + str(seconds)
return minutes, seconds
def print_progress(self):
remaining = self.mean_time * (self.total - self.n_iters)
elapsed = time.time() - self.init_time + 1e-9
remain_min, remain_sec = self._time_to_string(remaining)
elapse_min, elapse_sec = self._time_to_string(elapsed)
iter_per_sec = self.n_iters / elapsed
self.iter_per_sec = iter_per_sec
n_digits = len(str(self.total))
total_iters = self.start_at + self.n_iters
pct_complete = int(total_iters / self.total * self.bar_length)
bar = f'{self.verb} {total_iters:<{n_digits}} / {self.total} ['
bar = bar + '=' * pct_complete + ' ' * (self.bar_length - pct_complete) + ']'
time_info = f'elapsed: {elapse_min}:{elapse_sec}, '
time_info += f'remaining: {remain_min}:{remain_sec}, '
if iter_per_sec < 1:
time_info += f'{1 / iter_per_sec:0.2f}sec/iter'
else:
time_info += f'{iter_per_sec:0.2f}iter/sec'
complete = self.n_iters == self.total
print(bar, time_info, end='\n' if complete else '\r')
self.last_print_time = time.time()
def get_iters_per_sec(self):
return self.iter_per_sec