Repository: mlcommons/GaNDLF

Issue:
Add an `anonymize` module for GaNDLF
**Is your feature request related to a problem? Please describe.**
Currently, there is no anonymization mechanism in GaNDLF.

**Describe the solution you'd like**
Making #281 easier to manage, it would be good to have this module. It would fine to target radiology (i.e., DICOM) scans for the time being, and move to histology once the structure is formalized.

**Describe alternatives you've considered**
N.A.

**Additional context**
N.A.



Discussion:

Resolving patch:
diff --git a/GANDLF/anonymize/__init__.py b/GANDLF/anonymize/__init__.py
new file mode 100644
--- /dev/null
+++ b/GANDLF/anonymize/__init__.py
@@ -0,0 +1,37 @@
+import yaml
+from typing import Union
+from .dicomanonymizer.dicomanonymizer import anonymize
+
+
+def run_anonymizer(
+    input_path: str, output_path: str, parameters: Union[str, list, int]
+):
+    """
+    This function performs anonymization of a single image or a collection of images.
+
+    Args:
+        input_path (str): The input file or folder.
+        output_path (str): The output file or folder.
+        parameters (Union[str, list, int]): The parameters for anonymization; for DICOM scans, the only optional argument is "delete_private_tags", which defaults to True.
+
+    Returns:
+        torch.Tensor: The output image after morphological operations.
+    """
+    if not isinstance(parameters, dict):
+        with open(parameters, "r") as file_data:
+            yaml_data = file_data.read()
+        parameters = yaml.safe_load(yaml_data)
+
+    if "rad" in parameters["modality"]:
+        if "delete_private_tags" not in parameters:
+            parameters["delete_private_tags"] = True
+        return anonymize(
+            input_path,
+            output_path,
+            anonymization_actions={},
+            deletePrivateTags=parameters["delete_private_tags"],
+        )
+    elif parameters["modality"] in ["histo", "path"]:
+        raise NotImplementedError(
+            "Anonymization for histology images has not been implemented yet."
+        )
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -47,6 +47,7 @@
     "opencv-python",
     "torchmetrics",
     "OpenPatchMiner==0.1.6",
+    "pydicom",
 ]
 
 setup(
@@ -62,6 +63,7 @@
         "gandlf_collectStats",
         "gandlf_patchMiner",
         "gandlf_preprocess",
+        "gandlf_anonymizer",
     ],
     classifiers=[
         "Development Status :: 3 - Alpha",


Test patch:
diff --git a/testing/test_full.py b/testing/test_full.py
--- a/testing/test_full.py
+++ b/testing/test_full.py
@@ -3,6 +3,8 @@
 import SimpleITK as sitk
 import numpy as np
 
+from pydicom.data import get_testdata_file
+
 from GANDLF.data.ImagesFromDataFrame import ImagesFromDataFrame
 from GANDLF.utils import *
 from GANDLF.data.preprocessing import global_preprocessing_dict
@@ -16,6 +18,7 @@
 from GANDLF.optimizers import global_optimizer_dict
 from GANDLF.models import global_models_dict
 from GANDLF.post_process import torch_morphological, fill_holes
+from GANDLF.anonymize import run_anonymizer
 
 device = "cpu"
 ## global defines
@@ -1123,3 +1126,17 @@ def test_one_hot_logic():
     comparison = (random_array == 4) == img_tensor_oh_rev_array_sp
     assert comparison.all(), "Arrays at '4' are not equal"
     print("passed")
+
+
+def test_anonymizer():
+    input_file = get_testdata_file("MR_small.dcm")
+
+    output_file = os.path.join(testingDir, "MR_small_anonymized.yaml")
+    if os.path.exists(output_file):
+        os.remove(output_file)
+
+    config_file = os.path.join(baseConfigDir, "config_anonymizer.yaml")
+
+    run_anonymizer(input_file, output_file, config_file)
+
+    os.remove(output_file)