KitwareMedical / dicom-anonymizer

Tool to anonymize DICOM files according to the DICOM standard
BSD 3-Clause "New" or "Revised" License
100 stars 47 forks source link

Tag replacement fails during anonymization for structured reports #63

Closed smjoshiatglobus closed 6 months ago

smjoshiatglobus commented 7 months ago

Found while doing unit testing test-SR.dcm from pydicom's test files:

dicomanonymizer\simpledicomanonymizer.py:440: in anonymize_dataset
    action(dataset, tag)
dicomanonymizer\simpledicomanonymizer.py:134: in replace
    replace_element(element)
dicomanonymizer\simpledicomanonymizer.py:122: in replace_element
    replace_element(sub_element)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  

element = RawDataElement(tag=(0040, a010), VR='CS', length=16, value=b'HAS OBS CONTEXT ', value_tell=16, is_implicit_VR=False, is_little_endian=True, is_raw=True)

    def replace_element(element):
        """
        Replace element's value according to it's VR:
        - LO, LT, SH, PN, CS, ST, UT: replace with 'Anonymized'
        - UI: cf replace_element_UID
        - DS and IS: value will be replaced by '0'
        - FD, FL, SS, US, SL, UL: value will be replaced by 0
        - DA: value will be replaced by '00010101'
        - DT: value will be replaced by '00010101010101.000000+0000'
        - TM: value will be replaced by '000000.00'
        - UN: value will be replaced by b'Anonymized' (binary string)
        - SQ: call replace_element for all sub elements

        See https://laurelbridge.com/pdf/Dicom-Anonymization-Conformance-Statement.pdf
        """
        if element.VR in ('LO', 'LT', 'SH', 'PN', 'CS', 'ST', 'UT'):
>           element.value = 'Anonymized'
E           AttributeError: can't set attribute

dicomanonymizer\simpledicomanonymizer.py:108: AttributeError
smjoshiatglobus commented 7 months ago

I will keep the failing files excluded from unit tests for now. See PR #64. It does not the fix the issue, just clarifies what needs to be fixed.

smjoshiatglobus commented 6 months ago

Found the root cause... the element is of type RawDataElement, which is a NamedTuple. We cannot set an attribute for a NamedTuple. Fix is included in PR #64.

pchoisel commented 6 months ago

Thank you !

smjoshiatglobus commented 6 months ago

This is fixed now with PR #66