From 3858361bcd4e6c09da42267c868f794d95742506 Mon Sep 17 00:00:00 2001
From: Tamino Huxohl <thuxohl@techfak.uni-bielefeld.de>
Date: Fri, 23 Dec 2022 09:32:55 +0100
Subject: [PATCH] dicom to interfile conversion expects a default patient
 position

---
 mu_map/file/convert.py | 47 ++++++++++++++++++++++++------------------
 1 file changed, 27 insertions(+), 20 deletions(-)

diff --git a/mu_map/file/convert.py b/mu_map/file/convert.py
index 0f3e2ef..bc87ca2 100644
--- a/mu_map/file/convert.py
+++ b/mu_map/file/convert.py
@@ -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))
 
-- 
GitLab