|
| 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 |
| 11 | +from nipype.interfaces.freesurfer import model |
| 12 | + |
| 13 | + |
| 14 | +def test_concatenate(): |
| 15 | + tmp_dir = tempfile.mkdtemp() |
| 16 | + cwd = os.getcwd() |
| 17 | + os.chdir(tmp_dir) |
| 18 | + in1 = 'cont1.nii' |
| 19 | + in2 = 'cont2.nii' |
| 20 | + out = 'bar.nii' |
| 21 | + |
| 22 | + data1 = np.zeros((3, 3, 3, 1), dtype=np.float32) |
| 23 | + data2 = np.ones((3, 3, 3, 5), dtype=np.float32) |
| 24 | + out_data = np.concatenate((data1, data2), axis=3) |
| 25 | + mean_data = np.mean(out_data, axis=3) |
| 26 | + |
| 27 | + nib.Nifti1Image(data1, affine=np.eye(4)).to_filename(in1) |
| 28 | + nib.Nifti1Image(data2, affine=np.eye(4)).to_filename(in2) |
| 29 | + |
| 30 | + # Test default behavior |
| 31 | + res = model.Concatenate(in_files=[in1, in2]).run() |
| 32 | + yield (assert_equal, res.outputs.concatenated_file, |
| 33 | + os.path.join(tmp_dir, 'concat_output.nii.gz')) |
| 34 | + yield (assert_equal, nib.load('concat_output.nii.gz').get_data(), out_data) |
| 35 | + |
| 36 | + # Test specified concatenated_file |
| 37 | + res = model.Concatenate(in_files=[in1, in2], concatenated_file=out).run() |
| 38 | + yield (assert_equal, res.outputs.concatenated_file, |
| 39 | + os.path.join(tmp_dir, out)) |
| 40 | + yield (assert_equal, nib.load(out).get_data(), out_data) |
| 41 | + |
| 42 | + # Test a simple statistic |
| 43 | + res = model.Concatenate(in_files=[in1, in2], concatenated_file=out, |
| 44 | + stats='mean').run() |
| 45 | + yield (assert_equal, nib.load(out).get_data(), mean_data) |
| 46 | + |
| 47 | + os.chdir(cwd) |
| 48 | + shutil.rmtree(tmp_dir) |
0 commit comments