Commit 5b59ea7b authored by Gabriel Couture's avatar Gabriel Couture

Small refactoring, adding dvh type

parent 758a0075
Pipeline #175 passed with stage
in 7 seconds
......@@ -7,41 +7,50 @@ class DVH:
"""
This class is the representation of a DVH.
"""
VALID_DOSES_UNIT = ['GY', '%']
VALID_VOLUMES_UNIT = ['CM3', '%']
VALID_DOSE_UNITS = ['GY', '%']
VALID_VOLUME_UNITS = ['CM3', '%']
VALID_DVH_TYPES = ['cumulative', 'differential']
def __init__(self, doses: np.ndarray,
volumes: np.ndarray,
doses_unit: str,
volumes_unit: str,
dose_unit: str,
volume_unit: str,
dvh_type: str,
structure_name: str = None,
prescription_dose: float = None):
self.doses = doses
self.volumes = volumes
self.doses_unit = doses_unit
self.volumes_unit = volumes_unit
self._valid_units_format()
self.dose_unit = dose_unit
self.volume_unit = volume_unit
self.type = dvh_type
self.prescription_dose = prescription_dose
self.structure_name = structure_name
self._valid_units_format()
self._valid_dvh_type()
def _valid_units_format(self) -> None:
if not self._does_doses_unit_has_valid_format():
if not self._does_dose_unit_has_valid_format():
raise ValueError(
f'doses_unit should be {DVH.VALID_DOSES_UNIT}, given doses_unit is {self.doses_unit}'
f'dose_unit should be in {DVH.VALID_DOSE_UNITS}, given dose_unit is {self.dose_unit}'
)
if not self._does_volumes_unit_has_valid_format():
if not self._does_volume_unit_has_valid_format():
raise ValueError(
f'volumes_unit should be {DVH.VALID_VOLUMES_UNIT}, given volumes_unit is {self.volumes_unit}'
f'volume_unit should be in {DVH.VALID_VOLUME_UNITS}, given volume_unit is {self.volume_unit}'
)
def _does_doses_unit_has_valid_format(self) -> bool:
return True if self.doses_unit in DVH.VALID_DOSES_UNIT else False
def _does_dose_unit_has_valid_format(self) -> bool:
return True if self.dose_unit in DVH.VALID_DOSE_UNITS else False
def _does_volumes_unit_has_valid_format(self) -> bool:
return True if self.volumes_unit in DVH.VALID_VOLUMES_UNIT else False
def _does_volume_unit_has_valid_format(self) -> bool:
return True if self.volume_unit in DVH.VALID_VOLUME_UNITS else False
def _valid_dvh_type(self):
if self.type not in DVH.VALID_DVH_TYPES:
raise ValueError(
f'dvh_type should be in {DVH.VALID_DVH_TYPES}, given dvh_type is {self.type}'
)
......@@ -14,6 +14,9 @@ class TestDVH(unittest.TestCase):
A_VALID_VOLUME_UNIT = 'CM3'
AN_INVALID_VOLUME_UNIT = 'cc'
A_VALID_DVH_TYPE = 'cumulative'
AN_INVALID_DVH_TYPE = 'an_invalid_dvh_type'
A_DOSE_ARRAY = np.linspace(1, 2, 100)
A_VOLUME_ARRAY = np.linspace(2, 1, 100)
......@@ -21,19 +24,21 @@ class TestDVH(unittest.TestCase):
self.a_dvh = DVH(
doses=TestDVH.A_DOSE_ARRAY,
volumes=TestDVH.A_VOLUME_ARRAY,
doses_unit=TestDVH.A_VALID_DOSE_UNIT,
volumes_unit=TestDVH.A_VALID_VOLUME_UNIT
dose_unit=TestDVH.A_VALID_DOSE_UNIT,
volume_unit=TestDVH.A_VALID_VOLUME_UNIT,
dvh_type=TestDVH.A_VALID_DVH_TYPE,
)
def tearDown(self) -> None:
self.a_dvh = None
def test_givenValidDataAndUnits_whenCreatingADVH_thenResultIsADVH(self):
def test_givenValidDataAndUnitsAndDVHType_whenCreatingADVH_thenResultIsADVH(self):
result = DVH(
doses=TestDVH.A_DOSE_ARRAY,
volumes=TestDVH.A_VOLUME_ARRAY,
doses_unit=TestDVH.A_VALID_DOSE_UNIT,
volumes_unit=TestDVH.A_VALID_VOLUME_UNIT
dose_unit=TestDVH.A_VALID_DOSE_UNIT,
volume_unit=TestDVH.A_VALID_VOLUME_UNIT,
dvh_type=TestDVH.A_VALID_DVH_TYPE,
)
self.assertIsInstance(result, DVH)
......@@ -44,8 +49,9 @@ class TestDVH(unittest.TestCase):
lambda: DVH(
doses=TestDVH.A_DOSE_ARRAY,
volumes=TestDVH.A_VOLUME_ARRAY,
doses_unit=TestDVH.AN_INVALID_DOSE_UNIT,
volumes_unit=TestDVH.A_VALID_VOLUME_UNIT
dose_unit=TestDVH.AN_INVALID_DOSE_UNIT,
volume_unit=TestDVH.A_VALID_VOLUME_UNIT,
dvh_type=TestDVH.A_VALID_DVH_TYPE,
)
)
......@@ -55,8 +61,21 @@ class TestDVH(unittest.TestCase):
lambda: DVH(
doses=TestDVH.A_DOSE_ARRAY,
volumes=TestDVH.A_VOLUME_ARRAY,
doses_unit=TestDVH.A_VALID_DOSE_UNIT,
volumes_unit=TestDVH.AN_INVALID_VOLUME_UNIT
dose_unit=TestDVH.A_VALID_DOSE_UNIT,
volume_unit=TestDVH.AN_INVALID_VOLUME_UNIT,
dvh_type=TestDVH.A_VALID_DVH_TYPE,
)
)
def test_givenAnInvalidDVHType_whenCreatingADVH_thenRaiseValueError(self):
self.assertRaises(
ValueError,
lambda: DVH(
doses=TestDVH.A_DOSE_ARRAY,
volumes=TestDVH.A_VOLUME_ARRAY,
dose_unit=TestDVH.A_VALID_DOSE_UNIT,
volume_unit=TestDVH.A_VALID_VOLUME_UNIT,
dvh_type=TestDVH.AN_INVALID_DVH_TYPE,
)
)
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