Skip to content

Commit

Permalink
DICOM redactor improvement: Adding exceptions for when DICOM file doe…
Browse files Browse the repository at this point in the history
…s not have pixel data (#1104)

* Adding exceptions for when DICOM file does not have pixel data

* Updating unit tests to accomodate new exception

* Fixing linting errors

* Adding in test image

* Fixing f-string
  • Loading branch information
niwilso authored Jul 6, 2023
1 parent 42f30bd commit 23a89af
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ def verify_dicom_instance(
"""
instance_copy = deepcopy(instance)

try:
instance_copy.PixelData
except AttributeError:
raise AttributeError("Provided DICOM instance lacks pixel data.")

# Load image for processing
with tempfile.TemporaryDirectory() as tmpdirname:
# Convert DICOM to PNG and add padding for OCR (during analysis)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ def redact(
:return: DICOM instance with redacted pixel data.
"""
# Check input
if type(image) != pydicom.dataset.FileDataset:
if type(image) not in [pydicom.dataset.FileDataset, pydicom.dataset.Dataset]:
raise TypeError("The provided image must be a loaded DICOM instance.")
try:
image.PixelData
except AttributeError:
raise AttributeError("Provided DICOM instance lacks pixel data.")

instance = deepcopy(image)

# Load image for processing
Expand Down Expand Up @@ -771,6 +776,11 @@ def _redact_single_dicom_image(
# Load instance
instance = pydicom.dcmread(dst_path)

try:
instance.PixelData
except AttributeError:
raise AttributeError("Provided DICOM file lacks pixel data.")

# Load image for processing
with tempfile.TemporaryDirectory() as tmpdirname:
# Convert DICOM to PNG and add padding for OCR (during analysis)
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Unit tests for dicom_image_pii_verify_engine
"""
from copy import deepcopy
import pydicom

from presidio_image_redactor import (
Expand Down Expand Up @@ -121,6 +122,28 @@ def test_verify_dicom_instance_happy_path(
assert mock_analyze.call_count == 1
assert mock_verify.call_count == 1

def test_verify_dicom_instance_exception(
mock_engine: DicomImagePiiVerifyEngine,
get_mock_dicom_instance: pydicom.dataset.FileDataset,
):
"""Test exceptions for DicomImagePiiVerifyEngine.verify_dicom_instance
Args:
mock_engine (DicomImagePiiVerifyEngine): Instantiated engine.
get_mock_dicom_instance (pydicom.dataset.FileDataset): Loaded DICOM.
"""
with pytest.raises(Exception) as exc_info:
# Arrange
padding_width = 25
test_instance = deepcopy(get_mock_dicom_instance)
del test_instance.PixelData
expected_error_type = AttributeError

# Act
_, _, _ = mock_engine.verify_dicom_instance(test_instance, padding_width)

# Assert
assert expected_error_type == exc_info.typename


# ------------------------------------------------------
# DicomImagePiiVerifyEngine.eval_dicom_instance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def mock_engine():
Path(TEST_DICOM_PARENT_DIR),
[
Path(TEST_DICOM_PARENT_DIR, "0_ORIGINAL.dcm"),
Path(TEST_DICOM_PARENT_DIR, "0_ORIGINAL_no_pixels.dcm"),
Path(TEST_DICOM_PARENT_DIR, "RGB_ORIGINAL.dcm"),
Path(TEST_DICOM_DIR_2, "1_ORIGINAL.DCM"),
Path(TEST_DICOM_DIR_2, "2_ORIGINAL.dicom"),
Expand Down Expand Up @@ -537,7 +538,7 @@ def test_add_padding_exceptions(
"src_path, expected_num_of_files",
[
(Path(TEST_DICOM_PARENT_DIR, "0_ORIGINAL.dcm"), 1),
(Path(TEST_DICOM_PARENT_DIR), 15),
(Path(TEST_DICOM_PARENT_DIR), 16),
(Path(TEST_DICOM_DIR_1), 3),
(Path(TEST_DICOM_DIR_2), 2),
(Path(TEST_DICOM_DIR_3), 1),
Expand Down Expand Up @@ -1120,7 +1121,8 @@ def test_DicomImageRedactorEngine_redact_happy_path(
(Path(TEST_DICOM_PARENT_DIR), "TypeError"),
("path_here", "TypeError"),
(np.random.randint(255, size=(64, 64)), "TypeError"),
(Image.fromarray(np.random.randint(255, size=(400, 400),dtype=np.uint8)), "TypeError")
(Image.fromarray(np.random.randint(255, size=(400, 400),dtype=np.uint8)), "TypeError"),
(pydicom.dcmread(Path(TEST_DICOM_PARENT_DIR, "0_ORIGINAL_no_pixels.dcm")), "AttributeError")
],
)
def test_DicomImageRedactorEngine_redact_exceptions(
Expand Down Expand Up @@ -1255,6 +1257,7 @@ def save_as(self, dst_path: str):
[
(Path(TEST_DICOM_PARENT_DIR), "FileNotFoundError"),
(Path("nonexistentfile.extension"), "FileNotFoundError"),
(Path(TEST_DICOM_PARENT_DIR, "0_ORIGINAL_no_pixels.dcm"), "AttributeError")
],
)
def test_DicomImageRedactorEngine_redact_single_dicom_image_exceptions(
Expand Down

0 comments on commit 23a89af

Please sign in to comment.