Skip to content

Commit 038b143

Browse files
authored
Merge pull request #1781 from alexsavio/fix/createnifti
Fix small bug/typo in misc.CreateNifti
2 parents 1a4e7ea + 4890e9a commit 038b143

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

nipype/algorithms/misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def _run_interface(self, runtime):
247247
else:
248248
affine = None
249249

250-
with open(self.inputs.header_file, 'rb') as data_file:
250+
with open(self.inputs.data_file, 'rb') as data_file:
251251
data = hdr.data_from_fileobj(data_file)
252252

253253
img = nb.Nifti1Image(data, affine, hdr)

nipype/algorithms/tests/test_misc.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 pytest
5+
import os
6+
7+
import nibabel as nb
8+
9+
from nipype.algorithms import misc
10+
from nipype.utils.filemanip import fname_presuffix
11+
from nipype.testing.fixtures import create_analyze_pair_file_in_directory
12+
13+
14+
def test_CreateNifti(create_analyze_pair_file_in_directory):
15+
16+
filelist, outdir = create_analyze_pair_file_in_directory
17+
18+
create_nifti = misc.CreateNifti()
19+
20+
# test raising error with mandatory args absent
21+
with pytest.raises(ValueError):
22+
create_nifti.run()
23+
24+
# .inputs based parameters setting
25+
create_nifti.inputs.header_file = filelist[0]
26+
create_nifti.inputs.data_file = fname_presuffix(filelist[0],
27+
'',
28+
'.img',
29+
use_ext=False)
30+
31+
result = create_nifti.run()
32+
33+
assert os.path.exists(result.outputs.nifti_file)
34+
assert nb.load(result.outputs.nifti_file)

nipype/testing/fixtures.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
from nipype.interfaces.fsl.base import FSLCommand
1616

1717

18+
def analyze_pair_image_files(outdir, filelist, shape):
19+
for f in filelist:
20+
hdr = nb.Nifti1Header()
21+
hdr.set_data_shape(shape)
22+
img = np.random.random(shape)
23+
analyze = nb.AnalyzeImage(img, np.eye(4), hdr)
24+
analyze.to_filename(os.path.join(outdir, f))
25+
26+
1827
def nifti_image_files(outdir, filelist, shape):
1928
for f in filelist:
2029
hdr = nb.Nifti1Header()
@@ -39,6 +48,21 @@ def change_directory():
3948
return (filelist, outdir)
4049

4150

51+
@pytest.fixture()
52+
def create_analyze_pair_file_in_directory(request, tmpdir):
53+
outdir = str(tmpdir)
54+
cwd = os.getcwd()
55+
os.chdir(outdir)
56+
filelist = ['a.hdr']
57+
analyze_pair_image_files(outdir, filelist, shape=(3, 3, 3, 4))
58+
59+
def change_directory():
60+
os.chdir(cwd)
61+
62+
request.addfinalizer(change_directory)
63+
return (filelist, outdir)
64+
65+
4266
@pytest.fixture()
4367
def create_files_in_directory_plus_dummy_file(request, tmpdir):
4468
outdir = str(tmpdir)

0 commit comments

Comments
 (0)