Skip to content
Snippets Groups Projects
Commit 3858361b authored by Tamino Huxohl's avatar Tamino Huxohl
Browse files

dicom to interfile conversion expects a default patient position

parent 1c0319f1
No related branches found
No related tags found
No related merge requests found
......@@ -12,7 +12,11 @@ from mu_map.file.interfile import (
"""
Module for the conversion between the different formats.
"""
def parse_position(position: str) -> Tuple[str, str]:
def parse_position(
dcm: DICOM, default_orientation: str = "feet_in", default_rotation: str = "supine"
) -> Tuple[str, str]:
"""
Parse the patient position from the DICOM patient position
field into the orientation and rotation as used by the Interfile
......@@ -21,24 +25,25 @@ def parse_position(position: str) -> Tuple[str, str]:
:param position: the value of the patient position field in the DICOM header
:return: a tuple of patient orientation and rotation
"""
if len(position) != 3:
raise ValueError(f"Cannot parse patient position {position}")
_orienation = position[:2]
if _orienation == "FF":
orientation = "feet_in"
elif _orienation == "HF":
orientation = "head_in"
_rotation = position[2]
if _orienation == "P":
rotation = "prone"
elif _rotation == "S":
rotation = "supine"
if rotation is None or orientation is None:
raise ValueError(f"Cannot parse patient position {position}")
orientation = default_orientation
rotation = default_rotation
try:
position = dcm.PatientPosition
_orienation = position[:2]
if _orienation == "FF":
orientation = "feet_in"
elif _orienation == "HF":
orientation = "head_in"
_rotation = position[2]
if _orienation == "P":
rotation = "prone"
elif _rotation == "S":
rotation = "supine"
except:
pass
return orientation, rotation
......@@ -50,7 +55,6 @@ def dicom_to_interfile(dcm: DICOM, image: np.ndarray) -> Interfile:
:param image: the image as a numpy array
:return: an Interfile as a combination of header and image
"""
orientation, rotation = parse_position(dcm.PatientPosition)
header = TEMPLATE_HEADER_IMAGE.replace("{ROWS}", str(dcm.Rows))
header = header.replace("{SPACING_X}", f"{dcm.PixelSpacing[0]:.4f}")
......@@ -60,8 +64,11 @@ def dicom_to_interfile(dcm: DICOM, image: np.ndarray) -> Interfile:
header = header.replace("{SPACING_Z}", f"{dcm.SliceThickness:.4f}")
header = header.replace("{OFFSET_X}", str(-0.5 * dcm.Rows * dcm.PixelSpacing[0]))
header = header.replace("{OFFSET_Y}", str(-0.5 * dcm.Columns * dcm.PixelSpacing[1]))
orientation, rotation = parse_position(dcm)
header = header.replace("{PATIENT_ORIENTATION}", orientation)
header = header.replace("{PATIENT_ROTATION}", rotation)
header = parse_interfile_header_str(header)
return Interfile(header, image.astype(np.float32))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment