Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom rule example #72

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,36 +146,50 @@ by adding a suffix passed as a parameter.
import argparse
from dicomanonymizer import ALL_TAGS, anonymize, keep


def main():
parser = argparse.ArgumentParser(add_help=True)
parser.add_argument('input', help='Path to the input dicom file or input directory which contains dicom files')
parser.add_argument('output', help='Path to the output dicom file or output directory which will contains dicom files')
parser.add_argument('--suffix', action='store', help='Suffix that will be added at the end of series description')
parser.add_argument(
"input",
help="Path to the input dicom file or input directory which contains dicom files",
)
parser.add_argument(
"output",
help="Path to the output dicom file or output directory which will contains dicom files",
)
args = parser.parse_args()

input_dicom_path = args.input
output_dicom_path = args.output

extra_anonymization_rules = {}

def setup_series_description(dataset, tag):
# Per https://www.hhs.gov/hipaa/for-professionals/privacy/special-topics/de-identification/index.html
# it is all right to retain only the year part of the birth date for
# de-identification purposes.
def set_date_to_year(dataset, tag):
element = dataset.get(tag)
if element is not None:
element.value = f'{element.value}-{args.suffix}'
element.value = f"{element.value[:4]}0101" # YYYYMMDD format

# ALL_TAGS variable is defined on file dicomfields.py
# the 'keep' method is already defined into the dicom-anonymizer
# It will overrides the default behaviour
for i in ALL_TAGS:
extra_anonymization_rules[i] = keep

if args.suffix:
extra_anonymization_rules[(0x0008, 0x103E)] = setup_series_description
extra_anonymization_rules[(0x0010, 0x0030)] = set_date_to_year # Patient's Birth Date

# Launch the anonymization
anonymize(input_dicom_path, output_dicom_path, extra_anonymization_rules, delete_private_tags=False)
anonymize(
input_dicom_path,
output_dicom_path,
extra_anonymization_rules,
delete_private_tags=False,
)


if __name__ == '__main__':
if __name__ == "__main__":
main()
```

Expand Down
15 changes: 6 additions & 9 deletions examples/anonymize_extra_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,28 @@ def main():
"output",
help="Path to the output dicom file or output directory which will contains dicom files",
)
parser.add_argument(
"--suffix",
action="store",
help="Suffix that will be added at the end of series description",
)
args = parser.parse_args()

input_dicom_path = args.input
output_dicom_path = args.output

extra_anonymization_rules = {}

def setup_series_description(dataset, tag):
# Per https://www.hhs.gov/hipaa/for-professionals/privacy/special-topics/de-identification/index.html
# it is all right to retain only the year part of the birth date for
# de-identification purposes.
def set_date_to_year(dataset, tag):
element = dataset.get(tag)
if element is not None:
element.value = f"{element.value}-{args.suffix}"
element.value = f"{element.value[:4]}0101" # YYYYMMDD format

# ALL_TAGS variable is defined on file dicomfields.py
# the 'keep' method is already defined into the dicom-anonymizer
# It will overrides the default behaviour
for i in ALL_TAGS:
extra_anonymization_rules[i] = keep

if args.suffix:
extra_anonymization_rules[(0x0008, 0x103E)] = setup_series_description
extra_anonymization_rules[(0x0010, 0x0030)] = set_date_to_year # Patient's Birth Date

# Launch the anonymization
anonymize(
Expand Down
Loading