Skip to content

tst: explicitly set intent codes to allow proper loading #604

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 8 commits into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 0 deletions nibabel/cifti2/cifti2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1442,4 +1442,6 @@ def save(img, filename):
filename : str
filename to which to save image
"""
if img.nifti_header.get_intent()[0] == 'none':
raise AttributeError("CIFTI image has an invalid intent code.")
Copy link
Member

Choose a reason for hiding this comment

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

Actually, I think I would rather set the intent by default to 'NIFTI_INTENT_CONNECTIVITY_UNKNOWN', and I think we should do this in Cifti2Image.to_file_map(), maybe after resetting pixdim.

if header.get_intent()[0] == 'none':
    header.set_intent('NIFTI_INTENT_CONNECTIVITY_UNKNOWN')

This feels a little cleaner to me, as the CIFTI equivalent of saving a NIFTI without setting an intent code, which we also allow.

Cifti2Image.instance_to_filename(img, filename)
76 changes: 51 additions & 25 deletions nibabel/cifti2/tests/test_new_cifti2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""
import numpy as np

import nibabel as nib
from nibabel import cifti2 as ci
from nibabel.tmpdirs import InTemporaryDirectory

Expand Down Expand Up @@ -212,10 +213,12 @@ def test_dtseries():
hdr = ci.Cifti2Header(matrix)
data = np.random.randn(13, 9)
img = ci.Cifti2Image(data, hdr)
img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_DENSE_SERIES')

with InTemporaryDirectory():
ci.save(img, 'test.dtseries.nii')
img2 = ci.load('test.dtseries.nii')
img2 = nib.load('test.dtseries.nii')
assert_true(isinstance(img2, ci.Cifti2Image))
assert_true((img2.get_data() == data).all())
check_series_map(img2.header.matrix.get_index_map(0))
check_geometry_map(img2.header.matrix.get_index_map(1))
Expand All @@ -231,10 +234,12 @@ def test_dscalar():
hdr = ci.Cifti2Header(matrix)
data = np.random.randn(2, 9)
img = ci.Cifti2Image(data, hdr)
img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_DENSE_SCALARS')

with InTemporaryDirectory():
ci.save(img, 'test.dscalar.nii')
img2 = ci.load('test.dscalar.nii')
img2 = nib.load('test.dscalar.nii')
assert_true(isinstance(img2, ci.Cifti2Image))
assert_true((img2.get_data() == data).all())
check_scalar_map(img2.header.matrix.get_index_map(0))
check_geometry_map(img2.header.matrix.get_index_map(1))
Expand All @@ -250,10 +255,12 @@ def test_dlabel():
hdr = ci.Cifti2Header(matrix)
data = np.random.randn(2, 9)
img = ci.Cifti2Image(data, hdr)
img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_DENSE_LABELS')

with InTemporaryDirectory():
ci.save(img, 'test.dlabel.nii')
img2 = ci.load('test.dlabel.nii')
img2 = nib.load('test.dlabel.nii')
assert_true(isinstance(img2, ci.Cifti2Image))
assert_true((img2.get_data() == data).all())
check_label_map(img2.header.matrix.get_index_map(0))
check_geometry_map(img2.header.matrix.get_index_map(1))
Expand All @@ -267,10 +274,12 @@ def test_dconn():
hdr = ci.Cifti2Header(matrix)
data = np.random.randn(9, 9)
img = ci.Cifti2Image(data, hdr)
img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_DENSE')

with InTemporaryDirectory():
ci.save(img, 'test.dconn.nii')
img2 = ci.load('test.dconn.nii')
img2 = nib.load('test.dconn.nii')
assert_true(isinstance(img2, ci.Cifti2Image))
assert_true((img2.get_data() == data).all())
assert_equal(img2.header.matrix.get_index_map(0),
img2.header.matrix.get_index_map(1))
Expand All @@ -287,10 +296,12 @@ def test_ptseries():
hdr = ci.Cifti2Header(matrix)
data = np.random.randn(13, 3)
img = ci.Cifti2Image(data, hdr)
img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_PARCELLATED_SERIES')

with InTemporaryDirectory():
ci.save(img, 'test.ptseries.nii')
img2 = ci.load('test.ptseries.nii')
img2 = nib.load('test.ptseries.nii')
assert_true(isinstance(img2, ci.Cifti2Image))
assert_true((img2.get_data() == data).all())
check_series_map(img2.header.matrix.get_index_map(0))
check_parcel_map(img2.header.matrix.get_index_map(1))
Expand All @@ -306,10 +317,12 @@ def test_pscalar():
hdr = ci.Cifti2Header(matrix)
data = np.random.randn(2, 3)
img = ci.Cifti2Image(data, hdr)
img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_PARCELLATED_SCALAR')

with InTemporaryDirectory():
ci.save(img, 'test.pscalar.nii')
img2 = ci.load('test.pscalar.nii')
img2 = nib.load('test.pscalar.nii')
assert_true(isinstance(img2, ci.Cifti2Image))
assert_true((img2.get_data() == data).all())
check_scalar_map(img2.header.matrix.get_index_map(0))
check_parcel_map(img2.header.matrix.get_index_map(1))
Expand All @@ -325,10 +338,12 @@ def test_pdconn():
hdr = ci.Cifti2Header(matrix)
data = np.random.randn(2, 3)
img = ci.Cifti2Image(data, hdr)
img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_DENSE_PARCELLATED')

with InTemporaryDirectory():
ci.save(img, 'test.pdconn.nii')
img2 = ci.load('test.pdconn.nii')
assert_true(isinstance(img2, ci.Cifti2Image))
assert_true((img2.get_data() == data).all())
check_geometry_map(img2.header.matrix.get_index_map(0))
check_parcel_map(img2.header.matrix.get_index_map(1))
Expand All @@ -344,33 +359,38 @@ def test_dpconn():
hdr = ci.Cifti2Header(matrix)
data = np.random.randn(2, 3)
img = ci.Cifti2Image(data, hdr)
img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_DENSE_PARCELLATED')

with InTemporaryDirectory():
ci.save(img, 'test.dpconn.nii')
img2 = ci.load('test.dpconn.nii')
assert_true(isinstance(img2, ci.Cifti2Image))
assert_true((img2.get_data() == data).all())
check_parcel_map(img2.header.matrix.get_index_map(0))
check_geometry_map(img2.header.matrix.get_index_map(1))
del img2


def test_plabel():
label_map = create_label_map((0, ))
parcel_map = create_parcel_map((1, ))
matrix = ci.Cifti2Matrix()
matrix.append(label_map)
matrix.append(parcel_map)
hdr = ci.Cifti2Header(matrix)
data = np.random.randn(2, 3)
img = ci.Cifti2Image(data, hdr)

with InTemporaryDirectory():
ci.save(img, 'test.plabel.nii')
img2 = ci.load('test.plabel.nii')
assert_true((img2.get_data() == data).all())
check_label_map(img2.header.matrix.get_index_map(0))
check_parcel_map(img2.header.matrix.get_index_map(1))
del img2
# SKIP until intent code is specified
# https://www.nitrc.org/forum/attachment.php?attachid=341&group_id=454&forum_id=1955
# def test_plabel():
# label_map = create_label_map((0, ))
# parcel_map = create_parcel_map((1, ))
# matrix = ci.Cifti2Matrix()
# matrix.append(label_map)
# matrix.append(parcel_map)
# hdr = ci.Cifti2Header(matrix)
# data = np.random.randn(2, 3)
# img = ci.Cifti2Image(data, hdr)
#
# with InTemporaryDirectory():
# ci.save(img, 'test.plabel.nii')
# img2 = ci.load('test.plabel.nii')
# assert_true(isinstance(img2, ci.Cifti2Image))
# assert_true((img2.get_data() == data).all())
# check_label_map(img2.header.matrix.get_index_map(0))
# check_parcel_map(img2.header.matrix.get_index_map(1))
# del img2


def test_pconn():
Expand All @@ -380,10 +400,12 @@ def test_pconn():
hdr = ci.Cifti2Header(matrix)
data = np.random.randn(3, 3)
img = ci.Cifti2Image(data, hdr)
img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_PARCELLATED')

with InTemporaryDirectory():
ci.save(img, 'test.pconn.nii')
img2 = ci.load('test.pconn.nii')
assert_true(isinstance(img2, ci.Cifti2Image))
assert_true((img2.get_data() == data).all())
assert_equal(img2.header.matrix.get_index_map(0),
img2.header.matrix.get_index_map(1))
Expand All @@ -394,17 +416,19 @@ def test_pconn():
def test_pconnseries():
parcel_map = create_parcel_map((0, 1))
series_map = create_series_map((2, ))

Copy link
Member

Choose a reason for hiding this comment

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

I'd re-add this line to clean the diff.

matrix = ci.Cifti2Matrix()
matrix.append(parcel_map)
matrix.append(series_map)
hdr = ci.Cifti2Header(matrix)
data = np.random.randn(3, 3, 13)
img = ci.Cifti2Image(data, hdr)
img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_PARCELLATED_'
'PARCELLATED_SERIES')

with InTemporaryDirectory():
ci.save(img, 'test.pconnseries.nii')
img2 = ci.load('test.pconnseries.nii')
assert_true(isinstance(img2, ci.Cifti2Image))
assert_true((img2.get_data() == data).all())
assert_equal(img2.header.matrix.get_index_map(0),
img2.header.matrix.get_index_map(1))
Expand All @@ -416,17 +440,19 @@ def test_pconnseries():
def test_pconnscalar():
parcel_map = create_parcel_map((0, 1))
scalar_map = create_scalar_map((2, ))

Copy link
Member

Choose a reason for hiding this comment

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

Re-add.

matrix = ci.Cifti2Matrix()
matrix.append(parcel_map)
matrix.append(scalar_map)
hdr = ci.Cifti2Header(matrix)
data = np.random.randn(3, 3, 13)
img = ci.Cifti2Image(data, hdr)
img.nifti_header.set_intent('NIFTI_INTENT_CONNECTIVITY_PARCELLATED_'
'PARCELLATED_SCALAR')

with InTemporaryDirectory():
ci.save(img, 'test.pconnscalar.nii')
img2 = ci.load('test.pconnscalar.nii')
assert_true(isinstance(img2, ci.Cifti2Image))
assert_true((img2.get_data() == data).all())
assert_equal(img2.header.matrix.get_index_map(0),
img2.header.matrix.get_index_map(1))
Expand Down