Exemplo n.º 1
0
def _perform_action(dicom, field, action, value=None, item=None):
    '''_perform_action is the base function for performing an action.
       perform_action (above) typically is called using a loaded deid,
       and perform_addition is typically done via an addition in a config
       Both result in a call to this function. If an action fails or is not
       done, None is returned, and the calling function should handle this.
    '''
    if action not in valid_actions:
        bot.warning('''%s in not a valid choice [%s]. 
                       Defaulting to blanked.''' %
                    (action, ".".join(valid_actions)))
        action = "BLANK"

    if field in dicom and action != "ADD":

        # Blank the value
        if action == "BLANK":
            dicom = blank_tag(dicom, field)

        # Code the value with something in the response
        elif action == "REPLACE":
            newvalue = parse_value(item, value, field)
            if newvalue is not None:
                # If we make it here, do the replacement
                dicom = update_tag(dicom, field=field, value=newvalue)
            else:
                bot.warning("REPLACE %s unsuccessful" % field)

        elif action == "SCREEN":
            newvalue = parse_value(item, value, field)
            if newvalue is not None:
                # If we make it here, do the replacement
                dicom = update_tag(dicom, field=field, value=newvalue)
                bot.warning("SCREEN identified a hit in %s" % field)

        # Code the value with something in the response
        elif action == "JITTER":
            value = parse_value(item, value, field)
            if value is not None:

                # Jitter the field by the supplied value
                dicom = jitter_timestamp(dicom=dicom, field=field, value=value)
            else:
                bot.warning("JITTER %s unsuccessful" % field)

        # elif "KEEP" --> Do nothing. Keep the original

        # Remove the field entirely
        elif action == "REMOVE":
            dicom = remove_tag(dicom, field)

    elif action == "ADD":
        value = parse_value(item, value, field)
        if value is not None:
            dicom = add_tag(dicom, field, value, quiet=True)

    return dicom
Exemplo n.º 2
0
    def _run_action(self, field, action, value=None):
        """perform_action (above) typically is called using a loaded deid,
           and _run_addition is typically done via an addition in a config
           Both result in a call to this function. If an action fails or is not
           done, None is returned, and the calling function should handle this.
        """
        # Blank the value
        if action == "BLANK":
            self.blank_field(field)

        # Unlikely to be called from perform_action
        elif action == "ADD":
            self.add_field(field, value)

        # Code the value with something in the response
        elif action == "REPLACE":
            self.replace_field(field, value)

        # Code the value with something in the response
        elif action == "JITTER":
            value = parse_value(item=self.lookup,
                                dicom=self.dicom,
                                value=value,
                                field=field)
            if value is not None:

                # Jitter the field by the supplied value
                jitter_timestamp(dicom=self.dicom,
                                 field=field.element.keyword,
                                 value=value)
            else:
                bot.warning("JITTER %s unsuccessful" % field)

        # elif "KEEP" --> Do nothing. Keep the original

        # Remove the field entirely
        elif action == "REMOVE":

            # If a value is defined, parse it (could be filter)
            do_removal = True
            if value != None:
                do_removal = parse_value(item=self.lookup,
                                         dicom=self.dicom,
                                         value=value,
                                         field=field)

            if do_removal == True:
                self.delete_field(field)
Exemplo n.º 3
0
    def add_field(self, field, value):
        """add a field to the dicom. If it's already present, update the value.
        """
        value = parse_value(item=self.lookup,
                            value=value,
                            field=field,
                            dicom=self.dicom)

        # Assume we don't want to add an empty value
        if value is not None:

            # If provided a field object, create based on keyword or tag identifer
            name = field
            if isinstance(field, DicomField):
                name = field.element.keyword or field.stripped_tag

            # Generate a tag item, add if it's a name found in the dicom dictionary
            tag = get_tag(name)

            # Second try, it might be a private (or other numerical) string identifier
            if not tag:
                tag = add_tag(name)

            if tag:
                uid = str(tag["tag"])
                element = DataElement(tag["tag"], tag["VR"], value)
                self.dicom.add(element)
                self.fields[uid] = DicomField(element, name, uid)
                bot.warning("Cannot find tag for field %s, skipping." % name)
Exemplo n.º 4
0
def _perform_expanded_action(dicom,
                             expanded_field,
                             action,
                             value=None,
                             item=None):
    """akin to _perform_action, but we expect to be dealing with an expanded
       sequence, and need to step into the Dicom data structure. 

       Add, jitter, and delete are currently not supported.

       The field is expected to have the format FieldA__FieldB where the
       last one is where we want to do the replacement.
    """
    field = expanded_field.split("__")[-1]

    while field != expanded_field:
        next_field, expanded_field = expanded_field.split("__", 1)

        # Case 1: we have a Dataset
        if isinstance(dicom, Dataset):
            dicom = dicom.get(next_field)

        elif isinstance(dicom, Sequence):
            for sequence in dicom:
                for subitem in sequence:
                    if subitem.keyword == next_field:
                        dicom = subitem
                        break

    # Field should be equal to expanded_field, and in dicom
    if isinstance(dicom, Dataset):
        return _perform_action(dicom=dicom,
                               field=field,
                               item=item,
                               action=action,
                               value=value)

    elif isinstance(dicom, Sequence):
        for sequence in dicom:
            for subitem in sequence:
                if subitem.keyword == field:
                    dicom = subitem
                    break

    if not dicom:
        return

    # Not sure if this is possible
    if dicom.keyword != field:
        bot.warning("Early return, looking for %s, found %s" %
                    (field, dicom.keyword))
        return

    # Blank the value
    if action == "BLANK":
        if dicom.VR not in ["US", "SS"]:
            dicom.value = ""

    # Code the value with something in the response
    elif action == "REPLACE":

        value = parse_value(item, value, field)
        if value is not None:
            dicom.value = value