Skip to content

Commit 8302257

Browse files
committed
added progress bar
1 parent c64ff91 commit 8302257

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

pymc/progressbar.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
A simple progress bar to monitor MCMC sampling progress.
3+
Modified from original code by Corey Goldberg (2010)
4+
"""
5+
6+
from __future__ import print_function
7+
8+
import sys, time
9+
try:
10+
from IPython.core.display import clear_output
11+
have_ipython = True
12+
except ImportError:
13+
have_ipython = False
14+
15+
class ProgressBar:
16+
def __init__(self, iterations):
17+
self.iterations = iterations
18+
self.prog_bar = '[]'
19+
self.fill_char = '*'
20+
self.width = 40
21+
self.__update_amount(0)
22+
if have_ipython:
23+
self.animate = self.animate_ipython
24+
else:
25+
self.animate = self.animate_noipython
26+
27+
def animate_noipython(self, iter):
28+
if sys.platform.lower().startswith('win'):
29+
print(self, '\r', end='')
30+
else:
31+
print(self)
32+
self.update_iteration(iter)
33+
# time.sleep(0.5)
34+
35+
def animate_ipython(self, iter):
36+
try:
37+
clear_output()
38+
except Exception:
39+
# terminal IPython has no clear_output
40+
pass
41+
print('\r', self, end='')
42+
sys.stdout.flush()
43+
self.update_iteration(iter)
44+
45+
def update_iteration(self, elapsed_iter):
46+
self.__update_amount((elapsed_iter / float(self.iterations)) * 100.0)
47+
self.prog_bar += ' %d of %s complete' % (elapsed_iter, self.iterations)
48+
49+
def __update_amount(self, new_amount):
50+
percent_done = int(round((new_amount / 100.0) * 100.0))
51+
all_full = self.width - 2
52+
num_hashes = int(round((percent_done / 100.0) * all_full))
53+
self.prog_bar = '[' + self.fill_char * num_hashes + ' ' * (all_full - num_hashes) + ']'
54+
pct_place = (len(self.prog_bar) / 2) - len(str(percent_done))
55+
pct_string = '%d%%' % percent_done
56+
self.prog_bar = self.prog_bar[0:pct_place] + \
57+
(pct_string + self.prog_bar[pct_place + len(pct_string):])
58+
59+
def __str__(self):
60+
return str(self.prog_bar)

0 commit comments

Comments
 (0)