-
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
Changes from 5 commits
b560da6
8d5d70d
fc619c0
4ab086b
0036e0c
e995d50
5f6b64f
ba81e30
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -8,10 +8,11 @@ | |||
""" | ||||
import numpy as np | ||||
|
||||
import nibabel as nib | ||||
from nibabel import cifti2 as ci | ||||
from nibabel.tmpdirs import InTemporaryDirectory | ||||
|
||||
from nose.tools import assert_true, assert_equal | ||||
from nose.tools import assert_true, assert_equal, assert_raises | ||||
|
||||
affine = [[-1.5, 0, 0, 90], | ||||
[0, 1.5, 0, -85], | ||||
|
@@ -212,10 +213,15 @@ def test_dtseries(): | |||
hdr = ci.Cifti2Header(matrix) | ||||
data = np.random.randn(13, 9) | ||||
img = ci.Cifti2Image(data, hdr) | ||||
print(img.nifti_header.get_intent()) | ||||
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(img2.nifti_header.get_intent()[0] | ||||
== 'dense data series/fiber fans') | ||||
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. assert_equal(img2.nifti_header.get_intent()[0], 'dense data series/fiber fans') |
||||
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 +237,13 @@ 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(img2.nifti_header.get_intent()[0] == 'dense scalar') | ||||
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.
|
||||
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 +259,13 @@ 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(img2.nifti_header.get_intent()[0] == 'dense label') | ||||
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 +279,13 @@ 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(img2.nifti_header.get_intent()[0] == 'dense connectivity') | ||||
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.
|
||||
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 +302,14 @@ 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(img2.nifti_header.get_intent()[0] | ||||
== 'parcellated data series') | ||||
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.
|
||||
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 +325,13 @@ 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(img2.nifti_header.get_intent()[0] == 'parcellated scalar') | ||||
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.
|
||||
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 +347,14 @@ 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_PARCELLATED_DENSE') | ||||
|
||||
with InTemporaryDirectory(): | ||||
ci.save(img, 'test.pdconn.nii') | ||||
img2 = ci.load('test.pdconn.nii') | ||||
assert_true(img2.nifti_header.get_intent()[0] | ||||
== 'parcellated dense connectivity') | ||||
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.
|
||||
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,10 +370,14 @@ 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(img2.nifti_header.get_intent()[0] | ||||
== 'dense parcellated connectivity') | ||||
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.
|
||||
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)) | ||||
|
@@ -367,6 +397,9 @@ def test_plabel(): | |||
with InTemporaryDirectory(): | ||||
ci.save(img, 'test.plabel.nii') | ||||
img2 = ci.load('test.plabel.nii') | ||||
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. is this a valid CIFTI type? I cannot find *plabel.nii in the specifications 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. there is a plabel: https://github.com/Washington-University/workbench/blob/master/src/Commands/CommandParser.cxx#L661 but indeed i don't see it in the spec. 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. 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. If we will raise an exception upon saving, we have some options
WDYT? 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. how about skipping the plabel test for now till we find the INTENT code. i would rather not have a parameter. 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. 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. plabel is not in the cifti-2 spec. We didn't see it being useful at the time, so it was not in the draft. We later found that the information needed to reorder parcellated files to group certain parcels together in matrix display could be stored as a parcels by labels file, and decided to use that extention, matching the previous pattern of extension naming. Per the "unknown" special cifti intent (main cifti-2 document, top of page 13, middle of paragraph), it is in fact valid to make such a file, and its extension is technically open to be anything that ends in ".<something>.nii". Feel free to write a test that loads and/or saves a cifti mapping combination that is not in the spec, this is an expected use case. |
||||
assert_true(img.nifti_header.get_intent()[0] | ||||
== 'dense fiber/fan samples') | ||||
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. shouldn't this be unknown? 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 matched it with what's consistent in the intent codes nibabel/nibabel/cifti2/parse_cifti2.py Line 55 in e48b746
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.
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. There is something incorrect here (maybe more than one thing). Dense fiber samples and dense fan samples are just dscalar files with a specific number and meaning of maps - they use the dscalar intent code and name. Their extensions are .dfibersamp.nii and .dfansamp.nii. The dense fans file type is another instance of this. parcel by label files (which we use the extension .plabel.nii for) are not a "standard" mapping combination, so they should use intent 3000, and intent name "ConnUnknown". |
||||
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)) | ||||
|
@@ -380,10 +413,14 @@ 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(img.nifti_header.get_intent()[0] | ||||
== 'parcellated connectivity') | ||||
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.
|
||||
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 +431,21 @@ 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(img.nifti_header.get_intent()[0] | ||||
== 'parcellated connectivity series') | ||||
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.
|
||||
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 +457,21 @@ 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(img.nifti_header.get_intent()[0] | ||||
== 'parcellated connectivity scalar') | ||||
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.
|
||||
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)) | ||||
|
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.
Remove print.