I'm trying to convert a slew of DTI siemens DICOMs to NifTi using freesurfer's dcm2nii
utility but am failing on some files because they are missing the DiffusionGradientDirection tag (0x19,0x100E)
, which is necessary to generate the .bvec
and .bval
files. It's not that the tags don't have values, they don't appear to be there at all.
ds[0x19,0x100E] Traceback (most recent call last): File "", line 1, in File "/space/jazz/1/users/gwarner/anaconda/lib/python2.7/site-packages/pydicom-0.9.9-py2.7.egg/dicom/dataset.py", line 277, in getitem data_elem = dict.getitem(self, tag) KeyError: (0019, 100e)
I tried adding it but got the following error:
ds[0x19,0x100E].value = 'yes' Traceback (most recent call last): File "", line 1, in File "/space/jazz/1/users/gwarner/anaconda/lib/python2.7/site-packages/pydicom-0.9.9-py2.7.egg/dicom/dataset.py", line 277, in getitem data_elem = dict.getitem(self, tag) KeyError: (0019, 100e)
Is there a way I can manually insert this tag ?
To add a new private data element in pydicom to a dataset ds, the add_new
method can be used:
ds.add_new(tag, VR, value)
For this case, looking up the private tag in pydicom's _private_dict.py file (derived from gdcm's private tag info):
'SIEMENS MR HEADER': {
...
'0019xx0e': ('FD', '3', 'DiffusionGradientDirection', ''),
It is a repeating group kind of tag, where the xx can change to allow multiple data elements of this same type. Here FD is a double float, and 3 is the multiplicity (three values expected).
So in this case, to add the data element you need should look something like:
ds.add_new(0x19100e, 'FD', [0,1,0]) # I have no idea what this last vector should actually be
However, as malat pointed out, there needs to be a private creator tag as well to introduce the block, for the file to be valid DICOM. If it doesn't exist already you would likely have to add that also. Since you are converting the file to another format, perhaps you don't care if it works by adding only the single tag.
Once the data element has been added, you could change the value using ds[0x19100e].value = ...
as in your original question.
As an aside, add_new
is not needed for keywords that are in the standard dictionary; for those one can just directly set the item by name, e.g. ds.OtherPatientIDs='test'
, even if it does not yet exist in the dataset.