Commit 4911d782 authored by Gabriel Couture's avatar Gabriel Couture

Adding read/write functions for DVH class, with tests

parent 0d106c65
Pipeline #184 failed with stage
in 5 seconds
......@@ -3,7 +3,6 @@ before_script:
- virtualenv -p /opt/rh/rh-python36/root/usr/bin/python3.6 venv
- ./venv/bin/pip3 install -r requirements.txt
stages:
- test
......
# coding: utf-8
# author: gabriel couture
from __future__ import annotations
import json
from typing import List
......@@ -25,6 +27,7 @@ class DVH:
structure_name: The Structure name (i.e. prostate).
prescription_dose: The prescription doses, which is needed for the
conversion from absolute to relative dose values.
"""
self.doses = doses
self.volumes = volumes
......@@ -54,6 +57,51 @@ class DVH:
def _does_volume_unit_has_valid_format(self) -> bool:
return True if self.volume_unit in DVH.VALID_VOLUME_UNITS else False
@classmethod
def read_from_json(cls, file_path: str) -> DVH:
"""Read DVH data from given path.
Args:
file_path: File path to be read (a .json).
Returns:
DVH: a DVH object.
"""
with open(file_path, 'r') as file_handler:
dvh_data = json.load(file_handler)
return cls(
doses=dvh_data['doses'],
dose_unit=dvh_data['dose_units'],
volumes=dvh_data['volumes'],
volume_unit=dvh_data['volume_units'],
prescription_dose=dvh_data['prescription_dose'],
structure_name=dvh_data['structure_name']
)
def write_to_json(self, file_path: str) -> None:
"""Write DVH data at given path.
Args:
file_path: File path to be write as a .json.
Returns:
Nothing.
"""
dvh_data = {
'structure_name': self.get_structure_name(),
'doses': self.get_doses(),
'dose_units': self.get_dose_unit(),
'volumes': self.get_volumes(),
'volume_units': self.get_volume_unit(),
'prescription_dose': self.get_prescription_dose()
}
with open(file_path, 'w') as file_handler:
json.dump(dvh_data, file_handler)
def get_doses(self) -> List[float]:
return self.doses
......
# coding: utf-8
# author: gabriel couture
# coding: utf-8
# author: gabriel couture
......@@ -40,7 +40,7 @@ class TestConversion(unittest.TestCase):
self.assertTrue(result)
def test_givenADVH_whenCheckingIfDVHHasAPrescriptionDose_thenResultIsTrue(self):
def test_givenADVH_whenCheckingIfDVHHasAPrescriptionDose_thenResultIsFalse(self):
result = conversion.is_dvh_has_a_prescription_dose(self.a_dvh_with_absolute_doses)
self.assertFalse(result)
......
# coding: utf-8
# author: gabriel couture
import unittest
from unittest import mock
from dvh.dvh import DVH
A_VALID_DOSE_UNIT = 'GY'
AN_INVALID_DOSE_UNIT = 'cGY'
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 = list(range(16))
A_VOLUME_ARRAY = A_DOSE_ARRAY[::-1]
A_STRUCTURE_NAME = 'A_STRUCTURE_NAME'
A_PRESCRIPTION_DOSE = 15
A_DVH_DATA = {
'structure_name': A_STRUCTURE_NAME,
'doses': A_DOSE_ARRAY,
'dose_units': A_VALID_DOSE_UNIT,
'volumes': A_VOLUME_ARRAY,
'volume_units': A_VALID_VOLUME_UNIT,
'prescription_dose': A_PRESCRIPTION_DOSE
}
A_VALID_VOLUME_UNIT = 'CM3'
AN_INVALID_VOLUME_UNIT = 'cc'
A_DOSE_ARRAY = list(range(16))
A_VOLUME_ARRAY = list(range(16))[::-1]
class TestDVH(unittest.TestCase):
def test_givenValidDataAndUnitsAndDVHType_whenCreatingADVH_thenResultIsADVH(self):
result = 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,
doses=A_DOSE_ARRAY,
volumes=A_VOLUME_ARRAY,
dose_unit=A_VALID_DOSE_UNIT,
volume_unit=A_VALID_VOLUME_UNIT,
)
self.assertIsInstance(result, DVH)
......@@ -29,10 +42,10 @@ class TestDVH(unittest.TestCase):
self.assertRaises(
ValueError,
lambda: DVH(
doses=TestDVH.A_DOSE_ARRAY,
volumes=TestDVH.A_VOLUME_ARRAY,
dose_unit=TestDVH.AN_INVALID_DOSE_UNIT,
volume_unit=TestDVH.A_VALID_VOLUME_UNIT,
doses=A_DOSE_ARRAY,
volumes=A_VOLUME_ARRAY,
dose_unit=AN_INVALID_DOSE_UNIT,
volume_unit=A_VALID_VOLUME_UNIT,
)
)
......@@ -40,9 +53,35 @@ class TestDVH(unittest.TestCase):
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.AN_INVALID_VOLUME_UNIT,
doses=A_DOSE_ARRAY,
volumes=A_VOLUME_ARRAY,
dose_unit=A_VALID_DOSE_UNIT,
volume_unit=AN_INVALID_VOLUME_UNIT,
)
)
@mock.patch('builtins.open')
@mock.patch('json.dump')
def test_givenADVH_whenWritingToJson_thenJsonDumpFunctionIsCalled(self, mock_open: mock.Mock, mock_json_dump: mock.Mock):
a_file_path = 'a_file_path.json'
a_dvh = DVH(
doses=A_DOSE_ARRAY,
volumes=A_VOLUME_ARRAY,
dose_unit=A_VALID_DOSE_UNIT,
volume_unit=A_VALID_VOLUME_UNIT,
)
a_dvh.write_to_json(a_file_path)
mock_open.assert_called_once()
mock_json_dump.assert_called_once()
@mock.patch('builtins.open')
@mock.patch('json.load', lambda f: A_DVH_DATA)
def test_whenReadingDVHFromJsonFile_thenResultIsADVH(self, mock_open: mock.Mock):
a_file_path = 'a_file_path.json'
result = DVH.read_from_json(a_file_path)
mock_open.assert_called_once()
self.assertIsInstance(result, 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