|
| 1 | +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 2 | +# vi: set ft=python sts=4 ts=4 sw=4 et: |
| 3 | + |
| 4 | +import os |
| 5 | +import tempfile |
| 6 | +import shutil |
| 7 | +import numpy as np |
| 8 | +import nibabel as nib |
| 9 | + |
| 10 | +from nipype.testing import assert_equal, skipif |
| 11 | +from nipype.interfaces.freesurfer import model, no_freesurfer |
| 12 | +import nipype.pipeline.engine as pe |
| 13 | + |
| 14 | + |
| 15 | +@skipif(no_freesurfer) |
| 16 | +def test_concatenate(): |
| 17 | + tmp_dir = tempfile.mkdtemp() |
| 18 | + cwd = os.getcwd() |
| 19 | + os.chdir(tmp_dir) |
| 20 | + in1 = os.path.join(tmp_dir, 'cont1.nii') |
| 21 | + in2 = os.path.join(tmp_dir, 'cont2.nii') |
| 22 | + out = 'bar.nii' |
| 23 | + |
| 24 | + data1 = np.zeros((3, 3, 3, 1), dtype=np.float32) |
| 25 | + data2 = np.ones((3, 3, 3, 5), dtype=np.float32) |
| 26 | + out_data = np.concatenate((data1, data2), axis=3) |
| 27 | + mean_data = np.mean(out_data, axis=3) |
| 28 | + |
| 29 | + nib.Nifti1Image(data1, affine=np.eye(4)).to_filename(in1) |
| 30 | + nib.Nifti1Image(data2, affine=np.eye(4)).to_filename(in2) |
| 31 | + |
| 32 | + # Test default behavior |
| 33 | + res = model.Concatenate(in_files=[in1, in2]).run() |
| 34 | + yield (assert_equal, res.outputs.concatenated_file, |
| 35 | + os.path.join(tmp_dir, 'concat_output.nii.gz')) |
| 36 | + yield (assert_equal, nib.load('concat_output.nii.gz').get_data(), out_data) |
| 37 | + |
| 38 | + # Test specified concatenated_file |
| 39 | + res = model.Concatenate(in_files=[in1, in2], concatenated_file=out).run() |
| 40 | + yield (assert_equal, res.outputs.concatenated_file, |
| 41 | + os.path.join(tmp_dir, out)) |
| 42 | + yield (assert_equal, nib.load(out).get_data(), out_data) |
| 43 | + |
| 44 | + # Test in workflow |
| 45 | + wf = pe.Workflow('test_concatenate', base_dir=tmp_dir) |
| 46 | + concat = pe.Node(model.Concatenate(in_files=[in1, in2], |
| 47 | + concatenated_file=out), |
| 48 | + name='concat') |
| 49 | + wf.add_nodes([concat]) |
| 50 | + wf.run() |
| 51 | + yield (assert_equal, nib.load(os.path.join(tmp_dir, 'test_concatenate', |
| 52 | + 'concat', out)).get_data(), |
| 53 | + out_data) |
| 54 | + |
| 55 | + # Test a simple statistic |
| 56 | + res = model.Concatenate(in_files=[in1, in2], concatenated_file=out, |
| 57 | + stats='mean').run() |
| 58 | + yield (assert_equal, nib.load(out).get_data(), mean_data) |
| 59 | + |
| 60 | + os.chdir(cwd) |
| 61 | + shutil.rmtree(tmp_dir) |
0 commit comments