When assigning a value to a DataElement with a VR of 'SQ', there is no validation to ensure that it is actually a Sequence. Instead, it quietly accepts the assignment, but then exhibits strange behavior:
from dicom.dataset import Dataset
d = Dataset()
d.BeamSequence = [1,2,3]
d
Traceback (most recent call last):
File "", line 1, in
File "dicom/dataset.py", line 536, in str
return self._PrettyStr()
File "dicom/dataset.py", line 465, in _PrettyStr
strings.append(dataset._PrettyStr(indent+1))
AttributeError: 'int' object has no attribute '_PrettyStr'
Obviously, [1,2,3] is not a valid sequence, but was accepted regardless; however, attributes and methods of Sequence are necessary
Correct behavior should be:
from dicom.dataset import Dataset
from dicom.sequence import Sequence
d = Dataset()
d.BeamSequence = Sequence()
d
(300a, 00b0) Beam Sequence 0 item(s) ----
I've attached a patch for dataelem that checks that the input is a Sequence or attempts to convert it to a Sequence if possible.
If using Dataset.add_new() to add DataElements, you actually had to specify the VR, so it was ok to make the user provide a valid input; however with dot assignment (shown above), it could be a little misleading because you aren't specifying the VR explicitly.
From Suever@gmail.com on February 10, 2012 10:42:37
When assigning a value to a DataElement with a VR of 'SQ', there is no validation to ensure that it is actually a Sequence. Instead, it quietly accepts the assignment, but then exhibits strange behavior:
Obviously, [1,2,3] is not a valid sequence, but was accepted regardless; however, attributes and methods of Sequence are necessary
Correct behavior should be:
I've attached a patch for dataelem that checks that the input is a Sequence or attempts to convert it to a Sequence if possible.
If using Dataset.add_new() to add DataElements, you actually had to specify the VR, so it was ok to make the user provide a valid input; however with dot assignment (shown above), it could be a little misleading because you aren't specifying the VR explicitly.
Attachment: dataelem.diff
Original issue: http://code.google.com/p/pydicom/issues/detail?id=111