Skip to content

Commit

Permalink
Show progressbar if supported by backend
Browse files Browse the repository at this point in the history
  • Loading branch information
grst committed Jan 11, 2024
1 parent 790b61f commit 7eafed2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/scirpy/ir_dist/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from scanpy import logging
from scipy.sparse import coo_matrix, csr_matrix

from scirpy.util import _doc_params, deprecated, tqdm
from scirpy.util import _doc_params, _parallelize_with_joblib, deprecated

_doc_params_parallel_distance_calculator = """\
n_jobs
Expand Down Expand Up @@ -228,12 +228,8 @@ def calc_dist_mat(
# precompute blocks as list to have total number of blocks for progressbar
blocks = list(self._block_iter(seqs, seqs2, block_size=block_size))

# joblib + tqdm, see https://stackoverflow.com/a/76726101/2340703
block_results = tqdm(
joblib.Parallel(return_as="generator", n_jobs=self.n_jobs)(
joblib.delayed(self._compute_block)(*block) for block in blocks
),
total=len(blocks),
block_results = _parallelize_with_joblib(
(joblib.delayed(self._compute_block)(*block) for block in blocks), total=len(blocks), n_jobs=self.n_jobs
)

try:
Expand Down
16 changes: 16 additions & 0 deletions src/scirpy/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pandas as pd
import scipy.sparse
from anndata import AnnData
from joblib import Parallel
from mudata import MuData
from scanpy import logging
from scipy.sparse import issparse
Expand Down Expand Up @@ -561,3 +562,18 @@ def _translate_dna_to_protein(dna_seq: str):
else:
protein.append("N")
return "".join(protein)


def _parallelize_with_joblib(delayed_objects, *, total=None, **kwargs):
"""Wrapper around joblib.Parallel that shows a progressbar if the backend supports it.
Progressbar solution from https://stackoverflow.com/a/76726101/2340703
"""
try:
return tqdm(Parallel(return_as="generator", **kwargs)(delayed_objects), total=total)
except ValueError:
logging.info(

Check warning on line 575 in src/scirpy/util/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/scirpy/util/__init__.py#L574-L575

Added lines #L574 - L575 were not covered by tests
"Backend doesn't support return_as='generator'. No progress bar will be shown. "
"Consider setting verbosity in joblib.parallel_config"
)
return Parallel(return_as="list", **kwargs)(delayed_objects)

Check warning on line 579 in src/scirpy/util/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/scirpy/util/__init__.py#L579

Added line #L579 was not covered by tests

0 comments on commit 7eafed2

Please sign in to comment.