예제 #1
0
    def components(self, path):
        """Set the element's components str.

        Parameters
        ----------
        str
            The path to use for the current object.
        """
        value = ''
        if '=' in path:
            path, value = path.split('=', 1)

        self._components = path.split('.')

        # Parse current component and set attributes accordingly
        tag = self.tag
        try:
            # Try DICOM dictionary for public elements
            self._entry = get_entry(tag)
        except Exception as exc:
            # Private element
            self._entry = ('UN', '1', 'Unknown', False, 'Unknown')

        # Try to convert value to appropriate type
        self.value = value
예제 #2
0
def set_pydicom_meta_tag(dcm_seq: Union[FileDataset, Dataset], tag, value, force_vr=None):
    # Check tag
    if isinstance(tag, tuple):
        tag = Tag(tag[0], tag[1])

    elif isinstance(tag, list):
        tag = Tag(tag[0], tag[2])

    elif isinstance(tag, Tag):
        pass

    else:
        raise TypeError(f"Metadata tag {tag} is not a pydicom Tag, or can be parsed to one.")

    # Read the default VR information for non-existent tags.
    vr, vm, name, is_retired, keyword = datadict.get_entry(tag)

    if vr == "DS":
        # Decimal string (16-byte string representing decimal value)
        if isinstance(value, Iterable):
            value = [f"{x:.16f}"[:16] for x in value]
        else:
            value = f"{value:.16f}"[:16]

    if tag in dcm_seq and force_vr is None:
        # Update the value of an existing tag.
        dcm_seq[tag].value = value

    elif force_vr is None:
        # Add a new entry.
        dcm_seq.add_new(tag=tag, VR=vr, value=value)

    else:
        # Add a new entry
        dcm_seq.add_new(tag=tag, VR=force_vr, value=value)
예제 #3
0
 def get_entry(self, tag_property):
     """General function for the getters in the code block"""
     index = ['VR', 'VM', 'name', 'is_retired', 'keyword'].index(tag_property)
     if not self.has_x and self.group and self.element:
         try:
             return get_entry(self.tag_as_int)[index]
         except KeyError:
             pass
     return 'Not Found'
예제 #4
0
 def is_multiple_values(value):
     tag = value.tag
     if not tag.is_private:
         vvm = datadict.get_entry(tag)[1].split("-")
         return len(vvm) == 2 or 1 < int(vvm[0])
     elif hasattr(tag, "private_creator"):
         vvm = datadict.get_private_entry(tag,
                                          tag.private_creator)[1].split("-")
         return len(vvm) == 2 or 1 < int(vvm[0])
     else:
         return 1
예제 #5
0
def fix_VM1_callback(dataset, data_element):
    r"""Update the data element fixing VM based on public tag definition

    This addresses the following none conformance for element with string VR having
    a `\` in the their value which gets interpret as array by pydicom.
    This function re-join string and is aimed to be used as callback.

    From the DICOM Standard, Part 5, Section 6.2, for elements with a VR of LO, such as
    Series Description: A character string that may be padded with leading and/or
    spaces. The character code 5CH (the BACKSLASH "\" in ISO-IR 6) shall not be
    present, as it is used as the delimiter between values in multi-valued data
    elements. The string shall not have Control Characters except for ESC.

    Args:
        dataset (pydicom.DataSet): A pydicom DataSet
        data_element (pydicom.DataElement): A pydicom DataElement from the DataSet

    Returns:
        pydicom.DataElement: An updated pydicom DataElement
    """
    try:
        vr, vm, _, _, _ = get_entry(data_element.tag)
        # Check if it is a VR string
        if (
            vr
            not in [
                "UT",
                "ST",
                "LT",
                "FL",
                "FD",
                "AT",
                "OB",
                "OW",
                "OF",
                "SL",
                "SQ",
                "SS",
                "UL",
                "OB/OW",
                "OW/OB",
                "OB or OW",
                "OW or OB",
                "UN",
            ]
            and "US" not in vr
        ):
            if vm == "1" and hasattr(data_element, "VM") and data_element.VM > 1:
                data_element._value = "\\".join(data_element.value)
    except KeyError:
        # we are only fixing VM for tag supported by get_entry (i.e. DicomDictionary or
        # RepeatersDictionary)
        pass
예제 #6
0
 def get_value_rep(tag_as_int):
     """Get DICOM VR with an integer DICOM tag"""
     if tag_as_int is not None:
         return get_entry(tag_as_int)[0]
     return 'Unknown'