Skip to content

Commit a30de26

Browse files
committed
Fix anonymization logic
DICOM anonymization was previously not working due to incorrect if logic. This commit changes anonymization to work from a list of attributes to remove, lowering possibility for error and increasing ease of extension for future fields.
1 parent ca34dcc commit a30de26

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

rtCommon/imageHandling.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,28 @@ def getDicomFileName(cfg, scanNum, fileNum):
5858

5959
return fullFileName
6060

61+
attributesToAnonymize = [
62+
'PatientID', 'PatientAge', 'PatientBirthDate', 'PatientName',
63+
'PatientSex', 'PatientSize', 'PatientWeight', 'PatientPosition',
64+
'StudyDate', 'StudyTime', 'SeriesDate', 'SeriesTime',
65+
'AcquisitionDate', 'AcquisitionTime', 'ContentDate', 'ContentTime',
66+
'InstanceCreationDate', 'InstanceCreationTime',
67+
'PerformedProcedureStepStartDate', 'PerformedProcedureStepStartTime'
68+
]
6169

6270
def anonymizeDicom(dicomImg):
6371
"""
6472
This function takes in the dicom image that you read in and deletes
65-
lots of different variables. The purpose of this is to anonymize the
73+
lots of different attributes. The purpose of this is to anonymize the
6674
dicom data before transferring it to the cloud.
6775
6876
Used externally.
6977
"""
70-
if hasattr(dicomImg, 'PatientID'): del dicomImg.PatientID
71-
if hasattr(dicomImg, 'PatientID'): del dicomImg.PatientAge
72-
if hasattr(dicomImg, 'PatientID'): del dicomImg.PatientBirthDate
73-
if hasattr(dicomImg, 'PatientID'): del dicomImg.PatientName
74-
if hasattr(dicomImg, 'PatientID'): del dicomImg.PatientSex
75-
if hasattr(dicomImg, 'PatientID'): del dicomImg.PatientSize
76-
if hasattr(dicomImg, 'PatientID'): del dicomImg.PatientWeight
77-
if hasattr(dicomImg, 'PatientID'): del dicomImg.PatientPosition
78-
if hasattr(dicomImg, 'PatientID'): del dicomImg.StudyDate
79-
if hasattr(dicomImg, 'PatientID'): del dicomImg.StudyTime
80-
if hasattr(dicomImg, 'PatientID'): del dicomImg.SeriesDate
81-
if hasattr(dicomImg, 'PatientID'): del dicomImg.SeriesTime
82-
if hasattr(dicomImg, 'PatientID'): del dicomImg.AcquisitionDate
83-
if hasattr(dicomImg, 'PatientID'): del dicomImg.AcquisitionTime
84-
if hasattr(dicomImg, 'PatientID'): del dicomImg.ContentDate
85-
if hasattr(dicomImg, 'PatientID'): del dicomImg.ContentTime
86-
if hasattr(dicomImg, 'PatientID'): del dicomImg.InstanceCreationDate
87-
if hasattr(dicomImg, 'PatientID'): del dicomImg.InstanceCreationTime
88-
if hasattr(dicomImg, 'PatientID'): del dicomImg.PerformedProcedureStepStartDate
89-
if hasattr(dicomImg, 'PatientID'): del dicomImg.PerformedProcedureStepStartTime
90-
return dicomImg
78+
for toAnonymize in attributesToAnonymize:
79+
if hasattr(dicomImg, toAnonymize):
80+
setattr(dicomImg, toAnonymize, "")
9181

82+
return dicomImg
9283

9384
def readDicomFromFile(filename):
9485
"""

tests/test_readDicom.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,17 @@ def test_readDicom():
4040

4141
fileInterface.fileWatcher.__del__()
4242
fileInterface.fileWatcher = None
43+
44+
# Test anonymization of sensitive patient fields
45+
def countUnanonymizedSensitiveAttrs(dicomImg):
46+
sensitiveAttrs = 0
47+
for attr in rd.attributesToAnonymize:
48+
if hasattr(dicomImg, attr) and getattr(dicomImg, attr) != "":
49+
sensitiveAttrs += 1
50+
return sensitiveAttrs
51+
52+
dicomImg5 = rd.readDicomFromFile(dicomFile)
53+
assert countUnanonymizedSensitiveAttrs(dicomImg5) >= 1
54+
55+
rd.anonymizeDicom(dicomImg5)
56+
assert countUnanonymizedSensitiveAttrs(dicomImg5) == 0

0 commit comments

Comments
 (0)