RawDataElement vs DataElement question

267 views
Skip to first unread message

Adit Panchal

unread,
Apr 15, 2010, 5:07:09 PM4/15/10
to pyd...@googlegroups.com
I am running some code to iterate over the DataElements in a dataset
but have noticed some curious behavior.

When I do this in a python interactive interpreter or in ipython, the
type of each DataElement is DataElement. However, in my project for
whatever reason, using the same statements it shows up as a
RawDataElement.

Example:

import dicom
ds = dicom.read_file("somefile.dcm")
print type(ds.values()[0])

Interpreter will print: <class 'dicom.dataelem.DataElement'>
Code will print: <class 'dicom.dataelem.RawDataElement'>

Is there something obvious that I am missing?

Thanks,

Adit

Darcy Mason

unread,
Apr 16, 2010, 9:22:23 AM4/16/10
to pydicom
Hi Adit,

I tried this in ipython and python interpreter sessions and I'm always
getting RawDataElement. But it must come down to something triggering
access to the data element value through the __getattr__ or
__getitem__ methods of Dataset, which will cause the conversion from
RawDataElement to DataElement. Displaying the ds in the interpreter
will do that -- all elements will be converted. Perhaps also the
autocomplete in ipython might somehow ask for the value.

You could force the conversion of all elements if you wanted. Just
iterate through them: for example:
>> for data_elem in ds: pass
should do it.

I'm considering whether to change pydicom to force this conversion
anyway, because the raw vs regular data element is causing so many
problems. I'll have to evaluate whether this can be done without
losing too much of the performance gains that going to raw reading
gave.

Regards,
Darcy

--
You received this message because you are subscribed to the Google Groups "pydicom" group.
To post to this group, send email to pyd...@googlegroups.com.
To unsubscribe from this group, send email to pydicom+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pydicom?hl=en.

Adit Panchal

unread,
Apr 18, 2010, 12:54:54 AM4/18/10
to pyd...@googlegroups.com
Hi Darcy,

Thanks for the suggestion about iterating through the dataset. That
seemed to work fine.

I was actually trying to access the dataset as a list using the
values() method and was trying to access each element by list number
without having to iterate through them all (this assumes that I know
what item I want to access, since I know the length of the dataset).
Via this method, it won't trigger access to the data element value I
assume based on what you described.

Thanks,

Adit

Darcy Mason

unread,
Apr 18, 2010, 8:04:28 PM4/18/10
to pydicom
On Apr 18, 12:54 am, Adit Panchal <apanc...@bastula.org> wrote:
> ...
> I was actually trying to access the dataset as a list using the
> values() method and was trying to access each element by list number
> without having to iterate through them all (this assumes that I know
> what item I want to access, since I know the length of the dataset).
> Via this method, it won't trigger access to the data element value I
> assume based on what you described.
>

The values() method doesn't give an ordered list -- pydicom inherits
that method from python's dict, and dict does not guarantee any order
at all. However, pydicom does override iteration, because that is such
a common use and yielding the data elements in tag order would almost
certainly be wanted.

To get a data element by list position, you could do something like:
>>> sorted_items = sorted(ds.items())
>>> data_elem_i = sorted_items[i][1]
But you really have to be sure you know exactly what data element is
at that position.

Is speed the issue? You can get a fast lookup with just ds[tag], and
that item will be converted from raw before being returned. No
iteration or sorting, and no conversion except for the ones you ask
for.

-Darcy
Reply all
Reply to author
Forward
0 new messages