Repository: sbi-dev/sbi

Issue:
progress bar duplicated instead of replaced with np_slice_vectorized
Hi,

I'm using posterior inference with np_slice_vectorized, in a jupyter notebook.

The progress bar for the fit is duplicated each time it updates instead of replacing the previous state:

<img width="866" alt="image" src="https://user-images.githubusercontent.com/16220995/158087766-85d858c1-7944-47f0-88b4-f9513bf67883.png">




Discussion:
yes, good point, thanks for reporting this. Its probably an issue with `tqdm` in `jupyter` notebooks, e.g., we need to replace the import `from tqdm import tqdm` by `from tqdm.notebook import tqdm` or even `from tqdm.autonotebook import tqdm` (see https://pypi.org/project/tqdm/#ipython-jupyter-integration). 
We will fix this soon (or do you want to make a PR?).

Resolving patch:
diff --git a/sbi/inference/posteriors/mcmc_posterior.py b/sbi/inference/posteriors/mcmc_posterior.py
--- a/sbi/inference/posteriors/mcmc_posterior.py
+++ b/sbi/inference/posteriors/mcmc_posterior.py
@@ -11,7 +11,7 @@
 from pyro.infer.mcmc.api import MCMC
 from torch import Tensor
 from torch import multiprocessing as mp
-from tqdm import tqdm
+from tqdm.auto import tqdm
 
 from sbi.inference.posteriors.base_posterior import NeuralPosterior
 from sbi.samplers.mcmc import (
@@ -341,7 +341,7 @@ def seeded_init_fn(seed):
                 torch.manual_seed(seed)
                 return init_fn()
 
-            seeds = torch.randint(high=2**31, size=(num_workers,))
+            seeds = torch.randint(high=2**31, size=(num_chains,))
 
             # Generate initial params parallelized over num_workers.
             with tqdm_joblib(
diff --git a/sbi/inference/posteriors/vi_posterior.py b/sbi/inference/posteriors/vi_posterior.py
--- a/sbi/inference/posteriors/vi_posterior.py
+++ b/sbi/inference/posteriors/vi_posterior.py
@@ -8,7 +8,7 @@
 import torch
 from torch import Tensor
 from torch.distributions import Distribution
-from tqdm import tqdm
+from tqdm.auto import tqdm
 
 from sbi.inference.posteriors.base_posterior import NeuralPosterior
 from sbi.samplers.vi import (
diff --git a/sbi/samplers/mcmc/slice_numpy.py b/sbi/samplers/mcmc/slice_numpy.py
--- a/sbi/samplers/mcmc/slice_numpy.py
+++ b/sbi/samplers/mcmc/slice_numpy.py
@@ -10,7 +10,7 @@
 import torch
 from joblib import Parallel, delayed
 from matplotlib import pyplot as plt
-from tqdm import tqdm, trange
+from tqdm.auto import tqdm, trange
 
 from sbi.simulators.simutils import tqdm_joblib
 from sbi.utils import tensor2numpy
@@ -275,7 +275,10 @@ def run(self, num_samples: int) -> np.ndarray:
             self.state[c]["width"] = np.full(self.n_dims, self.init_width)
 
         if self.verbose:
-            pbar = tqdm(range(self.num_chains * num_samples))
+            pbar = tqdm(
+                range(self.num_chains * num_samples),
+                desc=f"Running vectorized MCMC with {self.num_chains} chains",
+            )
 
         num_chains_finished = 0
         while num_chains_finished != self.num_chains:
@@ -514,8 +517,9 @@ def run_slice_np_vectorized(inits, seed):
     with tqdm_joblib(
         tqdm(
             range(num_batches),  # type: ignore
-            disable=not show_progress_bars,
-            desc=f"Running {num_chains} MCMC chains in {num_batches} batches.",
+            disable=not show_progress_bars or num_workers == 1,
+            desc=f"""Running {num_chains} MCMC chains with {num_workers} worker{"s" if
+                  num_workers>1 else ""} (batch_size={batch_size}).""",
             total=num_chains,
         )
     ):


Test patch: