carlosqueiroz / pydicom

Automatically exported from code.google.com/p/pydicom
0 stars 0 forks source link

dataset.get() fails when passed type other than string or Tag #72

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm using pydicom 0.9.3.

The get() method in dataset.py doesn't work correctly when passed a key 
that's not a string or a Tag object (e.g. if I pass the key as a tuple). 
Specifically, get() returns the default even if the tag is present. 

Here's an example using the "rtplan.dcm" file that's part of the pydicom
distribution and using a tuple-style tag of (0x0010, 0x0010).

$ python
Python 2.5.1 (r251:54863, Nov 17 2007, 21:19:53) 
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dicom
>>> ds=dicom.read_file("rtplan.dcm")
>>> t = (0x0010, 0x0010)
>>> t in ds
True
>>> ds[t]
(0010, 0010) Patient's Name                      PN: 'Last^First^mid^pre'
>>> ds.get(t)
>>> ds.get(t, "foo")
'foo'
>>> 

I patched dataset.py (patch attached) and that seems to fix the problem. In
the interactive session below you can see that the patched get() behaves
correctly when fed (0x0010, 0x0010) as a tuple, string, long, or Tag
object. It also correctly returns the default when fed a non-existent tuple.

$ python
Python 2.5.1 (r251:54863, Nov 17 2007, 21:19:53) 
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dicom
>>> ds=dicom.read_file("rtplan.dcm")
>>> t = (0x0010, 0x0010)
>>> t in ds
True
>>> ds[t]
(0010, 0010) Patient's Name                      PN: 'Last^First^mid^pre'
>>> ds.get(t)
(0010, 0010) Patient's Name                      PN: 'Last^First^mid^pre'
>>> ds.get(t, "foo")
(0010, 0010) Patient's Name                      PN: 'Last^First^mid^pre'
>>> t = 0x0010<<16 | 0x0010
>>> ds.get(t)
(0010, 0010) Patient's Name                      PN: 'Last^First^mid^pre'
>>> ds.get("PatientsName")
'Last^First^mid^pre'
>>> tag = ds[(0x0010, 0x0010)].tag
>>> type(tag)
<class 'dicom.tag.Tag'>
>>> ds.get(tag)
(0010, 0010) Patient's Name                      PN: 'Last^First^mid^pre'
>>> t = (0x9999, 0x9999)
>>> ds.get(t, "foo")
'foo'

Original issue reported on code.google.com by NikitaTh...@gmail.com on 22 Jan 2010 at 9:10

Attachments:

GoogleCodeExporter commented 9 years ago
NikitaTheSpider, thanks for finding this bug and for contributing the patch. I 
made one 
small change -- raising a TypeError if the key was neither string nor some kind 
of tag, 
because no other kind of keys should be allowed in a Dataset anyway.

Fixed in revision bb7b95651c.

Original comment by darcymason@gmail.com on 25 Jan 2010 at 12:46