Skip to content

Commit

Permalink
a bugfix, more test.
Browse files Browse the repository at this point in the history
  • Loading branch information
smasuda committed Jun 21, 2024
1 parent 3266e11 commit c83e74d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
34 changes: 13 additions & 21 deletions dicomanonymizer/simpledicomanonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,10 @@
from typing import Callable, List, Union
from dataclasses import dataclass

from dicomanonymizer.dicomfields import (
D_TAGS,
Z_TAGS,
X_TAGS,
U_TAGS,
Z_D_TAGS,
X_Z_TAGS,
X_D_TAGS,
X_Z_D_TAGS,
X_Z_U_STAR_TAGS,
)
from dicomanonymizer.dicomfields_selector import selector
from dicomanonymizer.format_tag import tag_to_hex_strings


# keeps the mapping from old UID to new UID
dictionary = {}


Expand Down Expand Up @@ -351,18 +341,20 @@ def initialize_actions(dicom_version: str = "2013") -> dict:
:param dicom_version: DICOM version to use
:return Dict object which map actions to tags
"""
anonymization_actions = {tag: replace for tag in D_TAGS}
anonymization_actions.update({tag: empty for tag in Z_TAGS})
anonymization_actions.update({tag: delete for tag in X_TAGS})
anonymization_actions.update({tag: replace_UID for tag in U_TAGS})
anonymization_actions.update({tag: empty_or_replace for tag in Z_D_TAGS})
anonymization_actions.update({tag: delete_or_empty for tag in X_Z_TAGS})
anonymization_actions.update({tag: delete_or_replace for tag in X_D_TAGS})
tags = selector(dicom_version)

anonymization_actions = {tag: replace for tag in tags["D_TAGS"]}
anonymization_actions.update({tag: empty for tag in tags["Z_TAGS"]})
anonymization_actions.update({tag: delete for tag in tags["X_TAGS"]})
anonymization_actions.update({tag: replace_UID for tag in tags["U_TAGS"]})
anonymization_actions.update({tag: empty_or_replace for tag in tags["Z_D_TAGS"]})
anonymization_actions.update({tag: delete_or_empty for tag in tags["X_Z_TAGS"]})
anonymization_actions.update({tag: delete_or_replace for tag in tags["X_D_TAGS"]})
anonymization_actions.update(
{tag: delete_or_empty_or_replace for tag in X_Z_D_TAGS}
{tag: delete_or_empty_or_replace for tag in tags["X_Z_D_TAGS"]}
)
anonymization_actions.update(
{tag: delete_or_empty_or_replace_UID for tag in X_Z_U_STAR_TAGS}
{tag: delete_or_empty_or_replace_UID for tag in tags["X_Z_U_STAR_TAGS"]}
)
return anonymization_actions

Expand Down
39 changes: 39 additions & 0 deletions tests/test_dicomfields_selector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from dicomanonymizer.dicomfields_selector import selector

from dicomanonymizer import dicomfields, dicomfields_2024b


def test_selector():
assert selector("2013") == {
"D_TAGS": dicomfields.D_TAGS,
"Z_TAGS": dicomfields.Z_TAGS,
"X_TAGS": dicomfields.X_TAGS,
"U_TAGS": dicomfields.U_TAGS,
"Z_D_TAGS": dicomfields.Z_D_TAGS,
"X_Z_TAGS": dicomfields.X_Z_TAGS,
"X_D_TAGS": dicomfields.X_D_TAGS,
"X_Z_D_TAGS": dicomfields.X_Z_D_TAGS,
"X_Z_U_STAR_TAGS": dicomfields.X_Z_U_STAR_TAGS,
"ALL_TAGS": dicomfields.ALL_TAGS,
}
assert selector("2024b") == {
"D_TAGS": dicomfields_2024b.D_TAGS,
"Z_TAGS": dicomfields_2024b.Z_TAGS,
"X_TAGS": dicomfields_2024b.X_TAGS,
"U_TAGS": dicomfields_2024b.U_TAGS,
"Z_D_TAGS": dicomfields_2024b.Z_D_TAGS,
"X_Z_TAGS": dicomfields_2024b.X_Z_TAGS,
"X_D_TAGS": dicomfields_2024b.X_D_TAGS,
"X_Z_D_TAGS": dicomfields_2024b.X_Z_D_TAGS,
"X_Z_U_STAR_TAGS": dicomfields_2024b.X_Z_U_STAR_TAGS,
"ALL_TAGS": dicomfields_2024b.ALL_TAGS,
}

assert selector() == selector("2013")

try:
selector("2019")
except ValueError as e:
assert str(e) == "Unknown DICOM version: 2019"
else:
assert False

0 comments on commit c83e74d

Please sign in to comment.