Help: Deleting specific DICOM tags in a nested tree with pydicom
Ede1994 opened this issue · comments
I want to delete specific DICOM tags in a DICOM file, which are contained in a nested tree and which is repeated over and over again, here is an excerpt:
(5200, 9229) Shared Functional Groups Sequence 1 item(s) ----
(0021, 0010) Private Creator LO: 'SIEMENS MR SDS 01'
(0021, 10fe) Private tag data 1 item(s) ----
(0021, 0010) Private Creator LO: 'SIEMENS MR SDS 01'
(0021, 1019) Private tag data OB: Array of 146770 elements
(0021, 1026) Private tag data IS: Array of 38 elements
I've tried several approaches before, but most recently my idea was to do it via a callback function and then use walk(callback[, recursive])
from pydicom. So that I don't have this tree structure any longer I wanted to use iterall()→ Iterator[DataElement]
, so to say flatten the dataset.
def _private_callback(dataset, data_element):
tags_to_remove = [
(0x0021, 0x1019),
(0x0021, 0x1026),
(0x0021, 0x1171)
]
for data_element in dataset.iterall():
if data_element.tag in tags_to_remove:
print(data_element)
# del dataset[data_element.tag]
dcm.walk(_private_callback)
This way all tags from the tags_to_remove list will really be found. Now I want to delete them all, but as soon as I use
del dataset[data_element.tag]
I get the error:
(0021, 1019) Private tag data OB: Array of 146770 elements
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
File ~/.conda/envs/qa_pipeline/lib/python3.9/site-packages/pydicom/tag.py:28, in tag_in_exception(tag)
27 try:
---> 28 yield
29 except Exception as exc:
File ~/.conda/envs/qa_pipeline/lib/python3.9/site-packages/pydicom/dataset.py:2390, in Dataset.walk(self, callback, recursive)
2389 data_element = self[tag]
-> 2390 callback(self, data_element) # self = this Dataset
2391 # 'tag in self' below needed in case callback deleted
2392 # data_element
Cell In[26], line 14, in curves_callback(dataset, data_element)
13 print(data_element)
---> 14 del dataset[data_element.tag]
File ~/.conda/envs/qa_pipeline/lib/python3.9/site-packages/pydicom/dataset.py:636, in Dataset.__delitem__(self, key)
635 elif isinstance(key, BaseTag):
--> 636 del self._dict[key]
637 if self._private_blocks and key.is_private_creator:
KeyError: (0021, 1019)
The above exception was the direct cause of the following exception:
...
30 stack_trace = traceback.format_exc()
31 msg = f"With tag {tag} got exception: {str(exc)}\n{stack_trace}"
---> 32 raise type(exc)(msg) from exc
KeyError: 'With tag (0008, 0005) got exception: (0021, 1019)\nTraceback (most recent call last):\n File "/home/einspaennere/.conda/envs/qa_pipeline/lib/python3.9/site-packages/pydicom/tag.py"
This is an extremely large DICOM header with about 7000 lines.