Commit 66682df2 authored by Gabriel Couture's avatar Gabriel Couture

Basic DVH object, with units validation

parent 6d05e058
...@@ -7,7 +7,41 @@ class DVH: ...@@ -7,7 +7,41 @@ class DVH:
""" """
This class is the representation of a DVH. This class is the representation of a DVH.
""" """
VALID_DOSES_UNIT = ['GY', '%']
VALID_VOLUMES_UNIT = ['CM3', '%']
def __init__(self, doses: np.ndarray, volumes: np.ndarray): def __init__(self, doses: np.ndarray,
volumes: np.ndarray,
doses_unit: str,
volumes_unit: str,
structure_name: str = None,
prescription_dose: float = None):
self.doses = doses self.doses = doses
self.volumes = volumes self.volumes = volumes
self.doses_unit = doses_unit
self.volumes_unit = volumes_unit
self._valid_units_format()
self.prescription_dose = prescription_dose
self.structure_name = structure_name
def _valid_units_format(self) -> None:
if not self._does_doses_unit_has_valid_format():
raise ValueError(
f'doses_unit should be {DVH.VALID_DOSES_UNIT}, given doses_unit is {self.doses_unit}'
)
if not self._does_volumes_unit_has_valid_format():
raise ValueError(
f'volumes_unit should be {DVH.VALID_VOLUMES_UNIT}, given volumes_unit is {self.volumes_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_volumes_unit_has_valid_format(self) -> bool:
return True if self.volumes_unit in DVH.VALID_VOLUMES_UNIT else False
# coding: utf-8
# author: gabriel couture
# coding: utf-8
# author: gabriel couture
import unittest
import numpy as np
from dvh.dvh import DVH
class TestDVH(unittest.TestCase):
A_VALID_DOSE_UNIT = 'GY'
AN_INVALID_DOSE_UNIT = 'cGY'
A_VALID_VOLUME_UNIT = 'CM3'
AN_INVALID_VOLUME_UNIT = 'cc'
A_DOSE_ARRAY = np.linspace(1, 2, 100)
A_VOLUME_ARRAY = np.linspace(2, 1, 100)
def setUp(self) -> None:
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
)
def tearDown(self) -> None:
self.a_dvh = None
def test_givenValidDataAndUnits_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
)
self.assertIsInstance(result, DVH)
def test_givenAnInvalidDoseUnit_whenCreatingADVH_thenRaiseValueError(self):
self.assertRaises(
ValueError,
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
)
)
def test_givenAnInvalidVolumeUnit_whenCreatingADVH_thenRaiseValueError(self):
self.assertRaises(
ValueError,
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
)
)
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