Skip to content

Fix small bug/typo in misc.CreateNifti #1781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nipype/algorithms/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def _run_interface(self, runtime):
else:
affine = None

with open(self.inputs.header_file, 'rb') as data_file:
with open(self.inputs.data_file, 'rb') as data_file:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this interface seems to only work with hdr/img pair files. in that space, shouldn't this be the header_file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @alexsavio is right, here. These two lines were originally:

data = hdr.data_from_fileobj(open(self.inputs.data_file, 'rb'))

Copy link
Contributor Author

@alexsavio alexsavio Jan 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @effigies. @satra, my workflows were working until I got this update, it only worked when I added this fix. A Nipype Camino tractography example also uses CreateNifti, it probably won't work without this fix. I was thinking on adding a test, but I would need Analyze files, somehow. Any hint?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can generate them with nibabel easily enough. e.g.

analyze = nb.AnalyzeImage(np.zeros((3,4,5,6)), np.eye(4))
analyze.to_filename('test.hdr')  # Should create test.hdr/test.img

data = hdr.data_from_fileobj(data_file)

img = nb.Nifti1Image(data, affine, hdr)
Expand Down
34 changes: 34 additions & 0 deletions nipype/algorithms/tests/test_misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:

import pytest
import os

import nibabel as nb

from nipype.algorithms import misc
from nipype.utils.filemanip import fname_presuffix
from nipype.testing.fixtures import create_analyze_pair_file_in_directory


def test_CreateNifti(create_analyze_pair_file_in_directory):

filelist, outdir = create_analyze_pair_file_in_directory

create_nifti = misc.CreateNifti()

# test raising error with mandatory args absent
with pytest.raises(ValueError):
create_nifti.run()

# .inputs based parameters setting
create_nifti.inputs.header_file = filelist[0]
create_nifti.inputs.data_file = fname_presuffix(filelist[0],
'',
'.img',
use_ext=False)

result = create_nifti.run()

assert os.path.exists(result.outputs.nifti_file)
assert nb.load(result.outputs.nifti_file)
24 changes: 24 additions & 0 deletions nipype/testing/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
from nipype.interfaces.fsl.base import FSLCommand


def analyze_pair_image_files(outdir, filelist, shape):
for f in filelist:
hdr = nb.Nifti1Header()
hdr.set_data_shape(shape)
img = np.random.random(shape)
analyze = nb.AnalyzeImage(img, np.eye(4), hdr)
analyze.to_filename(os.path.join(outdir, f))


def nifti_image_files(outdir, filelist, shape):
for f in filelist:
hdr = nb.Nifti1Header()
Expand All @@ -39,6 +48,21 @@ def change_directory():
return (filelist, outdir)


@pytest.fixture()
def create_analyze_pair_file_in_directory(request, tmpdir):
outdir = str(tmpdir)
cwd = os.getcwd()
os.chdir(outdir)
filelist = ['a.hdr']
analyze_pair_image_files(outdir, filelist, shape=(3, 3, 3, 4))

def change_directory():
os.chdir(cwd)

request.addfinalizer(change_directory)
return (filelist, outdir)


@pytest.fixture()
def create_files_in_directory_plus_dummy_file(request, tmpdir):
outdir = str(tmpdir)
Expand Down