-
Notifications
You must be signed in to change notification settings - Fork 262
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
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b560da6
fix: explicitly set intent codes to allow proper loading
mgxd 8d5d70d
fix: do not allow saving of images without intent
mgxd fc619c0
fix: use unknown CIFTI type, raise ValueError if incorrect intent code
mgxd 4ab086b
enh: move cifti intent check into to_file_map
mgxd 0036e0c
enh: set default intent code when saving CIFTI images
mgxd e995d50
STY: remove unused import
mgxd 5f6b64f
sty: utilize assert_equals
mgxd ba81e30
fix: use correct CIFTI intent names
mgxd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
""" | ||
import numpy as np | ||
|
||
import nibabel as nib | ||
from nibabel import cifti2 as ci | ||
from nibabel.tmpdirs import InTemporaryDirectory | ||
|
||
|
@@ -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)) | ||
|
@@ -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)) | ||
|
@@ -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)) | ||
|
@@ -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)) | ||
|
@@ -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)) | ||
|
@@ -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)) | ||
|
@@ -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)) | ||
|
@@ -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(): | ||
|
@@ -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)) | ||
|
@@ -394,17 +416,19 @@ def test_pconn(): | |
def test_pconnseries(): | ||
parcel_map = create_parcel_map((0, 1)) | ||
series_map = create_series_map((2, )) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
@@ -416,17 +440,19 @@ def test_pconnseries(): | |
def test_pconnscalar(): | ||
parcel_map = create_parcel_map((0, 1)) | ||
scalar_map = create_scalar_map((2, )) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 inCifti2Image.to_file_map()
, maybe after resettingpixdim
.This feels a little cleaner to me, as the CIFTI equivalent of saving a NIFTI without setting an intent code, which we also allow.