|
| 1 | +# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 2 | +# vi: set ft=python sts=4 ts=4 sw=4 et: |
| 3 | +### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## |
| 4 | +# |
| 5 | +# See COPYING file distributed along with the NiBabel package for the |
| 6 | +# copyright and license terms. |
| 7 | +# |
| 8 | +### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## |
| 9 | +from __future__ import division, print_function, absolute_import |
| 10 | + |
| 11 | +from os.path import join as pjoin, dirname |
| 12 | +import sys |
| 13 | + |
| 14 | +import numpy as np |
| 15 | + |
| 16 | +from ... import cifti as ci |
| 17 | + |
| 18 | +from ...tmpdirs import InTemporaryDirectory |
| 19 | + |
| 20 | +from numpy.testing import assert_array_equal, assert_array_almost_equal |
| 21 | + |
| 22 | +from nose.tools import (assert_true, assert_false, assert_equal, |
| 23 | + assert_raises) |
| 24 | + |
| 25 | + |
| 26 | +IO_DATA_PATH = pjoin(dirname(__file__), 'data') |
| 27 | + |
| 28 | +DATA_FILE1 = pjoin(IO_DATA_PATH, '') |
| 29 | +DATA_FILE2 = pjoin(IO_DATA_PATH, |
| 30 | + 'Conte69.MyelinAndCorrThickness.32k_fs_LR.dscalar.nii') |
| 31 | +DATA_FILE3 = pjoin(IO_DATA_PATH, |
| 32 | + 'Conte69.MyelinAndCorrThickness.32k_fs_LR.dtseries.nii') |
| 33 | +DATA_FILE4 = pjoin(IO_DATA_PATH, |
| 34 | + 'Conte69.MyelinAndCorrThickness.32k_fs_LR.ptseries.nii') |
| 35 | +DATA_FILE5 = pjoin(IO_DATA_PATH, |
| 36 | + 'Conte69.parcellations_VGD11b.32k_fs_LR.dlabel.nii') |
| 37 | +DATA_FILE6 = pjoin(IO_DATA_PATH, 'ones.dscalar.nii') |
| 38 | + |
| 39 | +datafiles = [DATA_FILE2, DATA_FILE3, DATA_FILE4, DATA_FILE5, DATA_FILE6] |
| 40 | + |
| 41 | +def test_read_ordering(): |
| 42 | + # DATA_FILE1 has an expected darray[0].data shape of (3,3). However if we |
| 43 | + # read another image first (DATA_FILE2) then the shape is wrong |
| 44 | + # Read an image |
| 45 | + img2 = ci.load(DATA_FILE6) |
| 46 | + assert_equal(img2.data.shape, (91282,)) |
| 47 | + |
| 48 | + |
| 49 | +def test_version(): |
| 50 | + for i, dat in enumerate(datafiles): |
| 51 | + img = ci.load(dat) |
| 52 | + assert_equal(img.header.version, '2') |
| 53 | + |
| 54 | +''' |
| 55 | +def test_dataarray1(): |
| 56 | + img1 = gi.read(DATA_FILE1) |
| 57 | + # Round trip |
| 58 | + with InTemporaryDirectory(): |
| 59 | + gi.write(img1, 'test.gii') |
| 60 | + bimg = gi.read('test.gii') |
| 61 | + for img in (img1, bimg): |
| 62 | + assert_array_almost_equal(img.darrays[0].data, DATA_FILE1_darr1) |
| 63 | + assert_array_almost_equal(img.darrays[1].data, DATA_FILE1_darr2) |
| 64 | + me=img.darrays[0].meta.get_metadata() |
| 65 | + assert_true('AnatomicalStructurePrimary' in me) |
| 66 | + assert_true('AnatomicalStructureSecondary' in me) |
| 67 | + assert_equal(me['AnatomicalStructurePrimary'], 'CortexLeft') |
| 68 | + assert_array_almost_equal(img.darrays[0].coordsys.xform, np.eye(4,4)) |
| 69 | + assert_equal(xform_codes.niistring[img.darrays[0].coordsys.dataspace],'NIFTI_XFORM_TALAIRACH') |
| 70 | + assert_equal(xform_codes.niistring[img.darrays[0].coordsys.xformspace],'NIFTI_XFORM_TALAIRACH') |
| 71 | +''' |
| 72 | + |
| 73 | + |
| 74 | +def test_readwritedata(): |
| 75 | + with InTemporaryDirectory(): |
| 76 | + for name in datafiles: |
| 77 | + img = ci.load(name) |
| 78 | + img.to_filename('test.nii') |
| 79 | + img2 = ci.load('test.nii') |
| 80 | + assert_equal(img.header.matrix.numMIM,img2.header.matrix.numMIM) |
| 81 | + assert_array_almost_equal(img.data, |
| 82 | + img2.data) |
| 83 | + |
| 84 | + |
| 85 | +def test_newmetadata(): |
| 86 | + img = ci.CiftiImage() |
| 87 | + attr = ci.CiftiNVPair(name = 'mykey', value = 'val1') |
| 88 | + newmeta = ci.CiftiMetaData(attr) |
| 89 | + img.header.matrix.meta = newmeta |
| 90 | + myme = img.header.matrix.meta.get_metadata() |
| 91 | + assert_true('mykey' in myme) |
| 92 | + newmeta = ci.CiftiMetaData.from_dict( {'mykey1' : 'val2'} ) |
| 93 | + img.header.matrix.meta = newmeta |
| 94 | + myme = img.header.matrix.meta.get_metadata() |
| 95 | + assert_true('mykey1' in myme) |
| 96 | + assert_false('mykey' in myme) |
0 commit comments