最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to avoid AttributeError in multiple labels - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a comment  | 

2 Answers 2

Reset to default 2

Dataset 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))
发布评论

评论列表(0)

  1. 暂无评论