use has_progress_bar and add underscore
@@ -119,7 +119,7 @@ def optimize(
model=None,
return_info=False,
verbose=True,
- progressbar=True,
+ progress_bar=True,
**kwargs
):
"""Maximize the log prob of a PyMC3 model using scipy
@@ -134,7 +134,7 @@ def optimize(
return_info: Return both the coordinate dictionary and the result of
``scipy.optimize.minimize``
verbose: Print the success flag and log probability to the screen
- progressbar: A ``tqdm`` progress bar instance. Set to ``True``
+ progress_bar: A ``tqdm`` progress bar instance. Set to ``True``
(default) to use ``tqdm.auto.tqdm()``. Set to ``False`` to disable.
"""
@@ -174,19 +174,26 @@ def optimize(
"optimizing logp for variables: [{0}]\n".format(", ".join(names))
)
- if progressbar is True:
+ if progress_bar is True:
from tqdm.auto import tqdm
- progressbar = tqdm()
+ progress_bar = tqdm()
+
+ # Check whether the input progress bar has the expected methods
+ has_progress_bar = (
+ hasattr(progress_bar, "set_postfix")
+ and hasattr(progress_bar, "update")
+ and hasattr(progress_bar, "close")
+ )
# This returns the objective function and its derivatives
def objective(vec):
res = func(*get_args_for_theano_function(bij.rmap(vec), model=model))
d = dict(zip((v.name for v in vars), res[1:]))
g = bij.map(d)
- if verbose and progressbar is not False:
- progressbar.set_postfix(logp="{0:e}".format(-res[0]))
- progressbar.update()
+ if verbose and has_progress_bar:
+ progress_bar.set_postfix(logp="{0:e}".format(-res[0]))
+ progress_bar.update()
return res[0], g
# Optimize using scipy.optimize
@@ -206,8 +213,9 @@ def optimize(
}
if verbose:
- if progressbar is not False:
- progressbar.close()
+ if has_progress_bar:
+ has_progress_bar.close()
+
sys.stderr.write("message: {0}\n".format(info.message))
sys.stderr.write("logp: {0} -> {1}\n".format(-initial, -info.fun))
if not np.isfinite(info.fun):
