Skip to content

Commit

Permalink
Add DICOM data for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
fepegar committed Oct 14, 2020
1 parent b9ece0e commit 66b7a26
Show file tree
Hide file tree
Showing 19 changed files with 10 additions and 15 deletions.
22 changes: 7 additions & 15 deletions tests/data/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class TestIO(TorchioTestCase):
"""Tests for `io` module."""
def setUp(self):
super().setUp()
self.write_dicom()
self.nii_path = self.get_image_path('read_image')
self.dicom_dir = self.get_tests_data_dir() / 'dicom'
self.dicom_path = self.dicom_dir / 'IMG0001.dcm'
string = (
'1.5 0.18088 -0.124887 0.65072 '
'-0.20025 0.965639 -0.165653 -11.6452 '
Expand All @@ -25,18 +27,6 @@ def setUp(self):
tensor = torch.from_numpy(np.fromstring(string, sep=' ').reshape(4, 4))
self.matrix = tensor

def write_dicom(self):
self.dicom_dir = self.dir / 'dicom'
self.dicom_dir.mkdir(exist_ok=True)
self.dicom_path = self.dicom_dir / 'dicom.dcm'
self.nii_path = self.get_image_path('read_image')
writer = sitk.ImageFileWriter()
writer.SetFileName(str(self.dicom_path))
image = sitk.ReadImage(str(self.nii_path))
image = sitk.Cast(image, sitk.sitkUInt16)
image = image[0] # dicom reader supports 2D only
writer.Execute(image)

def test_read_image(self):
# I need to find something readable by nib but not sitk
io.read_image(self.nii_path)
Expand All @@ -47,10 +37,12 @@ def test_save_rgb(self):
im.save(self.dir / 'test.jpg')

def test_read_dicom_file(self):
io.read_image(self.dicom_path)
tensor, _ = io.read_image(self.dicom_path)
self.assertEqual(tuple(tensor.shape), (1, 88, 128, 1))

def test_read_dicom_dir(self):
io.read_image(self.dicom_dir)
tensor, _ = io.read_image(self.dicom_dir)
self.assertEqual(tuple(tensor.shape), (1, 88, 128, 17))

def test_dicom_dir_missing(self):
with self.assertRaises(FileNotFoundError):
Expand Down
Binary file added tests/image_data/dicom/IMG0001.dcm
Binary file not shown.
Binary file added tests/image_data/dicom/IMG0002.dcm
Binary file not shown.
Binary file added tests/image_data/dicom/IMG0003.dcm
Binary file not shown.
Binary file added tests/image_data/dicom/IMG0004.dcm
Binary file not shown.
Binary file added tests/image_data/dicom/IMG0005.dcm
Binary file not shown.
Binary file added tests/image_data/dicom/IMG0006.dcm
Binary file not shown.
Binary file added tests/image_data/dicom/IMG0007.dcm
Binary file not shown.
Binary file added tests/image_data/dicom/IMG0008.dcm
Binary file not shown.
Binary file added tests/image_data/dicom/IMG0009.dcm
Binary file not shown.
Binary file added tests/image_data/dicom/IMG0010.dcm
Binary file not shown.
Binary file added tests/image_data/dicom/IMG0011.dcm
Binary file not shown.
Binary file added tests/image_data/dicom/IMG0012.dcm
Binary file not shown.
Binary file added tests/image_data/dicom/IMG0013.dcm
Binary file not shown.
Binary file added tests/image_data/dicom/IMG0014.dcm
Binary file not shown.
Binary file added tests/image_data/dicom/IMG0015.dcm
Binary file not shown.
Binary file added tests/image_data/dicom/IMG0016.dcm
Binary file not shown.
Binary file added tests/image_data/dicom/IMG0017.dcm
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ def get_image_path(
image.save(path)
return path

def get_tests_data_dir(self):
return Path(__file__).parent / 'image_data'

def assertTensorNotEqual(self, *args, **kwargs): # noqa: N802
message_kwarg = dict(msg=args[2]) if len(args) == 3 else {}
with self.assertRaises(AssertionError, **message_kwarg):
Expand Down

0 comments on commit 66b7a26

Please sign in to comment.