Skip to content

Commit

Permalink
Fix romc progress bar updating (#368)
Browse files Browse the repository at this point in the history
* Modify ProgressBar-functionality of ROMC

* Add __init__ to inference-folder
  • Loading branch information
hpesonen authored Mar 29, 2021
1 parent 9036fa9 commit 399b94e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions elfi/methods/inference/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# noqa: D104
15 changes: 10 additions & 5 deletions elfi/methods/inference/romc.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,8 @@ def __init__(self, model, bounds=None, discrepancy_name=None, output_names=None,
self.distances = None
self.result = None # RomcSample object

self.progress_bar = ProgressBar()
self.progress_bar = ProgressBar(prefix='Progress', suffix='Complete',
decimals=1, length=50, fill='=')

super(ROMC, self).__init__(model, output_names, **kwargs)

Expand Down Expand Up @@ -633,8 +634,9 @@ def _solve_gradients(self, **kwargs):
attempted = [False for _ in range(n1)]
tic = timeit.default_timer()
if parallelize is False:
self.progress_bar.reinit_progressbar(reinit_msg="Solving gradients")
for i in range(n1):
self.progress_bar.update_progressbar(i, n1)
self.progress_bar.update_progressbar(i + 1, n1)
attempted[i] = True
is_solved = optim_probs[i].solve_gradients(**kwargs)
solved[i] = is_solved
Expand Down Expand Up @@ -681,8 +683,9 @@ def _solve_bo(self, **kwargs):
attempted = []
solved = []
tic = timeit.default_timer()
self.progress_bar.reinit_progressbar(reinit_msg="Bayesian Optimization")
for i in range(n1):
self.progress_bar.update_progressbar(i, n1)
self.progress_bar.update_progressbar(i + 1, n1)
attempted.append(True)
is_solved = optim_problems[i].solve_bo(**kwargs)
solved.append(is_solved)
Expand Down Expand Up @@ -774,8 +777,9 @@ def _build_boxes(self, **kwargs):
# main
computed_bb = [False for _ in range(n1)]
if parallelize is False:
self.progress_bar.reinit_progressbar(reinit_msg="Building boxes")
for i in range(n1):
self.progress_bar.update_progressbar(i, n1)
self.progress_bar.update_progressbar(i + 1, n1)
if accepted[i]:
is_built = optim_problems[i].build_region(**kwargs)
computed_bb.append(is_built)
Expand Down Expand Up @@ -808,8 +812,9 @@ def _fit_models(self, **kwargs):

# main
if parallelize is False:
self.progress_bar.reinit_progressbar(reinit_msg="Fitting models")
for i in range(n1):
self.progress_bar.update_progressbar(i, n1)
self.progress_bar.update_progressbar(i + 1, n1)
if accepted[i]:
optim_problems[i].fit_local_surrogate(**kwargs)
else:
Expand Down
6 changes: 4 additions & 2 deletions elfi/methods/posteriors.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ def __init__(self,
self.parallelize = parallelize
self.partition = None

self.progress_bar = ProgressBar()
self.progress_bar = ProgressBar(prefix='Progress', suffix='Complete',
decimals=1, length=50, fill='=')

def _pdf_unnorm_single_point(self, theta: np.ndarray) -> float:
"""Evaluate the unnormalised pdf, at a single input point.
Expand Down Expand Up @@ -588,11 +589,12 @@ def sample(self, n2: int, seed=None) -> (np.ndarray, np.ndarray):
if self.parallelize is False:
w = []
distances = []
self.progress_bar.reinit_progressbar(reinit_msg="Sampling posterior regions")
for i in range(nof_regions):
w.append([])
# indicator_region = self.regions[i].contains
for j in range(n2):
self.progress_bar.update_progressbar(i * n2 + j, nof_regions * n2)
self.progress_bar.update_progressbar(i * n2 + j + 1, nof_regions * n2)
cur_theta = theta[i, j]
q = regions[i].pdf(cur_theta)
if q == 0.0:
Expand Down

0 comments on commit 399b94e

Please sign in to comment.