test_scripts_mrs_tools.py 7.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
'''FSL-MRS test script

Test the tools script

Copyright Will Clarke, University of Oxford, 2021'''

# Imports
import subprocess
from pathlib import Path
William Clarke's avatar
William Clarke committed
10
import nibabel as nib
11
12
13
14
15
16

# Files
testsPath = Path(__file__).parent

# Testing vis option
svs = testsPath / 'testdata/fsl_mrs/metab.nii.gz'
17
svs_raw = testsPath / 'testdata/fsl_mrs_preproc/metab_raw.nii.gz'
18
19
20
21
basis = testsPath / 'testdata/fsl_mrs/steam_basis'


def test_vis_svs(tmp_path):
22
    subprocess.check_call(['mrs_tools', 'vis',
23
24
25
26
27
28
                           '--ppmlim', '0.2', '4.2',
                           '--save', str(tmp_path / 'svs.png'),
                           svs])

    assert (tmp_path / 'svs.png').exists()

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    subprocess.check_call(['mrs_tools', 'vis',
                           '--ppmlim', '0.2', '4.2',
                           '--save', str(tmp_path / 'svs2.png'),
                           svs.with_suffix('').with_suffix('')])

    assert (tmp_path / 'svs2.png').exists()

    subprocess.check_call(['mrs_tools', 'vis',
                           '--ppmlim', '0.2', '4.2',
                           '--display_dim', 'DIM_DYN',
                           '--save', str(tmp_path / 'svs3.png'),
                           svs_raw])

    assert (tmp_path / 'svs3.png').exists()

    subprocess.check_call(['mrs_tools', 'vis',
                           '--ppmlim', '0.2', '4.2',
                           '--display_dim', 'DIM_COIL',
                           '--no_mean',
                           '--save', str(tmp_path / 'svs4.png'),
                           svs_raw])

    assert (tmp_path / 'svs4.png').exists()

53
54

def test_vis_basis(tmp_path):
55
    subprocess.check_call(['mrs_tools', 'vis',
56
57
58
59
60
61
62
63
64
65
66
67
68
                           '--ppmlim', '0.2', '4.2',
                           '--save', str(tmp_path / 'basis.png'),
                           basis])

    assert (tmp_path / 'basis.png').exists()


# Testing info option
processed = testsPath / 'testdata/fsl_mrs/metab.nii.gz'
unprocessed = testsPath / 'testdata/fsl_mrs_preproc/metab_raw.nii.gz'


def test_single_info(tmp_path):
69
    subprocess.check_call(['mrs_tools', 'info', str(processed)])
70
71
72


def test_multi_info(tmp_path):
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
    subprocess.check_call(['mrs_tools', 'info', str(processed), str(unprocessed)])


# Testing merge option
test_data_merge_1 = testsPath / 'testdata' / 'fsl_mrs_preproc' / 'wref_raw.nii.gz'
test_data_merge_2 = testsPath / 'testdata' / 'fsl_mrs_preproc' / 'quant_raw.nii.gz'


def test_merge(tmp_path):
    """The tests here only check that the expected files are created.
    I rely on the much more detailed tests in test_utils_nifti_mrs_tools_split_merge.py
    to check that the merge is carried out correctly.
    """
    subprocess.check_call(['mrs_tools', 'merge',
                           '--dim', 'DIM_DYN',
                           '--output', str(tmp_path),
                           '--filename', 'test_2_merge',
                           '--files', str(test_data_merge_1), str(test_data_merge_2)])

    assert (tmp_path / 'test_2_merge.nii.gz').exists()

    subprocess.check_call(['mrs_tools', 'merge',
                           '--dim', 'DIM_DYN',
                           '--output', str(tmp_path),
                           '--filename', 'test_3_merge',
                           '--files', str(test_data_merge_1), str(test_data_merge_2), str(test_data_merge_2)])

    assert (tmp_path / 'test_3_merge.nii.gz').exists()

    subprocess.check_call(['mrs_tools', 'merge',
                           '--dim', 'DIM_DYN',
                           '--output', str(tmp_path),
                           '--files', str(test_data_merge_1), str(test_data_merge_2)])

    assert (tmp_path / 'wref_raw_quant_raw_merged.nii.gz').exists()


# Test split option
test_data_split = testsPath / 'testdata' / 'fsl_mrs_preproc' / 'metab_raw.nii.gz'


def test_split(tmp_path):
    """The tests here only check that the expected files are created.
    I rely on the much more detailed tests in test_utils_nifti_mrs_tools_split_merge.py
    to check that the merge is carried out correctly.
    """
    subprocess.check_call(['mrs_tools', 'split',
                           '--dim', 'DIM_DYN',
                           '--index', '31',
                           '--output', str(tmp_path),
                           '--filename', 'split_file',
                           '--file', str(test_data_split)])

    assert (tmp_path / 'split_file_1.nii.gz').exists()
    assert (tmp_path / 'split_file_2.nii.gz').exists()
William Clarke's avatar
William Clarke committed
128
129
130
131
    f1 = nib.load(tmp_path / 'split_file_1.nii.gz')
    f2 = nib.load(tmp_path / 'split_file_2.nii.gz')
    assert f1.shape[5] == 32
    assert f2.shape[5] == 32
132
133
134
135
136
137
138
139
140

    subprocess.check_call(['mrs_tools', 'split',
                           '--dim', 'DIM_DYN',
                           '--index', '31',
                           '--output', str(tmp_path),
                           '--file', str(test_data_split)])

    assert (tmp_path / 'metab_raw_1.nii.gz').exists()
    assert (tmp_path / 'metab_raw_2.nii.gz').exists()
William Clarke's avatar
William Clarke committed
141
142
143
144
    f1 = nib.load(tmp_path / 'metab_raw_1.nii.gz')
    f2 = nib.load(tmp_path / 'metab_raw_2.nii.gz')
    assert f1.shape[5] == 32
    assert f2.shape[5] == 32
145
146
147
148

    subprocess.check_call(['mrs_tools', 'split',
                           '--dim', 'DIM_DYN',
                           '--indices', '31', '34', '40',
William Clarke's avatar
William Clarke committed
149
                           '--filename', 'indicies_select',
150
151
152
                           '--output', str(tmp_path),
                           '--file', str(test_data_split)])

William Clarke's avatar
William Clarke committed
153
154
155
156
157
158
    assert (tmp_path / 'indicies_select_1.nii.gz').exists()
    assert (tmp_path / 'indicies_select_2.nii.gz').exists()
    f1 = nib.load(tmp_path / 'indicies_select_1.nii.gz')
    f2 = nib.load(tmp_path / 'indicies_select_2.nii.gz')
    assert f1.shape[5] == 61
    assert f2.shape[5] == 3
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184


# Test reorder option
def test_reorder(tmp_path):
    subprocess.check_call(['mrs_tools', 'reorder',
                           '--dim_order', 'DIM_DYN', 'DIM_COIL',
                           '--output', str(tmp_path),
                           '--filename', 'reordered_file',
                           '--file', str(test_data_split)])

    assert (tmp_path / 'reordered_file.nii.gz').exists()

    subprocess.check_call(['mrs_tools', 'reorder',
                           '--dim_order', 'DIM_COIL', 'DIM_DYN', 'DIM_EDIT',
                           '--output', str(tmp_path),
                           '--file', str(test_data_split)])

    assert (tmp_path / 'metab_raw_reordered.nii.gz').exists()

    subprocess.check_call(['mrs_tools', 'reorder',
                           '--dim_order', 'DIM_EDIT', 'DIM_COIL', 'DIM_DYN',
                           '--output', str(tmp_path),
                           '--filename', 'reordered_file',
                           '--file', str(test_data_split)])

    assert (tmp_path / 'reordered_file.nii.gz').exists()
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200


# Test conjugate option
def test_conjugate(tmp_path):
    subprocess.check_call(['mrs_tools', 'conjugate',
                           '--output', str(tmp_path),
                           '--filename', 'conj_file',
                           '--file', str(svs)])

    assert (tmp_path / 'conj_file.nii.gz').exists()

    subprocess.check_call(['mrs_tools', 'conjugate',
                           '--output', str(tmp_path),
                           '--file', str(svs)])

    assert (tmp_path / 'metab.nii.gz').exists()