I am trying to create a program that can add multiple undefined tags in DICOM files. For the moment, I'd import the tags and values thanks to a CSV file.
This is my current code, it parse through the csv file and add the tags to the dataset of the dicom file
for index, row in tags_df.iterrows():
ds = dicom.read_file(row["Label")
row_tags = row.drop("Label")
for col, val in row_tags.items():
try:
tag = Tag(col)
except:
print(f"{col} is not a valid tag")
exit(-1)
print(f"{tag} {col} : {dictionary_VR(tag)} with {val} encoded by {type(val)}")
ds.add(dicom.DataElement(tag, dictionary_VR(tag), val))
And this is the output on the command line :
(0010, 0010) PatientName : PN with Fly encoded by <class 'str'>
(0010, 0020) PatientID : LO with 42 encoded by <class 'int'>
~/.local/lib/python3.10/site-packages/pydicom/valuerep.py:443: UserWarning: A value of type 'int' cannot be assigned to a tag with VR LO.
warnings.warn(msg)
(0020, 0010) StudyID : SH with 4123274 encoded by <class 'int'>
~/.local/lib/python3.10/site-packages/pydicom/valuerep.py:443: UserWarning: A value of type 'int' cannot be assigned to a tag with VR SH.
warnings.warn(msg)
(0020, 000d) StudyInstanceUID : UI with 1.2.840.113745.101000.1008000.38179.6792.6324567 encoded by <class 'str'>
(0020, 000e) SeriesInstanceUID : UI with 1.3.12.2.1107.5.99.1.24063.4.0.446793548272429 encoded by <class 'str'>
(0020, 0011) SeriesNumber : IS with 4 encoded by <class 'int'>
(0020, 0013) InstanceNumber : IS with 1 encoded by <class 'int'>
(0008, 0022) AcquisitionDate : DA with 20231110 encoded by <class 'int'>
~/.local/lib/python3.10/site-packages/pydicom/valuerep.py:443: UserWarning: A value of type 'int' cannot be assigned to a tag with VR DA.
warnings.warn(msg)
(0028, 0100) BitsAllocated : US with 16 encoded by <class 'int'>
How can I automatically check that the when I add the tag to the Dataset, the "val" is in the right type? (Depending on the known ValueRepresentation).
For example, this is a DA variable "20231110", pandas will read it as an integer but I need to have it as a string. But at the same moment, I can't cast str() to every variable, because BitsAllocated Tag needs an int (or sends a warning)
As mentionned on a comment made on this post from @MrBeanBremen, pydicom has sets that contains what types are needed for which VR.
The code looks like this now :
The warning were count as errors as pydicom only sends a warning if the data isn't the right type.