Commit 232ed683 authored by Gabriel Couture's avatar Gabriel Couture

Complete tests for conversion module

parent fd8ff100
Pipeline #177 passed with stage
in 4 seconds
......@@ -3,35 +3,40 @@
from dvh.dvh import DVH
class PrescriptionDoseIsMissingException(Exception):
def __init__(self, *args, **kwargs):
pass
def is_dvh_has_a_prescription_dose(dvh: DVH) -> bool:
return False if dvh.prescription_dose is None else True
return False if dvh.get_prescription_dose() is None else True
def convert_dvh_doses_to_relative(dvh: DVH) -> DVH:
if dvh.dose_unit == '%':
if dvh.get_dose_unit() == '%':
return dvh
if not is_dvh_has_a_prescription_dose(dvh):
raise ValueError(
raise PrescriptionDoseIsMissingException(
'The DVH has no prescription dose. Therefor, relative doses can\'t be calculated.'
)
dvh.doses = [dose / dvh.prescription_dose for dose in dvh.doses]
dvh.doses = [dose / dvh.get_prescription_dose() for dose in dvh.get_doses()]
dvh.dose_unit = '%'
return dvh
def convert_dvh_doses_to_absolute(dvh: DVH) -> DVH:
if dvh.dose_unit == 'GY':
if dvh.get_dose_unit() == 'GY':
return dvh
if not is_dvh_has_a_prescription_dose(dvh):
raise ValueError(
raise PrescriptionDoseIsMissingException(
'The DVH has no prescription dose. Therefor, absolute doses can\'t be calculated from relative doses.'
)
dvh.doses = [dose * dvh.prescription_dose for dose in dvh.doses]
dvh.doses = [dose * dvh.get_prescription_dose() for dose in dvh.get_doses()]
dvh.dose_unit = 'GY'
return dvh
......
......@@ -43,3 +43,27 @@ class DVH:
def _does_volume_unit_has_valid_format(self) -> bool:
return True if self.volume_unit in DVH.VALID_VOLUME_UNITS else False
def get_doses(self) -> List[float]:
return self.doses
def get_volumes(self) -> List[float]:
return self.volumes
def get_dose_unit(self) -> str:
return self.dose_unit
def get_volume_unit(self) -> str:
return self.volume_unit
def get_prescription_dose(self) -> float:
return self.prescription_dose
def get_structure_name(self) -> str:
return self.structure_name
def set_prescription_dose(self, prescription_dose: float) -> None:
self.prescription_dose = prescription_dose
def set_structure_name(self, structure_name: str) -> None:
self.structure_name = structure_name
......@@ -2,4 +2,4 @@
# author: gabriel couture
if __name__ == '__main__':
pass
raise NotImplementedError
# coding: utf-8
# author: gabriel couture
import unittest
from copy import deepcopy
from unittest.mock import Mock
from dvh import conversion
......@@ -14,22 +15,75 @@ class TestConversion(unittest.TestCase):
A_PRESCRIPTION_DOSE = 15
A_DOSE_ARRAY = list(range(0, 16))
AN_ABSOLUTE_DOSE_ARRAY = list(range(0, 16))
A_RELATIVE_DOSE_ARRAY = [dose / 15 for dose in range(0, 16)]
def setUp(self) -> None:
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_givenADVHWithAbsoluteDosesAndPrescriptionDose_whenConvertingDVHDoseToRelative_thenResultIsADVHWithRelativeDoses(self):
a_dvh_with_absolute_doses = self._setup_dvh_with_absolute_doses()
a_dvh_with_absolute_doses.prescription_dose = TestConversion.A_PRESCRIPTION_DOSE
self.a_dvh_with_absolute_doses.get_prescription_dose.return_value = TestConversion.A_PRESCRIPTION_DOSE
result = conversion.convert_dvh_doses_to_relative(a_dvh_with_absolute_doses)
result = conversion.convert_dvh_doses_to_relative(self.a_dvh_with_absolute_doses)
expected_dose_array = [dose / TestConversion.A_PRESCRIPTION_DOSE for dose in TestConversion.A_DOSE_ARRAY]
self.assertEqual(result.dose_unit, TestConversion.RELATIVE_DOSE_UNIT)
self.assertEqual(result.doses, expected_dose_array)
self.assertEqual(result.doses, TestConversion.A_RELATIVE_DOSE_ARRAY)
def test_givenADVHWithAbsoluteDosesAndNoPrescriptionDose_whenConvertingDVHDoseToRelative_thenRaisePrescriptionDoseIsMissingException(self):
self.assertRaises(
conversion.PrescriptionDoseIsMissingException,
lambda: conversion.convert_dvh_doses_to_relative(self.a_dvh_with_absolute_doses)
)
def test_givenADVHWithRelativeDoses_whenConvertingDVHDoseToRelative_thenResultHasTheSameDosesAndThePrescriptionDoseWasNeverUsed(self):
result = conversion.convert_dvh_doses_to_relative(self.a_dvh_with_relative_doses)
self.assertEqual(result.doses, TestConversion.A_RELATIVE_DOSE_ARRAY)
self.a_dvh_with_relative_doses.get_prescription_dose.assert_not_called()
def test_givenADVHWithRelativeDosesAndPrescriptionDose_whenConvertingDVHDoseToAbsolute_thenResultIsADVHWithAbsoluteDoses(self):
self.a_dvh_with_relative_doses.get_prescription_dose.return_value = TestConversion.A_PRESCRIPTION_DOSE
result = conversion.convert_dvh_doses_to_absolute(self.a_dvh_with_relative_doses)
self.assertEqual(result.dose_unit, TestConversion.ABSOLUTE_DOSE_UNIT)
self.assertEqual(result.doses, TestConversion.AN_ABSOLUTE_DOSE_ARRAY)
def test_givenADVHWithRelativeDosesAndNoPrescriptionDose_whenConvertingDVHDoseToAbsolute_thenRaisePrescriptionDoseIsMissingException(self):
self.assertRaises(
conversion.PrescriptionDoseIsMissingException,
lambda: conversion.convert_dvh_doses_to_absolute(self.a_dvh_with_relative_doses)
)
def test_givenADVHWithAbsoluteDoses_whenConvertingDVHDoseToAbsolute_thenResultHasTheSameDosesAndThePrescriptionDoseWasNeverUsed(self):
expected_dvh = deepcopy(self.a_dvh_with_absolute_doses)
result = conversion.convert_dvh_doses_to_absolute(self.a_dvh_with_absolute_doses)
self.assertEqual(expected_dvh.doses, result.doses)
self.a_dvh_with_absolute_doses.get_prescription_dose.assert_not_called()
def _setup_dvh_with_absolute_doses(self) -> DVH:
dvh = Mock()
dvh.doses = TestConversion.A_DOSE_ARRAY
dvh.doses = TestConversion.AN_ABSOLUTE_DOSE_ARRAY
dvh.dose_unit = TestConversion.ABSOLUTE_DOSE_UNIT
dvh.get_prescription_dose.return_value = None
dvh.get_dose_unit.return_value = TestConversion.ABSOLUTE_DOSE_UNIT
dvh.get_doses.return_value = TestConversion.AN_ABSOLUTE_DOSE_ARRAY
return dvh
def _setup_dvh_with_relative_doses(self) -> DVH:
dvh = Mock()
dvh.doses = TestConversion.A_RELATIVE_DOSE_ARRAY
dvh.dose_unit = TestConversion.RELATIVE_DOSE_UNIT
dvh.get_prescription_dose.return_value = None
dvh.get_dose_unit.return_value = TestConversion.RELATIVE_DOSE_UNIT
dvh.get_doses.return_value = TestConversion.A_RELATIVE_DOSE_ARRAY
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