我的目标是为已安装模块内发生的迭代创建一个进度条。
为了在用户定义的函数中为迭代创建一个进度条,我将一个tqdm.notebook.tqdm_notebook
对象作为可迭代对象传递:
import time
import numpy as np
from tqdm.notebook import tqdm
def iterate(over):
for x in over: # creating progress bar for this
print(x, end='')
time.sleep(0.5)
xs = np.arange(5)
tqdm_xs = tqdm(xs) # creating tqdm.notebook.tqdm_notebook object
iterate(tqdm_xs) # progress bar, as expected
iterate(xs) # no progress bar
哪个有效:
但是,当我尝试对已安装模块内的 for 循环执行相同操作时,此操作会失败。在 Astropy 的 Photutils 模块中,有for label in labels
一行(此处),我可以传递标签对象。
可重现的示例(主要基于此- 在安装 photutils: 后工作pip install photutils
):
import photutils.datasets as phdat
import photutils.segmentation as phsegm
import astropy.convolution as conv
import astropy.stats as stats
data = phdat.make_100gaussians_image()
threshold = phsegm.detect_threshold(data, nsigma=2.)
sigma = 1.5
kernel = conv.Gaussian2DKernel(sigma, x_size=3, y_size=3)
kernel.normalize()
segm = phsegm.detect_sources(data, threshold, npixels=5, kernel=kernel)
这有效:
segm_deblend = phsegm.deblend_sources(data, segm, npixels=5, kernel=kernel,
nlevels=32, contrast=0.001, labels = segm.labels)
试图传递tqdm.notebook.tqdm_notebook
对象来创建进度条:
tqdm_segm_labels = tqdm(segm.labels)
segm_deblend = phsegm.deblend_sources(data, segm, npixels=5, kernel=kernel,
nlevels=32, contrast=0.001, labels = tqdm_segm_labels)
我得到一个AttributeError: 'int' object has no attribute '_comparable'
. 完整追溯:
0%
0/92 [00:00<?, ?it/s]
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-d101466650ae> in <module>()
1 tqdm_segm_labels = tqdm(segm.labels)
2 segm_deblend = phsegm.deblend_sources(data, segm, npixels=5, kernel=kernel,
----> 3 nlevels=32, contrast=0.001, labels = tqdm_segm_labels)
4 frames
/usr/local/lib/python3.7/dist-packages/astropy/utils/decorators.py in wrapper(*args, **kwargs)
534 warnings.warn(message, warning_type, stacklevel=2)
535
--> 536 return function(*args, **kwargs)
537
538 return wrapper
/usr/local/lib/python3.7/dist-packages/photutils/segmentation/deblend.py in deblend_sources(data, segment_img, npixels, kernel, labels, nlevels, contrast, mode, connectivity, relabel)
112 labels = segment_img.labels
113 labels = np.atleast_1d(labels)
--> 114 segment_img.check_labels(labels)
115
116 if kernel is not None:
/usr/local/lib/python3.7/dist-packages/photutils/segmentation/core.py in check_labels(self, labels)
355
356 # check for positive label numbers
--> 357 idx = np.where(labels <= 0)[0]
358 if idx.size > 0:
359 bad_labels.update(labels[idx])
/usr/local/lib/python3.7/dist-packages/tqdm/utils.py in __le__(self, other)
70
71 def __le__(self, other):
---> 72 return (self < other) or (self == other)
73
74 def __eq__(self, other):
/usr/local/lib/python3.7/dist-packages/tqdm/utils.py in __lt__(self, other)
67 """Assumes child has self._comparable attr/@property"""
68 def __lt__(self, other):
---> 69 return self._comparable < other._comparable
70
71 def __le__(self, other):
AttributeError: 'int' object has no attribute '_comparable'
一种解决方法是修改 Photutils 并tqdm
在其中使用(我在这个 fork上做过,它有效),但这似乎有点过头了,我希望有一种更简单的方法来做到这一点。