Commit 9bbbaa2a authored by Gabriel Couture's avatar Gabriel Couture

Adding get_layer fonction to dao

parent aa13d3b9
......@@ -4,9 +4,10 @@
This module is a Data Access Object (DAO) for the DICOM
file and corresponding DVHs.
"""
import json
import os
from os.path import join
from typing import List
from typing import Dict, Optional
import pandas as pd
import pydicom
......@@ -53,6 +54,8 @@ def _format_dose_gradient(dose_gradient: str) -> str:
elif dose_gradient == 'SI':
return 'SupInf'
return 'WRONG_DOSE_GRADIENT'
def get_rtstruct(geometry: str, spacing: float) -> pydicom.FileDataset:
"""Get RTSTRUCT DICOM files.
......@@ -71,7 +74,6 @@ def get_rtstruct(geometry: str, spacing: float) -> pydicom.FileDataset:
_valid_geometry(geometry), _valid_spacing(spacing)
directory, _ = os.path.split(__file__)
geometry_path = join(directory, 'data', 'RTSTRUCT', geometry)
if not os.path.exists(geometry_path):
......@@ -171,3 +173,18 @@ def get_analytic_dvhs(geometry: str, spacing: float, dose_gradient: str, roi: st
spacing_str = str(spacing).replace('.0', '').replace('.', '')
return pd.read_csv(filename)[['Dose (cGy)', f'{spacing_str}mm{dose_gradient}']]
def get_layer(layer_name: Optional[str] = None, file_path: Optional[str] = None) -> Dict:
if (layer_name and file_path) or (not layer_name and not file_path):
raise ValueError('Choose a layer name, or a path to your own layer (should be a .json file).')
if file_path:
with open(file_path) as fh:
return json.load(fh)
directory, _ = os.path.split(__file__)
path = join(directory, 'data', 'layers', layer_name if '.json' in layer_name else f'{layer_name}.json')
with open(path) as fh:
return json.load(fh)
{
"PatientID": "03HDQ000"
}
\ No newline at end of file
# coding: utf-8
# author: Gabriel Couture
from typing import Dict, Tuple
from typing import Dict
from pydicom import FileDataset
def apply_layer(
layer: Dict,
rtplan: FileDataset,
rtdose: FileDataset,
rtstruct: FileDataset) -> Tuple[FileDataset, FileDataset, FileDataset]:
return rtplan, rtdose, rtstruct
def apply_layer(layer: Dict, dataset: FileDataset) -> FileDataset:
for key, value in layer.items():
if type(value) is list or type(value) is dict:
pass
dataset[key] = value
return dataset
......@@ -2,7 +2,7 @@
# author: Gabriel Couture
import os
from os.path import join
from typing import Dict, Optional
from typing import Dict
import pydicom
......@@ -34,7 +34,7 @@ def write_dicom_rt(
spacing: float,
dose_gradient: str,
roi: str,
layer: Optional[Dict] = None):
layer: Dict = {}):
"""Write the DICOM-RT files (RTDOSE, RTSTRUCT, RTPLAN).
Parameters
......@@ -64,7 +64,9 @@ def write_dicom_rt(
dvh_data = dao.get_analytic_dvhs(geometry, spacing, dose_gradient, roi)
# Applying desired layers
rtplan, rtdose, rtstruct = apply_layer(layer, rtplan, rtdose, rtstruct)
rtplan = apply_layer(layer, rtplan)
rtdose = apply_layer(layer, rtdose)
rtstruct = apply_layer(layer, rtstruct)
# Writing results
os.makedirs(target_directory)
......
......@@ -6,7 +6,6 @@ from setuptools import setup, find_packages
from setuptools.command.test import test as TestCommand
FLAKE8_COMMAND = ['./venv/bin/flake8', '--ignore=E501', 'dicomgenerator', 'tests']
MYPY_COMMAND = ['./venv/bin/mypy', 'dicomgenerator']
TESTS_COMMAND = ['./venv/bin/python', '-m', 'unittest', 'discover', '-s', './tests']
......@@ -25,7 +24,6 @@ class LintTests(TestCommand):
def run_tests(self):
_run_command(FLAKE8_COMMAND)
_run_command(MYPY_COMMAND)
class Tests(TestCommand):
......@@ -35,7 +33,6 @@ class Tests(TestCommand):
def run_tests(self):
_run_command(TESTS_COMMAND)
_run_command(FLAKE8_COMMAND)
_run_command(MYPY_COMMAND)
with open('./README.md', 'r') as file_handler:
......
This diff is collapsed.
# Metadata
This directory contains the DICOM-RT files for a case presented below: - geometry: cone
- spacing: 1.0mm
- dose_gradient: AP
- roi: axial
......@@ -12,6 +12,8 @@ A_GEOMETRY = 'cone'
A_SPACING = 1.0
A_DOSE_GRADIENT = 'AP'
A_ROI = 'axial'
A_LAYER_NAME = 'HDQID'
A_LAYER_FILE_PATH = "./dicomgenerator/data/layers/HDQID.json"
A_BAD_GEOMETRY = 'A_BAD_GEOMETRY'
A_BAD_SPACING = 0.1
A_BAD_DOSE_GRADIENT = 'A_BAD_DOSE_GRADIENT'
......@@ -67,6 +69,30 @@ class TestDao(unittest.TestCase):
self.assertIsInstance(result, pd.DataFrame)
self.assertEqual(list(result.columns), expected_columns)
def test_givenALayerName_whenGettingLayer_thenResultIsExpectedDictionary(self):
result = dao.get_layer(A_LAYER_NAME)
self.assertIsInstance(result, dict)
self.assertEqual(result, {'PatientID': '03HDQ000'})
def test_givenALayerPath_whenGettingLayer_thenResultIsExpectedDictionary(self):
result = dao.get_layer(file_path=A_LAYER_FILE_PATH)
self.assertIsInstance(result, dict)
self.assertEqual(result, {'PatientID': '03HDQ000'})
def test_givenALayerNameAndALayerPath_whenGettingLayer_thenRaiseValueError(self):
self.assertRaises(
ValueError,
lambda: dao.get_layer(A_LAYER_NAME, A_LAYER_FILE_PATH)
)
def test_givenNothing_whenGettingLayer_thenRaiseValueError(self):
self.assertRaises(
ValueError,
lambda: dao.get_layer()
)
def assert_is_rtstruct(self, data_set: pydicom.FileDataset):
self.assertIsInstance(data_set, pydicom.FileDataset)
self.assertEqual(data_set['Modality'].value, 'RTSTRUCT')
......
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