gpt4 book ai didi

python - 处理来自包/模块的错误时如何处理 Python 异常

转载 作者:行者123 更新时间:2023-12-01 09:06:35 28 4
gpt4 key购买 nike

我正在使用一个名为 WCS.all_world2pixastropy 包,以便将许多坐标(以度为单位)转换为图像上的像素坐标。在许多对坐标上运行此程序时,我最终遇到了一个错误,该错误使我的程序无法运行。这是导致错误的代码以及错误本身:

import glob
import numpy as np
import re
from astropy.io import fits
from astropy.wcs import WCS

initial_data, centroid_coords = [], []
image_files = glob.glob('/home/username/Desktop/myfolder/*.fits')

for image in image_files:
img_data = np.nan_to_num(fits.getdata(image))

obj_num = int(re.search('2\d{6}', image).group(0))
count_num = int(re.search('_(\d+)_', image).group(1))
obj_index = int(np.where(good_data == obj_num)[0])
count_index = int(np.where(np.array(all_counters[obj_index]) == count_num)[0])

ra, dec = pos_match[obj_index][count_index]
w = WCS(image)
x, y = w.all_world2pix(ra, dec, 0)
initial_data.append(img_data)
centroid_coords.append((float(x), float(y)))

错误:

x, y = w.all_world2pix(ra, dec, 0)  
File "/usr/local/anaconda3/lib/python3.6/site-packages/astropy/wcs/wcs.py", line 1827, in all_world2pix
'input', *args, **kwargs
File "/usr/local/anaconda3/lib/python3.6/site-packages/astropy/wcs/wcs.py", line 1269, in _array_converter
return _return_list_of_arrays(axes, origin)
File "/usr/local/anaconda3/lib/python3.6/site-packages/astropy/wcs/wcs.py", line 1225, in _return_list_of_arrays
output = func(xy, origin)
File "/usr/local/anaconda3/lib/python3.6/site-packages/astropy/wcs/wcs.py", line 1826, in <lambda>
quiet=quiet),
File "/usr/local/anaconda3/lib/python3.6/site-packages/astropy/wcs/wcs.py", line 1812, in _all_world2pix
slow_conv=ind, divergent=inddiv)
astropy.wcs.wcs.NoConvergence: 'WCS.all_world2pix' failed to converge to the requested accuracy.
After 2 iterations, the solution is diverging at least for one input point.

我只是希望能够跳过那些导致此错误的图像。但我不确定如何将此作为异常处理,因为它不是通常的类型/值/语法错误等......

当它通过我的循环时,当/如果发生此错误时,我只是希望它继续循环中的下一个元素,而不为导致错误的元素添加任何内容。我的想法是这样的:

for image in image_files:
img_data = np.nan_to_num(fits.getdata(image))
obj_num = int(re.search('2\d{6}', image).group(0))
count_num = int(re.search('_(\d+)_', image).group(1))
obj_index = int(np.where(good_data == obj_num)[0])
count_index = int(np.where(np.array(all_counters[obj_index]) == count_num)[0])

ra, dec = pos_match[obj_index][count_index]
w = WCS(image)
try:
x, y = w.all_world2pix(ra, dec, 0)
initial_data.append(img_data)
centroid_coords.append((float(x), float(y)))
except: # skip images where coordinates fail to converge
# Not sure how to identify the error here
continue

如何处理这个异常?我以前从未真正处理过这些问题,因此非常感谢您的帮助。谢谢!

最佳答案

你现在所拥有的看起来应该可以工作。不过,我想向您推荐两个小修改:

  • try block 中包含尽可能少的行数,这样就不会掩盖意外的错误情况
  • 再次捕获特定错误,这样您就不会掩盖意外情况( except 本身会捕获每个错误)
<小时/>
for image in image_files:
< the code you already have, up to the `try` block >

try:
x, y = w.all_world2pix(ra, dec, 0)
except NoConvergence:
continue

initial_data.append(img_data)
centroid_coords.append((float(x), float(y)))

请注意,您可能还需要在脚本顶部添加针对 NoConvergence 错误的导入:

from astropy.wcs.wcs import NoConvergence

关于python - 处理来自包/模块的错误时如何处理 Python 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51996087/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com