Exemplo n.º 1
0
 def test_remove_tag(self):
     print("Test deid.dicom.tags remove_tag")
     from deid.dicom.tags import remove_tag
     dicom = get_dicom(self.dataset)
     self.assertTrue('PatientID' in dicom)
     updated = remove_tag(dicom, field='PatientID')
     self.assertTrue("PatientID" not in updated)
Exemplo n.º 2
0
def _remove_tag(dicom, item, field, value=None):
    """A wrapper to handle removal of a tag by calling tags.remove_tag.
       The user can optionally provide a value with a function
       to determine if a tag should be removed (returns True or False)
    """
    value = value or ""
    do_removal = True

    # The user can optionally provide a function to return a boolean
    if re.search("[:]", value):
        value_type, value_option = value.split(":", 1)
        if value_type.lower() == "func":

            # An item must be provided
            if item == None:
                bot.warning(
                    "The item parameter (dict) with values must be provided for a REMOVE func:%s"
                    % value_option)

            # The function must be included in the item
            if value_option not in item:
                bot.warning("%s not found as key included with item." %
                            value_option)

            # To the removal, this should return True/False
            # The calling function (currently) is required to handle parsing fields
            # that are tags.
            do_removal = item[value_option](dicom, value, field)
            if not isinstance(do_removal, bool):
                bot.warning(
                    "function %s returned an invalid type %s. Must be bool." %
                    (value_option, type(do_removal)))

        # A filter such as contains, notcontains, equals, etc.
        elif value_type.lower() in value_filters:

            # These functions are known to return boolean
            do_removal = apply_filter(
                dicom=dicom,
                field=field,
                filter_name=value_type,
                value=value_option or None,
            )

        else:
            bot.exit("%s is an invalid variable type for REMOVE." % value_type)

    if do_removal:
        dicom = remove_tag(dicom, field)
    return dicom