Skip to content

Commit

Permalink
Merge pull request #9 from qurit/8-optinal-fields
Browse files Browse the repository at this point in the history
feat: make certain ds fields optional and test
  • Loading branch information
asim-shrestha authored Feb 24, 2021
2 parents 4e19d64 + 8e78e45 commit 8c18a38
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 26 deletions.
18 changes: 9 additions & 9 deletions rt_utils/ds_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,22 @@ def add_study_and_series_information(ds: FileDataset, series_data):
ds.SeriesDate = reference_ds.SeriesDate
ds.StudyTime = reference_ds.StudyTime
ds.SeriesTime = reference_ds.SeriesTime
ds.StudyDescription = reference_ds.StudyDescription
ds.SeriesDescription = reference_ds.SeriesDescription
ds.StudyDescription = getattr(reference_ds, 'StudyDescription', '')
ds.SeriesDescription = getattr(reference_ds, 'SeriesDescription', '')
ds.StudyInstanceUID = reference_ds.StudyInstanceUID
ds.SeriesInstanceUID = generate_uid() # TODO: find out if random generation is ok
ds.StudyID = reference_ds.StudyID
ds.SeriesNumber = "1" # TODO: find out if we can just use 1 (Should be fine since its a new series)

def add_patient_information(ds: FileDataset, series_data):
reference_ds = series_data[0] # All elements in series should have the same data
ds.PatientName = reference_ds.PatientName
ds.PatientID = reference_ds.PatientID
ds.PatientBirthDate = reference_ds.PatientBirthDate
ds.PatientSex = reference_ds.PatientSex
ds.PatientAge = reference_ds.PatientAge
ds.PatientSize = reference_ds.PatientSize
ds.PatientWeight = reference_ds.PatientWeight
ds.PatientName = getattr(reference_ds, 'PatientName', '')
ds.PatientID = getattr(reference_ds, 'PatientID', '')
ds.PatientBirthDate = getattr(reference_ds, 'PatientBirthDate', '')
ds.PatientSex = getattr(reference_ds, 'PatientSex', '')
ds.PatientAge = getattr(reference_ds, 'PatientAge', '')
ds.PatientSize = getattr(reference_ds, 'PatientSize', '')
ds.PatientWeight = getattr(reference_ds, 'PatientWeight', '')


def add_refd_frame_of_ref_sequence(ds: FileDataset, series_data):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import setuptools

VERSION = '1.0.3'
VERSION = '1.0.4'
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
with open('requirements.txt') as f:
Expand Down
20 changes: 20 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from rt_utils.rtstruct import RTStruct
import pytest
import os
from rt_utils import RTStructBuilder

@pytest.fixture()
def series_path() -> str:
return get_and_test_series_path()

@pytest.fixture()
def new_rtstruct() -> RTStruct:
path = get_and_test_series_path()
rtstruct = RTStructBuilder.create_new(path)
return rtstruct

def get_and_test_series_path() -> str:
series_path = os.path.join(os.path.dirname(__file__), 'mock_data/')
assert os.path.exists(series_path)
return series_path

27 changes: 27 additions & 0 deletions tests/test_ds_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
from rt_utils import ds_helper, image_helper

def test_correctly_acquire_optional_ds_field(series_path):
series_data = image_helper.load_sorted_image_series(series_path)

patient_age = series_data[0]['PatientAge'].value
ds = ds_helper.create_rtstruct_dataset(series_data)

assert ds.PatientAge == patient_age

def test_ds_creation_without_patient_age(series_path):
# Ensure only 1 file in series data so ds_helper uses the file for header reference
series_data = image_helper.load_sorted_image_series(series_path)
series_data = [series_data[0]]

# Remove optional field
original_age = series_data[0]['PatientAge'].value
del series_data[0]['PatientAge']
with pytest.raises(Exception):
access = series_data[0]['PatientAge']

ds = ds_helper.create_rtstruct_dataset(series_data)

assert ds.PatientAge != original_age
assert ds.PatientAge == ''

22 changes: 6 additions & 16 deletions tests/test_rtstruct_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ def test_get_invalid_roi_mask_by_name(new_rtstruct: RTStruct):
new_rtstruct.get_roi_mask_by_name("FAKE_NAME")


def test_loading_invalid_rt_struct():
invalid_rt_struct_path = os.path.join(get_series_path(), 'ct_1.dcm')
def test_loading_invalid_rt_struct(series_path):
invalid_rt_struct_path = os.path.join(series_path, 'ct_1.dcm')
assert os.path.exists(invalid_rt_struct_path)
with pytest.raises(Exception):
RTStructBuilder.create_from(get_series_path(), invalid_rt_struct_path)
RTStructBuilder.create_from(series_path, invalid_rt_struct_path)


def test_loading_valid_rt_struct():
valid_rt_struct_path = os.path.join(get_series_path(), 'rt.dcm')
def test_loading_valid_rt_struct(series_path):
valid_rt_struct_path = os.path.join(series_path, 'rt.dcm')
assert os.path.exists(valid_rt_struct_path)
rtstruct = RTStructBuilder.create_from(get_series_path(), valid_rt_struct_path)
rtstruct = RTStructBuilder.create_from(series_path, valid_rt_struct_path)

# Tests existing values predefined in the file are found
assert hasattr(rtstruct.ds, 'ROIContourSequence')
Expand Down Expand Up @@ -157,13 +157,3 @@ def get_empty_mask(rtstruct) -> np.ndarray:
mask = np.zeros(mask_dims)
return mask.astype(bool)

def get_series_path():
series_path = os.path.join(os.path.dirname(__file__), 'mock_data')
assert os.path.exists(series_path)
return series_path


@pytest.fixture
def new_rtstruct() -> RTStruct:
rtstruct = RTStructBuilder.create_new(get_series_path())
return rtstruct

0 comments on commit 8c18a38

Please sign in to comment.