admin管理员组文章数量:1245816
I have dicom files from different sources and sometimes i get error like this:
AttributeError: 'FileDataset' object has no attribute 'PatientAge' because some files doesn't have this attribute.
I've created function to check objects for errors:
def check_if_exists(self, object_attr):
try:
return str(object_attr)
except:
return 'NULL'
But I get error in setText line before executing code from my function:
self.label.setText(self.check_if_exists(ds.PatientAge))
I have several labels like this so I wanted to create one function and check if each attribute exists but I'm not sure how to do this. Sometimes I use ds.PatientName, ds.PatientAge and sometimes ds[0x0022,0x7031].value. Using try except: or hasattr() function for each label is redundant and i'm trying to get the best solution.
Can you give me some ideas?
I have dicom files from different sources and sometimes i get error like this:
AttributeError: 'FileDataset' object has no attribute 'PatientAge' because some files doesn't have this attribute.
I've created function to check objects for errors:
def check_if_exists(self, object_attr):
try:
return str(object_attr)
except:
return 'NULL'
But I get error in setText line before executing code from my function:
self.label.setText(self.check_if_exists(ds.PatientAge))
I have several labels like this so I wanted to create one function and check if each attribute exists but I'm not sure how to do this. Sometimes I use ds.PatientName, ds.PatientAge and sometimes ds[0x0022,0x7031].value. Using try except: or hasattr() function for each label is redundant and i'm trying to get the best solution.
Can you give me some ideas?
Share Improve this question asked Feb 15 at 4:30 James JacquesJames Jacques 2,0612 gold badges7 silver badges9 bronze badges2 Answers
Reset to default 2Dataset
has a get
method that returns a default value if the attribute doesn't exist. I would suggest something like:
self.label.setText(ds.get("PatientAge", "N/A"))
You could of course use "NULL" as in your example instead of "N/A".
Edit: you might have to do str(ds.get(...))
for non-string values.
Edit2: for tags as a number:
You can use get
with a numeric Tag value, and then take the .value
after. So the default has to be something with a .value to it:
class Null:
value = "Null"
self.label.setText(str(ds.get((0x0022, 0x7031), Null).value))
You can use a function like this
def get_dicom_attribute(dataset, attribute_or_tag):
try:
if isinstance(attribute_or_tag, str): # if it is a string
if attribute_or_tag.startswith('0x'): # if it is an hex tag, ex. "0x0022,0x7031"
tag = tuple(int(x, 16) for x in attribute_or_tag[2:].split(',')) # convert hex string to tuple tag
return str(dataset[tag].value)
else:
return str(getattr(dataset, attribute_or_tag)) # if it is name, ex. "PatientName"
elif isinstance(attribute_or_tag, tuple): # if it is a tuple tag, ex. (0x0022,0x7031)
return str(dataset[attribute_or_tag].value)
else:
return "NULL"
except (AttributeError, KeyError, TypeError):
return "NULL"
An example of using this function
import pydicom
ds = pydicom.dcmread("path/to/your/dicom.dcm")
value = get_dicom_attribute(ds, 'PatientName')
value = get_dicom_attribute(ds, '0x0022,0x7031')
value = get_dicom_attribute(ds, (0x0022,0x7031))
本文标签: pythonHow to avoid AttributeError in multiple labelsStack Overflow
版权声明:本文标题:python - How to avoid AttributeError in multiple labels - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740252006a2248674.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论