Unable to assign a value inside of Dicom element

258 views
Skip to first unread message

Jared Newton

unread,
Sep 16, 2017, 12:11:28 AM9/16/17
to pydicom
I'm attempting to edit the value of an element inside of a Dicom file but am getting an error. I'm pretty new to python so I'm hoping this may be pretty simple. The error I get is, "TypeError: 'DataElement' object does not support item assignment". I was able to change some other values in different fields but this one doesn't seem to want to let me change it. I think the datatype or something as i cannot use the ".value" to return a number. The piece of code I am trying to execute looks like this.

ds[0x300a00b0][1][0x300a0111][1][0x300a011a][0][0x300a011c][60]=ds[0x300a00b0][1][0x300a0111][1][0x300a011a][0][0x300a011c][60]-1

TypeError: 'DataElement' object does not support item assignment

executed on its own

 print ds[0x300a00b0][1][0x300a0111][1][0x300a011a][0][0x300a011c][60]

returns 0

And if I try to execute the following I get

ds[0x300a00b0][1][0x300a0111][1][0x300a011a][0][0x300a011c][60].value

AttributeError: 'DSfloat' object has no attribute 'value'

Finally executing the following

print len(ds[0x300a00b0][1][0x300a0111][t][0x300a011a][0][0x300a011c].value)

returns 120



Anyone have any ideas on how to properly do this? Thank you.

Darcy Mason

unread,
Sep 16, 2017, 12:52:15 AM9/16/17
to pydicom
Quick answer:
ds[0x300a00b0][1][0x300a0111][1][0x300a011a][0][0x300a011c].value[60]=ds[0x300a00b0][1][0x300a0111][1][0x300a011a][0][0x300a011c].value[60]-1
should work.

Explanation:
With code using tag numbers as you have, pydicom returns the DataElement rather than the value of it.  But if you ask to get an index from that data element, pydicom will try to look up that index in the data element's value, which works for Sequence data elements.  However, setting does not do this.  So in your code, everything down to the last element is getting, but it is the final list item that is going to be set, then the '.value' has to be there.

This lack of symmetry between getting a data element item and setting an item should probably be fixed in pydicom. We'd have to just think through whether there are any other consequences.

Suggestions for more pythonic/'pydicomic' code:
You can also use the decrement operator here to save some retyping:
ds[0x300a00b0][1][0x300a0111][1][0x300a011a][0][0x300a011c].value[60] -= 1

and using keywords rather than tag numbers makes it easier to figure these kinds of things out, and no need to worry about ".value":
cp = ds.BeamSequence[1].ControlPointSequence[1]
leafjaw = cp.BeamLimitingDevicePositionSequence[0].LeafJawPositions
leafjaw[60] -= 1

Jared Newton

unread,
Sep 18, 2017, 1:42:01 PM9/18/17
to pydicom
Worked like a charm. I was hoping it was something relatively simple I was missing. Thank you for the quick reply.
Reply all
Reply to author
Forward
0 new messages