我正在寻找一个优化我的代码,以避免以下错误,并使我的程序更快。maximum recursion depth exceeded while calling a Python object
我想在一个码头容器中执行这段代码。在我的机器上,它在本地工作得很好。
我在寻找一种减少循环次数的方法。
def reidentify(self):
try:
# Check if file is re-identify
self.check_if_dicom_is_reidentify()
LOGGER.info(f"Anonymization in progress for {self.dirpath_output}")
# Get patient data
json_data = json.loads(self.get_patient_data().decode('utf-8'))
# Re-identification
archive = zipfile.ZipFile(self.download.dirpath_download, 'r')
for file in archive.namelist():
# Check if the file is a dicom
if not file.endswith('.dcm'):
continue
# Reading dicom file
dicom_data = io.BytesIO(archive.read(file))
ds = pydicom.dcmread(dicom_data)
# Edit Dicom general fields
for key in json_data['tags']:
ds.data_element(key).value = json_data['tags'][key]
# Edit Dicom series field
for key in json_data['series']:
if key['obfuscated_uid'] != ds.data_element('SeriesInstanceUID').value:
continue
for tag in key['tags']:
ds.data_element(tag).value = key['tags'][tag]
# Save re-identify dicom
ds.save_as(f'{self.dirpath_tmp}/{os.path.basename(file)}')
except Exception as e:
LOGGER.error(e)这段代码给出了预期的结果,但我认为这不是一个优化的方法,而且非常慢。
编辑:这是堆栈跟踪
reidentify_1 | 2019-10-17 11:29:53,001] With tag (0010, 1030) got exception: maximum recursion depth exceeded while calling a Python object
reidentify_1 | Traceback (most recent call last):
reidentify_1 | File "/usr/local/lib/python3.8/site-packages/pydicom/tag.py", line 30, in tag_in_exception
reidentify_1 | yield
reidentify_1 | File "/usr/local/lib/python3.8/site-packages/pydicom/filewriter.py", line 541, in write_dataset
reidentify_1 | write_data_element(fp, dataset.get_item(tag), dataset_encoding)
reidentify_1 | File "/usr/local/lib/python3.8/site-packages/pydicom/filewriter.py", line 485, in write_data_element
reidentify_1 | writer_function(buffer, data_element)
reidentify_1 | File "/usr/local/lib/python3.8/site-packages/pydicom/filewriter.py", line 338, in write_number_string
reidentify_1 | val = str(val)
reidentify_1 | File "/usr/local/lib/python3.8/site-packages/pydicom/valuerep.py", line 344, in __str__
reidentify_1 | return super(DSfloat, self).__str__()
reidentify_1 | File "/usr/local/lib/python3.8/site-packages/pydicom/valuerep.py", line 347, in __repr__
reidentify_1 | return "\"" + str(self) + "\""非常感谢你的帮助。
发布于 2019-10-17 19:18:08
此bug的起源描述如下:
Python3.8从int和object.repr().中移除str(),现在调用对象。 DSfloat.str(),DSfloat.repr()和IS.repr()都调用父类str(),后者调用子类‘repr()方法等等,这将导致递归错误。 这一公共关系: 修复DSfloat.__str__(),方法是创建一个调用repr(),删除“mark. DSfloat.__repr__()”,方法是调用父类的__repr__(),而不是str(),方法是以类似于DSfloat.__str__()的方式实现IS.__str__(),并使IS.__repr__()与 DSfloat.repr()
您可以尝试直接从git安装它:
pip uninstall pydicom
pip install git+https://github.com/pydicom/pydicom.githttps://stackoverflow.com/questions/58432935
复制相似问题