File: test_workflows.py

package info (click to toggle)
python-trx-python 0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 408 kB
  • sloc: python: 3,394; makefile: 66
file content (322 lines) | stat: -rw-r--r-- 14,123 bytes parent folder | download | duplicates (2)
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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

from deepdiff import DeepDiff
import os
import tempfile

import pytest
import numpy as np
from numpy.testing import assert_equal, assert_array_equal, assert_allclose
try:
    from dipy.io.streamline import load_tractogram
    dipy_available = True
except ImportError:
    dipy_available = False

from trx.fetcher import (get_testing_files_dict,
                         fetch_data, get_home)
import trx.trx_file_memmap as tmm
from trx.workflows import (convert_dsi_studio,
                           convert_tractogram,
                           manipulate_trx_datatype,
                           generate_trx_from_scratch,
                           validate_tractogram,)


# If they already exist, this only takes 5 seconds (check md5sum)
fetch_data(get_testing_files_dict(), keys=['DSI.zip', 'trx_from_scratch.zip'])


def test_help_option_convert_dsi(script_runner):
    ret = script_runner.run('tff_convert_dsi_studio.py', '--help')
    assert ret.success


def test_help_option_convert(script_runner):
    ret = script_runner.run('tff_convert_tractogram.py', '--help')
    assert ret.success


def test_help_option_generate_trx_from_scratch(script_runner):
    ret = script_runner.run('tff_generate_trx_from_scratch.py', '--help')
    assert ret.success


@pytest.mark.skipif(not dipy_available,
                    reason='Dipy is not installed.')
def test_execution_convert_dsi():
    with tempfile.TemporaryDirectory() as tmp_dir:
        in_trk = os.path.join(get_home(), 'DSI',
                              'CC.trk.gz')
        in_nii = os.path.join(get_home(), 'DSI',
                              'CC.nii.gz')
        exp_data = os.path.join(get_home(), 'DSI',
                                'CC_fix_data.npy')
        exp_offsets = os.path.join(get_home(), 'DSI',
                                   'CC_fix_offsets.npy')
        out_fix_path = os.path.join(tmp_dir, 'fixed.trk')
        convert_dsi_studio(in_trk, in_nii, out_fix_path,
                           remove_invalid=False,
                           keep_invalid=True)

        data_fix = np.load(exp_data)
        offsets_fix = np.load(exp_offsets)

        sft = load_tractogram(out_fix_path, 'same')
        assert_equal(sft.streamlines._data, data_fix)
        assert_equal(sft.streamlines._offsets, offsets_fix)


@pytest.mark.skipif(not dipy_available,
                    reason='Dipy is not installed.')
def test_execution_convert_to_trx():
    with tempfile.TemporaryDirectory() as tmp_dir:
        in_trk = os.path.join(get_home(), 'DSI',
                              'CC_fix.trk')
        exp_data = os.path.join(get_home(), 'DSI',
                                'CC_fix_data.npy')
        exp_offsets = os.path.join(get_home(), 'DSI',
                                   'CC_fix_offsets.npy')
        out_trx_path = os.path.join(tmp_dir, 'CC_fix.trx')
        convert_tractogram(in_trk, out_trx_path, None)

        data_fix = np.load(exp_data)
        offsets_fix = np.load(exp_offsets)

        trx = tmm.load(out_trx_path)
        assert_equal(trx.streamlines._data.dtype, np.float32)
        assert_equal(trx.streamlines._offsets.dtype, np.uint32)
        assert_array_equal(trx.streamlines._data, data_fix)
        assert_array_equal(trx.streamlines._offsets, offsets_fix)
        trx.close()


@pytest.mark.skipif(not dipy_available,
                    reason='Dipy is not installed.')
def test_execution_convert_from_trx():
    with tempfile.TemporaryDirectory() as tmp_dir:
        in_trk = os.path.join(get_home(), 'DSI',
                              'CC_fix.trk')
        in_nii = os.path.join(get_home(), 'DSI',
                              'CC.nii.gz')
        exp_data = os.path.join(get_home(), 'DSI',
                                'CC_fix_data.npy')
        exp_offsets = os.path.join(get_home(), 'DSI',
                                   'CC_fix_offsets.npy')

        # Sequential conversions
        out_trx_path = os.path.join(tmp_dir, 'CC_fix.trx')
        out_trk_path = os.path.join(tmp_dir, 'CC_fix.trk')
        out_tck_path = os.path.join(tmp_dir, 'CC_fix.tck')
        convert_tractogram(in_trk, out_trx_path, None)
        convert_tractogram(out_trx_path, out_tck_path, None)
        convert_tractogram(out_trx_path, out_trk_path, None)

        data_fix = np.load(exp_data)
        offsets_fix = np.load(exp_offsets)

        sft = load_tractogram(out_trk_path, 'same')
        assert_equal(sft.streamlines._data, data_fix)
        assert_equal(sft.streamlines._offsets, offsets_fix)

        sft = load_tractogram(out_tck_path, in_nii)
        assert_equal(sft.streamlines._data, data_fix)
        assert_equal(sft.streamlines._offsets, offsets_fix)


@pytest.mark.skipif(not dipy_available,
                    reason='Dipy is not installed.')
def test_execution_convert_dtype_p16_o64():
    with tempfile.TemporaryDirectory() as tmp_dir:
        in_trk = os.path.join(get_home(), 'DSI',
                              'CC_fix.trk')
        out_convert_path = os.path.join(tmp_dir, 'CC_fix_p16_o64.trx')
        convert_tractogram(in_trk, out_convert_path, None,
                           pos_dtype='float16', offsets_dtype='uint64')

        trx = tmm.load(out_convert_path)
        assert_equal(trx.streamlines._data.dtype, np.float16)
        assert_equal(trx.streamlines._offsets.dtype, np.uint64)
        trx.close()


@pytest.mark.skipif(not dipy_available,
                    reason='Dipy is not installed.')
def test_execution_convert_dtype_p64_o32():
    with tempfile.TemporaryDirectory() as tmp_dir:
        in_trk = os.path.join(get_home(), 'DSI',
                              'CC_fix.trk')
        out_convert_path = os.path.join(tmp_dir, 'CC_fix_p16_o64.trx')
        convert_tractogram(in_trk, out_convert_path, None,
                           pos_dtype='float64', offsets_dtype='uint32')

        trx = tmm.load(out_convert_path)
        assert_equal(trx.streamlines._data.dtype, np.float64)
        assert_equal(trx.streamlines._offsets.dtype, np.uint32)
        trx.close()


def test_execution_generate_trx_from_scratch():
    with tempfile.TemporaryDirectory() as tmp_dir:
        reference_fa = os.path.join(get_home(), 'trx_from_scratch',
                                    'fa.nii.gz')
        raw_arr_dir = os.path.join(get_home(), 'trx_from_scratch',
                                   'test_npy')
        expected_trx = os.path.join(get_home(), 'trx_from_scratch',
                                    'expected.trx')

        dpv = [(os.path.join(raw_arr_dir, 'dpv_cx.npy'), 'uint8'),
               (os.path.join(raw_arr_dir, 'dpv_cy.npy'), 'uint8'),
               (os.path.join(raw_arr_dir, 'dpv_cz.npy'), 'uint8')]
        dps = [(os.path.join(raw_arr_dir, 'dps_algo.npy'), 'uint8'),
               (os.path.join(raw_arr_dir, 'dps_cw.npy'), 'float64')]
        dpg = [('g_AF_L', os.path.join(raw_arr_dir, 'dpg_AF_L_mean_fa.npy'), 'float32'),
               ('g_AF_R', os.path.join(raw_arr_dir, 'dpg_AF_R_mean_fa.npy'), 'float32'),
               ('g_AF_L', os.path.join(raw_arr_dir, 'dpg_AF_L_volume.npy'), 'float32')]
        groups = [(os.path.join(raw_arr_dir, 'g_AF_L.npy'), 'int32'),
                  (os.path.join(raw_arr_dir, 'g_AF_R.npy'), 'int32'),
                  (os.path.join(raw_arr_dir, 'g_CST_L.npy'), 'int32')]

        out_gen_path = os.path.join(tmp_dir, 'generated.trx')
        generate_trx_from_scratch(reference_fa, out_gen_path,
                                  positions=os.path.join(raw_arr_dir,
                                                         'positions.npy'),
                                  offsets=os.path.join(raw_arr_dir,
                                                       'offsets.npy'),
                                  positions_dtype='float16',
                                  offsets_dtype='uint64',
                                  space_str='rasmm', origin_str='nifti',
                                  verify_invalid=False, dpv=dpv, dps=dps,
                                  groups=groups, dpg=dpg)
        exp_trx = tmm.load(expected_trx)
        gen_trx = tmm.load(out_gen_path)

        assert DeepDiff(exp_trx.get_dtype_dict(),
                        gen_trx.get_dtype_dict()) == {}

        assert_allclose(exp_trx.streamlines._data, gen_trx.streamlines._data,
                        atol=0.1, rtol=0.1)
        assert_equal(exp_trx.streamlines._offsets,
                     gen_trx.streamlines._offsets)

        for key in exp_trx.data_per_vertex.keys():
            assert_equal(exp_trx.data_per_vertex[key]._data,
                         gen_trx.data_per_vertex[key]._data)
            assert_equal(exp_trx.data_per_vertex[key]._offsets,
                         gen_trx.data_per_vertex[key]._offsets)
        for key in exp_trx.data_per_streamline.keys():
            assert_equal(exp_trx.data_per_streamline[key],
                         gen_trx.data_per_streamline[key])
        for key in exp_trx.groups.keys():
            assert_equal(exp_trx.groups[key], gen_trx.groups[key])

        for group in exp_trx.groups.keys():
            if group in exp_trx.data_per_group:
                for key in exp_trx.data_per_group[group].keys():
                    assert_equal(exp_trx.data_per_group[group][key],
                                 gen_trx.data_per_group[group][key])
        exp_trx.close()
        gen_trx.close()


@pytest.mark.skipif(not dipy_available,
                    reason='Dipy is not installed.')
def test_execution_concatenate_validate_trx():
    with tempfile.TemporaryDirectory() as tmp_dir:
        trx1 = tmm.load(os.path.join(get_home(), 'gold_standard',
                                     'gs.trx'))
        trx2 = tmm.load(os.path.join(get_home(), 'gold_standard',
                                     'gs.trx'))
        # trx2.streamlines._data += 0.001
        trx = tmm.concatenate([trx1, trx2], preallocation=False)

        # Right size
        assert_equal(len(trx.streamlines), 2*len(trx1.streamlines))

        # Right data
        end_idx = trx1.header['NB_VERTICES']
        assert_allclose(
            trx.streamlines._data[:end_idx], trx1.streamlines._data)
        assert_allclose(
            trx.streamlines._data[end_idx:], trx2.streamlines._data)

        # Right data_per_*
        for key in trx.data_per_vertex.keys():
            assert_equal(trx.data_per_vertex[key]._data[:end_idx],
                         trx1.data_per_vertex[key]._data)
            assert_equal(trx.data_per_vertex[key]._data[end_idx:],
                         trx2.data_per_vertex[key]._data)

        end_idx = trx1.header['NB_STREAMLINES']
        for key in trx.data_per_streamline.keys():
            assert_equal(trx.data_per_streamline[key][:end_idx],
                         trx1.data_per_streamline[key])
            assert_equal(trx.data_per_streamline[key][end_idx:],
                         trx2.data_per_streamline[key])

        # Validate
        out_concat_path = os.path.join(tmp_dir, 'concat.trx')
        out_valid_path = os.path.join(tmp_dir, 'valid.trx')
        tmm.save(trx, out_concat_path)
        validate_tractogram(out_concat_path, None, out_valid_path,
                            remove_identical_streamlines=True,
                            precision=0)
        trx_val = tmm.load(out_valid_path)

        # # Right dtype and size
        assert DeepDiff(trx.get_dtype_dict(), trx_val.get_dtype_dict()) == {}
        assert_equal(len(trx1.streamlines), len(trx_val.streamlines))

        trx.close()
        trx1.close()
        trx2.close()
        trx_val.close()


@pytest.mark.skipif(not dipy_available,
                    reason='Dipy is not installed.')
def test_execution_manipulate_trx_datatype():
    with tempfile.TemporaryDirectory() as tmp_dir:
        expected_trx = os.path.join(get_home(), 'trx_from_scratch',
                                    'expected.trx')
        trx = tmm.load(expected_trx)

        expected_dtype = {'positions': np.dtype('float16'),
                          'offsets': np.dtype('uint64'),
                          'dpv': {'dpv_cx': np.dtype('uint8'),
                                  'dpv_cy': np.dtype('uint8'),
                                  'dpv_cz': np.dtype('uint8')},
                          'dps': {'dps_algo': np.dtype('uint8'),
                                  'dps_cw': np.dtype('float64')},
                          'dpg': {'g_AF_L':
                                  {'dpg_AF_L_mean_fa': np.dtype('float32'),
                                   'dpg_AF_L_volume': np.dtype('float32')},
                                  'g_AF_R':
                                  {'dpg_AF_R_mean_fa': np.dtype('float32')}},
                          'groups': {'g_AF_L': np.dtype('int32'),
                                     'g_AF_R': np.dtype('int32')}}

        assert DeepDiff(trx.get_dtype_dict(), expected_dtype) == {}
        trx.close()

        generated_dtype = {'positions': np.dtype('float32'),
                           'offsets': np.dtype('uint32'),
                           'dpv': {'dpv_cx': np.dtype('uint16'),
                                   'dpv_cy': np.dtype('uint16'),
                                   'dpv_cz': np.dtype('uint16')},
                           'dps': {'dps_algo': np.dtype('uint8'),
                                   'dps_cw': np.dtype('float32')},
                           'dpg': {'g_AF_L':
                                   {'dpg_AF_L_mean_fa': np.dtype('float64'),
                                    'dpg_AF_L_volume': np.dtype('float32')},
                                   'g_AF_R':
                                   {'dpg_AF_R_mean_fa': np.dtype('float64')}},
                           'groups': {'g_AF_L': np.dtype('uint16'),
                                      'g_AF_R': np.dtype('uint16')}}

        out_gen_path = os.path.join(tmp_dir, 'generated.trx')
        manipulate_trx_datatype(expected_trx, out_gen_path, generated_dtype)
        trx = tmm.load(out_gen_path)
        assert DeepDiff(trx.get_dtype_dict(), generated_dtype) == {}
        trx.close()