0

I'm currently trying to edit a private dicom tag which is causing problems with a radiotherapy treatment, using pydicom in python. Bit of a python newbie here so bear with me.

The dicom file imports correctly into python; I've attached some of the output in the first image from the commands

ds = dicomio.read_file("xy.dcm")
print(ds)

This returns the following data: pydicom output

The highlighted tag is the one I need to edit.

When trying something like

ds[0x10,0x10].value

This gives the correct output:

'SABR Spine'

However, trying something along the lines of

ds[3249,1000]

or

ds[3249,1000].value

returns the following output:

> Traceback (most recent call last):
  File "<pyshell#64>", line 1, in <module>
    ds[3249,1000].value
  File "C:\Users\...\dataset.py", line 317, in __getitem__
    data_elem = dict.__getitem__(self, tag)
KeyError: (0cb1, 03e8)

If I try accessing [3249,1010] via the same method, it returns a KeyError of (0cb1, 03f2).

I have tried adding the tag to the _dicom_dict.py file, as highlighted in the second image:

end of _dicom_dict.py

Have I done this right? I'm not even sure if I'm accessing the tags correctly - using

ds[300a,0070]

gives me 'SyntaxError: invalid syntax' as the output, for example, even though this is present in the file as fraction group sequence. I have also been made aware that [3249,1000] is connected to [3249,1010] somehow, and apparently since they are proprietary tags, they cannot be edited in Matlab, however it was suggested they could be edited in python for some reason.

Thanks a lot

Suever
  • 64,497
  • 14
  • 82
  • 101
Piethon
  • 227
  • 2
  • 11

2 Answers2

1

It looks like your dicomio lookup is converting all inputs to hexadecimal.

You could try:

ds[0x3249,0x1000]

This should prevent any forced conversion to hexadecimal.

You can apparently access them directly as strings:

ds['3249', '1000']

However, your issue is that you are trying to access a data element that is nested several layers deep. Based on your output at the top, I would suggest trying:

first_list_item = ds['300a', '0070'][0]

for item in first_list_item['300c', '0004']:
    print(item['3249','1000'])

Essentially, a data element from the top level Dataset object can be either a list or another Dataset object. Makes parsing the data a little harder, but probably unavoidable.

Have a look at this for more info.

Andrew Guy
  • 9,310
  • 3
  • 28
  • 40
  • Using `ds[0x3249,0x1000]` returns the same error message as before, but with `KeyError: (3249,1000)`. So I think you're right about the hexadecimal thing. Does this mean I need to modify my _dicom_dict.py entry somehow? – Piethon Dec 12 '16 at 02:52
  • What about `ds[0x32491000]`? – Andrew Guy Dec 12 '16 at 02:59
  • This returns the same: `KeyError: (3249,1000)`. – Piethon Dec 12 '16 at 03:03
  • Also, `ds[int(3249, base=16), int(1000, base=16)]` returns `TypeError: int() can't convert non-string with explicit base`. – Piethon Dec 12 '16 at 03:05
  • Yep, sorry, those should have been strings. – Andrew Guy Dec 12 '16 at 03:06
  • 1
    Added edits. I think your issue is that you are trying to access nested data with a single dictionary call. You have to drill down to the data you want first. You can see the nested structure in the first image you supplied. – Andrew Guy Dec 12 '16 at 03:38
  • Awesome thanks heaps! You were right, I failed to access the nested data correctly. All it took to edit the value in the end was this line: `ds[0x300a,0x0070][0][0x300c,0x0004][1][0x3249,0x1000].value = b'99 '` where the previous value was b'4 '. – Piethon Dec 13 '16 at 02:25
0

As Andrew Guy notes in his last comment, you need to get the first sequence item for 300a,0070. Then get the second sequence item from the 300c,0004 sequence in that item. In that sequence item, you should be able to get the 3249,1000 attribute.

cneller
  • 1,542
  • 1
  • 15
  • 24