delete a list of attributes

191 views
Skip to first unread message

John Miller

unread,
Aug 20, 2014, 7:34:24 AM8/20/14
to pyd...@googlegroups.com


Hi folks,

Thank you very much for your efforts to make pydicom available!

I was wondering if there is a way to feed the del ds.attribute function with a variable, preferably from a list.
The idea is: make a list of attributes, check whether ds includes a certain attribute of that list and if so delete that attribute. (trying to delete an attribute which is not element of the dicom-file caused an error)

[...]
unwanted_attributes = ['ReferringPhysicianName','StudyDescription',etc]
number_of_entries = len(unwanted_attributes)

    for x in range (0,number_of_entries):
        unwanted_attribute = unwanted_attributes[x]
        if unwanted_attribute in ds ==True:
            del ds.unwanted_attribute
[...]

From my understanding the function is searching ds for the literal attribute "unwanted_attribute" which of course is causing an error. I tried different things like del ds.unwanted_attributes[x],... but with no success so far.

Do you have any insight you can share?
Many thanks!
Martin

Eli Stevens (Gmail)

unread,
Aug 20, 2014, 12:18:42 PM8/20/14
to pyd...@googlegroups.com
You might try delattr(ds, unwanted_attribute), though I'm not certain
if that will work on Dataset objects. del ds[unwanted_attribute] might
also work, not sure.

Aside, you can also structure the loop like so:

for unwanted_attribute in unwanted_attributes:

And skip the len/range/[x] parts.
> --
> You received this message because you are subscribed to the Google Groups
> "pydicom" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to pydicom+u...@googlegroups.com.
> To post to this group, send email to pyd...@googlegroups.com.
> Visit this group at http://groups.google.com/group/pydicom.
> For more options, visit https://groups.google.com/d/optout.

Jonathan Suever

unread,
Aug 20, 2014, 1:21:02 PM8/20/14
to pyd...@googlegroups.com
Either del or delattr will successfully remove the field from the DICOM dataset. Python typically prefers catching an exception to checking explicitly that the field is a member of the dataset. So something like:

for attr in unwanted_attributes:
    try:
        delattr(ds, attr)
    except AttributeError:
        continue


If you want to tackle this in one line I'd go with the following which I think is pretty straightforward:

[delattr(ds, attr) for attr in unwanted_attributes if hasattr(d, attr)]


-Jonathan

John Miller

unread,
Sep 10, 2014, 11:23:53 AM9/10/14
to pyd...@googlegroups.com



Hi Eli and Suever,

many thanks for your replies. I apologize for my late response, I actually thought my question hasn't been posted after it hasn't shown up after an hour.
I solved it as follows:
    for name in ['some attributes','some more']:
        if name in ds:
            delattr(ds, name)
Probably not as elegant as your solutions, but it works for me.
Again, thank you both for the effort!



Reply all
Reply to author
Forward
0 new messages