-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_segmentor.py
97 lines (84 loc) · 3.8 KB
/
test_segmentor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import numpy.testing as npt
import nibabel as nib
import os
import pandas as pd
import pytest
import renal_segmentor as rs
import shutil
import sys
from gooey.python_bindings import argparse_to_json
from segment.data import fetch
from segment.tests.utils import same_image
from unittest.mock import patch
class TestParser:
@pytest.mark.parametrize('action, widget, expected', [
(1, 'FileChooser', {'id': 'input',
'type': 'FileChooser',
'required': True}),
(2, 'CheckBox', {'id': '-b',
'type': 'CheckBox',
'required': False}),
(3, 'CheckBox', {'id': '-p',
'type': 'CheckBox',
'required': False}),
(4, 'CheckBox', {'id': '-r',
'type': 'CheckBox',
'required': False}),
(5, 'CheckBox', {'id': '-v',
'type': 'CheckBox',
'required': False}),
(6, 'DirChooser', {'id': '-output',
'type': 'DirChooser',
'required': False})
])
def test_parser(self, action, widget, expected):
parser = rs.get_parser()
assert len(parser._actions) == 7
result = argparse_to_json.action_to_json(parser._actions[action],
widget, {})
assert expected.items() <= result.items()
class TestCli:
def test_single_file(self):
test_args = ['renal_segmentor',
fetch.Sub1('PAR').path,
'-output', 'test_output']
os.makedirs('test_output', exist_ok=True)
with patch.object(sys, 'argv', test_args):
rs.main()
output_files = os.listdir('test_output')
assert len(output_files) == 1
assert os.path.exists('test_output/test_sub_01_mask.nii.gz') == 1
for f in os.listdir('test_output'):
os.remove(os.path.join('test_output', f))
shutil.rmtree('test_output')
def test_multiple_files(self):
test_args = ['renal_segmentor',
fetch.Sub1('PAR').path,
fetch.Sub2('PAR').path,
'-output', 'test_output', '-v', '-r']
os.makedirs('test_output', exist_ok=True)
with patch.object(sys, 'argv', test_args):
rs.main()
output_files = os.listdir('test_output')
assert len(output_files) == 5
assert os.path.exists('test_output/test_sub_01_mask.nii.gz') == 1
assert os.path.exists('test_output/test_sub_02_mask.nii.gz') == 1
assert os.path.exists('test_output/test_sub_01.nii.gz') == 1
assert os.path.exists('test_output/test_sub_02.nii.gz') == 1
assert os.path.exists('test_output/volumes.csv') == 1
volumes = pd.read_csv('test_output/volumes.csv')
npt.assert_allclose(volumes['TKV (ml)'].mean(), 304.663341,
atol=0.5, rtol=1E-2)
npt.assert_allclose(volumes['LKV (ml)'].mean(), 159.1703901,
atol=0.5, rtol=1E-2)
npt.assert_allclose(volumes['RKV (ml)'].mean(), 145.4929511,
atol=0.5, rtol=1E-2)
mask_sub_01 = nib.load(
'test_output/test_sub_01_mask.nii.gz').get_fdata()
mask_sub_02 = nib.load(
'test_output/test_sub_02_mask.nii.gz').get_fdata()
same_image(mask_sub_01, [0.040264, 0.19392, 1.0, 0.0, 13.0, 0.0])
same_image(mask_sub_02, [0.018617, 0.135167, 1.0, 0.0, 17.0, 0.0])
for f in os.listdir('test_output'):
os.remove(os.path.join('test_output', f))
shutil.rmtree('test_output')