|
a |
|
b/tests/test_patient.py |
|
|
1 |
from __future__ import division, print_function |
|
|
2 |
|
|
|
3 |
import unittest |
|
|
4 |
|
|
|
5 |
import os |
|
|
6 |
import dicom |
|
|
7 |
import numpy as np |
|
|
8 |
|
|
|
9 |
from rvseg import patient |
|
|
10 |
|
|
|
11 |
class TestPatientData(unittest.TestCase): |
|
|
12 |
def setUp(self): |
|
|
13 |
# test data has frames 0,1,..21, with the 20th frame labeled |
|
|
14 |
self.directory = "../test-assets/patient09/" |
|
|
15 |
self.dicom_path = self.directory + "P09dicom" |
|
|
16 |
|
|
|
17 |
def test_patient_data(self): |
|
|
18 |
p = patient.PatientData(self.directory) |
|
|
19 |
self.assertEqual(p.index, 9) |
|
|
20 |
self.assertEqual(p.dicom_path, self.dicom_path) |
|
|
21 |
self.assertEqual(len(p.images), 3) |
|
|
22 |
self.assertEqual(len(p.dicoms), 3) |
|
|
23 |
self.assertEqual(len(p.all_images), 24) |
|
|
24 |
self.assertEqual(len(p.all_dicoms), 24) |
|
|
25 |
self.assertEqual(p.image_width, 256) |
|
|
26 |
self.assertEqual(p.image_height, 216) |
|
|
27 |
self.assertEqual(p.rotated, True) |
|
|
28 |
self.assertEqual(p.labeled, [20, 22, 23]) |
|
|
29 |
self.assertEqual(len(p.endocardium_masks), 3) |
|
|
30 |
self.assertEqual(len(p.epicardium_masks), 3) |
|
|
31 |
self.assertEqual(len(p.endocardium_contours), 3) |
|
|
32 |
self.assertEqual(len(p.epicardium_contours), 3) |
|
|
33 |
|
|
|
34 |
# check dicom MRI image |
|
|
35 |
plan = dicom.read_file(self.directory + "P09dicom/P09-0000.dcm") |
|
|
36 |
np.testing.assert_array_equal(p.all_dicoms[0].pixel_array, |
|
|
37 |
plan.pixel_array) |
|
|
38 |
self.assertEqual(p.images[0].dtype, 'uint16') |
|
|
39 |
|
|
|
40 |
# check endo- and epicardium masks |
|
|
41 |
endo_mask = np.loadtxt(self.directory + "endocardium-p09-0020.mask") |
|
|
42 |
np.testing.assert_array_equal(p.endocardium_masks[0], endo_mask) |
|
|
43 |
self.assertEqual(p.endocardium_masks[0].dtype, 'uint8') |
|
|
44 |
self.assertSetEqual(set(p.endocardium_masks[0].flatten()), set([0, 1])) |
|
|
45 |
|
|
|
46 |
epi_mask = np.loadtxt(self.directory + "epicardium-p09-0020.mask") |
|
|
47 |
np.testing.assert_array_equal(p.epicardium_masks[0], epi_mask) |
|
|
48 |
self.assertEqual(p.epicardium_masks[0].dtype, 'uint8') |
|
|
49 |
self.assertSetEqual(set(p.epicardium_masks[0].flatten()), set([0, 1])) |
|
|
50 |
|
|
|
51 |
@unittest.skip("Skipping video write: OpenCV is a pain to install in test env.") |
|
|
52 |
def test_write_video(self): |
|
|
53 |
p = patient.PatientData(self.directory) |
|
|
54 |
outfile = "test_write_video.mp4" |
|
|
55 |
path = os.path.join(os.getcwd(), outfile) |
|
|
56 |
p.write_video(path) |
|
|
57 |
# simply check file exists and isn't empty |
|
|
58 |
self.assertTrue(os.path.isfile(path)) |
|
|
59 |
self.assertGreater(os.path.getsize(path), 0) |
|
|
60 |
os.remove(path) |