|
| 1 | +import os |
| 2 | +from io import BytesIO |
| 3 | +from pathlib import Path |
| 4 | +from typing import BinaryIO, TypeAlias, cast |
| 5 | + |
| 6 | +from pydicom import dcmread |
| 7 | +from pydicom.dataset import FileDataset |
| 8 | + |
| 9 | +from imgtools.exceptions import ( |
| 10 | + InvalidDicomError, |
| 11 | + NotRTSTRUCTError, |
| 12 | + NotSEGError, |
| 13 | +) |
| 14 | + |
| 15 | +# Define a type alias for DICOM input types |
| 16 | +DicomInput: TypeAlias = FileDataset | str | Path | bytes | BinaryIO |
| 17 | + |
| 18 | + |
| 19 | +def path_from_pathlike(file_object: str | Path | BinaryIO) -> str | BinaryIO: |
| 20 | + """Return the string representation if file_object is path-like, |
| 21 | + otherwise return the object itself. |
| 22 | +
|
| 23 | + Parameters |
| 24 | + ---------- |
| 25 | + file_object : str | Path | BinaryIO |
| 26 | + File path or file-like object. |
| 27 | +
|
| 28 | + Returns |
| 29 | + ------- |
| 30 | + str | BinaryIO |
| 31 | + String representation of the path or the original file-like object. |
| 32 | + """ |
| 33 | + try: |
| 34 | + return os.fspath(file_object) # type: ignore[arg-type] |
| 35 | + except TypeError: |
| 36 | + return cast(BinaryIO, file_object) |
| 37 | + |
| 38 | + |
| 39 | +def load_dicom( |
| 40 | + dicom_input: DicomInput, |
| 41 | + force: bool = True, |
| 42 | + stop_before_pixels: bool = True, |
| 43 | +) -> FileDataset: |
| 44 | + """Load a DICOM file and return the parsed FileDataset object. |
| 45 | +
|
| 46 | + This function supports various input types including file paths, byte streams, |
| 47 | + and file-like objects. It uses the `pydicom.dcmread` function to read the DICOM file. |
| 48 | +
|
| 49 | + Notes |
| 50 | + ----- |
| 51 | + - If `dicom_input` is already a `FileDataset`, it is returned as is. |
| 52 | + - If `dicom_input` is a file path or file-like object, it is read using `pydicom.dcmread`. |
| 53 | + - If `dicom_input` is a byte stream, it is wrapped in a `BytesIO` object and then read. |
| 54 | + - An `InvalidDicomError` is raised if the input type is unsupported. |
| 55 | +
|
| 56 | + Parameters |
| 57 | + ---------- |
| 58 | + dicom_input : FileDataset | str | Path | bytes | BinaryIO |
| 59 | + Input DICOM file as a `pydicom.FileDataset`, file path, byte stream, or file-like object. |
| 60 | + force : bool, optional |
| 61 | + Whether to allow reading DICOM files missing the *File Meta Information* |
| 62 | + header, by default True. |
| 63 | + stop_before_pixels : bool, optional |
| 64 | + Whether to stop reading the DICOM file before loading pixel data, by default True. |
| 65 | +
|
| 66 | + Returns |
| 67 | + ------- |
| 68 | + FileDataset |
| 69 | + Parsed DICOM dataset. |
| 70 | +
|
| 71 | + Raises |
| 72 | + ------ |
| 73 | + InvalidDicomError |
| 74 | + If the input is of an unsupported type or cannot be read as a DICOM file. |
| 75 | + """ |
| 76 | + match dicom_input: |
| 77 | + case FileDataset(): |
| 78 | + return dicom_input |
| 79 | + case str() | Path() | BinaryIO(): |
| 80 | + dicom_source = path_from_pathlike(dicom_input) |
| 81 | + return dcmread( |
| 82 | + dicom_source, |
| 83 | + force=force, |
| 84 | + stop_before_pixels=stop_before_pixels, |
| 85 | + ) |
| 86 | + case bytes(): |
| 87 | + return dcmread( |
| 88 | + BytesIO(dicom_input), |
| 89 | + force=force, |
| 90 | + stop_before_pixels=stop_before_pixels, |
| 91 | + ) |
| 92 | + case _: |
| 93 | + msg = ( |
| 94 | + f"Invalid input type for 'dicom_input': {type(dicom_input)}. " |
| 95 | + "Must be a FileDataset, str, Path, bytes, or BinaryIO object." |
| 96 | + ) |
| 97 | + raise InvalidDicomError(msg) |
| 98 | + |
| 99 | + |
| 100 | +def load_rtstruct_dcm( |
| 101 | + rtstruct_input: DicomInput, |
| 102 | + force: bool = True, |
| 103 | + stop_before_pixels: bool = True, |
| 104 | +) -> FileDataset: |
| 105 | + """Load an RTSTRUCT DICOM file and return the parsed FileDataset object. |
| 106 | +
|
| 107 | + Parameters |
| 108 | + ---------- |
| 109 | + rtstruct_input : FileDataset | str | Path | bytes |
| 110 | + Input DICOM file as a `pydicom.FileDataset`, file path, or byte stream. |
| 111 | + force : bool, optional |
| 112 | + Whether to allow reading DICOM files missing the *File Meta Information* |
| 113 | + header, by default True. |
| 114 | + stop_before_pixels : bool, optional |
| 115 | + Whether to stop reading the DICOM file before loading pixel data, by default True. |
| 116 | +
|
| 117 | + Returns |
| 118 | + ------- |
| 119 | + FileDataset |
| 120 | + Parsed RTSTRUCT DICOM dataset. |
| 121 | +
|
| 122 | + Raises |
| 123 | + ------ |
| 124 | + InvalidDicomError |
| 125 | + If the input is of an unsupported type or cannot be read as a DICOM file. |
| 126 | + NotRTSTRUCTError |
| 127 | + If the input file is not an RTSTRUCT (i.e., `Modality` field is not "RTSTRUCT"). |
| 128 | + """ |
| 129 | + |
| 130 | + dicom = load_dicom(rtstruct_input, force, stop_before_pixels) |
| 131 | + |
| 132 | + if dicom.Modality != "RTSTRUCT": |
| 133 | + msg = f"The provided DICOM is not an RTSTRUCT file. Found Modality: {dicom.Modality}" |
| 134 | + raise NotRTSTRUCTError(msg) |
| 135 | + |
| 136 | + return dicom |
| 137 | + |
| 138 | + |
| 139 | +def load_seg_dcm( |
| 140 | + seg_input: DicomInput, |
| 141 | + force: bool = True, |
| 142 | + stop_before_pixels: bool = True, |
| 143 | +) -> FileDataset: |
| 144 | + """Load a SEG DICOM file and return the parsed FileDataset object. |
| 145 | +
|
| 146 | + Parameters |
| 147 | + ---------- |
| 148 | + seg_input : FileDataset | str | Path | bytes |
| 149 | + Input DICOM file as a `pydicom.FileDataset`, file path, or byte stream. |
| 150 | + force : bool, optional |
| 151 | + Whether to allow reading DICOM files missing the *File Meta Information* |
| 152 | + header, by default True. |
| 153 | + stop_before_pixels : bool, optional |
| 154 | + Whether to stop reading the DICOM file before loading pixel data, by default True. |
| 155 | +
|
| 156 | + Returns |
| 157 | + ------- |
| 158 | + FileDataset |
| 159 | + Parsed SEG DICOM dataset. |
| 160 | +
|
| 161 | + Raises |
| 162 | + ------ |
| 163 | + InvalidDicomError |
| 164 | + If the input is of an unsupported type or cannot be read as a DICOM file. |
| 165 | + NotSEGError |
| 166 | + If the input file is not a SEG (i.e., `Modality` field is not "SEG"). |
| 167 | + """ |
| 168 | + dicom = load_dicom(seg_input, force, stop_before_pixels) |
| 169 | + |
| 170 | + if dicom.Modality != "SEG": |
| 171 | + msg = f"The provided DICOM is not a SEG file. Found Modality: {dicom.Modality}" |
| 172 | + raise NotSEGError(msg) |
| 173 | + |
| 174 | + return dicom |
0 commit comments