dicompydicom

How to edit nested/child DICOM tags for sequences using pyDicom?


I'm trying to set all the TIME (VR = TM) tags contained within the DICOM dataset to a placeholder value, using pyDicom.

I can remove the value of all the TIME (VR = TM) tags that are within the root of the DICOM metadata:

TIME_TAGS = [
    (0x10, 0x32),  # Patient's Birth Time
    (0x40, 0x245),  # Performed Procedure Step Start Time
    (0x8, 0x13),  # Instance Creation Time
    (0x8, 0x30),  # Study Time
    (0x8, 0x31),  # Series Time
    (0x8, 0x32),  # Acquisition Time
    (0x8, 0x33),  # Content Time
]
TIME_VAL_REPLACEMENT = '120000'


def _clear_times(dir_name: str) -> None:
    '''
    Set all DICOM standard (i.e. non-vendor) time tags
    to a non-unique value (defined in _presets.py)

    dir_name:
        full path of the directory to process
    '''
    for dcm_file in os.listdir(dir_name):
        dcm_file = os.path.join(dir_name, dcm_file)
        # _presets defines what time tags to change
        for time_str in TIME_TAGS:
            dcmfile = pydicom.dcmread(dcm_file)
            if dcmfile.get(time_str, None) and dcmfile.get(time_str,
                                                           None).VR == 'TM':
                logging.debug("Removing time (%s)", time_str)
                new_data = pydicom.dataelem.DataElement(
                    time_str, 'TM', TIME_VAL_REPLACEMENT)
                dcmfile[time_str] = new_data
                dcmfile.save_as(dcm_file)
            else:
                logging.debug("%s not set", time_str)

However, this misses nested/child tags for sequences.

What's the best way, using pyDicom, to remove all relevant nested/child tags too?


Solution

  • Using the walk method with a callback is probably the easiest way to do this. You could see the source code for remove_private_tags() as an example, but your function would check for VR of 'TM' and act on those data elements.