group length question

46 views
Skip to first unread message

mrbago

unread,
Oct 17, 2011, 10:34:13 PM10/17/11
to pydicom
Hi all,
I'm trying to use pydicom to modify and save some mri images. When I
try to read this images in another dicom reader it complains that the
group lengths are not correct. It seems to me that the simplest
solution is to remove the group lengths because they're not required
(except for group 2). Is there an easy way of doing this using
pydicom?

Thanks for you help
Bago

Darcy Mason

unread,
Oct 18, 2011, 9:55:32 AM10/18/11
to pydicom
Hi Bago,

pydicom follows the python method for deleting, using the del keyword:

>>> del dataset[tag]

or

>>> del dataset.name

In your case, with the group lengths, if I recall correctly they will
all have the same name, so you would have to use the tag to make sure
of deleting the correct ones.

Now, to answer the bigger issue, pydicom should recalculate the group
lengths, so what you are seeing is a bug. I thought that the code
either deleted them or recalculated, but after a quick review of the
code I only see the recalc happening for the file meta data. Would you
mind entering it as an issue on the issue list? And if possible, is
there a non-confidential file you could provide?

Regards,
Darcy

mrbago

unread,
Oct 18, 2011, 10:18:01 AM10/18/11
to pydicom
I believe this is already described in issue #30.

http://code.google.com/p/pydicom/issues/detail?id=30

So in my case I know which fields I'm modifying so I know the handful
of Group Length tags that I might need to remove, but because I can't
know for sure that they exist I need to wrap each del in a try-except.
Is there a better way to handle this, for example could I get a list
of all the Group Length tags and just remove them all?

try:
del dataset[tag1]
except KeyError:
pass
try:
del dataset[tag2]
except KeyError:
pass
...

Bago

Darcy Mason

unread,
Oct 18, 2011, 10:39:53 PM10/18/11
to pydicom
On Oct 18, 10:18 am, mrbago <mrb...@gmail.com> wrote:
> I believe this is already described in issue #30.
>
> http://code.google.com/p/pydicom/issues/detail?id=30

Huh. So it is; I forgot that was there. Thanks for pointing that out.

>
> So in my case I know which fields I'm modifying so I know the handful
> of Group Length tags that I might need to remove, but because I can't
> know for sure that they exist I need to wrap each del in a try-except.
> Is there a better way to handle this, for example could I get a list
> of all the Group Length tags and just remove them all?
>

I'm thinking the easiest way would be to mimic the remove_private_tags
function (in dataset.py). That is, use the dataset.walk() function and
check for any tags with element 0 and remove them. Should look
something like (untested):
def remove_grouplength_tags(ds):
def RemoveCallback(dataset, data_element):
if data_element.tag.element == 0:
del dataset[data_element.tag]
ds.walk(RemoveCallback)

Then you would be sure that they are gone from all nested datasets.

But ... if you want to do something simpler, to answer your question
about the try/except, you could test for the existence using python
'in':

if tag1 in dataset:
del dataset[tag1]
etc.

Regards,
Darcy

Bago

unread,
Oct 19, 2011, 3:02:50 PM10/19/11
to pyd...@googlegroups.com
Seems like either of those approaches should work well for me.

On a related note, maybe I've gone overboard with the whole 'easier to
ask for forgiveness' thing, I prety much forgot that there are other
ways to check if something exists/is-present.

Thanks for the help,
Bago

Reply all
Reply to author
Forward
0 new messages