Skip to content

Commit 551cdde

Browse files
author
Christopher Fonnesbeck
committed
Fixed progressbar to show the proper number of iterations (n rather than n+1); silenced Matplotlib backend warnings on test_examples
1 parent 9d75e90 commit 551cdde

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

pymc/progressbar.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,26 @@
1414

1515
__all__ = ['progress_bar']
1616

17-
class ProgressBar(object):
17+
class ProgressBar(object):
1818
def __init__(self, iterations, animation_interval = .5):
1919
self.iterations = iterations
2020
self.start = time.time()
2121
self.last = 0
2222
self.animation_interval = animation_interval
2323

24-
def percentage(self, i):
24+
def percentage(self, i):
2525
return 100*i/float(self.iterations)
2626

2727
def update(self, i):
2828
elapsed = time.time() - self.start
29-
i = i + 1
30-
31-
if elapsed - self.last > self.animation_interval or i == self.iterations:
32-
self.animate(i+1, elapsed)
33-
self.last = elapsed
34-
29+
i += 1
30+
31+
if elapsed - self.last > self.animation_interval:
32+
self.animate(i+1, elapsed)
33+
self.last = elapsed
34+
elif i == self.iterations:
35+
self.animate(i, elapsed)
36+
3537
class TextProgressBar(ProgressBar):
3638
def __init__(self, iterations, printer):
3739
self.fill_char = '-'
@@ -45,16 +47,16 @@ def animate(self, i, elapsed):
4547
self.printer(self.progbar(i, elapsed))
4648

4749

48-
def progbar(self,i, elapsed):
50+
def progbar(self,i, elapsed):
4951
bar = self.bar(self.percentage(i))
5052
return "[%s] %i of %i complete in %.1f sec" % (bar, i, self.iterations, round(elapsed,1))
51-
53+
5254
def bar(self, percent):
5355
all_full = self.width - 2
5456
num_hashes = int(percent/100 * all_full)
55-
57+
5658
bar = self.fill_char * num_hashes + ' ' * (all_full - num_hashes)
57-
59+
5860
info = '%d%%' % percent
5961
loc = ( len(bar) - len(info) )/2
6062
return replace_at(bar, info, loc, loc + len(info))
@@ -68,7 +70,7 @@ def consoleprint(s):
6870
print(s, '\r', end='')
6971
else:
7072
print(s)
71-
73+
7274
def ipythonprint(s):
7375
print('\r', s, end='')
7476
sys.stdout.flush()
@@ -82,7 +84,7 @@ def __init__(self, iterations):
8284
"""
8385
<div style="float: left; border: 1px solid black; width:500px">
8486
<div id="%s" style="background-color:blue; width:0%%">&nbsp;</div>
85-
</div>
87+
</div>
8688
<label id="%s" style="padding-left: 10px;" text = ""/>
8789
""" % (self.divid,self.sec_id))
8890
display(pb)
@@ -103,10 +105,10 @@ def run_from_ipython():
103105
return False
104106

105107
def progress_bar(iters):
106-
if run_from_ipython():
108+
if run_from_ipython():
107109
if None:
108110
return NotebookProgressBar(iters)
109-
else:
111+
else:
110112
return TextProgressBar(iters, ipythonprint)
111113
else:
112114
return TextProgressBar(iters, consoleprint)

pymc/tests/test_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import matplotlib
2-
matplotlib.use('Agg')
2+
matplotlib.use('Agg', warn=False)
33

44
from os import path
55
import os

0 commit comments

Comments
 (0)