Skip to content

Commit 07ac3b6

Browse files
authored
[FIX] tqdm not displaying on notebooks (#878)
* test tqdm change * replace the corrector * replace tqdm * fix zip error * replace all with normal calls of tqdm * run isort * change list to dict
1 parent 4b33efe commit 07ac3b6

File tree

6 files changed

+104
-93
lines changed

6 files changed

+104
-93
lines changed

nimare/decode/continuous.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@
1919
from nimare.meta.cbma.mkda import MKDAChi2
2020
from nimare.results import MetaResult
2121
from nimare.stats import pearson
22-
from nimare.utils import (
23-
_check_ncores,
24-
_check_type,
25-
_safe_transform,
26-
get_masker,
27-
tqdm_joblib,
28-
)
22+
from nimare.utils import _check_ncores, _check_type, _safe_transform, get_masker
2923

3024
LGR = logging.getLogger(__name__)
3125

@@ -200,12 +194,15 @@ def _fit(self, dataset):
200194
MetaResult with meta-analytic maps and masker added.
201195
"""
202196
n_features = len(self.features_)
203-
with tqdm_joblib(tqdm(total=n_features)):
204-
maps = dict(
205-
Parallel(n_jobs=self.n_cores)(
197+
maps = {
198+
r: v
199+
for r, v in tqdm(
200+
Parallel(return_as="generator", n_jobs=self.n_cores)(
206201
delayed(self._run_fit)(feature, dataset) for feature in self.features_
207-
)
202+
),
203+
total=n_features,
208204
)
205+
}
209206

210207
self.results_ = MetaResult(self, mask=dataset.masker, maps=maps)
211208

@@ -390,12 +387,15 @@ def _fit(self, dataset):
390387
MetaResult with meta-analytic maps and masker added.
391388
"""
392389
n_features = len(self.features_)
393-
with tqdm_joblib(tqdm(total=n_features)):
394-
maps = dict(
395-
Parallel(n_jobs=self.n_cores)(
390+
maps = {
391+
r: v
392+
for r, v in tqdm(
393+
Parallel(return_as="generator", n_jobs=self.n_cores)(
396394
delayed(self._run_fit)(feature, dataset) for feature in self.features_
397-
)
395+
),
396+
total=n_features,
398397
)
398+
}
399399

400400
self.results_ = MetaResult(self, mask=dataset.masker, maps=maps)
401401

nimare/diagnostics.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from nimare.base import NiMAREBase
1616
from nimare.meta.cbma.base import PairwiseCBMAEstimator
1717
from nimare.meta.ibma import IBMAEstimator
18-
from nimare.utils import _check_ncores, get_masker, mm2vox, tqdm_joblib
18+
from nimare.utils import _check_ncores, get_masker, mm2vox
1919

2020
LGR = logging.getLogger(__name__)
2121

@@ -219,10 +219,16 @@ def transform(self, result):
219219
contribution_table = pd.DataFrame(index=rows, columns=cols)
220220
contribution_table.index.name = "id"
221221

222-
with tqdm_joblib(tqdm(total=len(meta_ids))):
223-
contributions = Parallel(n_jobs=self.n_cores)(
224-
delayed(self._transform)(expid, label_map, sign, result) for expid in meta_ids
222+
contributions = [
223+
r
224+
for r in tqdm(
225+
Parallel(return_as="generator", n_jobs=self.n_cores)(
226+
delayed(self._transform)(expid, label_map, sign, result)
227+
for expid in meta_ids
228+
),
229+
total=len(meta_ids),
225230
)
231+
]
226232

227233
# Add results to table
228234
for expid, stat_prop_values in zip(meta_ids, contributions):

nimare/meta/cbma/ale.py

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from nimare.meta.kernel import ALEKernel
1414
from nimare.stats import null_to_p, nullhist_to_p
1515
from nimare.transforms import p_to_z
16-
from nimare.utils import _check_ncores, tqdm_joblib, use_memmap
16+
from nimare.utils import _check_ncores, use_memmap
1717

1818
LGR = logging.getLogger(__name__)
1919
__version__ = _version.get_versions()["version"]
@@ -520,26 +520,34 @@ def _fit(self, dataset1, dataset2):
520520
shape=(self.n_iters, n_voxels),
521521
)
522522

523-
with tqdm_joblib(tqdm(total=self.n_iters)):
524-
Parallel(n_jobs=self.n_cores)(
525-
delayed(self._run_permutation)(i_iter, n_grp1, ma_arr, iter_diff_values)
526-
for i_iter in range(self.n_iters)
523+
_ = [
524+
r
525+
for r in tqdm(
526+
Parallel(return_as="generator", n_jobs=self.n_cores)(
527+
delayed(self._run_permutation)(i_iter, n_grp1, ma_arr, iter_diff_values)
528+
for i_iter in range(self.n_iters)
529+
),
530+
total=self.n_iters,
527531
)
532+
]
528533

529534
# Determine p-values based on voxel-wise null distributions
530535
# I know that joblib probably preserves order of outputs, but I'm paranoid, so we track
531536
# the iteration as well and sort the resulting p-value array based on that.
532-
with tqdm_joblib(tqdm(total=n_voxels)):
533-
p_values, voxel_idx = zip(
534-
*Parallel(n_jobs=self.n_cores)(
537+
p_values, voxel_idx = tqdm(
538+
zip(
539+
*Parallel(return_as="generator", n_jobs=self.n_cores)(
535540
delayed(self._alediff_to_p_voxel)(
536541
i_voxel,
537542
diff_ale_values[i_voxel],
538543
iter_diff_values[:, i_voxel],
539544
)
540545
for i_voxel in range(n_voxels)
541-
)
542-
)
546+
),
547+
),
548+
total=n_voxels,
549+
)
550+
543551
# Convert to an array and sort the p-values array based on the voxel index.
544552
p_values = np.array(p_values)[np.array(voxel_idx)]
545553

@@ -801,13 +809,18 @@ def _fit(self, dataset):
801809
mode="w+",
802810
shape=(self.n_iters, stat_values.shape[0]),
803811
)
804-
with tqdm_joblib(tqdm(total=self.n_iters)):
805-
Parallel(n_jobs=self.n_cores)(
806-
delayed(self._run_permutation)(
807-
i_iter, iter_xyzs[i_iter], iter_df, perm_scale_values
808-
)
809-
for i_iter in range(self.n_iters)
812+
_ = [
813+
r
814+
for r in tqdm(
815+
Parallel(return_as="generator", n_jobs=self.n_cores)(
816+
delayed(self._run_permutation)(
817+
i_iter, iter_xyzs[i_iter], iter_df, perm_scale_values
818+
)
819+
for i_iter in range(self.n_iters)
820+
),
821+
total=self.n_iters,
810822
)
823+
]
811824

812825
p_values, z_values = self._scale_to_p(stat_values, perm_scale_values)
813826

@@ -876,15 +889,18 @@ def _scale_to_p(self, stat_values, scale_values):
876889

877890
# I know that joblib probably preserves order of outputs, but I'm paranoid, so we track
878891
# the iteration as well and sort the resulting p-value array based on that.
879-
with tqdm_joblib(tqdm(total=n_voxels)):
880-
p_values, voxel_idx = zip(
881-
*Parallel(n_jobs=self.n_cores)(
892+
p_values, voxel_idx = tqdm(
893+
zip(
894+
*Parallel(return_as="generator", n_jobs=self.n_cores)(
882895
delayed(self._scale_to_p_voxel)(
883896
i_voxel, stat_values[i_voxel], scale_values[:, i_voxel]
884897
)
885898
for i_voxel in range(n_voxels)
886899
)
887-
)
900+
),
901+
total=n_voxels,
902+
)
903+
888904
# Convert to an array and sort the p-values array based on the voxel index.
889905
p_values = np.array(p_values)[np.array(voxel_idx)]
890906

nimare/meta/cbma/base.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
_check_type,
2525
get_masker,
2626
mm2vox,
27-
tqdm_joblib,
2827
vox2mm,
2928
)
3029

@@ -506,13 +505,18 @@ def _compute_null_montecarlo(self, n_iters, n_cores):
506505
iter_xyzs = np.split(rand_xyz, rand_xyz.shape[1], axis=1)
507506
iter_df = self.inputs_["coordinates"].copy()
508507

509-
with tqdm_joblib(tqdm(total=n_iters)):
510-
perm_histograms = Parallel(n_jobs=n_cores)(
511-
delayed(self._compute_null_montecarlo_permutation)(
512-
iter_xyzs[i_iter], iter_df=iter_df
513-
)
514-
for i_iter in range(n_iters)
508+
perm_histograms = [
509+
r
510+
for r in tqdm(
511+
Parallel(return_as="generator", n_jobs=n_cores)(
512+
delayed(self._compute_null_montecarlo_permutation)(
513+
iter_xyzs[i_iter], iter_df=iter_df
514+
)
515+
for i_iter in range(n_iters)
516+
),
517+
total=n_iters,
515518
)
519+
]
516520

517521
perm_histograms = np.vstack(perm_histograms)
518522
self.null_distributions_["histweights_corr-none_method-montecarlo"] = np.sum(
@@ -720,17 +724,22 @@ def correct_fwe_montecarlo(
720724
# Define connectivity matrix for cluster labeling
721725
conn = ndimage.generate_binary_structure(rank=3, connectivity=1)
722726

723-
with tqdm_joblib(tqdm(total=n_iters)):
724-
perm_results = Parallel(n_jobs=n_cores)(
725-
delayed(self._correct_fwe_montecarlo_permutation)(
726-
iter_xyzs[i_iter],
727-
iter_df=iter_df,
728-
conn=conn,
729-
voxel_thresh=ss_thresh,
730-
vfwe_only=vfwe_only,
731-
)
732-
for i_iter in range(n_iters)
727+
perm_results = [
728+
r
729+
for r in tqdm(
730+
Parallel(return_as="generator", n_jobs=n_cores)(
731+
delayed(self._correct_fwe_montecarlo_permutation)(
732+
iter_xyzs[i_iter],
733+
iter_df=iter_df,
734+
conn=conn,
735+
voxel_thresh=ss_thresh,
736+
vfwe_only=vfwe_only,
737+
)
738+
for i_iter in range(n_iters)
739+
),
740+
total=n_iters,
733741
)
742+
]
734743

735744
fwe_voxel_max, fwe_cluster_size_max, fwe_cluster_mass_max = zip(*perm_results)
736745

nimare/meta/cbma/mkda.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from nimare.meta.utils import _calculate_cluster_measures
1818
from nimare.stats import null_to_p, one_way, two_way
1919
from nimare.transforms import p_to_z
20-
from nimare.utils import _check_ncores, tqdm_joblib, vox2mm
20+
from nimare.utils import _check_ncores, vox2mm
2121

2222
LGR = logging.getLogger(__name__)
2323
__version__ = _version.get_versions()["version"]
@@ -849,18 +849,23 @@ def correct_fwe_montecarlo(self, result, voxel_thresh=0.001, n_iters=1000, n_cor
849849
# Define connectivity matrix for cluster labeling
850850
conn = ndimage.generate_binary_structure(rank=3, connectivity=1)
851851

852-
with tqdm_joblib(tqdm(total=n_iters)):
853-
perm_results = Parallel(n_jobs=n_cores)(
854-
delayed(self._run_fwe_permutation)(
855-
iter_xyz1=iter_xyzs1[i_iter],
856-
iter_xyz2=iter_xyzs2[i_iter],
857-
iter_df1=iter_df1,
858-
iter_df2=iter_df2,
859-
conn=conn,
860-
voxel_thresh=ss_thresh,
861-
)
862-
for i_iter in range(n_iters)
852+
perm_results = [
853+
r
854+
for r in tqdm(
855+
Parallel(return_as="generator", n_jobs=n_cores)(
856+
delayed(self._run_fwe_permutation)(
857+
iter_xyz1=iter_xyzs1[i_iter],
858+
iter_xyz2=iter_xyzs2[i_iter],
859+
iter_df1=iter_df1,
860+
iter_df2=iter_df2,
861+
conn=conn,
862+
voxel_thresh=ss_thresh,
863+
)
864+
for i_iter in range(n_iters)
865+
),
866+
total=n_iters,
863867
)
868+
]
864869

865870
del rand_idx1, rand_xyz1, iter_xyzs1
866871
del rand_idx2, rand_xyz2, iter_xyzs2

nimare/utils.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Utility functions for NiMARE."""
22

3-
import contextlib
43
import datetime
54
import inspect
65
import json
@@ -947,30 +946,6 @@ def _boolean_unmask(data_array, bool_array):
947946
return unmasked_data
948947

949948

950-
@contextlib.contextmanager
951-
def tqdm_joblib(tqdm_object):
952-
"""Context manager to patch joblib to report into tqdm progress bar given as argument.
953-
954-
From https://stackoverflow.com/a/58936697/2589328.
955-
"""
956-
957-
class TqdmBatchCompletionCallback(joblib.parallel.BatchCompletionCallBack):
958-
def __init__(self, *args, **kwargs):
959-
super().__init__(*args, **kwargs)
960-
961-
def __call__(self, *args, **kwargs):
962-
tqdm_object.update(n=self.batch_size)
963-
return super().__call__(*args, **kwargs)
964-
965-
old_batch_callback = joblib.parallel.BatchCompletionCallBack
966-
joblib.parallel.BatchCompletionCallBack = TqdmBatchCompletionCallback
967-
try:
968-
yield tqdm_object
969-
finally:
970-
joblib.parallel.BatchCompletionCallBack = old_batch_callback
971-
tqdm_object.close()
972-
973-
974949
def unique_rows(ar, return_counts=False):
975950
"""Remove repeated rows from a 2D array.
976951

0 commit comments

Comments
 (0)