Skip to content

Commit

Permalink
added MedicalImagingData base class
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed Sep 28, 2024
1 parent dce7d92 commit 43038ec
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
4 changes: 4 additions & 0 deletions extras/fileformats/extras/medimage/tests/test_deidentify.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ def dicom(request):

def test_deidentify_dicom(dicom):
assert str(dicom.metadata["PatientName"]) == "Doe^John"
assert dicom.metadata["InstitutionAddress"]
assert not dicom.metadata["PatientBirthDate"].endswith("0101")
deidentified = dicom.deidentify()
assert str(deidentified.metadata["PatientName"]) == "Anonymous^Anonymous"
assert deidentified.metadata["InstitutionAddress"] == ""
assert deidentified.metadata["PatientBirthDate"] == "19800101"


def test_nifti_deidentify():
Expand Down
3 changes: 2 additions & 1 deletion fileformats/medimage/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ._version import __version__
from .base import MedicalImage
from .base import MedicalImagingData, MedicalImage


from .misc import (
Expand Down Expand Up @@ -128,6 +128,7 @@

__all__ = [
"__version__",
"MedicalImagingData",
"MedicalImage",
"DicomImage",
"Analyze",
Expand Down
32 changes: 19 additions & 13 deletions fileformats/medimage/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,25 @@
) # In Py<3.9 this is problematic "numpy.typing.NDArray[typing.Union[numpy.floating[typing.Any], numpy.integer[typing.Any]]]"


class MedicalImage(WithClassifiers, FileSet):
class MedicalImagingData(FileSet):
"""Base class for all medical imaging data including pre-image raw data and
associated data"""

contains_phi: bool = True

@extra
def deidentify(
self,
out_dir: ty.Optional[Path] = None,
new_stem: ty.Optional[str] = None,
copy_mode: FileSet.CopyMode = FileSet.CopyMode.copy,
) -> Self:
"""Returns a new copy of the image with any subject-identifying information
stripped from the from the image header"""
raise NotImplementedError

Check warning on line 51 in fileformats/medimage/base.py

View check run for this annotation

Codecov / codecov/patch

fileformats/medimage/base.py#L51

Added line #L51 was not covered by tests


class MedicalImage(WithClassifiers, MedicalImagingData):

INCLUDE_HDR_KEYS: ty.Optional[ty.Tuple[str, ...]] = None
IGNORE_HDR_KEYS: ty.Optional[ty.Tuple[str, ...]] = None
Expand All @@ -42,7 +60,6 @@ class MedicalImage(WithClassifiers, FileSet):
image_contents = ()
allowed_classifiers = (ContentsClassifier,)
exclusive_classifiers = (ImagingModality, AnatomicalEntity, Derivative)
contains_phi: bool = True

@extra
def read_array(self) -> DataArrayType:
Expand All @@ -63,14 +80,3 @@ def vox_sizes(self) -> ty.Tuple[float, float, float]:
def dims(self) -> ty.Tuple[int, int, int]:
"""The dimensions of the image"""
raise NotImplementedError

@extra
def deidentify(
self,
out_dir: ty.Optional[Path] = None,
new_stem: ty.Optional[str] = None,
copy_mode: FileSet.CopyMode = FileSet.CopyMode.copy,
) -> Self:
"""Returns a new copy of the image with any subject-identifying information
stripped from the from the image header"""
raise NotImplementedError
3 changes: 2 additions & 1 deletion fileformats/medimage/raw/pet/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import typing as ty
from pathlib import Path
from fileformats.core import FileSet, extra
from fileformats.medimage import MedicalImagingData
from fileformats.generic import BinaryFile

if sys.version_info >= (3, 12):
Expand All @@ -10,7 +11,7 @@
from typing_extensions import Self

Check warning on line 11 in fileformats/medimage/raw/pet/base.py

View check run for this annotation

Codecov / codecov/patch

fileformats/medimage/raw/pet/base.py#L11

Added line #L11 was not covered by tests


class PetRawData(BinaryFile):
class PetRawData(BinaryFile, MedicalImagingData):
"""Base class for raw PET data files"""

@extra
Expand Down

0 comments on commit 43038ec

Please sign in to comment.