-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAC-9129: create icobridge compatible policies + docs
Merge in BAC/icometrix-sdk from feature/align-ib-anonymization to master Squashed commit of the following: commit 330c8e961c5aff4256e3f839937491667d1e780a Author: Jeroen Pinxten <jeroen.pinxten@icometrix.com> Date: Fri Apr 12 11:02:41 2024 +0200 BAC-9129: typos commit 8377b100be7f34b83a82c192264a3146597ad226 Author: Jeroen Pinxten <jeroen.pinxten@icometrix.com> Date: Fri Apr 12 09:47:58 2024 +0200 BAC-9129: create icobridge compatible policies + docs
- Loading branch information
Showing
21 changed files
with
399 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
Anonymization | ||
============= | ||
|
||
The :class:`~icometrix_sdk.anonymizer.anonymizer.Anonymizer` provides functionality for anonymizing DICOM datasets | ||
according to specified policies. | ||
|
||
|
||
Policies | ||
-------- | ||
|
||
A Policy is a dictionary with the key bing a DICOM tag and the value a :class:`~icometrix_sdk.anonymizer.models.TagPolicy` | ||
This defines how individual tags or groups should be anonymized. | ||
|
||
.. code-block:: python | ||
from icometrix_sdk.anonymizer.models import TagPolicy, Policy | ||
from icometrix_sdk.anonymizer.hash_factory import SHA3 | ||
def replace_vl(el: DataElement, _): | ||
el.value = "Jane^Doe" | ||
def sha3_hash(element: DataElement, _): | ||
element.value = SHA3(size=512).calculate_hash(element.value)[:64] | ||
# A policy defining how tags should be anonymized | ||
policy: Policy = { | ||
0x00080020: TagPolicy("keep", "StudyDate"), # Don't change the StudyDate | ||
0x0020000d: TagPolicy("hash", "StudyInstanceUID"), # Hash the StudyInstanceUID by the default hash function | ||
0x00100010: TagPolicy("replace", "PatientName", replace_fn=replace_vl), # Replace the PatientName by "Jane^Doe" | ||
0x00100020: TagPolicy("replace", "PatientID", replace_fn=sha3_hash), # Replace the PatientID by a sha3 hash of the PatientID | ||
0x00100030: TagPolicy("round", "PatientBirthday"), # Round the PatientBirthday to YYYY0101 | ||
} | ||
# A policy defining how groups should be anonymized | ||
group_policy: Policy = { | ||
0x0018: TagPolicy("keep", "Acquisition: mage acquisition device and imaging procedure"), | ||
0x5200: TagPolicy("keep", "Multi-frame Functional Groups"), | ||
} | ||
Anonymizer | ||
---------- | ||
|
||
The :class:`~icometrix_sdk.anonymizer.anonymizer.Anonymizer` requires 3 parameters: | ||
|
||
- policy: a Policy for DICOM tags | ||
- group_policy: a Policy for DICOM groups | ||
- hash_algo: The hash algorithm you want to use when using a hash :attr:`~icometrix_sdk.anonymizer.models.TagPolicy.action` | ||
|
||
|
||
.. code-block:: python | ||
from icometrix_sdk.anonymizer.anonymizer import Anonymizer | ||
from icometrix_sdk.anonymizer.hash_factory import HashFactory | ||
from pydicom.data import get_testdata_file | ||
hash_algo = HashFactory.create_hash_method("md5") | ||
# You can use the policy examples above, make your own or import one: | ||
# from icometrix_sdk.anonymizer.policy import policy, group_policy | ||
anonymizer = Anonymizer(policy, group_policy, hash_algo) | ||
dataset = pydicom.read_file(get_testdata_file("MR_small.dcm")) | ||
anonymizer.anonymize(dataset).save_as("anonymized_MR_small.dcm") | ||
Settings | ||
-------- | ||
Some default behaviour can be overwritten by setting environment variables: | ||
|
||
- ROOT_UID: The root of UID used when hashing DICOM tags with the VR UI (https://dicom.nema.org/dicom/2013/output/chtml/part05/chapter_B.html) | ||
- VALIDATION_MODE: and int defining how validation should be done (0: ignore, 1:warn, 2: raise), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ Developer Guide | |
|
||
paginators | ||
session | ||
models | ||
models | ||
anonymization |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Anonymizer | ||
========== | ||
|
||
.. automodule:: icometrix_sdk.anonymizer.anonymizer | ||
:members: | ||
:undoc-members: | ||
|
||
.. automodule:: icometrix_sdk.anonymizer.hash_factory | ||
:members: | ||
:undoc-members: | ||
|
||
.. automodule:: icometrix_sdk.anonymizer.models | ||
:members: | ||
:undoc-members: | ||
|
||
.. automodule:: icometrix_sdk.anonymizer.policy | ||
:members: | ||
:undoc-members: | ||
|
||
.. automodule:: icometrix_sdk.anonymizer.utils | ||
:members: | ||
:undoc-members: | ||
|
||
.. automodule:: icometrix_sdk.anonymizer.exceptions | ||
:members: | ||
:undoc-members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ Models | |
upload | ||
customer_result | ||
customer_report | ||
anonymizer | ||
base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import os | ||
from pydicom.config import RAISE | ||
|
||
# Organization root, default to icometrix (https://dicom.nema.org/dicom/2013/output/chtml/part05/chapter_B.html) | ||
ROOT_UID: str = os.getenv("ROOT_UID", "1.2.826.0.1.3680043.9.5542") | ||
VALIDATION_MODE: int = int(os.getenv("VALIDATION_MODE", RAISE)) | ||
|
||
PATIENT_IDENTITY_REMOVED_TAG: int = 0x00120062 | ||
DE_IDENTIFICATION_METHOD_TAG: int = 0x00120063 | ||
PRIVATE_ICOMETRIX_GROUPS: list[int] = [0x0009, 0x0015, 0x0017] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,34 @@ | ||
from dataclasses import dataclass | ||
from typing import Literal, get_args | ||
from typing import Literal, Callable | ||
from pydicom import DataElement, Dataset | ||
|
||
Action = Literal["keep", "remove", "replace", "hash", "round"] | ||
Action = Literal["keep", "empty", "remove", "replace", "hash", "round"] | ||
HashAlgo = Literal["sha3", "md5", "short_md5"] | ||
ReplaceFn = Callable[[DataElement, Dataset], None] | ||
|
||
|
||
@dataclass | ||
class TagPolicy: | ||
"""Class for defining a DICOM tag anonymization policy""" | ||
""" | ||
represents a DICOM tag anonymization policy, defining how individual tags or groups should be anonymized. | ||
:param: Action to be performed on the tag (keep, remove, replace, hash, round) | ||
:param: Description of the tag anonymization policy. | ||
:param: Replace Function to be used when the action is "replace" (optional). | ||
Actions: | ||
- keep: Keep the original tag value. | ||
- remove: Remove the tag from the DICOM dataset. | ||
- replace: Replace the tag value with a specified :attr:`~icometrix_sdk.anonymizer.models.TagPolicy.value`. | ||
- hash: Hash the tag value using a specified algorithm. | ||
- round: Round the tag value to the nearest value. (currently only dates VRs are supported) | ||
""" | ||
action: Action | ||
description: str | ||
value: int | str = None | ||
|
||
# value: int | str = None | ||
replace_fn: ReplaceFn = None | ||
|
||
|
||
Policy = dict[int, TagPolicy] |
Oops, something went wrong.