Commit 241409a2 authored by Gabriel Couture's avatar Gabriel Couture

Adding basic dose conversion fucntion

parent f4ac147e
# coding: utf-8
# author: gabriel couture
from typing import List
from dvh.dvh import DVH
......@@ -20,6 +22,32 @@ def is_dvh_has_a_prescription_dose(dvh: DVH) -> bool:
return False if dvh.get_prescription_dose() is None else True
def convert_absolute_doses_to_relative(absolute_doses: List[float], prescription_dose: float) -> List[float]:
"""Convert absolute doses to relative dose.
Args:
absolute_doses: List of absolute doses.
prescription_dose: Prescription dose.
Returns:
List of relative doses.
"""
return [dose / prescription_dose for dose in absolute_doses]
def convert_relative_doses_to_absolute(relative_doses: List[float], prescription_dose: float) -> List[float]:
"""Convert relative doses to absolute dose.
Args:
relative_doses: List of relative doses.
prescription_dose: Prescription dose.
Returns:
List of absolute doses.
"""
return [dose * prescription_dose for dose in relative_doses]
def convert_dvh_doses_to_relative(dvh: DVH) -> DVH:
"""Convert a DVH with absolute doses to a DVH with relative doses.
......@@ -37,7 +65,7 @@ def convert_dvh_doses_to_relative(dvh: DVH) -> DVH:
'The DVH has no prescription dose. Therefor, relative doses can\'t be calculated.'
)
dvh.doses = [dose / dvh.get_prescription_dose() for dose in dvh.get_doses()]
dvh.doses = convert_absolute_doses_to_relative(dvh.get_doses(), dvh.get_prescription_dose())
dvh.dose_unit = '%'
return dvh
......@@ -60,7 +88,7 @@ def convert_dvh_doses_to_absolute(dvh: DVH) -> DVH:
'The DVH has no prescription dose. Therefor, absolute doses can\'t be calculated from relative doses.'
)
dvh.doses = [dose * dvh.get_prescription_dose() for dose in dvh.get_doses()]
dvh.doses = convert_relative_doses_to_absolute(dvh.get_doses(), dvh.get_prescription_dose())
dvh.dose_unit = 'GY'
return dvh
......@@ -13,8 +13,8 @@ RELATIVE_DOSE_UNIT = '%'
A_PRESCRIPTION_DOSE = 15
AN_ABSOLUTE_DOSE_ARRAY = list(range(0, 16))
A_RELATIVE_DOSE_ARRAY = [dose / A_PRESCRIPTION_DOSE for dose in AN_ABSOLUTE_DOSE_ARRAY]
AN_ABSOLUTE_DOSE_LIST = list(range(0, 16))
A_RELATIVE_DOSE_LIST = [dose / A_PRESCRIPTION_DOSE for dose in AN_ABSOLUTE_DOSE_LIST]
class TestConversion(unittest.TestCase):
......@@ -23,13 +23,23 @@ class TestConversion(unittest.TestCase):
self.a_dvh_with_absolute_doses = self._setup_dvh_with_absolute_doses()
self.a_dvh_with_relative_doses = self._setup_dvh_with_relative_doses()
def test_givenAbsoluteDoseListAndAPrescriptionDose_whenConvertingAbsoluteDosesToRelative_thenResultIsARelativeDoseList(self):
result = conversion.convert_absolute_doses_to_relative(AN_ABSOLUTE_DOSE_LIST, A_PRESCRIPTION_DOSE)
self.assertEqual(result, A_RELATIVE_DOSE_LIST)
def test_givenARelativeDoseListAndAPrescriptionDose_whenConvertingAbsoluteDosesToRelative_thenResultIsAnAbsoluteDoseList(self):
result = conversion.convert_relative_doses_to_absolute(A_RELATIVE_DOSE_LIST, A_PRESCRIPTION_DOSE)
self.assertEqual(result, AN_ABSOLUTE_DOSE_LIST)
def test_givenADVHWithAbsoluteDosesAndPrescriptionDose_whenConvertingDVHDoseToRelative_thenResultIsADVHWithRelativeDoses(self):
self.a_dvh_with_absolute_doses.get_prescription_dose.return_value = A_PRESCRIPTION_DOSE
result = conversion.convert_dvh_doses_to_relative(self.a_dvh_with_absolute_doses)
self.assertEqual(result.dose_unit, RELATIVE_DOSE_UNIT)
self.assertEqual(result.doses, A_RELATIVE_DOSE_ARRAY)
self.assertEqual(result.doses, A_RELATIVE_DOSE_LIST)
def test_givenADVHWithAbsoluteDosesAndNoPrescriptionDose_whenConvertingDVHDoseToRelative_thenRaisePrescriptionDoseIsMissingException(self):
self.assertRaises(
......@@ -40,7 +50,7 @@ class TestConversion(unittest.TestCase):
def test_givenADVHWithRelativeDoses_whenConvertingDVHDoseToRelative_thenResultHasTheSameDosesAndThePrescriptionDoseWasNeverUsed(self):
result = conversion.convert_dvh_doses_to_relative(self.a_dvh_with_relative_doses)
self.assertEqual(result.doses, A_RELATIVE_DOSE_ARRAY)
self.assertEqual(result.doses, A_RELATIVE_DOSE_LIST)
self.a_dvh_with_relative_doses.get_prescription_dose.assert_not_called()
def test_givenADVHWithRelativeDosesAndPrescriptionDose_whenConvertingDVHDoseToAbsolute_thenResultIsADVHWithAbsoluteDoses(self):
......@@ -49,7 +59,7 @@ class TestConversion(unittest.TestCase):
result = conversion.convert_dvh_doses_to_absolute(self.a_dvh_with_relative_doses)
self.assertEqual(result.dose_unit, ABSOLUTE_DOSE_UNIT)
self.assertEqual(result.doses, AN_ABSOLUTE_DOSE_ARRAY)
self.assertEqual(result.doses, AN_ABSOLUTE_DOSE_LIST)
def test_givenADVHWithRelativeDosesAndNoPrescriptionDose_whenConvertingDVHDoseToAbsolute_thenRaisePrescriptionDoseIsMissingException(self):
self.assertRaises(
......@@ -68,23 +78,23 @@ class TestConversion(unittest.TestCase):
def _setup_dvh_with_absolute_doses(self) -> DVH:
dvh = Mock()
dvh.doses = AN_ABSOLUTE_DOSE_ARRAY
dvh.doses = AN_ABSOLUTE_DOSE_LIST
dvh.dose_unit = ABSOLUTE_DOSE_UNIT
dvh.get_prescription_dose.return_value = None
dvh.get_dose_unit.return_value = ABSOLUTE_DOSE_UNIT
dvh.get_doses.return_value = AN_ABSOLUTE_DOSE_ARRAY
dvh.get_doses.return_value = AN_ABSOLUTE_DOSE_LIST
return dvh
def _setup_dvh_with_relative_doses(self) -> DVH:
dvh = Mock()
dvh.doses = A_RELATIVE_DOSE_ARRAY
dvh.doses = A_RELATIVE_DOSE_LIST
dvh.dose_unit = RELATIVE_DOSE_UNIT
dvh.get_prescription_dose.return_value = None
dvh.get_dose_unit.return_value = RELATIVE_DOSE_UNIT
dvh.get_doses.return_value = A_RELATIVE_DOSE_ARRAY
dvh.get_doses.return_value = A_RELATIVE_DOSE_LIST
return dvh
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment